blob: e54c6ad628a810a047e1754f8196f2ec5e11ec42 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Carsten Otte043405e2007-10-10 17:16:19 +02002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * derived from drivers/kvm/kvm_main.c
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03008 * Copyright (C) 2008 Qumranet, Inc.
9 * Copyright IBM Corporation, 2008
Nicolas Kaiser9611c182010-10-06 14:23:22 +020010 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Carsten Otte043405e2007-10-10 17:16:19 +020011 *
12 * Authors:
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030015 * Amit Shah <amit.shah@qumranet.com>
16 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020017 */
18
Avi Kivityedf88412007-12-16 11:02:48 +020019#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020020#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080022#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020023#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Sean Christopherson2f728d62020-02-18 15:29:49 -080025#include "kvm_emulate.h"
Avi Kivity26eef702008-07-03 14:59:22 +030026#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020027#include "cpuid.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020028#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030029#include "hyperv.h"
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -060030#include "lapic.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020031
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020032#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030033#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020034#include <linux/kvm.h>
35#include <linux/fs.h>
36#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040037#include <linux/export.h>
38#include <linux/moduleparam.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080039#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050040#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010041#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030042#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010043#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030044#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020045#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080047#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080048#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020049#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020050#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020051#include <linux/timekeeper_internal.h>
52#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080053#include <linux/kvm_irqfd.h>
54#include <linux/irqbypass.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010055#include <linux/sched/stat.h>
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080056#include <linux/sched/isolation.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050057#include <linux/mem_encrypt.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010058
Avi Kivityaec51dc2009-07-01 16:01:02 +030059#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080060
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020061#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080062#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020063#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080064#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020065#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020066#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100067#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030068#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080069#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010070#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010071#include <asm/hypervisor.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080072#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090073#include <asm/emulate_prefix.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000074#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020075
Dave Hansend1898b72016-06-01 10:42:20 -070076#define CREATE_TRACE_POINTS
77#include "trace.h"
78
Carsten Otte313a3dc2007-10-11 19:16:52 +020079#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080080#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080081u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
82EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080083
Avi Kivity0f65dd72011-04-20 13:37:53 +030084#define emul_to_vcpu(ctxt) \
Sean Christophersonc9b8b072020-02-18 15:29:48 -080085 ((struct kvm_vcpu *)(ctxt)->vcpu)
Avi Kivity0f65dd72011-04-20 13:37:53 +030086
Joerg Roedel50a37eb2008-01-31 14:57:38 +010087/* EFER defaults:
88 * - enable syscall per default because its emulated by KVM
89 * - enable LME and LMA per default on 64 bit KVM
90 */
91#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080092static
93u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010094#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080095static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010096#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020097
Sean Christophersonb11306b2019-12-10 14:44:13 -080098static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
99
Paolo Bonzini833b45d2019-09-30 18:48:44 +0200100#define VM_STAT(x, ...) offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__
101#define VCPU_STAT(x, ...) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__
Hollis Blanchard417bc302007-10-31 17:24:23 -0500102
Radim Krčmářc5192652016-07-12 22:09:28 +0200103#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
104 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200105
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300106static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300107static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200108static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100109static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800110static void store_regs(struct kvm_vcpu *vcpu);
111static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200112
Paolo Bonzini893590c2015-11-06 11:46:24 +0100113struct kvm_x86_ops *kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300114EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800115
Paolo Bonzini893590c2015-11-06 11:46:24 +0100116static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030117module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200118
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200119static bool __read_mostly report_ignored_msrs = true;
120module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
121
Wanpeng Li4c276252018-05-05 04:02:32 -0700122unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200123module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
124
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300125static bool __read_mostly kvmclock_periodic_sync = true;
126module_param(kvmclock_periodic_sync, bool, S_IRUGO);
127
Paolo Bonzini893590c2015-11-06 11:46:24 +0100128bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100129EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100130u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100131EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800132u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
133EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
134u64 __read_mostly kvm_max_tsc_scaling_ratio;
135EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700136u64 __read_mostly kvm_default_tsc_scaling_ratio;
137EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100138
Zachary Amsdencc578282012-02-03 15:43:50 -0200139/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100140static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200141module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
142
Sean Christophersonc3941d92019-04-17 10:15:33 -0700143/*
144 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
145 * adaptive tuning starting from default advancment of 1000ns. '0' disables
146 * advancement entirely. Any other value is used as-is and disables adaptive
147 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
148 */
149static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800150module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500151
Feng Wu520040142016-01-25 16:53:33 +0800152static bool __read_mostly vector_hashing = true;
153module_param(vector_hashing, bool, S_IRUGO);
154
Liran Alonc4ae60e2018-03-12 13:12:47 +0200155bool __read_mostly enable_vmware_backdoor = false;
156module_param(enable_vmware_backdoor, bool, S_IRUGO);
157EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
158
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700159static bool __read_mostly force_emulation_prefix = false;
160module_param(force_emulation_prefix, bool, S_IRUGO);
161
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800162int __read_mostly pi_inject_timer = -1;
163module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
164
Avi Kivity18863bd2009-09-07 11:12:18 +0300165#define KVM_NR_SHARED_MSRS 16
166
167struct kvm_shared_msrs_global {
168 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800169 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300170};
171
172struct kvm_shared_msrs {
173 struct user_return_notifier urn;
174 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800175 struct kvm_shared_msr_values {
176 u64 host;
177 u64 curr;
178 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300179};
180
181static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200182static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300183
Sean Christophersoncfc48182020-03-02 15:56:23 -0800184#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
185 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
186 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
187 | XFEATURE_MASK_PKRU)
188
Sean Christopherson91661982020-03-02 15:57:06 -0800189u64 __read_mostly host_efer;
190EXPORT_SYMBOL_GPL(host_efer);
191
Aaron Lewis139a12c2019-10-21 16:30:25 -0700192static u64 __read_mostly host_xss;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100193u64 __read_mostly supported_xss;
194EXPORT_SYMBOL_GPL(supported_xss);
Aaron Lewis139a12c2019-10-21 16:30:25 -0700195
Hollis Blanchard417bc302007-10-31 17:24:23 -0500196struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200197 { "pf_fixed", VCPU_STAT(pf_fixed) },
198 { "pf_guest", VCPU_STAT(pf_guest) },
199 { "tlb_flush", VCPU_STAT(tlb_flush) },
200 { "invlpg", VCPU_STAT(invlpg) },
201 { "exits", VCPU_STAT(exits) },
202 { "io_exits", VCPU_STAT(io_exits) },
203 { "mmio_exits", VCPU_STAT(mmio_exits) },
204 { "signal_exits", VCPU_STAT(signal_exits) },
205 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800206 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200207 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100208 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200209 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +0200210 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200211 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530212 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200213 { "request_irq", VCPU_STAT(request_irq_exits) },
214 { "irq_exits", VCPU_STAT(irq_exits) },
215 { "host_state_reload", VCPU_STAT(host_state_reload) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200216 { "fpu_reload", VCPU_STAT(fpu_reload) },
217 { "insn_emulation", VCPU_STAT(insn_emulation) },
218 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300219 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200220 { "nmi_injections", VCPU_STAT(nmi_injections) },
Paolo Bonzini0f1e2612016-12-17 16:05:19 +0100221 { "req_event", VCPU_STAT(req_event) },
Paolo Bonzinic595cee2018-07-02 13:07:14 +0200222 { "l1d_flush", VCPU_STAT(l1d_flush) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200223 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
224 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
225 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
226 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
227 { "mmu_flooded", VM_STAT(mmu_flooded) },
228 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200229 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300230 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200231 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Paolo Bonzini833b45d2019-09-30 18:48:44 +0200232 { "largepages", VM_STAT(lpages, .mode = 0444) },
Paolo Bonzinib8e8c832019-11-04 12:22:02 +0100233 { "nx_largepages_splitted", VM_STAT(nx_lpage_splits, .mode = 0444) },
David Matlackf3414bc2016-12-20 15:25:57 -0800234 { "max_mmu_page_hash_collisions",
235 VM_STAT(max_mmu_page_hash_collisions) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500236 { NULL }
237};
238
Dexuan Cui2acf9232010-06-10 11:27:12 +0800239u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800240u64 __read_mostly supported_xcr0;
241EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800242
Marc Orrb666a4b2018-11-06 14:53:56 -0800243struct kmem_cache *x86_fpu_cache;
244EXPORT_SYMBOL_GPL(x86_fpu_cache);
245
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800246static struct kmem_cache *x86_emulator_cache;
247
248static struct kmem_cache *kvm_alloc_emulator_cache(void)
249{
Sean Christopherson06add252020-02-18 15:29:50 -0800250 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
251 unsigned int size = sizeof(struct x86_emulate_ctxt);
252
253 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800254 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800255 SLAB_ACCOUNT, useroffset,
256 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800257}
258
Jan Kiszkab6785de2012-09-20 07:43:17 +0200259static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300260
Gleb Natapovaf585b92010-10-14 11:22:46 +0200261static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
262{
263 int i;
264 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
265 vcpu->arch.apf.gfns[i] = ~0;
266}
267
Avi Kivity18863bd2009-09-07 11:12:18 +0300268static void kvm_on_user_return(struct user_return_notifier *urn)
269{
270 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300271 struct kvm_shared_msrs *locals
272 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800273 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700274 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300275
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700276 /*
277 * Disabling irqs at this point since the following code could be
278 * interrupted and executed through kvm_arch_hardware_disable()
279 */
280 local_irq_save(flags);
281 if (locals->registered) {
282 locals->registered = false;
283 user_return_notifier_unregister(urn);
284 }
285 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300286 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800287 values = &locals->values[slot];
288 if (values->host != values->curr) {
289 wrmsrl(shared_msrs_global.msrs[slot], values->host);
290 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300291 }
292 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300293}
294
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800295void kvm_define_shared_msr(unsigned slot, u32 msr)
296{
Nadav Amit0123be42014-07-24 15:06:56 +0300297 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200298 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300299 if (slot >= shared_msrs_global.nr)
300 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300301}
302EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
303
304static void kvm_shared_msr_cpu_online(void)
305{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800306 unsigned int cpu = smp_processor_id();
307 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
308 u64 value;
309 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300310
Sean Christopherson05c19c22019-11-22 12:04:50 -0800311 for (i = 0; i < shared_msrs_global.nr; ++i) {
312 rdmsrl_safe(shared_msrs_global.msrs[i], &value);
313 smsr->values[i].host = value;
314 smsr->values[i].curr = value;
315 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300316}
317
Andy Honig8b3c3102014-08-27 11:16:44 -0700318int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300319{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200320 unsigned int cpu = smp_processor_id();
321 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700322 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300323
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500324 value = (value & mask) | (smsr->values[slot].host & ~mask);
325 if (value == smsr->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700326 return 0;
Andy Honig8b3c3102014-08-27 11:16:44 -0700327 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
328 if (err)
329 return 1;
330
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500331 smsr->values[slot].curr = value;
Avi Kivity18863bd2009-09-07 11:12:18 +0300332 if (!smsr->registered) {
333 smsr->urn.on_user_return = kvm_on_user_return;
334 user_return_notifier_register(&smsr->urn);
335 smsr->registered = true;
336 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700337 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300338}
339EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
340
Radim Krčmář13a34e02014-08-28 15:13:03 +0200341static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200342{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200343 unsigned int cpu = smp_processor_id();
344 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200345
346 if (smsr->registered)
347 kvm_on_user_return(&smsr->urn);
348}
349
Carsten Otte6866b832007-10-29 16:09:10 +0100350u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
351{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300352 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100353}
354EXPORT_SYMBOL_GPL(kvm_get_apic_base);
355
Jim Mattson58871642018-05-09 16:56:04 -0400356enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
357{
358 return kvm_apic_mode(kvm_get_apic_base(vcpu));
359}
360EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
361
Jan Kiszka58cb6282014-01-24 16:48:44 +0100362int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100363{
Jim Mattson58871642018-05-09 16:56:04 -0400364 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
365 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200366 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
367 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100368
Jim Mattson58871642018-05-09 16:56:04 -0400369 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700370 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400371 if (!msr_info->host_initiated) {
372 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
373 return 1;
374 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
375 return 1;
376 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100377
378 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800379 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100380 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100381}
382EXPORT_SYMBOL_GPL(kvm_set_apic_base);
383
Andi Kleen2605fc22014-05-02 00:44:37 +0200384asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000385{
386 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200387 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000388}
389EXPORT_SYMBOL_GPL(kvm_spurious_fault);
390
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200391#define EXCPT_BENIGN 0
392#define EXCPT_CONTRIBUTORY 1
393#define EXCPT_PF 2
394
395static int exception_class(int vector)
396{
397 switch (vector) {
398 case PF_VECTOR:
399 return EXCPT_PF;
400 case DE_VECTOR:
401 case TS_VECTOR:
402 case NP_VECTOR:
403 case SS_VECTOR:
404 case GP_VECTOR:
405 return EXCPT_CONTRIBUTORY;
406 default:
407 break;
408 }
409 return EXCPT_BENIGN;
410}
411
Nadav Amitd6e8c852014-07-24 14:51:24 +0300412#define EXCPT_FAULT 0
413#define EXCPT_TRAP 1
414#define EXCPT_ABORT 2
415#define EXCPT_INTERRUPT 3
416
417static int exception_type(int vector)
418{
419 unsigned int mask;
420
421 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
422 return EXCPT_INTERRUPT;
423
424 mask = 1 << vector;
425
426 /* #DB is trap, as instruction watchpoints are handled elsewhere */
427 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
428 return EXCPT_TRAP;
429
430 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
431 return EXCPT_ABORT;
432
433 /* Reserved exceptions will result in fault */
434 return EXCPT_FAULT;
435}
436
Jim Mattsonda998b42018-10-16 14:29:22 -0700437void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
438{
439 unsigned nr = vcpu->arch.exception.nr;
440 bool has_payload = vcpu->arch.exception.has_payload;
441 unsigned long payload = vcpu->arch.exception.payload;
442
443 if (!has_payload)
444 return;
445
446 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700447 case DB_VECTOR:
448 /*
449 * "Certain debug exceptions may clear bit 0-3. The
450 * remaining contents of the DR6 register are never
451 * cleared by the processor".
452 */
453 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
454 /*
455 * DR6.RTM is set by all #DB exceptions that don't clear it.
456 */
457 vcpu->arch.dr6 |= DR6_RTM;
458 vcpu->arch.dr6 |= payload;
459 /*
460 * Bit 16 should be set in the payload whenever the #DB
461 * exception should clear DR6.RTM. This makes the payload
462 * compatible with the pending debug exceptions under VMX.
463 * Though not currently documented in the SDM, this also
464 * makes the payload compatible with the exit qualification
465 * for #DB exceptions under VMX.
466 */
467 vcpu->arch.dr6 ^= payload & DR6_RTM;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800468
469 /*
470 * The #DB payload is defined as compatible with the 'pending
471 * debug exceptions' field under VMX, not DR6. While bit 12 is
472 * defined in the 'pending debug exceptions' field (enabled
473 * breakpoint), it is reserved and must be zero in DR6.
474 */
475 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700476 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700477 case PF_VECTOR:
478 vcpu->arch.cr2 = payload;
479 break;
480 }
481
482 vcpu->arch.exception.has_payload = false;
483 vcpu->arch.exception.payload = 0;
484}
485EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
486
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200487static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200488 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700489 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200490{
491 u32 prev_nr;
492 int class1, class2;
493
Avi Kivity3842d132010-07-27 12:30:24 +0300494 kvm_make_request(KVM_REQ_EVENT, vcpu);
495
Wanpeng Li664f8e22017-08-24 03:35:09 -0700496 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200497 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200498 if (has_error && !is_protmode(vcpu))
499 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700500 if (reinject) {
501 /*
502 * On vmentry, vcpu->arch.exception.pending is only
503 * true if an event injection was blocked by
504 * nested_run_pending. In that case, however,
505 * vcpu_enter_guest requests an immediate exit,
506 * and the guest shouldn't proceed far enough to
507 * need reinjection.
508 */
509 WARN_ON_ONCE(vcpu->arch.exception.pending);
510 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700511 if (WARN_ON_ONCE(has_payload)) {
512 /*
513 * A reinjected event has already
514 * delivered its payload.
515 */
516 has_payload = false;
517 payload = 0;
518 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700519 } else {
520 vcpu->arch.exception.pending = true;
521 vcpu->arch.exception.injected = false;
522 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200523 vcpu->arch.exception.has_error_code = has_error;
524 vcpu->arch.exception.nr = nr;
525 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700526 vcpu->arch.exception.has_payload = has_payload;
527 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800528 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700529 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200530 return;
531 }
532
533 /* to check exception */
534 prev_nr = vcpu->arch.exception.nr;
535 if (prev_nr == DF_VECTOR) {
536 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300537 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200538 return;
539 }
540 class1 = exception_class(prev_nr);
541 class2 = exception_class(nr);
542 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
543 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700544 /*
545 * Generate double fault per SDM Table 5-5. Set
546 * exception.pending = true so that the double fault
547 * can trigger a nested vmexit.
548 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200549 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700550 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200551 vcpu->arch.exception.has_error_code = true;
552 vcpu->arch.exception.nr = DF_VECTOR;
553 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700554 vcpu->arch.exception.has_payload = false;
555 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200556 } else
557 /* replace previous exception with a new one in a hope
558 that instruction re-execution will regenerate lost
559 exception */
560 goto queue;
561}
562
Avi Kivity298101d2007-11-25 13:41:11 +0200563void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
564{
Jim Mattson91e86d22018-10-16 14:29:21 -0700565 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200566}
567EXPORT_SYMBOL_GPL(kvm_queue_exception);
568
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200569void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
570{
Jim Mattson91e86d22018-10-16 14:29:21 -0700571 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200572}
573EXPORT_SYMBOL_GPL(kvm_requeue_exception);
574
Jim Mattsonf10c7292018-10-16 14:29:23 -0700575static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
576 unsigned long payload)
577{
578 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
579}
580
Jim Mattsonda998b42018-10-16 14:29:22 -0700581static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
582 u32 error_code, unsigned long payload)
583{
584 kvm_multiple_exception(vcpu, nr, true, error_code,
585 true, payload, false);
586}
587
Kyle Huey6affcbe2016-11-29 12:40:40 -0800588int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200589{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100590 if (err)
591 kvm_inject_gp(vcpu, 0);
592 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800593 return kvm_skip_emulated_instruction(vcpu);
594
595 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100596}
597EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200598
Avi Kivity6389ee92010-11-29 16:12:30 +0200599void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200600{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200601 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700602 vcpu->arch.exception.nested_apf =
603 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700604 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700605 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700606 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
607 } else {
608 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
609 fault->address);
610 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200611}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300612EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200613
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200614static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200615{
Avi Kivity6389ee92010-11-29 16:12:30 +0200616 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
617 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200618 else
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +0200619 vcpu->arch.mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200620
621 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200622}
623
Sheng Yang3419ffc2008-05-15 09:52:48 +0800624void kvm_inject_nmi(struct kvm_vcpu *vcpu)
625{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300626 atomic_inc(&vcpu->arch.nmi_queued);
627 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800628}
629EXPORT_SYMBOL_GPL(kvm_inject_nmi);
630
Avi Kivity298101d2007-11-25 13:41:11 +0200631void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
632{
Jim Mattson91e86d22018-10-16 14:29:21 -0700633 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200634}
635EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
636
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200637void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
638{
Jim Mattson91e86d22018-10-16 14:29:21 -0700639 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200640}
641EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
642
Carsten Ottea03490e2007-10-29 16:09:35 +0100643/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300644 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
645 * a #GP and return false.
646 */
647bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200648{
Avi Kivity0a79b002009-09-01 12:03:25 +0300649 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
650 return true;
651 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
652 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100653}
Avi Kivity0a79b002009-09-01 12:03:25 +0300654EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100655
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300656bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
657{
658 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
659 return true;
660
661 kvm_queue_exception(vcpu, UD_VECTOR);
662 return false;
663}
664EXPORT_SYMBOL_GPL(kvm_require_dr);
665
Carsten Ottea03490e2007-10-29 16:09:35 +0100666/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200667 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200668 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200669 * can read from guest physical or from the guest's guest physical memory.
670 */
671int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
672 gfn_t ngfn, void *data, int offset, int len,
673 u32 access)
674{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200675 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200676 gfn_t real_gfn;
677 gpa_t ngpa;
678
679 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200680 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200681 if (real_gfn == UNMAPPED_GVA)
682 return -EFAULT;
683
684 real_gfn = gpa_to_gfn(real_gfn);
685
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200686 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200687}
688EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
689
Fengguang Wu69b00492015-01-19 22:33:39 +0800690static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200691 void *data, int offset, int len, u32 access)
692{
693 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
694 data, offset, len, access);
695}
696
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700697static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
698{
699 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
700 rsvd_bits(1, 2);
701}
702
Joerg Roedelec92fe42010-09-10 17:30:51 +0200703/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700704 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100705 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200706int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100707{
708 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
709 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
710 int i;
711 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200712 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100713
Joerg Roedelff03a072010-09-10 17:30:57 +0200714 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
715 offset * sizeof(u64), sizeof(pdpte),
716 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100717 if (ret < 0) {
718 ret = 0;
719 goto out;
720 }
721 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400722 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700723 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100724 ret = 0;
725 goto out;
726 }
727 }
728 ret = 1;
729
Joerg Roedelff03a072010-09-10 17:30:57 +0200730 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700731 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
732
Carsten Ottea03490e2007-10-29 16:09:35 +0100733out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100734
735 return ret;
736}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100737EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100738
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100739bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200740{
Joerg Roedelff03a072010-09-10 17:30:57 +0200741 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200742 int offset;
743 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200744 int r;
745
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200746 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200747 return false;
748
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700749 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300750 return true;
751
Paolo Bonzinia5121772017-07-24 18:54:38 +0200752 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
753 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200754 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
755 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200756 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800757 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200758
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800759 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200760}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100761EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200762
Avi Kivity49a9b072010-06-10 17:02:14 +0300763int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100764{
Sheng Yangaad82702010-05-12 16:40:42 +0800765 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800766 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800767
Avi Kivityf9a48e62010-01-06 19:10:22 +0200768 cr0 |= X86_CR0_ET;
769
Gleb Natapovab344822010-01-21 15:28:46 +0200770#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300771 if (cr0 & 0xffffffff00000000UL)
772 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200773#endif
774
775 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100776
Gleb Natapov0f122442010-04-28 19:15:31 +0300777 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
778 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100779
Gleb Natapov0f122442010-04-28 19:15:31 +0300780 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
781 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100782
783 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
784#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200785 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100786 int cs_db, cs_l;
787
Gleb Natapov0f122442010-04-28 19:15:31 +0300788 if (!is_pae(vcpu))
789 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100790 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300791 if (cs_l)
792 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100793 } else
794#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200795 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200796 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300797 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100798 }
799
Mao, Junjiead756a12012-07-02 01:18:48 +0000800 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
801 return 1;
802
Carsten Ottea03490e2007-10-29 16:09:35 +0100803 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100804
Lai Jiangshand170c412011-02-21 11:21:30 +0800805 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800806 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800807 kvm_async_pf_hash_reset(vcpu);
808 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800809
Sheng Yangaad82702010-05-12 16:40:42 +0800810 if ((cr0 ^ old_cr0) & update_bits)
811 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800812
Laszlo Ersek879ae182015-11-04 12:54:41 +0100813 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
814 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
815 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800816 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
817
Gleb Natapov0f122442010-04-28 19:15:31 +0300818 return 0;
819}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200820EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100821
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200822void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100823{
Avi Kivity49a9b072010-06-10 17:02:14 +0300824 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100825}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200826EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100827
Aaron Lewis139a12c2019-10-21 16:30:25 -0700828void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300829{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700830 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300831
Aaron Lewis139a12c2019-10-21 16:30:25 -0700832 if (vcpu->arch.xcr0 != host_xcr0)
833 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
834
835 if (vcpu->arch.xsaves_enabled &&
836 vcpu->arch.ia32_xss != host_xss)
837 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
838 }
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300839}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700840EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
841
842void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
843{
844 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
845
846 if (vcpu->arch.xcr0 != host_xcr0)
847 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
848
849 if (vcpu->arch.xsaves_enabled &&
850 vcpu->arch.ia32_xss != host_xss)
851 wrmsrl(MSR_IA32_XSS, host_xss);
852 }
853
854}
855EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300856
Fengguang Wu69b00492015-01-19 22:33:39 +0800857static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800858{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000859 u64 xcr0 = xcr;
860 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200861 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800862
863 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
864 if (index != XCR_XFEATURE_ENABLED_MASK)
865 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700866 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800867 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700868 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800869 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200870
871 /*
872 * Do not allow the guest to set bits that we do not support
873 * saving. However, xcr0 bit 0 is always set, even if the
874 * emulated CPU does not support XSAVE (see fx_init).
875 */
Dave Hansend91cab72015-09-02 16:31:26 -0700876 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200877 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800878 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200879
Dave Hansend91cab72015-09-02 16:31:26 -0700880 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
881 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000882 return 1;
883
Dave Hansend91cab72015-09-02 16:31:26 -0700884 if (xcr0 & XFEATURE_MASK_AVX512) {
885 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800886 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700887 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800888 return 1;
889 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800890 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000891
Dave Hansend91cab72015-09-02 16:31:26 -0700892 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000893 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800894 return 0;
895}
896
897int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
898{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000899 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
900 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800901 kvm_inject_gp(vcpu, 0);
902 return 1;
903 }
904 return 0;
905}
906EXPORT_SYMBOL_GPL(kvm_set_xcr);
907
Sean Christopherson345599f2019-12-10 14:44:16 -0800908#define __cr4_reserved_bits(__cpu_has, __c) \
909({ \
910 u64 __reserved_bits = CR4_RESERVED_BITS; \
911 \
912 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
913 __reserved_bits |= X86_CR4_OSXSAVE; \
914 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
915 __reserved_bits |= X86_CR4_SMEP; \
916 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
917 __reserved_bits |= X86_CR4_SMAP; \
918 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
919 __reserved_bits |= X86_CR4_FSGSBASE; \
920 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
921 __reserved_bits |= X86_CR4_PKE; \
922 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
923 __reserved_bits |= X86_CR4_LA57; \
Sean Christophersond76c7fb2020-01-28 15:53:44 -0800924 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
925 __reserved_bits |= X86_CR4_UMIP; \
Sean Christopherson345599f2019-12-10 14:44:16 -0800926 __reserved_bits; \
927})
928
Sean Christophersonb11306b2019-12-10 14:44:13 -0800929static u64 kvm_host_cr4_reserved_bits(struct cpuinfo_x86 *c)
930{
Sean Christopherson345599f2019-12-10 14:44:16 -0800931 u64 reserved_bits = __cr4_reserved_bits(cpu_has, c);
Sean Christophersonb11306b2019-12-10 14:44:13 -0800932
Sean Christophersonc10398b2020-03-02 15:56:46 -0800933 if (kvm_cpu_cap_has(X86_FEATURE_LA57))
Sean Christopherson345599f2019-12-10 14:44:16 -0800934 reserved_bits &= ~X86_CR4_LA57;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800935
Sean Christopherson90d2f602020-03-02 15:56:47 -0800936 if (kvm_cpu_cap_has(X86_FEATURE_UMIP))
Sean Christopherson345599f2019-12-10 14:44:16 -0800937 reserved_bits &= ~X86_CR4_UMIP;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800938
939 return reserved_bits;
940}
941
Wanpeng Li3ca94192019-09-18 17:50:10 +0800942static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
943{
Sean Christophersonb11306b2019-12-10 14:44:13 -0800944 if (cr4 & cr4_reserved_bits)
Wanpeng Li3ca94192019-09-18 17:50:10 +0800945 return -EINVAL;
946
Sean Christopherson345599f2019-12-10 14:44:16 -0800947 if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu))
Wanpeng Li3ca94192019-09-18 17:50:10 +0800948 return -EINVAL;
949
950 return 0;
951}
952
Avi Kivitya83b29c2010-06-10 17:02:15 +0300953int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100954{
Avi Kivityfc78f512009-12-07 12:16:48 +0200955 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800956 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800957 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800958
Wanpeng Li3ca94192019-09-18 17:50:10 +0800959 if (kvm_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200960 return 1;
961
Carsten Ottea03490e2007-10-29 16:09:35 +0100962 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300963 if (!(cr4 & X86_CR4_PAE))
964 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300965 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
966 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200967 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
968 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300969 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100970
Mao, Junjiead756a12012-07-02 01:18:48 +0000971 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200972 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000973 return 1;
974
975 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
976 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
977 return 1;
978 }
979
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300980 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300981 return 1;
982
Mao, Junjiead756a12012-07-02 01:18:48 +0000983 if (((cr4 ^ old_cr4) & pdptr_bits) ||
984 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800985 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300986
Huaitong Hanb9baba82016-03-22 16:51:21 +0800987 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200988 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800989
Gleb Natapov0f122442010-04-28 19:15:31 +0300990 return 0;
991}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200992EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100993
Avi Kivity23902182010-06-10 17:02:16 +0300994int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100995{
Junaid Shahidade61e22018-06-27 14:59:15 -0700996 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +0100997#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -0700998 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
999
Junaid Shahidade61e22018-06-27 14:59:15 -07001000 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001001 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1002 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001003 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001004#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001005
Avi Kivity9f8fe502010-12-05 17:30:00 +02001006 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001007 if (!skip_tlb_flush) {
1008 kvm_mmu_sync_roots(vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -07001009 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001010 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001011 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001012 }
1013
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001014 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -07001015 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001016 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02001017 else if (is_pae_paging(vcpu) &&
1018 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001019 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001020
Junaid Shahidade61e22018-06-27 14:59:15 -07001021 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001022 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001023 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001024
Gleb Natapov0f122442010-04-28 19:15:31 +03001025 return 0;
1026}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001027EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001028
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001029int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001030{
Gleb Natapov0f122442010-04-28 19:15:31 +03001031 if (cr8 & CR8_RESERVED_BITS)
1032 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001033 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001034 kvm_lapic_set_tpr(vcpu, cr8);
1035 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001036 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001037 return 0;
1038}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001039EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001040
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001041unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001042{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001043 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001044 return kvm_lapic_get_cr8(vcpu);
1045 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001046 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001047}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001048EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001049
Nadav Amitae561ed2015-04-02 03:10:37 +03001050static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1051{
1052 int i;
1053
1054 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1055 for (i = 0; i < KVM_NR_DB_REGS; i++)
1056 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1057 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1058 }
1059}
1060
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001061static void kvm_update_dr6(struct kvm_vcpu *vcpu)
1062{
1063 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1064 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
1065}
1066
Jan Kiszkac8639012012-09-21 05:42:55 +02001067static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1068{
1069 unsigned long dr7;
1070
1071 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1072 dr7 = vcpu->arch.guest_debug_dr7;
1073 else
1074 dr7 = vcpu->arch.dr7;
1075 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001076 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1077 if (dr7 & DR7_BP_EN_MASK)
1078 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001079}
1080
Nadav Amit6f43ed02014-07-15 17:37:46 +03001081static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1082{
1083 u64 fixed = DR6_FIXED_1;
1084
Radim Krčmářd6321d42017-08-05 00:12:49 +02001085 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001086 fixed |= DR6_RTM;
1087 return fixed;
1088}
1089
Gleb Natapov338dbc92010-04-28 19:15:32 +03001090static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001091{
Marios Pomonisea740052019-12-11 12:47:52 -08001092 size_t size = ARRAY_SIZE(vcpu->arch.db);
1093
Gleb Natapov020df072010-04-13 10:05:23 +03001094 switch (dr) {
1095 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001096 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001097 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1098 vcpu->arch.eff_db[dr] = val;
1099 break;
1100 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001101 /* fall through */
1102 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001103 if (val & 0xffffffff00000000ULL)
1104 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001105 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001106 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001107 break;
1108 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001109 /* fall through */
1110 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001111 if (!kvm_dr7_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001112 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001113 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001114 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001115 break;
1116 }
1117
1118 return 0;
1119}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001120
1121int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1122{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001123 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001124 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001125 return 1;
1126 }
1127 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001128}
Gleb Natapov020df072010-04-13 10:05:23 +03001129EXPORT_SYMBOL_GPL(kvm_set_dr);
1130
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001131int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001132{
Marios Pomonisea740052019-12-11 12:47:52 -08001133 size_t size = ARRAY_SIZE(vcpu->arch.db);
1134
Gleb Natapov020df072010-04-13 10:05:23 +03001135 switch (dr) {
1136 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001137 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001138 break;
1139 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001140 /* fall through */
1141 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001142 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1143 *val = vcpu->arch.dr6;
1144 else
1145 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001146 break;
1147 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001148 /* fall through */
1149 default: /* 7 */
1150 *val = vcpu->arch.dr7;
1151 break;
1152 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001153 return 0;
1154}
Gleb Natapov020df072010-04-13 10:05:23 +03001155EXPORT_SYMBOL_GPL(kvm_get_dr);
1156
Avi Kivity022cd0e2011-11-10 14:57:23 +02001157bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1158{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001159 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001160 u64 data;
1161 int err;
1162
Wei Huangc6702c92015-06-19 13:44:45 +02001163 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001164 if (err)
1165 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001166 kvm_rax_write(vcpu, (u32)data);
1167 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001168 return err;
1169}
1170EXPORT_SYMBOL_GPL(kvm_rdpmc);
1171
Carsten Otte043405e2007-10-10 17:16:19 +02001172/*
1173 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1174 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1175 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001176 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1177 * extract the supported MSRs from the related const lists.
1178 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001179 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001180 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001181 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001182 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001183
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001184static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001185 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001186 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001187#ifdef CONFIG_X86_64
1188 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1189#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001190 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001191 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001192 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001193 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1194 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1195 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1196 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1197 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1198 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001199 MSR_IA32_UMWAIT_CONTROL,
1200
Jim Mattsone2ada662019-08-21 11:20:04 -07001201 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1202 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1203 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1204 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1205 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1206 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1207 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1208 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1209 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1210 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1211 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1212 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1213 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001214 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1215 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1216 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1217 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1218 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1219 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1220 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1221 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1222 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001223};
1224
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001225static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001226static unsigned num_msrs_to_save;
1227
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001228static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001229 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1230 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1231 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1232 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001233 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001234 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1235 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001236 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001237 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001238 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001239 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001240 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001241 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001242 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1243 HV_X64_MSR_TSC_EMULATION_STATUS,
1244
1245 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001246 MSR_KVM_PV_EOI_EN,
1247
Will Auldba904632012-11-29 12:42:50 -08001248 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001249 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001250 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001251 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001252 MSR_IA32_MCG_STATUS,
1253 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001254 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001255 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001256 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001257 MSR_PLATFORM_INFO,
1258 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001259 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001260 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001261 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001262
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001263 /*
1264 * The following list leaves out MSRs whose values are determined
1265 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1266 * We always support the "true" VMX control MSRs, even if the host
1267 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001268 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001269 */
1270 MSR_IA32_VMX_BASIC,
1271 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1272 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1273 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1274 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1275 MSR_IA32_VMX_MISC,
1276 MSR_IA32_VMX_CR0_FIXED0,
1277 MSR_IA32_VMX_CR4_FIXED0,
1278 MSR_IA32_VMX_VMCS_ENUM,
1279 MSR_IA32_VMX_PROCBASED_CTLS2,
1280 MSR_IA32_VMX_EPT_VPID_CAP,
1281 MSR_IA32_VMX_VMFUNC,
1282
Borislav Petkov191c8132019-04-18 18:32:50 +02001283 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001284 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001285};
1286
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001287static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001288static unsigned num_emulated_msrs;
1289
Tom Lendacky801e4592018-02-21 13:39:51 -06001290/*
1291 * List of msr numbers which are used to expose MSR-based features that
1292 * can be used by a hypervisor to validate requested CPU features.
1293 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001294static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001295 MSR_IA32_VMX_BASIC,
1296 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1297 MSR_IA32_VMX_PINBASED_CTLS,
1298 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1299 MSR_IA32_VMX_PROCBASED_CTLS,
1300 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1301 MSR_IA32_VMX_EXIT_CTLS,
1302 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1303 MSR_IA32_VMX_ENTRY_CTLS,
1304 MSR_IA32_VMX_MISC,
1305 MSR_IA32_VMX_CR0_FIXED0,
1306 MSR_IA32_VMX_CR0_FIXED1,
1307 MSR_IA32_VMX_CR4_FIXED0,
1308 MSR_IA32_VMX_CR4_FIXED1,
1309 MSR_IA32_VMX_VMCS_ENUM,
1310 MSR_IA32_VMX_PROCBASED_CTLS2,
1311 MSR_IA32_VMX_EPT_VPID_CAP,
1312 MSR_IA32_VMX_VMFUNC,
1313
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001314 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001315 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001316 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001317};
1318
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001319static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001320static unsigned int num_msr_based_features;
1321
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001322static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001323{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001324 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001325
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001326 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1327 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001328
1329 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001330 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1331 * the nested hypervisor runs with NX huge pages. If it is not,
1332 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1333 * L1 guests, so it need not worry about its own (L2) guests.
1334 */
1335 data |= ARCH_CAP_PSCHANGE_MC_NO;
1336
1337 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001338 * If we're doing cache flushes (either "always" or "cond")
1339 * we will do one whenever the guest does a vmlaunch/vmresume.
1340 * If an outer hypervisor is doing the cache flush for us
1341 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1342 * capability to the guest too, and if EPT is disabled we're not
1343 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1344 * require a nested hypervisor to do a flush of its own.
1345 */
1346 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1347 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1348
Paolo Bonzini0c549142019-08-19 17:24:07 +02001349 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1350 data |= ARCH_CAP_RDCL_NO;
1351 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1352 data |= ARCH_CAP_SSB_NO;
1353 if (!boot_cpu_has_bug(X86_BUG_MDS))
1354 data |= ARCH_CAP_MDS_NO;
1355
Pawan Guptae1d38b62019-10-23 12:23:33 +02001356 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001357 * On TAA affected systems:
1358 * - nothing to do if TSX is disabled on the host.
1359 * - we emulate TSX_CTRL if present on the host.
1360 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001361 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001362 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001363 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001364 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1365 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001366
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001367 return data;
1368}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001369
Wanpeng Li66421c12018-02-28 14:03:30 +08001370static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1371{
1372 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001373 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001374 msr->data = kvm_get_arch_capabilities();
1375 break;
1376 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001377 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001378 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001379 default:
1380 if (kvm_x86_ops->get_msr_feature(msr))
1381 return 1;
1382 }
1383 return 0;
1384}
1385
Tom Lendacky801e4592018-02-21 13:39:51 -06001386static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1387{
1388 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001389 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001390
1391 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001392 r = kvm_get_msr_feature(&msr);
1393 if (r)
1394 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001395
1396 *data = msr.data;
1397
1398 return 0;
1399}
1400
Sean Christopherson11988492019-04-02 08:19:15 -07001401static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1402{
1403 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1404 return false;
1405
1406 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1407 return false;
1408
Sean Christopherson0a629562019-04-02 08:19:16 -07001409 if (efer & (EFER_LME | EFER_LMA) &&
1410 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1411 return false;
1412
1413 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1414 return false;
1415
Sean Christopherson11988492019-04-02 08:19:15 -07001416 return true;
1417
1418}
Jan Kiszka384bb782013-04-20 10:52:36 +02001419bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001420{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001421 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001422 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001423
Sean Christopherson11988492019-04-02 08:19:15 -07001424 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001425}
1426EXPORT_SYMBOL_GPL(kvm_valid_efer);
1427
Sean Christopherson11988492019-04-02 08:19:15 -07001428static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001429{
1430 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001431 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001432
Sean Christopherson11988492019-04-02 08:19:15 -07001433 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001434 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001435
Sean Christopherson11988492019-04-02 08:19:15 -07001436 if (!msr_info->host_initiated) {
1437 if (!__kvm_valid_efer(vcpu, efer))
1438 return 1;
1439
1440 if (is_paging(vcpu) &&
1441 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1442 return 1;
1443 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001444
Carsten Otte15c4a642007-10-30 18:44:17 +01001445 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001446 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001447
Sheng Yanga3d204e2010-05-12 16:40:40 +08001448 kvm_x86_ops->set_efer(vcpu, efer);
1449
Sheng Yangaad82702010-05-12 16:40:42 +08001450 /* Update reserved bits */
1451 if ((efer ^ old_efer) & EFER_NX)
1452 kvm_mmu_reset_context(vcpu);
1453
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001454 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001455}
1456
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001457void kvm_enable_efer_bits(u64 mask)
1458{
1459 efer_reserved_bits &= ~mask;
1460}
1461EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1462
Carsten Otte15c4a642007-10-30 18:44:17 +01001463/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001464 * Write @data into the MSR specified by @index. Select MSR specific fault
1465 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001466 * Returns 0 on success, non-0 otherwise.
1467 * Assumes vcpu_load() was already called.
1468 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001469static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1470 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001471{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001472 struct msr_data msr;
1473
1474 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001475 case MSR_FS_BASE:
1476 case MSR_GS_BASE:
1477 case MSR_KERNEL_GS_BASE:
1478 case MSR_CSTAR:
1479 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001480 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001481 return 1;
1482 break;
1483 case MSR_IA32_SYSENTER_EIP:
1484 case MSR_IA32_SYSENTER_ESP:
1485 /*
1486 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1487 * non-canonical address is written on Intel but not on
1488 * AMD (which ignores the top 32-bits, because it does
1489 * not implement 64-bit SYSENTER).
1490 *
1491 * 64-bit code should hence be able to write a non-canonical
1492 * value on AMD. Making the address canonical ensures that
1493 * vmentry does not fail on Intel after writing a non-canonical
1494 * value, and that something deterministic happens if the guest
1495 * invokes 64-bit SYSENTER.
1496 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001497 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001498 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001499
1500 msr.data = data;
1501 msr.index = index;
1502 msr.host_initiated = host_initiated;
1503
1504 return kvm_x86_ops->set_msr(vcpu, &msr);
1505}
1506
1507/*
1508 * Read the MSR specified by @index into @data. Select MSR specific fault
1509 * checks are bypassed if @host_initiated is %true.
1510 * Returns 0 on success, non-0 otherwise.
1511 * Assumes vcpu_load() was already called.
1512 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001513int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1514 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001515{
1516 struct msr_data msr;
1517 int ret;
1518
1519 msr.index = index;
1520 msr.host_initiated = host_initiated;
1521
1522 ret = kvm_x86_ops->get_msr(vcpu, &msr);
1523 if (!ret)
1524 *data = msr.data;
1525 return ret;
1526}
1527
1528int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1529{
1530 return __kvm_get_msr(vcpu, index, data, false);
1531}
1532EXPORT_SYMBOL_GPL(kvm_get_msr);
1533
1534int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1535{
1536 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001537}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001538EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001539
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001540int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1541{
1542 u32 ecx = kvm_rcx_read(vcpu);
1543 u64 data;
1544
1545 if (kvm_get_msr(vcpu, ecx, &data)) {
1546 trace_kvm_msr_read_ex(ecx);
1547 kvm_inject_gp(vcpu, 0);
1548 return 1;
1549 }
1550
1551 trace_kvm_msr_read(ecx, data);
1552
1553 kvm_rax_write(vcpu, data & -1u);
1554 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1555 return kvm_skip_emulated_instruction(vcpu);
1556}
1557EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1558
1559int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1560{
1561 u32 ecx = kvm_rcx_read(vcpu);
1562 u64 data = kvm_read_edx_eax(vcpu);
1563
1564 if (kvm_set_msr(vcpu, ecx, data)) {
1565 trace_kvm_msr_write_ex(ecx, data);
1566 kvm_inject_gp(vcpu, 0);
1567 return 1;
1568 }
1569
1570 trace_kvm_msr_write(ecx, data);
1571 return kvm_skip_emulated_instruction(vcpu);
1572}
1573EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1574
Carsten Otte313a3dc2007-10-11 19:16:52 +02001575/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001576 * The fast path for frequent and performance sensitive wrmsr emulation,
1577 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1578 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1579 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1580 * other cases which must be called after interrupts are enabled on the host.
1581 */
1582static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1583{
1584 if (lapic_in_kernel(vcpu) && apic_x2apic_mode(vcpu->arch.apic) &&
1585 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
1586 ((data & APIC_MODE_MASK) == APIC_DM_FIXED)) {
1587
1588 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
1589 return kvm_lapic_reg_write(vcpu->arch.apic, APIC_ICR, (u32)data);
1590 }
1591
1592 return 1;
1593}
1594
1595enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
1596{
1597 u32 msr = kvm_rcx_read(vcpu);
1598 u64 data = kvm_read_edx_eax(vcpu);
1599 int ret = 0;
1600
1601 switch (msr) {
1602 case APIC_BASE_MSR + (APIC_ICR >> 4):
1603 ret = handle_fastpath_set_x2apic_icr_irqoff(vcpu, data);
1604 break;
1605 default:
1606 return EXIT_FASTPATH_NONE;
1607 }
1608
1609 if (!ret) {
1610 trace_kvm_msr_write(msr, data);
1611 return EXIT_FASTPATH_SKIP_EMUL_INS;
1612 }
1613
1614 return EXIT_FASTPATH_NONE;
1615}
1616EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1617
1618/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001619 * Adapt set_msr() to msr_io()'s calling convention
1620 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001621static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1622{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001623 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001624}
1625
Carsten Otte313a3dc2007-10-11 19:16:52 +02001626static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1627{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001628 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001629}
1630
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001631#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001632struct pvclock_clock {
1633 int vclock_mode;
1634 u64 cycle_last;
1635 u64 mask;
1636 u32 mult;
1637 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001638 u64 base_cycles;
1639 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001640};
1641
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001642struct pvclock_gtod_data {
1643 seqcount_t seq;
1644
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001645 struct pvclock_clock clock; /* extract of a clocksource struct */
1646 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001647
Paolo Bonzini917f9472020-01-22 14:32:20 +01001648 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001649 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001650};
1651
1652static struct pvclock_gtod_data pvclock_gtod_data;
1653
1654static void update_pvclock_gtod(struct timekeeper *tk)
1655{
1656 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1657
1658 write_seqcount_begin(&vdata->seq);
1659
1660 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001661 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1662 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1663 vdata->clock.mask = tk->tkr_mono.mask;
1664 vdata->clock.mult = tk->tkr_mono.mult;
1665 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001666 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1667 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001668
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001669 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->archdata.vclock_mode;
1670 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1671 vdata->raw_clock.mask = tk->tkr_raw.mask;
1672 vdata->raw_clock.mult = tk->tkr_raw.mult;
1673 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001674 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1675 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001676
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001677 vdata->wall_time_sec = tk->xtime_sec;
1678
Paolo Bonzini917f9472020-01-22 14:32:20 +01001679 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001680
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001681 write_seqcount_end(&vdata->seq);
1682}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001683
1684static s64 get_kvmclock_base_ns(void)
1685{
1686 /* Count up from boot time, but with the frequency of the raw clock. */
1687 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1688}
1689#else
1690static s64 get_kvmclock_base_ns(void)
1691{
1692 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1693 return ktime_get_boottime_ns();
1694}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001695#endif
1696
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001697void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1698{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001699 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001700 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001701}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001702
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001703static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1704{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001705 int version;
1706 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001707 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001708 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001709
1710 if (!wall_clock)
1711 return;
1712
Avi Kivity9ed3c442010-05-04 15:00:37 +03001713 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1714 if (r)
1715 return;
1716
1717 if (version & 1)
1718 ++version; /* first time write, random junk */
1719
1720 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001721
Nicholas Krause1dab1342015-12-30 13:08:46 -05001722 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1723 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001724
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001725 /*
1726 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001727 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001728 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001729 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001730 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001731
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001732 wc.nsec = do_div(wall_nsec, 1000000000);
1733 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001734 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001735
1736 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1737
1738 version++;
1739 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001740}
1741
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001742static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1743{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001744 do_shl32_div32(dividend, divisor);
1745 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001746}
1747
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001748static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001749 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001750{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001751 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001752 int32_t shift = 0;
1753 uint64_t tps64;
1754 uint32_t tps32;
1755
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001756 tps64 = base_hz;
1757 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001758 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001759 tps64 >>= 1;
1760 shift--;
1761 }
1762
1763 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001764 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1765 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001766 scaled64 >>= 1;
1767 else
1768 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001769 shift++;
1770 }
1771
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001772 *pshift = shift;
1773 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001774}
1775
Marcelo Tosattid8281992012-11-27 23:29:01 -02001776#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001777static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001778#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001779
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001780static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001781static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001782
Zachary Amsdencc578282012-02-03 15:43:50 -02001783static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001784{
Zachary Amsdencc578282012-02-03 15:43:50 -02001785 u64 v = (u64)khz * (1000000 + ppm);
1786 do_div(v, 1000000);
1787 return v;
1788}
1789
Haozhong Zhang381d5852015-10-20 15:39:04 +08001790static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1791{
1792 u64 ratio;
1793
1794 /* Guest TSC same frequency as host TSC? */
1795 if (!scale) {
1796 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1797 return 0;
1798 }
1799
1800 /* TSC scaling supported? */
1801 if (!kvm_has_tsc_control) {
1802 if (user_tsc_khz > tsc_khz) {
1803 vcpu->arch.tsc_catchup = 1;
1804 vcpu->arch.tsc_always_catchup = 1;
1805 return 0;
1806 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001807 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001808 return -1;
1809 }
1810 }
1811
1812 /* TSC scaling required - calculate ratio */
1813 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1814 user_tsc_khz, tsc_khz);
1815
1816 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001817 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1818 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001819 return -1;
1820 }
1821
1822 vcpu->arch.tsc_scaling_ratio = ratio;
1823 return 0;
1824}
1825
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001826static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001827{
1828 u32 thresh_lo, thresh_hi;
1829 int use_scaling = 0;
1830
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001831 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001832 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001833 /* set tsc_scaling_ratio to a safe value */
1834 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001835 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001836 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001837
Zachary Amsdenc2855452010-09-18 14:38:15 -10001838 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001839 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001840 &vcpu->arch.virtual_tsc_shift,
1841 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001842 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001843
1844 /*
1845 * Compute the variation in TSC rate which is acceptable
1846 * within the range of tolerance and decide if the
1847 * rate being applied is within that bounds of the hardware
1848 * rate. If so, no scaling or compensation need be done.
1849 */
1850 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1851 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001852 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1853 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 -02001854 use_scaling = 1;
1855 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001856 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001857}
1858
1859static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1860{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001861 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001862 vcpu->arch.virtual_tsc_mult,
1863 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001864 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001865 return tsc;
1866}
1867
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001868static inline int gtod_is_based_on_tsc(int mode)
1869{
1870 return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK;
1871}
1872
Fengguang Wu69b00492015-01-19 22:33:39 +08001873static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001874{
1875#ifdef CONFIG_X86_64
1876 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001877 struct kvm_arch *ka = &vcpu->kvm->arch;
1878 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1879
1880 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1881 atomic_read(&vcpu->kvm->online_vcpus));
1882
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001883 /*
1884 * Once the masterclock is enabled, always perform request in
1885 * order to update it.
1886 *
1887 * In order to enable masterclock, the host clocksource must be TSC
1888 * and the vcpus need to have matched TSCs. When that happens,
1889 * perform request to enable masterclock.
1890 */
1891 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001892 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001893 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1894
1895 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1896 atomic_read(&vcpu->kvm->online_vcpus),
1897 ka->use_master_clock, gtod->clock.vclock_mode);
1898#endif
1899}
1900
Will Auldba904632012-11-29 12:42:50 -08001901static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1902{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001903 u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001904 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1905}
1906
Haozhong Zhang35181e82015-10-20 15:39:03 +08001907/*
1908 * Multiply tsc by a fixed point number represented by ratio.
1909 *
1910 * The most significant 64-N bits (mult) of ratio represent the
1911 * integral part of the fixed point number; the remaining N bits
1912 * (frac) represent the fractional part, ie. ratio represents a fixed
1913 * point number (mult + frac * 2^(-N)).
1914 *
1915 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1916 */
1917static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1918{
1919 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1920}
1921
1922u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1923{
1924 u64 _tsc = tsc;
1925 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1926
1927 if (ratio != kvm_default_tsc_scaling_ratio)
1928 _tsc = __scale_tsc(ratio, tsc);
1929
1930 return _tsc;
1931}
1932EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1933
Haozhong Zhang07c14192015-10-20 15:39:05 +08001934static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1935{
1936 u64 tsc;
1937
1938 tsc = kvm_scale_tsc(vcpu, rdtsc());
1939
1940 return target_tsc - tsc;
1941}
1942
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001943u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1944{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001945 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1946
1947 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001948}
1949EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1950
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001951static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1952{
Leonid Shatz326e7422018-11-06 12:14:25 +02001953 vcpu->arch.tsc_offset = kvm_x86_ops->write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001954}
1955
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001956static inline bool kvm_check_tsc_unstable(void)
1957{
1958#ifdef CONFIG_X86_64
1959 /*
1960 * TSC is marked unstable when we're running on Hyper-V,
1961 * 'TSC page' clocksource is good.
1962 */
1963 if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK)
1964 return false;
1965#endif
1966 return check_tsc_unstable();
1967}
1968
Will Auld8fe8ab42012-11-29 12:42:12 -08001969void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001970{
1971 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001972 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001973 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001974 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001975 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001976 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001977 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001978
Jan Kiszka038f8c12011-02-04 10:49:11 +01001979 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001980 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001981 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001982 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001983
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001984 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03001985 if (data == 0 && msr->host_initiated) {
1986 /*
1987 * detection of vcpu initialization -- need to sync
1988 * with other vCPUs. This particularly helps to keep
1989 * kvm_clock stable after CPU hotplug
1990 */
1991 synchronizing = true;
1992 } else {
1993 u64 tsc_exp = kvm->arch.last_tsc_write +
1994 nsec_to_cycles(vcpu, elapsed);
1995 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
1996 /*
1997 * Special case: TSC write with a small delta (1 second)
1998 * of virtual cycle time against real time is
1999 * interpreted as an attempt to synchronize the CPU.
2000 */
2001 synchronizing = data < tsc_exp + tsc_hz &&
2002 data + tsc_hz > tsc_exp;
2003 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002004 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002005
2006 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002007 * For a reliable TSC, we can match TSC offsets, and for an unstable
2008 * TSC, we add elapsed time in this computation. We could let the
2009 * compensation code attempt to catch up if we fall behind, but
2010 * it's better to try to match offsets from the beginning.
2011 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002012 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002013 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002014 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002015 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002016 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002017 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002018 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002019 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002020 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002021 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002022 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002023 } else {
2024 /*
2025 * We split periods of matched TSC writes into generations.
2026 * For each generation, we track the original measured
2027 * nanosecond time, offset, and write, so if TSCs are in
2028 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002029 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002030 *
2031 * These values are tracked in kvm->arch.cur_xxx variables.
2032 */
2033 kvm->arch.cur_tsc_generation++;
2034 kvm->arch.cur_tsc_nsec = ns;
2035 kvm->arch.cur_tsc_write = data;
2036 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002037 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002038 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002039
2040 /*
2041 * We also track th most recent recorded KHZ, write and time to
2042 * allow the matching interval to be extended at each write.
2043 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002044 kvm->arch.last_tsc_nsec = ns;
2045 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002046 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002047
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002048 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002049
2050 /* Keep track of which generation this VCPU has synchronized to */
2051 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2052 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2053 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2054
Radim Krčmářd6321d42017-08-05 00:12:49 +02002055 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08002056 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002057
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002058 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002059 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002060
2061 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002062 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002063 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002064 } else if (!already_matched) {
2065 kvm->arch.nr_vcpus_matched_tsc++;
2066 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002067
2068 kvm_track_tsc_matching(vcpu);
2069 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002070}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002071
Zachary Amsden99e3e302010-08-19 22:07:17 -10002072EXPORT_SYMBOL_GPL(kvm_write_tsc);
2073
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002074static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2075 s64 adjustment)
2076{
Leonid Shatz326e7422018-11-06 12:14:25 +02002077 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
2078 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002079}
2080
2081static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2082{
2083 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2084 WARN_ON(adjustment < 0);
2085 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002086 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002087}
2088
Marcelo Tosattid8281992012-11-27 23:29:01 -02002089#ifdef CONFIG_X86_64
2090
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002091static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002092{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002093 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002094 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002095
2096 if (likely(ret >= last))
2097 return ret;
2098
2099 /*
2100 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002101 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002102 * very likely) and there's a data dependence, so force GCC
2103 * to generate a branch instead. I don't barrier() because
2104 * we don't actually need a barrier, and if this function
2105 * ever gets inlined it will generate worse code.
2106 */
2107 asm volatile ("");
2108 return last;
2109}
2110
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002111static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2112 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002113{
2114 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002115 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002116
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002117 switch (clock->vclock_mode) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002118 case VCLOCK_HVCLOCK:
2119 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2120 tsc_timestamp);
2121 if (tsc_pg_val != U64_MAX) {
2122 /* TSC page valid */
2123 *mode = VCLOCK_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002124 v = (tsc_pg_val - clock->cycle_last) &
2125 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002126 } else {
2127 /* TSC page invalid */
2128 *mode = VCLOCK_NONE;
2129 }
2130 break;
2131 case VCLOCK_TSC:
2132 *mode = VCLOCK_TSC;
2133 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002134 v = (*tsc_timestamp - clock->cycle_last) &
2135 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002136 break;
2137 default:
2138 *mode = VCLOCK_NONE;
2139 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002140
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002141 if (*mode == VCLOCK_NONE)
2142 *tsc_timestamp = v = 0;
2143
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002144 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002145}
2146
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002147static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002148{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002149 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002150 unsigned long seq;
2151 int mode;
2152 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002153
Marcelo Tosattid8281992012-11-27 23:29:01 -02002154 do {
2155 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002156 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002157 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002158 ns >>= gtod->raw_clock.shift;
2159 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002160 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002161 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002162
2163 return mode;
2164}
2165
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002166static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002167{
2168 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2169 unsigned long seq;
2170 int mode;
2171 u64 ns;
2172
2173 do {
2174 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002175 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002176 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002177 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002178 ns >>= gtod->clock.shift;
2179 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2180
2181 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2182 ts->tv_nsec = ns;
2183
2184 return mode;
2185}
2186
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002187/* returns true if host is using TSC based clocksource */
2188static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002189{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002190 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002191 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002192 return false;
2193
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002194 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002195 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002196}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002197
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002198/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002199static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002200 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002201{
2202 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002203 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002204 return false;
2205
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002206 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002207}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002208#endif
2209
2210/*
2211 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002212 * Assuming a stable TSC across physical CPUS, and a stable TSC
2213 * across virtual CPUs, the following condition is possible.
2214 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002215 * CPUs at the next numbered event.
2216 *
2217 * "timespecX" represents host monotonic time. "tscX" represents
2218 * RDTSC value.
2219 *
2220 * VCPU0 on CPU0 | VCPU1 on CPU1
2221 *
2222 * 1. read timespec0,tsc0
2223 * 2. | timespec1 = timespec0 + N
2224 * | tsc1 = tsc0 + M
2225 * 3. transition to guest | transition to guest
2226 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2227 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2228 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2229 *
2230 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2231 *
2232 * - ret0 < ret1
2233 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2234 * ...
2235 * - 0 < N - M => M < N
2236 *
2237 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2238 * always the case (the difference between two distinct xtime instances
2239 * might be smaller then the difference between corresponding TSC reads,
2240 * when updating guest vcpus pvclock areas).
2241 *
2242 * To avoid that problem, do not allow visibility of distinct
2243 * system_timestamp/tsc_timestamp values simultaneously: use a master
2244 * copy of host monotonic time values. Update that master copy
2245 * in lockstep.
2246 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002247 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002248 *
2249 */
2250
2251static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2252{
2253#ifdef CONFIG_X86_64
2254 struct kvm_arch *ka = &kvm->arch;
2255 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002256 bool host_tsc_clocksource, vcpus_matched;
2257
2258 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2259 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002260
2261 /*
2262 * If the host uses TSC clock, then passthrough TSC as stable
2263 * to the guest.
2264 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002265 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002266 &ka->master_kernel_ns,
2267 &ka->master_cycle_now);
2268
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002269 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002270 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002271 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002272
Marcelo Tosattid8281992012-11-27 23:29:01 -02002273 if (ka->use_master_clock)
2274 atomic_set(&kvm_guest_has_master_clock, 1);
2275
2276 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002277 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2278 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002279#endif
2280}
2281
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002282void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2283{
2284 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2285}
2286
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002287static void kvm_gen_update_masterclock(struct kvm *kvm)
2288{
2289#ifdef CONFIG_X86_64
2290 int i;
2291 struct kvm_vcpu *vcpu;
2292 struct kvm_arch *ka = &kvm->arch;
2293
2294 spin_lock(&ka->pvclock_gtod_sync_lock);
2295 kvm_make_mclock_inprogress_request(kvm);
2296 /* no guest entries from this point */
2297 pvclock_update_vm_gtod_copy(kvm);
2298
2299 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002300 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002301
2302 /* guest entries allowed */
2303 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002304 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002305
2306 spin_unlock(&ka->pvclock_gtod_sync_lock);
2307#endif
2308}
2309
Marcelo Tosattie891a322017-04-17 12:51:37 -03002310u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002311{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002312 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002313 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002314 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002315
Paolo Bonzini8b953442016-11-16 18:31:30 +01002316 spin_lock(&ka->pvclock_gtod_sync_lock);
2317 if (!ka->use_master_clock) {
2318 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002319 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002320 }
2321
Paolo Bonzini8b953442016-11-16 18:31:30 +01002322 hv_clock.tsc_timestamp = ka->master_cycle_now;
2323 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2324 spin_unlock(&ka->pvclock_gtod_sync_lock);
2325
Wanpeng Lie2c22062017-05-11 18:12:05 -07002326 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2327 get_cpu();
2328
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002329 if (__this_cpu_read(cpu_tsc_khz)) {
2330 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2331 &hv_clock.tsc_shift,
2332 &hv_clock.tsc_to_system_mul);
2333 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2334 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002335 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002336
2337 put_cpu();
2338
2339 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002340}
2341
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002342static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2343{
2344 struct kvm_vcpu_arch *vcpu = &v->arch;
2345 struct pvclock_vcpu_time_info guest_hv_clock;
2346
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002347 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002348 &guest_hv_clock, sizeof(guest_hv_clock))))
2349 return;
2350
2351 /* This VCPU is paused, but it's legal for a guest to read another
2352 * VCPU's kvmclock, so we really have to follow the specification where
2353 * it says that version is odd if data is being modified, and even after
2354 * it is consistent.
2355 *
2356 * Version field updates must be kept separate. This is because
2357 * kvm_write_guest_cached might use a "rep movs" instruction, and
2358 * writes within a string instruction are weakly ordered. So there
2359 * are three writes overall.
2360 *
2361 * As a small optimization, only write the version field in the first
2362 * and third write. The vcpu->pv_time cache is still valid, because the
2363 * version field is the first in the struct.
2364 */
2365 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2366
Liran Alon51c4b8b2017-11-05 16:11:30 +02002367 if (guest_hv_clock.version & 1)
2368 ++guest_hv_clock.version; /* first time write, random junk */
2369
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002370 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002371 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2372 &vcpu->hv_clock,
2373 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002374
2375 smp_wmb();
2376
2377 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2378 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2379
2380 if (vcpu->pvclock_set_guest_stopped_request) {
2381 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2382 vcpu->pvclock_set_guest_stopped_request = false;
2383 }
2384
2385 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2386
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002387 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2388 &vcpu->hv_clock,
2389 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002390
2391 smp_wmb();
2392
2393 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002394 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2395 &vcpu->hv_clock,
2396 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002397}
2398
Zachary Amsden34c238a2010-09-18 14:38:14 -10002399static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002400{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002401 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002402 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002403 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002404 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002405 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002406 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002407 bool use_master_clock;
2408
2409 kernel_ns = 0;
2410 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002411
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002412 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002413 * If the host uses TSC clock, then passthrough TSC as stable
2414 * to the guest.
2415 */
2416 spin_lock(&ka->pvclock_gtod_sync_lock);
2417 use_master_clock = ka->use_master_clock;
2418 if (use_master_clock) {
2419 host_tsc = ka->master_cycle_now;
2420 kernel_ns = ka->master_kernel_ns;
2421 }
2422 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002423
2424 /* Keep irq disabled to prevent changes to the clock */
2425 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002426 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2427 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002428 local_irq_restore(flags);
2429 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2430 return 1;
2431 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002432 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002433 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002434 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002435 }
2436
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002437 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002438
2439 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002440 * We may have to catch up the TSC to match elapsed wall clock
2441 * time for two reasons, even if kvmclock is used.
2442 * 1) CPU could have been running below the maximum TSC rate
2443 * 2) Broken TSC compensation resets the base at each VCPU
2444 * entry to avoid unknown leaps of TSC even when running
2445 * again on the same CPU. This may cause apparent elapsed
2446 * time to disappear, and the guest to stand still or run
2447 * very slowly.
2448 */
2449 if (vcpu->tsc_catchup) {
2450 u64 tsc = compute_guest_tsc(v, kernel_ns);
2451 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002452 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002453 tsc_timestamp = tsc;
2454 }
2455 }
2456
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002457 local_irq_restore(flags);
2458
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002459 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002460
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002461 if (kvm_has_tsc_control)
2462 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2463
2464 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002465 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002466 &vcpu->hv_clock.tsc_shift,
2467 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002468 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002469 }
2470
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002471 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002472 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002473 vcpu->last_guest_tsc = tsc_timestamp;
Paolo Bonzini9446e6f2020-02-12 13:27:10 +01002474 WARN_ON((s64)vcpu->hv_clock.system_time < 0);
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002475
Marcelo Tosattid8281992012-11-27 23:29:01 -02002476 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002477 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002478 if (use_master_clock)
2479 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2480
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002481 vcpu->hv_clock.flags = pvclock_flags;
2482
Paolo Bonzini095cf552016-02-08 12:54:12 +01002483 if (vcpu->pv_time_enabled)
2484 kvm_setup_pvclock_page(v);
2485 if (v == kvm_get_vcpu(v->kvm, 0))
2486 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002487 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002488}
2489
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002490/*
2491 * kvmclock updates which are isolated to a given vcpu, such as
2492 * vcpu->cpu migration, should not allow system_timestamp from
2493 * the rest of the vcpus to remain static. Otherwise ntp frequency
2494 * correction applies to one vcpu's system_timestamp but not
2495 * the others.
2496 *
2497 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002498 * We need to rate-limit these requests though, as they can
2499 * considerably slow guests that have a large number of vcpus.
2500 * The time for a remote vcpu to update its kvmclock is bound
2501 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002502 */
2503
Andrew Jones7e44e442014-02-28 12:52:54 +01002504#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2505
2506static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002507{
2508 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002509 struct delayed_work *dwork = to_delayed_work(work);
2510 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2511 kvmclock_update_work);
2512 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002513 struct kvm_vcpu *vcpu;
2514
2515 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002516 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002517 kvm_vcpu_kick(vcpu);
2518 }
2519}
2520
Andrew Jones7e44e442014-02-28 12:52:54 +01002521static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2522{
2523 struct kvm *kvm = v->kvm;
2524
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002525 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002526 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2527 KVMCLOCK_UPDATE_DELAY);
2528}
2529
Andrew Jones332967a2014-02-28 12:52:55 +01002530#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2531
2532static void kvmclock_sync_fn(struct work_struct *work)
2533{
2534 struct delayed_work *dwork = to_delayed_work(work);
2535 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2536 kvmclock_sync_work);
2537 struct kvm *kvm = container_of(ka, struct kvm, arch);
2538
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002539 if (!kvmclock_periodic_sync)
2540 return;
2541
Andrew Jones332967a2014-02-28 12:52:55 +01002542 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2543 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2544 KVMCLOCK_SYNC_PERIOD);
2545}
2546
Borislav Petkov191c8132019-04-18 18:32:50 +02002547/*
2548 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2549 */
2550static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2551{
2552 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08002553 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02002554 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2555
2556 return false;
2557}
2558
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002559static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002560{
2561 u64 mcg_cap = vcpu->arch.mcg_cap;
2562 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002563 u32 msr = msr_info->index;
2564 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002565
2566 switch (msr) {
2567 case MSR_IA32_MCG_STATUS:
2568 vcpu->arch.mcg_status = data;
2569 break;
2570 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002571 if (!(mcg_cap & MCG_CTL_P) &&
2572 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002573 return 1;
2574 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002575 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002576 vcpu->arch.mcg_ctl = data;
2577 break;
2578 default:
2579 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002580 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002581 u32 offset = array_index_nospec(
2582 msr - MSR_IA32_MC0_CTL,
2583 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2584
Andre Przywara114be422010-03-24 17:46:42 +01002585 /* only 0 or all 1s can be written to IA32_MCi_CTL
2586 * some Linux kernels though clear bit 10 in bank 4 to
2587 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2588 * this to avoid an uncatched #GP in the guest
2589 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002590 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002591 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002592 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002593
2594 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002595 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002596 (offset & 0x3) == 1 && data != 0) {
2597 if (!can_set_mci_status(vcpu))
2598 return -1;
2599 }
2600
Huang Ying890ca9a2009-05-11 16:48:15 +08002601 vcpu->arch.mce_banks[offset] = data;
2602 break;
2603 }
2604 return 1;
2605 }
2606 return 0;
2607}
2608
Ed Swierkffde22a2009-10-15 15:21:43 -07002609static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2610{
2611 struct kvm *kvm = vcpu->kvm;
2612 int lm = is_long_mode(vcpu);
2613 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2614 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2615 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2616 : kvm->arch.xen_hvm_config.blob_size_32;
2617 u32 page_num = data & ~PAGE_MASK;
2618 u64 page_addr = data & PAGE_MASK;
2619 u8 *page;
2620 int r;
2621
2622 r = -E2BIG;
2623 if (page_num >= blob_size)
2624 goto out;
2625 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002626 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2627 if (IS_ERR(page)) {
2628 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002629 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002630 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002631 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002632 goto out_free;
2633 r = 0;
2634out_free:
2635 kfree(page);
2636out:
2637 return r;
2638}
2639
Gleb Natapov344d9582010-10-14 11:22:50 +02002640static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2641{
2642 gpa_t gpa = data & ~0x3f;
2643
Wanpeng Li52a5c152017-07-13 18:30:42 -07002644 /* Bits 3:5 are reserved, Should be zero */
2645 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002646 return 1;
2647
2648 vcpu->arch.apf.msr_val = data;
2649
2650 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2651 kvm_clear_async_pf_completion_queue(vcpu);
2652 kvm_async_pf_hash_reset(vcpu);
2653 return 0;
2654 }
2655
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002656 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002657 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002658 return 1;
2659
Gleb Natapov6adba522010-10-14 11:22:55 +02002660 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002661 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002662 kvm_async_pf_wakeup_all(vcpu);
2663 return 0;
2664}
2665
Glauber Costa12f9a482011-02-01 14:16:40 -05002666static void kvmclock_reset(struct kvm_vcpu *vcpu)
2667{
Andy Honig0b794592013-02-20 14:48:10 -08002668 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002669 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002670}
2671
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002672static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2673{
2674 ++vcpu->stat.tlb_flush;
2675 kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa);
2676}
2677
Glauber Costac9aaa892011-07-11 15:28:14 -04002678static void record_steal_time(struct kvm_vcpu *vcpu)
2679{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002680 struct kvm_host_map map;
2681 struct kvm_steal_time *st;
2682
Glauber Costac9aaa892011-07-11 15:28:14 -04002683 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2684 return;
2685
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002686 /* -EAGAIN is returned in atomic context so we can just return. */
2687 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2688 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002689 return;
2690
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002691 st = map.hva +
2692 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2693
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002694 /*
2695 * Doing a TLB flush here, on the guest's behalf, can avoid
2696 * expensive IPIs.
2697 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002698 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002699 st->preempted & KVM_VCPU_FLUSH_TLB);
2700 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002701 kvm_vcpu_flush_tlb(vcpu, false);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002702
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002703 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002704
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002705 if (st->version & 1)
2706 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002707
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002708 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002709
2710 smp_wmb();
2711
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002712 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002713 vcpu->arch.st.last_steal;
2714 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002715
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002716 smp_wmb();
2717
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002718 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002719
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002720 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002721}
2722
Will Auld8fe8ab42012-11-29 12:42:12 -08002723int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002724{
Gleb Natapov57537852012-01-15 14:17:22 +02002725 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002726 u32 msr = msr_info->index;
2727 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002728
Carsten Otte15c4a642007-10-30 18:44:17 +01002729 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002730 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002731 case MSR_IA32_UCODE_WRITE:
2732 case MSR_VM_HSAVE_PA:
2733 case MSR_AMD64_PATCH_LOADER:
2734 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002735 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002736 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002737 break;
2738
Wanpeng Li518e7b92018-02-28 14:03:31 +08002739 case MSR_IA32_UCODE_REV:
2740 if (msr_info->host_initiated)
2741 vcpu->arch.microcode_version = data;
2742 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002743 case MSR_IA32_ARCH_CAPABILITIES:
2744 if (!msr_info->host_initiated)
2745 return 1;
2746 vcpu->arch.arch_capabilities = data;
2747 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002748 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002749 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002750 case MSR_K7_HWCR:
2751 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002752 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002753 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002754
2755 /* Handle McStatusWrEn */
2756 if (data == BIT_ULL(18)) {
2757 vcpu->arch.msr_hwcr = data;
2758 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002759 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2760 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002761 return 1;
2762 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002763 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002764 case MSR_FAM10H_MMIO_CONF_BASE:
2765 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002766 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2767 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002768 return 1;
2769 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002770 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002771 case MSR_IA32_DEBUGCTLMSR:
2772 if (!data) {
2773 /* We support the non-activated case already */
2774 break;
2775 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2776 /* Values other than LBR and BTF are vendor-specific,
2777 thus reserved and should throw a #GP */
2778 return 1;
2779 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002780 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2781 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002782 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002783 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002784 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002785 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002786 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002787 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2788 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002789 case MSR_IA32_TSCDEADLINE:
2790 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2791 break;
Will Auldba904632012-11-29 12:42:50 -08002792 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002793 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002794 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002795 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002796 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002797 }
2798 vcpu->arch.ia32_tsc_adjust_msr = data;
2799 }
2800 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002801 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002802 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2803 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2804 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2805 return 1;
2806 vcpu->arch.ia32_misc_enable_msr = data;
2807 kvm_update_cpuid(vcpu);
2808 } else {
2809 vcpu->arch.ia32_misc_enable_msr = data;
2810 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002811 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002812 case MSR_IA32_SMBASE:
2813 if (!msr_info->host_initiated)
2814 return 1;
2815 vcpu->arch.smbase = data;
2816 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002817 case MSR_IA32_POWER_CTL:
2818 vcpu->arch.msr_ia32_power_ctl = data;
2819 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002820 case MSR_IA32_TSC:
2821 kvm_write_tsc(vcpu, msr_info);
2822 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002823 case MSR_IA32_XSS:
2824 if (!msr_info->host_initiated &&
2825 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2826 return 1;
2827 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08002828 * KVM supports exposing PT to the guest, but does not support
2829 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
2830 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002831 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01002832 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002833 return 1;
2834 vcpu->arch.ia32_xss = data;
2835 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002836 case MSR_SMI_COUNT:
2837 if (!msr_info->host_initiated)
2838 return 1;
2839 vcpu->arch.smi_count = data;
2840 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002841 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002842 case MSR_KVM_WALL_CLOCK:
2843 vcpu->kvm->arch.wall_clock = data;
2844 kvm_write_wall_clock(vcpu->kvm, data);
2845 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002846 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002847 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002848 struct kvm_arch *ka = &vcpu->kvm->arch;
2849
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002850 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2851 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2852
2853 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002854 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002855
2856 ka->boot_vcpu_runs_old_kvmclock = tmp;
2857 }
2858
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002859 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002860 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002861
2862 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002863 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002864 if (!(data & 1))
2865 break;
2866
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002867 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002868 &vcpu->arch.pv_time, data & ~1ULL,
2869 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002870 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002871
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002872 break;
2873 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002874 case MSR_KVM_ASYNC_PF_EN:
2875 if (kvm_pv_enable_async_pf(vcpu, data))
2876 return 1;
2877 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002878 case MSR_KVM_STEAL_TIME:
2879
2880 if (unlikely(!sched_info_on()))
2881 return 1;
2882
2883 if (data & KVM_STEAL_RESERVED_MASK)
2884 return 1;
2885
Glauber Costac9aaa892011-07-11 15:28:14 -04002886 vcpu->arch.st.msr_val = data;
2887
2888 if (!(data & KVM_MSR_ENABLED))
2889 break;
2890
Glauber Costac9aaa892011-07-11 15:28:14 -04002891 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2892
2893 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002894 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002895 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002896 return 1;
2897 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002898
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002899 case MSR_KVM_POLL_CONTROL:
2900 /* only enable bit supported */
2901 if (data & (-1ULL << 1))
2902 return 1;
2903
2904 vcpu->arch.msr_kvm_poll_control = data;
2905 break;
2906
Huang Ying890ca9a2009-05-11 16:48:15 +08002907 case MSR_IA32_MCG_CTL:
2908 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002909 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002910 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002911
Wei Huang6912ac32015-06-12 01:34:56 -04002912 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2913 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2914 pr = true; /* fall through */
2915 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2916 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002917 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002918 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002919
2920 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002921 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2922 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002923 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002924 case MSR_K7_CLK_CTL:
2925 /*
2926 * Ignore all writes to this no longer documented MSR.
2927 * Writes are only relevant for old K7 processors,
2928 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002929 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002930 * affected processor models on the command line, hence
2931 * the need to ignore the workaround.
2932 */
2933 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002934 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002935 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2936 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002937 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002938 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2939 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2940 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002941 return kvm_hv_set_msr_common(vcpu, msr, data,
2942 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002943 case MSR_IA32_BBL_CR_CTL3:
2944 /* Drop writes to this legacy MSR -- see rdmsr
2945 * counterpart for further detail.
2946 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002947 if (report_ignored_msrs)
2948 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2949 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002950 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002951 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002952 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002953 return 1;
2954 vcpu->arch.osvw.length = data;
2955 break;
2956 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002957 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002958 return 1;
2959 vcpu->arch.osvw.status = data;
2960 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002961 case MSR_PLATFORM_INFO:
2962 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002963 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2964 cpuid_fault_enabled(vcpu)))
2965 return 1;
2966 vcpu->arch.msr_platform_info = data;
2967 break;
2968 case MSR_MISC_FEATURES_ENABLES:
2969 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2970 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2971 !supports_cpuid_fault(vcpu)))
2972 return 1;
2973 vcpu->arch.msr_misc_features_enables = data;
2974 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002975 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002976 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2977 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002978 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002979 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002980 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002981 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002982 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002983 return 1;
2984 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002985 if (report_ignored_msrs)
2986 vcpu_unimpl(vcpu,
2987 "ignored wrmsr: 0x%x data 0x%llx\n",
2988 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002989 break;
2990 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002991 }
2992 return 0;
2993}
2994EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2995
Paolo Bonzini44883f02018-07-26 13:01:52 +02002996static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002997{
2998 u64 data;
2999 u64 mcg_cap = vcpu->arch.mcg_cap;
3000 unsigned bank_num = mcg_cap & 0xff;
3001
3002 switch (msr) {
3003 case MSR_IA32_P5_MC_ADDR:
3004 case MSR_IA32_P5_MC_TYPE:
3005 data = 0;
3006 break;
3007 case MSR_IA32_MCG_CAP:
3008 data = vcpu->arch.mcg_cap;
3009 break;
3010 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003011 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003012 return 1;
3013 data = vcpu->arch.mcg_ctl;
3014 break;
3015 case MSR_IA32_MCG_STATUS:
3016 data = vcpu->arch.mcg_status;
3017 break;
3018 default:
3019 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003020 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003021 u32 offset = array_index_nospec(
3022 msr - MSR_IA32_MC0_CTL,
3023 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3024
Huang Ying890ca9a2009-05-11 16:48:15 +08003025 data = vcpu->arch.mce_banks[offset];
3026 break;
3027 }
3028 return 1;
3029 }
3030 *pdata = data;
3031 return 0;
3032}
3033
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003034int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003035{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003036 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003037 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003038 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003039 case MSR_IA32_DEBUGCTLMSR:
3040 case MSR_IA32_LASTBRANCHFROMIP:
3041 case MSR_IA32_LASTBRANCHTOIP:
3042 case MSR_IA32_LASTINTFROMIP:
3043 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303044 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003045 case MSR_K8_TSEG_ADDR:
3046 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003047 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003048 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003049 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003050 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003051 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003052 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003053 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003054 case MSR_F15H_EX_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003055 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003056 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003057 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003058 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3059 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3060 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3061 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003062 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003063 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
3064 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003065 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003066 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003067 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003068 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003069 case MSR_IA32_ARCH_CAPABILITIES:
3070 if (!msr_info->host_initiated &&
3071 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3072 return 1;
3073 msr_info->data = vcpu->arch.arch_capabilities;
3074 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003075 case MSR_IA32_POWER_CTL:
3076 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3077 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003078 case MSR_IA32_TSC:
3079 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3080 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003081 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003082 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003083 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003084 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003085 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003086 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003087 /*
3088 * MSR_EBC_FREQUENCY_ID
3089 * Conservative value valid for even the basic CPU models.
3090 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3091 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3092 * and 266MHz for model 3, or 4. Set Core Clock
3093 * Frequency to System Bus Frequency Ratio to 1 (bits
3094 * 31:24) even though these are only valid for CPU
3095 * models > 2, however guests may end up dividing or
3096 * multiplying by zero otherwise.
3097 */
3098 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003099 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003100 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003101 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003102 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003103 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003104 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003105 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003106 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003107 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003108 break;
Will Auldba904632012-11-29 12:42:50 -08003109 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003110 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003111 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003112 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003113 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003114 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003115 case MSR_IA32_SMBASE:
3116 if (!msr_info->host_initiated)
3117 return 1;
3118 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003119 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003120 case MSR_SMI_COUNT:
3121 msr_info->data = vcpu->arch.smi_count;
3122 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003123 case MSR_IA32_PERF_STATUS:
3124 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003125 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003126 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003127 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003128 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003129 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003130 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003131 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003132 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003133 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003134 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003135 break;
3136 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003137 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003138 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003139 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003140 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003141 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003142 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003143 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003144 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003145 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003146 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003147 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003148 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003149 case MSR_KVM_POLL_CONTROL:
3150 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3151 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003152 case MSR_IA32_P5_MC_ADDR:
3153 case MSR_IA32_P5_MC_TYPE:
3154 case MSR_IA32_MCG_CAP:
3155 case MSR_IA32_MCG_CTL:
3156 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003157 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003158 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3159 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003160 case MSR_IA32_XSS:
3161 if (!msr_info->host_initiated &&
3162 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3163 return 1;
3164 msr_info->data = vcpu->arch.ia32_xss;
3165 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003166 case MSR_K7_CLK_CTL:
3167 /*
3168 * Provide expected ramp-up count for K7. All other
3169 * are set to zero, indicating minimum divisors for
3170 * every field.
3171 *
3172 * This prevents guest kernels on AMD host with CPU
3173 * type 6, model 8 and higher from exploding due to
3174 * the rdmsr failing.
3175 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003176 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003177 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003178 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003179 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3180 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003181 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003182 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3183 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3184 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003185 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003186 msr_info->index, &msr_info->data,
3187 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003188 case MSR_IA32_BBL_CR_CTL3:
3189 /* This legacy MSR exists but isn't fully documented in current
3190 * silicon. It is however accessed by winxp in very narrow
3191 * scenarios where it sets bit #19, itself documented as
3192 * a "reserved" bit. Best effort attempt to source coherent
3193 * read data here should the balance of the register be
3194 * interpreted by the guest:
3195 *
3196 * L2 cache control register 3: 64GB range, 256KB size,
3197 * enabled, latency 0x1, configured
3198 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003199 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003200 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003201 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003202 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003203 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003204 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003205 break;
3206 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003207 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003208 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003209 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003210 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003211 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003212 if (!msr_info->host_initiated &&
3213 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3214 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003215 msr_info->data = vcpu->arch.msr_platform_info;
3216 break;
3217 case MSR_MISC_FEATURES_ENABLES:
3218 msr_info->data = vcpu->arch.msr_misc_features_enables;
3219 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003220 case MSR_K7_HWCR:
3221 msr_info->data = vcpu->arch.msr_hwcr;
3222 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003223 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003224 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003225 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003226 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003227 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3228 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003229 return 1;
3230 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003231 if (report_ignored_msrs)
3232 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3233 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003234 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003235 }
3236 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003237 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003238 return 0;
3239}
3240EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3241
Carsten Otte313a3dc2007-10-11 19:16:52 +02003242/*
3243 * Read or write a bunch of msrs. All parameters are kernel addresses.
3244 *
3245 * @return number of msrs set successfully.
3246 */
3247static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3248 struct kvm_msr_entry *entries,
3249 int (*do_msr)(struct kvm_vcpu *vcpu,
3250 unsigned index, u64 *data))
3251{
Tom Lendacky801e4592018-02-21 13:39:51 -06003252 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003253
Carsten Otte313a3dc2007-10-11 19:16:52 +02003254 for (i = 0; i < msrs->nmsrs; ++i)
3255 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3256 break;
3257
Carsten Otte313a3dc2007-10-11 19:16:52 +02003258 return i;
3259}
3260
3261/*
3262 * Read or write a bunch of msrs. Parameters are user addresses.
3263 *
3264 * @return number of msrs set successfully.
3265 */
3266static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3267 int (*do_msr)(struct kvm_vcpu *vcpu,
3268 unsigned index, u64 *data),
3269 int writeback)
3270{
3271 struct kvm_msrs msrs;
3272 struct kvm_msr_entry *entries;
3273 int r, n;
3274 unsigned size;
3275
3276 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003277 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003278 goto out;
3279
3280 r = -E2BIG;
3281 if (msrs.nmsrs >= MAX_IO_MSRS)
3282 goto out;
3283
Carsten Otte313a3dc2007-10-11 19:16:52 +02003284 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003285 entries = memdup_user(user_msrs->entries, size);
3286 if (IS_ERR(entries)) {
3287 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003288 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003289 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003290
3291 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3292 if (r < 0)
3293 goto out_free;
3294
3295 r = -EFAULT;
3296 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3297 goto out_free;
3298
3299 r = n;
3300
3301out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003302 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003303out:
3304 return r;
3305}
3306
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003307static inline bool kvm_can_mwait_in_guest(void)
3308{
3309 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003310 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3311 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003312}
3313
Alexander Graf784aa3d2014-07-14 18:27:35 +02003314int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003315{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003316 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003317
3318 switch (ext) {
3319 case KVM_CAP_IRQCHIP:
3320 case KVM_CAP_HLT:
3321 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003322 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003323 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003324 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003325 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003326 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003327 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003328 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003329 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003330 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003331 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003332 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003333 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003334 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003335 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003336 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003337 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003338 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003339 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003340 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003341 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003342 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003343 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003344 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003345 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003346 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003347 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003348 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003349 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003350 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003351 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003352 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003353 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003354 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003355 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003356 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003357 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003358 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003359 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003360 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003361 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003362 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003363 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003364 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003365 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003366 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003367 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003368 case KVM_CAP_EXCEPTION_PAYLOAD:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003369 r = 1;
3370 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003371 case KVM_CAP_SYNC_REGS:
3372 r = KVM_SYNC_X86_VALID_FIELDS;
3373 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003374 case KVM_CAP_ADJUST_CLOCK:
3375 r = KVM_CLOCK_TSC_STABLE;
3376 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003377 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003378 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3379 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003380 if(kvm_can_mwait_in_guest())
3381 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003382 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003383 case KVM_CAP_X86_SMM:
3384 /* SMBASE is usually relocated above 1M on modern chipsets,
3385 * and SMM handlers might indeed rely on 4G segment limits,
3386 * so do not report SMM to be available if real mode is
3387 * emulated via vm86 mode. Still, do not go to great lengths
3388 * to avoid userspace's usage of the feature, because it is a
3389 * fringe case that is not enabled except via specific settings
3390 * of the module parameters.
3391 */
Tom Lendackybc226f02018-05-10 22:06:39 +02003392 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003393 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003394 case KVM_CAP_VAPIC:
3395 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
3396 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003397 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003398 r = KVM_SOFT_MAX_VCPUS;
3399 break;
3400 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003401 r = KVM_MAX_VCPUS;
3402 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003403 case KVM_CAP_MAX_VCPU_ID:
3404 r = KVM_MAX_VCPU_ID;
3405 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003406 case KVM_CAP_PV_MMU: /* obsolete */
3407 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003408 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003409 case KVM_CAP_MCE:
3410 r = KVM_MAX_MCE_BANKS;
3411 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003412 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003413 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003414 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003415 case KVM_CAP_TSC_CONTROL:
3416 r = kvm_has_tsc_control;
3417 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003418 case KVM_CAP_X2APIC_API:
3419 r = KVM_X2APIC_API_VALID_FLAGS;
3420 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003421 case KVM_CAP_NESTED_STATE:
3422 r = kvm_x86_ops->get_nested_state ?
Hariprasad Kelambe43c442019-04-06 15:06:58 +05303423 kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003424 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003425 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003426 r = kvm_x86_ops->enable_direct_tlbflush != NULL;
3427 break;
3428 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
3429 r = kvm_x86_ops->nested_enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003430 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003431 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003432 break;
3433 }
3434 return r;
3435
3436}
3437
Carsten Otte043405e2007-10-10 17:16:19 +02003438long kvm_arch_dev_ioctl(struct file *filp,
3439 unsigned int ioctl, unsigned long arg)
3440{
3441 void __user *argp = (void __user *)arg;
3442 long r;
3443
3444 switch (ioctl) {
3445 case KVM_GET_MSR_INDEX_LIST: {
3446 struct kvm_msr_list __user *user_msr_list = argp;
3447 struct kvm_msr_list msr_list;
3448 unsigned n;
3449
3450 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003451 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003452 goto out;
3453 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003454 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003455 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003456 goto out;
3457 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003458 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003459 goto out;
3460 r = -EFAULT;
3461 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3462 num_msrs_to_save * sizeof(u32)))
3463 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003464 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003465 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003466 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003467 goto out;
3468 r = 0;
3469 break;
3470 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003471 case KVM_GET_SUPPORTED_CPUID:
3472 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003473 struct kvm_cpuid2 __user *cpuid_arg = argp;
3474 struct kvm_cpuid2 cpuid;
3475
3476 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003477 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003478 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003479
3480 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3481 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003482 if (r)
3483 goto out;
3484
3485 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003486 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003487 goto out;
3488 r = 0;
3489 break;
3490 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003491 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
Huang Ying890ca9a2009-05-11 16:48:15 +08003492 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003493 if (copy_to_user(argp, &kvm_mce_cap_supported,
3494 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003495 goto out;
3496 r = 0;
3497 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003498 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3499 struct kvm_msr_list __user *user_msr_list = argp;
3500 struct kvm_msr_list msr_list;
3501 unsigned int n;
3502
3503 r = -EFAULT;
3504 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3505 goto out;
3506 n = msr_list.nmsrs;
3507 msr_list.nmsrs = num_msr_based_features;
3508 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3509 goto out;
3510 r = -E2BIG;
3511 if (n < msr_list.nmsrs)
3512 goto out;
3513 r = -EFAULT;
3514 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3515 num_msr_based_features * sizeof(u32)))
3516 goto out;
3517 r = 0;
3518 break;
3519 }
3520 case KVM_GET_MSRS:
3521 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3522 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003523 }
Carsten Otte043405e2007-10-10 17:16:19 +02003524 default:
3525 r = -EINVAL;
3526 }
3527out:
3528 return r;
3529}
3530
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003531static void wbinvd_ipi(void *garbage)
3532{
3533 wbinvd();
3534}
3535
3536static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3537{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003538 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003539}
3540
Carsten Otte313a3dc2007-10-11 19:16:52 +02003541void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3542{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003543 /* Address WBINVD may be executed by guest */
3544 if (need_emulate_wbinvd(vcpu)) {
3545 if (kvm_x86_ops->has_wbinvd_exit())
3546 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3547 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3548 smp_call_function_single(vcpu->cpu,
3549 wbinvd_ipi, NULL, 1);
3550 }
3551
Carsten Otte313a3dc2007-10-11 19:16:52 +02003552 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003553
3554 /* Apply any externally detected TSC adjustments (due to suspend) */
3555 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3556 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3557 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003558 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003559 }
3560
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003561 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003562 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003563 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003564 if (tsc_delta < 0)
3565 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003566
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003567 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003568 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003569 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003570 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003571 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003572 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003573
3574 if (kvm_lapic_hv_timer_in_use(vcpu))
3575 kvm_lapic_restart_hv_timer(vcpu);
3576
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003577 /*
3578 * On a host with synchronized TSC, there is no need to update
3579 * kvmclock on vcpu->cpu migration
3580 */
3581 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003582 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003583 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003584 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003585 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003586 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003587
Glauber Costac9aaa892011-07-11 15:28:14 -04003588 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003589}
3590
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003591static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3592{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003593 struct kvm_host_map map;
3594 struct kvm_steal_time *st;
3595
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003596 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3597 return;
3598
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003599 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003600 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003601
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003602 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3603 &vcpu->arch.st.cache, true))
3604 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003605
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003606 st = map.hva +
3607 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3608
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003609 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003610
3611 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003612}
3613
Carsten Otte313a3dc2007-10-11 19:16:52 +02003614void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3615{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003616 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003617
3618 if (vcpu->preempted)
3619 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3620
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003621 /*
3622 * Disable page faults because we're in atomic context here.
3623 * kvm_write_guest_offset_cached() would call might_fault()
3624 * that relies on pagefault_disable() to tell if there's a
3625 * bug. NOTE: the write to guest memory may not go through if
3626 * during postcopy live migration or if there's heavy guest
3627 * paging.
3628 */
3629 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003630 /*
3631 * kvm_memslots() will be called by
3632 * kvm_write_guest_offset_cached() so take the srcu lock.
3633 */
3634 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003635 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003636 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003637 pagefault_enable();
Avi Kivity02daab22009-12-30 12:40:26 +02003638 kvm_x86_ops->vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003639 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003640 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003641 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3642 * on every vmexit, but if not, we might have a stale dr6 from the
3643 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003644 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003645 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003646}
3647
Carsten Otte313a3dc2007-10-11 19:16:52 +02003648static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3649 struct kvm_lapic_state *s)
3650{
Liran Alonfa59cc02017-12-24 18:12:53 +02003651 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03003652 kvm_x86_ops->sync_pir_to_irr(vcpu);
3653
Radim Krčmářa92e2542016-07-12 22:09:22 +02003654 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003655}
3656
3657static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3658 struct kvm_lapic_state *s)
3659{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003660 int r;
3661
3662 r = kvm_apic_set_state(vcpu, s);
3663 if (r)
3664 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003665 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003666
3667 return 0;
3668}
3669
Matt Gingell127a4572015-11-17 17:32:05 +01003670static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3671{
3672 return (!lapic_in_kernel(vcpu) ||
3673 kvm_apic_accept_pic_intr(vcpu));
3674}
3675
Matt Gingell782d4222015-11-16 15:26:00 -08003676/*
3677 * if userspace requested an interrupt window, check that the
3678 * interrupt window is open.
3679 *
3680 * No need to exit to userspace if we already have an interrupt queued.
3681 */
3682static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3683{
3684 return kvm_arch_interrupt_allowed(vcpu) &&
3685 !kvm_cpu_has_interrupt(vcpu) &&
3686 !kvm_event_needs_reinjection(vcpu) &&
3687 kvm_cpu_accept_dm_intr(vcpu);
3688}
3689
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003690static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3691 struct kvm_interrupt *irq)
3692{
Chen Gang02cdb502013-02-27 11:33:25 +08003693 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003694 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003695
3696 if (!irqchip_in_kernel(vcpu->kvm)) {
3697 kvm_queue_interrupt(vcpu, irq->irq, false);
3698 kvm_make_request(KVM_REQ_EVENT, vcpu);
3699 return 0;
3700 }
3701
3702 /*
3703 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3704 * fail for in-kernel 8259.
3705 */
3706 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003707 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003708
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003709 if (vcpu->arch.pending_external_vector != -1)
3710 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003711
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003712 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003713 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003714 return 0;
3715}
3716
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003717static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3718{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003719 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003720
3721 return 0;
3722}
3723
Paolo Bonzinif0778252015-04-01 15:06:40 +02003724static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3725{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003726 kvm_make_request(KVM_REQ_SMI, vcpu);
3727
Paolo Bonzinif0778252015-04-01 15:06:40 +02003728 return 0;
3729}
3730
Avi Kivityb209749f2007-10-22 16:50:39 +02003731static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3732 struct kvm_tpr_access_ctl *tac)
3733{
3734 if (tac->flags)
3735 return -EINVAL;
3736 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3737 return 0;
3738}
3739
Huang Ying890ca9a2009-05-11 16:48:15 +08003740static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3741 u64 mcg_cap)
3742{
3743 int r;
3744 unsigned bank_num = mcg_cap & 0xff, bank;
3745
3746 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003747 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003748 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003749 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003750 goto out;
3751 r = 0;
3752 vcpu->arch.mcg_cap = mcg_cap;
3753 /* Init IA32_MCG_CTL to all 1s */
3754 if (mcg_cap & MCG_CTL_P)
3755 vcpu->arch.mcg_ctl = ~(u64)0;
3756 /* Init IA32_MCi_CTL to all 1s */
3757 for (bank = 0; bank < bank_num; bank++)
3758 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003759
Sean Christopherson92735b12019-08-02 15:06:17 -07003760 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003761out:
3762 return r;
3763}
3764
3765static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3766 struct kvm_x86_mce *mce)
3767{
3768 u64 mcg_cap = vcpu->arch.mcg_cap;
3769 unsigned bank_num = mcg_cap & 0xff;
3770 u64 *banks = vcpu->arch.mce_banks;
3771
3772 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3773 return -EINVAL;
3774 /*
3775 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3776 * reporting is disabled
3777 */
3778 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3779 vcpu->arch.mcg_ctl != ~(u64)0)
3780 return 0;
3781 banks += 4 * mce->bank;
3782 /*
3783 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3784 * reporting is disabled for the bank
3785 */
3786 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3787 return 0;
3788 if (mce->status & MCI_STATUS_UC) {
3789 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003790 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003791 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003792 return 0;
3793 }
3794 if (banks[1] & MCI_STATUS_VAL)
3795 mce->status |= MCI_STATUS_OVER;
3796 banks[2] = mce->addr;
3797 banks[3] = mce->misc;
3798 vcpu->arch.mcg_status = mce->mcg_status;
3799 banks[1] = mce->status;
3800 kvm_queue_exception(vcpu, MC_VECTOR);
3801 } else if (!(banks[1] & MCI_STATUS_VAL)
3802 || !(banks[1] & MCI_STATUS_UC)) {
3803 if (banks[1] & MCI_STATUS_VAL)
3804 mce->status |= MCI_STATUS_OVER;
3805 banks[2] = mce->addr;
3806 banks[3] = mce->misc;
3807 banks[1] = mce->status;
3808 } else
3809 banks[1] |= MCI_STATUS_OVER;
3810 return 0;
3811}
3812
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003813static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3814 struct kvm_vcpu_events *events)
3815{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003816 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003817
Wanpeng Li664f8e22017-08-24 03:35:09 -07003818 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003819 * In guest mode, payload delivery should be deferred,
3820 * so that the L1 hypervisor can intercept #PF before
3821 * CR2 is modified (or intercept #DB before DR6 is
3822 * modified under nVMX). Unless the per-VM capability,
3823 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3824 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3825 * opportunistically defer the exception payload, deliver it if the
3826 * capability hasn't been requested before processing a
3827 * KVM_GET_VCPU_EVENTS.
3828 */
3829 if (!vcpu->kvm->arch.exception_payload_enabled &&
3830 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3831 kvm_deliver_exception_payload(vcpu);
3832
3833 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003834 * The API doesn't provide the instruction length for software
3835 * exceptions, so don't report them. As long as the guest RIP
3836 * isn't advanced, we should expect to encounter the exception
3837 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003838 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003839 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3840 events->exception.injected = 0;
3841 events->exception.pending = 0;
3842 } else {
3843 events->exception.injected = vcpu->arch.exception.injected;
3844 events->exception.pending = vcpu->arch.exception.pending;
3845 /*
3846 * For ABI compatibility, deliberately conflate
3847 * pending and injected exceptions when
3848 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3849 */
3850 if (!vcpu->kvm->arch.exception_payload_enabled)
3851 events->exception.injected |=
3852 vcpu->arch.exception.pending;
3853 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003854 events->exception.nr = vcpu->arch.exception.nr;
3855 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3856 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003857 events->exception_has_payload = vcpu->arch.exception.has_payload;
3858 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003859
Jan Kiszka03b82a32010-02-15 10:45:41 +01003860 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003861 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003862 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003863 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003864 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003865
3866 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003867 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003868 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003869 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003870
Jan Kiszka66450a22013-03-13 12:42:34 +01003871 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003872
Paolo Bonzinif0778252015-04-01 15:06:40 +02003873 events->smi.smm = is_smm(vcpu);
3874 events->smi.pending = vcpu->arch.smi_pending;
3875 events->smi.smm_inside_nmi =
3876 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3877 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3878
Jan Kiszkadab4b912009-12-06 18:24:15 +01003879 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003880 | KVM_VCPUEVENT_VALID_SHADOW
3881 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003882 if (vcpu->kvm->arch.exception_payload_enabled)
3883 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3884
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003885 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003886}
3887
Sean Christophersonc5833c72019-04-02 08:03:10 -07003888static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003889
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003890static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3891 struct kvm_vcpu_events *events)
3892{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003893 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003894 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003895 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003896 | KVM_VCPUEVENT_VALID_SMM
3897 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003898 return -EINVAL;
3899
Jim Mattson59073aa2018-10-16 14:29:20 -07003900 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3901 if (!vcpu->kvm->arch.exception_payload_enabled)
3902 return -EINVAL;
3903 if (events->exception.pending)
3904 events->exception.injected = 0;
3905 else
3906 events->exception_has_payload = 0;
3907 } else {
3908 events->exception.pending = 0;
3909 events->exception_has_payload = 0;
3910 }
3911
3912 if ((events->exception.injected || events->exception.pending) &&
3913 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003914 return -EINVAL;
3915
David Hildenbrand28bf2882017-03-23 11:46:03 +01003916 /* INITs are latched while in SMM */
3917 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3918 (events->smi.smm || events->smi.pending) &&
3919 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3920 return -EINVAL;
3921
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003922 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003923 vcpu->arch.exception.injected = events->exception.injected;
3924 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003925 vcpu->arch.exception.nr = events->exception.nr;
3926 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3927 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003928 vcpu->arch.exception.has_payload = events->exception_has_payload;
3929 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003930
Liran Alon04140b42018-03-23 03:01:31 +03003931 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003932 vcpu->arch.interrupt.nr = events->interrupt.nr;
3933 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003934 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3935 kvm_x86_ops->set_interrupt_shadow(vcpu,
3936 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003937
3938 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003939 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3940 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003941 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3942
Jan Kiszka66450a22013-03-13 12:42:34 +01003943 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003944 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003945 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003946
Paolo Bonzinif0778252015-04-01 15:06:40 +02003947 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003948 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3949 if (events->smi.smm)
3950 vcpu->arch.hflags |= HF_SMM_MASK;
3951 else
3952 vcpu->arch.hflags &= ~HF_SMM_MASK;
3953 kvm_smm_changed(vcpu);
3954 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003955
Paolo Bonzinif0778252015-04-01 15:06:40 +02003956 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003957
3958 if (events->smi.smm) {
3959 if (events->smi.smm_inside_nmi)
3960 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003961 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003962 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02003963 }
3964
3965 if (lapic_in_kernel(vcpu)) {
3966 if (events->smi.latched_init)
3967 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3968 else
3969 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02003970 }
3971 }
3972
Avi Kivity3842d132010-07-27 12:30:24 +03003973 kvm_make_request(KVM_REQ_EVENT, vcpu);
3974
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003975 return 0;
3976}
3977
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003978static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3979 struct kvm_debugregs *dbgregs)
3980{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003981 unsigned long val;
3982
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003983 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003984 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003985 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003986 dbgregs->dr7 = vcpu->arch.dr7;
3987 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003988 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003989}
3990
3991static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3992 struct kvm_debugregs *dbgregs)
3993{
3994 if (dbgregs->flags)
3995 return -EINVAL;
3996
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02003997 if (dbgregs->dr6 & ~0xffffffffull)
3998 return -EINVAL;
3999 if (dbgregs->dr7 & ~0xffffffffull)
4000 return -EINVAL;
4001
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004002 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004003 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004004 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004005 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004006 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004007 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004008
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004009 return 0;
4010}
4011
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004012#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4013
4014static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4015{
Marc Orrb666a4b2018-11-06 14:53:56 -08004016 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004017 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004018 u64 valid;
4019
4020 /*
4021 * Copy legacy XSAVE area, to avoid complications with CPUID
4022 * leaves 0 and 1 in the loop below.
4023 */
4024 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4025
4026 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004027 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004028 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4029
4030 /*
4031 * Copy each region from the possibly compacted offset to the
4032 * non-compacted offset.
4033 */
Dave Hansend91cab72015-09-02 16:31:26 -07004034 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004035 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004036 u64 xfeature_mask = valid & -valid;
4037 int xfeature_nr = fls64(xfeature_mask) - 1;
4038 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004039
4040 if (src) {
4041 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004042 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004043 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004044 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004045 memcpy(dest + offset, &vcpu->arch.pkru,
4046 sizeof(vcpu->arch.pkru));
4047 else
4048 memcpy(dest + offset, src, size);
4049
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004050 }
4051
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004052 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004053 }
4054}
4055
4056static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4057{
Marc Orrb666a4b2018-11-06 14:53:56 -08004058 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004059 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4060 u64 valid;
4061
4062 /*
4063 * Copy legacy XSAVE area, to avoid complications with CPUID
4064 * leaves 0 and 1 in the loop below.
4065 */
4066 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4067
4068 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004069 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004070 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004071 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004072
4073 /*
4074 * Copy each region from the non-compacted offset to the
4075 * possibly compacted offset.
4076 */
Dave Hansend91cab72015-09-02 16:31:26 -07004077 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004078 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004079 u64 xfeature_mask = valid & -valid;
4080 int xfeature_nr = fls64(xfeature_mask) - 1;
4081 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004082
4083 if (dest) {
4084 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004085 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004086 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004087 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004088 memcpy(&vcpu->arch.pkru, src + offset,
4089 sizeof(vcpu->arch.pkru));
4090 else
4091 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004092 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004093
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004094 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004095 }
4096}
4097
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004098static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4099 struct kvm_xsave *guest_xsave)
4100{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004101 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004102 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4103 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004104 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004105 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004106 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004107 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004108 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004109 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004110 }
4111}
4112
Wanpeng Lia5758132017-05-11 02:58:55 -07004113#define XSAVE_MXCSR_OFFSET 24
4114
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004115static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4116 struct kvm_xsave *guest_xsave)
4117{
4118 u64 xstate_bv =
4119 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004120 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004121
Borislav Petkovd366bf72016-04-04 22:25:02 +02004122 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004123 /*
4124 * Here we allow setting states that are not present in
4125 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4126 * with old userspace.
4127 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004128 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004129 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004130 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004131 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004132 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4133 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004134 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004135 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004136 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004137 }
4138 return 0;
4139}
4140
4141static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4142 struct kvm_xcrs *guest_xcrs)
4143{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004144 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004145 guest_xcrs->nr_xcrs = 0;
4146 return;
4147 }
4148
4149 guest_xcrs->nr_xcrs = 1;
4150 guest_xcrs->flags = 0;
4151 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4152 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4153}
4154
4155static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4156 struct kvm_xcrs *guest_xcrs)
4157{
4158 int i, r = 0;
4159
Borislav Petkovd366bf72016-04-04 22:25:02 +02004160 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004161 return -EINVAL;
4162
4163 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4164 return -EINVAL;
4165
4166 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4167 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004168 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004169 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004170 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004171 break;
4172 }
4173 if (r)
4174 r = -EINVAL;
4175 return r;
4176}
4177
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004178/*
4179 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4180 * stopped by the hypervisor. This function will be called from the host only.
4181 * EINVAL is returned when the host attempts to set the flag for a guest that
4182 * does not support pv clocks.
4183 */
4184static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4185{
Andy Honig0b794592013-02-20 14:48:10 -08004186 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004187 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004188 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004189 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4190 return 0;
4191}
4192
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004193static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4194 struct kvm_enable_cap *cap)
4195{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004196 int r;
4197 uint16_t vmcs_version;
4198 void __user *user_ptr;
4199
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004200 if (cap->flags)
4201 return -EINVAL;
4202
4203 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004204 case KVM_CAP_HYPERV_SYNIC2:
4205 if (cap->args[0])
4206 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004207 /* fall through */
4208
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004209 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004210 if (!irqchip_in_kernel(vcpu->kvm))
4211 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004212 return kvm_hv_activate_synic(vcpu, cap->cap ==
4213 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004214 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christopherson51589172018-12-03 13:53:10 -08004215 if (!kvm_x86_ops->nested_enable_evmcs)
4216 return -ENOTTY;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004217 r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version);
4218 if (!r) {
4219 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4220 if (copy_to_user(user_ptr, &vmcs_version,
4221 sizeof(vmcs_version)))
4222 r = -EFAULT;
4223 }
4224 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004225 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
4226 if (!kvm_x86_ops->enable_direct_tlbflush)
4227 return -ENOTTY;
4228
4229 return kvm_x86_ops->enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004230
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004231 default:
4232 return -EINVAL;
4233 }
4234}
4235
Carsten Otte313a3dc2007-10-11 19:16:52 +02004236long kvm_arch_vcpu_ioctl(struct file *filp,
4237 unsigned int ioctl, unsigned long arg)
4238{
4239 struct kvm_vcpu *vcpu = filp->private_data;
4240 void __user *argp = (void __user *)arg;
4241 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004242 union {
4243 struct kvm_lapic_state *lapic;
4244 struct kvm_xsave *xsave;
4245 struct kvm_xcrs *xcrs;
4246 void *buffer;
4247 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004248
Christoffer Dall9b0624712017-12-04 21:35:36 +01004249 vcpu_load(vcpu);
4250
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004251 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004252 switch (ioctl) {
4253 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004254 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004255 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004256 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004257 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4258 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004259
Dave Hansenb772ff32008-08-11 10:01:47 -07004260 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004261 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004262 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004263 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004264 if (r)
4265 goto out;
4266 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004267 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004268 goto out;
4269 r = 0;
4270 break;
4271 }
4272 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004273 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004274 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004275 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004276 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004277 if (IS_ERR(u.lapic)) {
4278 r = PTR_ERR(u.lapic);
4279 goto out_nofree;
4280 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004281
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004282 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004283 break;
4284 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004285 case KVM_INTERRUPT: {
4286 struct kvm_interrupt irq;
4287
4288 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004289 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004290 goto out;
4291 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004292 break;
4293 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004294 case KVM_NMI: {
4295 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004296 break;
4297 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004298 case KVM_SMI: {
4299 r = kvm_vcpu_ioctl_smi(vcpu);
4300 break;
4301 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004302 case KVM_SET_CPUID: {
4303 struct kvm_cpuid __user *cpuid_arg = argp;
4304 struct kvm_cpuid cpuid;
4305
4306 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004307 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004308 goto out;
4309 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004310 break;
4311 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004312 case KVM_SET_CPUID2: {
4313 struct kvm_cpuid2 __user *cpuid_arg = argp;
4314 struct kvm_cpuid2 cpuid;
4315
4316 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004317 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004318 goto out;
4319 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004320 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004321 break;
4322 }
4323 case KVM_GET_CPUID2: {
4324 struct kvm_cpuid2 __user *cpuid_arg = argp;
4325 struct kvm_cpuid2 cpuid;
4326
4327 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004328 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004329 goto out;
4330 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004331 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004332 if (r)
4333 goto out;
4334 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004335 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004336 goto out;
4337 r = 0;
4338 break;
4339 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004340 case KVM_GET_MSRS: {
4341 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004342 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004343 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004344 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004345 }
4346 case KVM_SET_MSRS: {
4347 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004348 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004349 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004350 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004351 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004352 case KVM_TPR_ACCESS_REPORTING: {
4353 struct kvm_tpr_access_ctl tac;
4354
4355 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004356 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004357 goto out;
4358 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4359 if (r)
4360 goto out;
4361 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004362 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004363 goto out;
4364 r = 0;
4365 break;
4366 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004367 case KVM_SET_VAPIC_ADDR: {
4368 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004369 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004370
4371 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004372 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004373 goto out;
4374 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004375 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004376 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004377 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004378 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004379 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004380 break;
4381 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004382 case KVM_X86_SETUP_MCE: {
4383 u64 mcg_cap;
4384
4385 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004386 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004387 goto out;
4388 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4389 break;
4390 }
4391 case KVM_X86_SET_MCE: {
4392 struct kvm_x86_mce mce;
4393
4394 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004395 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004396 goto out;
4397 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4398 break;
4399 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004400 case KVM_GET_VCPU_EVENTS: {
4401 struct kvm_vcpu_events events;
4402
4403 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4404
4405 r = -EFAULT;
4406 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4407 break;
4408 r = 0;
4409 break;
4410 }
4411 case KVM_SET_VCPU_EVENTS: {
4412 struct kvm_vcpu_events events;
4413
4414 r = -EFAULT;
4415 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4416 break;
4417
4418 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4419 break;
4420 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004421 case KVM_GET_DEBUGREGS: {
4422 struct kvm_debugregs dbgregs;
4423
4424 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4425
4426 r = -EFAULT;
4427 if (copy_to_user(argp, &dbgregs,
4428 sizeof(struct kvm_debugregs)))
4429 break;
4430 r = 0;
4431 break;
4432 }
4433 case KVM_SET_DEBUGREGS: {
4434 struct kvm_debugregs dbgregs;
4435
4436 r = -EFAULT;
4437 if (copy_from_user(&dbgregs, argp,
4438 sizeof(struct kvm_debugregs)))
4439 break;
4440
4441 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4442 break;
4443 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004444 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004445 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004446 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004447 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004448 break;
4449
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004450 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004451
4452 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004453 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004454 break;
4455 r = 0;
4456 break;
4457 }
4458 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004459 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004460 if (IS_ERR(u.xsave)) {
4461 r = PTR_ERR(u.xsave);
4462 goto out_nofree;
4463 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004464
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004465 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004466 break;
4467 }
4468 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004469 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004470 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004471 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004472 break;
4473
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004474 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004475
4476 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004477 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004478 sizeof(struct kvm_xcrs)))
4479 break;
4480 r = 0;
4481 break;
4482 }
4483 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004484 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004485 if (IS_ERR(u.xcrs)) {
4486 r = PTR_ERR(u.xcrs);
4487 goto out_nofree;
4488 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004489
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004490 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004491 break;
4492 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004493 case KVM_SET_TSC_KHZ: {
4494 u32 user_tsc_khz;
4495
4496 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004497 user_tsc_khz = (u32)arg;
4498
4499 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4500 goto out;
4501
Zachary Amsdencc578282012-02-03 15:43:50 -02004502 if (user_tsc_khz == 0)
4503 user_tsc_khz = tsc_khz;
4504
Haozhong Zhang381d5852015-10-20 15:39:04 +08004505 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4506 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004507
Joerg Roedel92a1f122011-03-25 09:44:51 +01004508 goto out;
4509 }
4510 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004511 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004512 goto out;
4513 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004514 case KVM_KVMCLOCK_CTRL: {
4515 r = kvm_set_guest_paused(vcpu);
4516 goto out;
4517 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004518 case KVM_ENABLE_CAP: {
4519 struct kvm_enable_cap cap;
4520
4521 r = -EFAULT;
4522 if (copy_from_user(&cap, argp, sizeof(cap)))
4523 goto out;
4524 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4525 break;
4526 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004527 case KVM_GET_NESTED_STATE: {
4528 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4529 u32 user_data_size;
4530
4531 r = -EINVAL;
4532 if (!kvm_x86_ops->get_nested_state)
4533 break;
4534
4535 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004536 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004537 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004538 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004539
4540 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4541 user_data_size);
4542 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004543 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004544
4545 if (r > user_data_size) {
4546 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004547 r = -EFAULT;
4548 else
4549 r = -E2BIG;
4550 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004551 }
Liran Alon26b471c2018-09-16 14:28:20 +03004552
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004553 r = 0;
4554 break;
4555 }
4556 case KVM_SET_NESTED_STATE: {
4557 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4558 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004559 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004560
4561 r = -EINVAL;
4562 if (!kvm_x86_ops->set_nested_state)
4563 break;
4564
Liran Alon26b471c2018-09-16 14:28:20 +03004565 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004566 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004567 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004568
Liran Alon26b471c2018-09-16 14:28:20 +03004569 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004570 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004571 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004572
4573 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004574 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4575 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004576 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004577
4578 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004579 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4580 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004581 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004582
Sean Christophersonad5996d2019-11-22 08:58:18 -08004583 idx = srcu_read_lock(&vcpu->kvm->srcu);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004584 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004585 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004586 break;
4587 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004588 case KVM_GET_SUPPORTED_HV_CPUID: {
4589 struct kvm_cpuid2 __user *cpuid_arg = argp;
4590 struct kvm_cpuid2 cpuid;
4591
4592 r = -EFAULT;
4593 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4594 goto out;
4595
4596 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4597 cpuid_arg->entries);
4598 if (r)
4599 goto out;
4600
4601 r = -EFAULT;
4602 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4603 goto out;
4604 r = 0;
4605 break;
4606 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004607 default:
4608 r = -EINVAL;
4609 }
4610out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004611 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004612out_nofree:
4613 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004614 return r;
4615}
4616
Souptick Joarder1499fa82018-04-19 00:49:58 +05304617vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004618{
4619 return VM_FAULT_SIGBUS;
4620}
4621
Carsten Otte1fe779f2007-10-29 16:08:35 +01004622static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4623{
4624 int ret;
4625
4626 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004627 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004628 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4629 return ret;
4630}
4631
Sheng Yangb927a3c2009-07-21 10:42:48 +08004632static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4633 u64 ident_addr)
4634{
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004635 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004636}
4637
Carsten Otte1fe779f2007-10-29 16:08:35 +01004638static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004639 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004640{
4641 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4642 return -EINVAL;
4643
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004644 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004645
4646 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004647 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004648
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004649 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004650 return 0;
4651}
4652
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004653static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004654{
Dave Hansen39de71e2010-08-19 18:11:14 -07004655 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004656}
4657
Carsten Otte1fe779f2007-10-29 16:08:35 +01004658static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4659{
David Hildenbrand90bca052017-04-07 10:50:23 +02004660 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004661 int r;
4662
4663 r = 0;
4664 switch (chip->chip_id) {
4665 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004666 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004667 sizeof(struct kvm_pic_state));
4668 break;
4669 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004670 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004671 sizeof(struct kvm_pic_state));
4672 break;
4673 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004674 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004675 break;
4676 default:
4677 r = -EINVAL;
4678 break;
4679 }
4680 return r;
4681}
4682
4683static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4684{
David Hildenbrand90bca052017-04-07 10:50:23 +02004685 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004686 int r;
4687
4688 r = 0;
4689 switch (chip->chip_id) {
4690 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004691 spin_lock(&pic->lock);
4692 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004693 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004694 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004695 break;
4696 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004697 spin_lock(&pic->lock);
4698 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004699 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004700 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004701 break;
4702 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004703 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004704 break;
4705 default:
4706 r = -EINVAL;
4707 break;
4708 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004709 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004710 return r;
4711}
4712
Sheng Yange0f63cb2008-03-04 00:50:59 +08004713static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4714{
Radim Krčmář34f39412016-03-02 22:56:50 +01004715 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4716
4717 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4718
4719 mutex_lock(&kps->lock);
4720 memcpy(ps, &kps->channels, sizeof(*ps));
4721 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304722 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004723}
4724
4725static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4726{
Andrew Honig01856042015-11-18 14:50:23 -08004727 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004728 struct kvm_pit *pit = kvm->arch.vpit;
4729
4730 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004731 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004732 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004733 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4734 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304735 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004736}
4737
4738static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4739{
Beth Kone9f42752009-07-07 11:50:38 -04004740 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4741 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4742 sizeof(ps->channels));
4743 ps->flags = kvm->arch.vpit->pit_state.flags;
4744 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004745 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304746 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004747}
4748
4749static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4750{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304751 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004752 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004753 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004754 struct kvm_pit *pit = kvm->arch.vpit;
4755
4756 mutex_lock(&pit->pit_state.lock);
4757 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004758 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4759 if (!prev_legacy && cur_legacy)
4760 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004761 memcpy(&pit->pit_state.channels, &ps->channels,
4762 sizeof(pit->pit_state.channels));
4763 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004764 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004765 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004766 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004767 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304768 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004769}
4770
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004771static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4772 struct kvm_reinject_control *control)
4773{
Radim Krčmář71474e22016-03-02 22:56:45 +01004774 struct kvm_pit *pit = kvm->arch.vpit;
4775
Radim Krčmář71474e22016-03-02 22:56:45 +01004776 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4777 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4778 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4779 */
4780 mutex_lock(&pit->pit_state.lock);
4781 kvm_pit_set_reinject(pit, control->pit_reinject);
4782 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004783
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004784 return 0;
4785}
4786
Sean Christopherson0dff0842020-02-18 13:07:29 -08004787void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004788{
Kai Huang88178fd2015-01-28 10:54:27 +08004789 /*
4790 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4791 */
4792 if (kvm_x86_ops->flush_log_dirty)
4793 kvm_x86_ops->flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004794}
4795
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004796int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4797 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004798{
4799 if (!irqchip_in_kernel(kvm))
4800 return -ENXIO;
4801
4802 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004803 irq_event->irq, irq_event->level,
4804 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004805 return 0;
4806}
4807
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004808int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4809 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004810{
4811 int r;
4812
4813 if (cap->flags)
4814 return -EINVAL;
4815
4816 switch (cap->cap) {
4817 case KVM_CAP_DISABLE_QUIRKS:
4818 kvm->arch.disabled_quirks = cap->args[0];
4819 r = 0;
4820 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004821 case KVM_CAP_SPLIT_IRQCHIP: {
4822 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004823 r = -EINVAL;
4824 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4825 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004826 r = -EEXIST;
4827 if (irqchip_in_kernel(kvm))
4828 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004829 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004830 goto split_irqchip_unlock;
4831 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004832 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004833 goto split_irqchip_unlock;
4834 /* Pairs with irqchip_in_kernel. */
4835 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004836 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004837 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004838 r = 0;
4839split_irqchip_unlock:
4840 mutex_unlock(&kvm->lock);
4841 break;
4842 }
Radim Krčmář371313132016-07-12 22:09:27 +02004843 case KVM_CAP_X2APIC_API:
4844 r = -EINVAL;
4845 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4846 break;
4847
4848 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4849 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004850 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4851 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004852
4853 r = 0;
4854 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004855 case KVM_CAP_X86_DISABLE_EXITS:
4856 r = -EINVAL;
4857 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4858 break;
4859
4860 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4861 kvm_can_mwait_in_guest())
4862 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004863 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004864 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004865 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4866 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004867 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4868 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004869 r = 0;
4870 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004871 case KVM_CAP_MSR_PLATFORM_INFO:
4872 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4873 r = 0;
4874 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004875 case KVM_CAP_EXCEPTION_PAYLOAD:
4876 kvm->arch.exception_payload_enabled = cap->args[0];
4877 r = 0;
4878 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004879 default:
4880 r = -EINVAL;
4881 break;
4882 }
4883 return r;
4884}
4885
Carsten Otte1fe779f2007-10-29 16:08:35 +01004886long kvm_arch_vm_ioctl(struct file *filp,
4887 unsigned int ioctl, unsigned long arg)
4888{
4889 struct kvm *kvm = filp->private_data;
4890 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004891 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004892 /*
4893 * This union makes it completely explicit to gcc-3.x
4894 * that these two variables' stack usage should be
4895 * combined, not added together.
4896 */
4897 union {
4898 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004899 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004900 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004901 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004902
4903 switch (ioctl) {
4904 case KVM_SET_TSS_ADDR:
4905 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004906 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004907 case KVM_SET_IDENTITY_MAP_ADDR: {
4908 u64 ident_addr;
4909
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004910 mutex_lock(&kvm->lock);
4911 r = -EINVAL;
4912 if (kvm->created_vcpus)
4913 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004914 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004915 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004916 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004917 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004918set_identity_unlock:
4919 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004920 break;
4921 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004922 case KVM_SET_NR_MMU_PAGES:
4923 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004924 break;
4925 case KVM_GET_NR_MMU_PAGES:
4926 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4927 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004928 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004929 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004930
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004931 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004932 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004933 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004934
Avi Kivity3e515702012-03-05 14:23:29 +02004935 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004936 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004937 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004938
4939 r = kvm_pic_init(kvm);
4940 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004941 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004942
4943 r = kvm_ioapic_init(kvm);
4944 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004945 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004946 goto create_irqchip_unlock;
4947 }
4948
Avi Kivity399ec802008-11-19 13:58:46 +02004949 r = kvm_setup_default_irq_routing(kvm);
4950 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004951 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004952 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004953 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004954 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004955 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004956 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004957 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004958 create_irqchip_unlock:
4959 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004960 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004961 }
Sheng Yang78376992008-01-28 05:10:22 +08004962 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004963 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4964 goto create_pit;
4965 case KVM_CREATE_PIT2:
4966 r = -EFAULT;
4967 if (copy_from_user(&u.pit_config, argp,
4968 sizeof(struct kvm_pit_config)))
4969 goto out;
4970 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004971 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004972 r = -EEXIST;
4973 if (kvm->arch.vpit)
4974 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004975 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004976 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004977 if (kvm->arch.vpit)
4978 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004979 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004980 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004981 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004982 case KVM_GET_IRQCHIP: {
4983 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004984 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004985
Sasha Levinff5c2c02011-12-04 19:36:29 +02004986 chip = memdup_user(argp, sizeof(*chip));
4987 if (IS_ERR(chip)) {
4988 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004989 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004990 }
4991
Carsten Otte1fe779f2007-10-29 16:08:35 +01004992 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004993 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004994 goto get_irqchip_out;
4995 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
4996 if (r)
4997 goto get_irqchip_out;
4998 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004999 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005000 goto get_irqchip_out;
5001 r = 0;
5002 get_irqchip_out:
5003 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005004 break;
5005 }
5006 case KVM_SET_IRQCHIP: {
5007 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005008 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005009
Sasha Levinff5c2c02011-12-04 19:36:29 +02005010 chip = memdup_user(argp, sizeof(*chip));
5011 if (IS_ERR(chip)) {
5012 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005013 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005014 }
5015
Carsten Otte1fe779f2007-10-29 16:08:35 +01005016 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005017 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005018 goto set_irqchip_out;
5019 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005020 set_irqchip_out:
5021 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005022 break;
5023 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005024 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005025 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005026 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005027 goto out;
5028 r = -ENXIO;
5029 if (!kvm->arch.vpit)
5030 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005031 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005032 if (r)
5033 goto out;
5034 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005035 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005036 goto out;
5037 r = 0;
5038 break;
5039 }
5040 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005041 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005042 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005043 goto out;
5044 r = -ENXIO;
5045 if (!kvm->arch.vpit)
5046 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005047 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005048 break;
5049 }
Beth Kone9f42752009-07-07 11:50:38 -04005050 case KVM_GET_PIT2: {
5051 r = -ENXIO;
5052 if (!kvm->arch.vpit)
5053 goto out;
5054 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5055 if (r)
5056 goto out;
5057 r = -EFAULT;
5058 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5059 goto out;
5060 r = 0;
5061 break;
5062 }
5063 case KVM_SET_PIT2: {
5064 r = -EFAULT;
5065 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5066 goto out;
5067 r = -ENXIO;
5068 if (!kvm->arch.vpit)
5069 goto out;
5070 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04005071 break;
5072 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005073 case KVM_REINJECT_CONTROL: {
5074 struct kvm_reinject_control control;
5075 r = -EFAULT;
5076 if (copy_from_user(&control, argp, sizeof(control)))
5077 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005078 r = -ENXIO;
5079 if (!kvm->arch.vpit)
5080 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005081 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005082 break;
5083 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005084 case KVM_SET_BOOT_CPU_ID:
5085 r = 0;
5086 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005087 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005088 r = -EBUSY;
5089 else
5090 kvm->arch.bsp_vcpu_id = arg;
5091 mutex_unlock(&kvm->lock);
5092 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005093 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005094 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005095 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005096 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005097 goto out;
5098 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005099 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005100 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005101 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005102 r = 0;
5103 break;
5104 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005105 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005106 struct kvm_clock_data user_ns;
5107 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005108
5109 r = -EFAULT;
5110 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5111 goto out;
5112
5113 r = -EINVAL;
5114 if (user_ns.flags)
5115 goto out;
5116
5117 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005118 /*
5119 * TODO: userspace has to take care of races with VCPU_RUN, so
5120 * kvm_gen_update_masterclock() can be cut down to locked
5121 * pvclock_update_vm_gtod_copy().
5122 */
5123 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005124 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005125 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005126 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005127 break;
5128 }
5129 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005130 struct kvm_clock_data user_ns;
5131 u64 now_ns;
5132
Marcelo Tosattie891a322017-04-17 12:51:37 -03005133 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005134 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005135 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005136 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005137
5138 r = -EFAULT;
5139 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5140 goto out;
5141 r = 0;
5142 break;
5143 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005144 case KVM_MEMORY_ENCRYPT_OP: {
5145 r = -ENOTTY;
5146 if (kvm_x86_ops->mem_enc_op)
5147 r = kvm_x86_ops->mem_enc_op(kvm, argp);
5148 break;
5149 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005150 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5151 struct kvm_enc_region region;
5152
5153 r = -EFAULT;
5154 if (copy_from_user(&region, argp, sizeof(region)))
5155 goto out;
5156
5157 r = -ENOTTY;
5158 if (kvm_x86_ops->mem_enc_reg_region)
5159 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
5160 break;
5161 }
5162 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5163 struct kvm_enc_region region;
5164
5165 r = -EFAULT;
5166 if (copy_from_user(&region, argp, sizeof(region)))
5167 goto out;
5168
5169 r = -ENOTTY;
5170 if (kvm_x86_ops->mem_enc_unreg_region)
5171 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
5172 break;
5173 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005174 case KVM_HYPERV_EVENTFD: {
5175 struct kvm_hyperv_eventfd hvevfd;
5176
5177 r = -EFAULT;
5178 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5179 goto out;
5180 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5181 break;
5182 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005183 case KVM_SET_PMU_EVENT_FILTER:
5184 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5185 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005186 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005187 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005188 }
5189out:
5190 return r;
5191}
5192
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005193static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005194{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005195 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005196 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005197 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005198
Jim Mattsone2ada662019-08-21 11:20:04 -07005199 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005200 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005201
5202 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005203
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005204 num_msrs_to_save = 0;
5205 num_emulated_msrs = 0;
5206 num_msr_based_features = 0;
5207
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005208 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5209 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005210 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005211
5212 /*
5213 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005214 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005215 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005216 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005217 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005218 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005219 continue;
5220 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005221 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005222 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005223 continue;
5224 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005225 case MSR_IA32_RTIT_CTL:
5226 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005227 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005228 continue;
5229 break;
5230 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005231 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005232 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5233 continue;
5234 break;
5235 case MSR_IA32_RTIT_OUTPUT_BASE:
5236 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005237 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005238 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5239 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5240 continue;
5241 break;
5242 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
Sean Christopherson7b874c22020-03-02 15:56:59 -08005243 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005244 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005245 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5246 continue;
5247 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005248 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005249 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005250 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5251 continue;
5252 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005253 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005254 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005255 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5256 continue;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005257 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005258 default:
5259 break;
5260 }
5261
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005262 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005263 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005264
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005265 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
5266 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005267 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005268
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005269 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005270 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005271
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005272 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005273 struct kvm_msr_entry msr;
5274
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005275 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005276 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005277 continue;
5278
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005279 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005280 }
Carsten Otte043405e2007-10-10 17:16:19 +02005281}
5282
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005283static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5284 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005285{
Avi Kivity70252a12010-01-19 12:51:22 +02005286 int handled = 0;
5287 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005288
Avi Kivity70252a12010-01-19 12:51:22 +02005289 do {
5290 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005291 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005292 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5293 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005294 break;
5295 handled += n;
5296 addr += n;
5297 len -= n;
5298 v += n;
5299 } while (len);
5300
5301 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005302}
5303
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005304static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005305{
Avi Kivity70252a12010-01-19 12:51:22 +02005306 int handled = 0;
5307 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005308
Avi Kivity70252a12010-01-19 12:51:22 +02005309 do {
5310 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005311 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005312 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5313 addr, n, v))
5314 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005315 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005316 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005317 handled += n;
5318 addr += n;
5319 len -= n;
5320 v += n;
5321 } while (len);
5322
5323 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005324}
5325
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005326static void kvm_set_segment(struct kvm_vcpu *vcpu,
5327 struct kvm_segment *var, int seg)
5328{
5329 kvm_x86_ops->set_segment(vcpu, var, seg);
5330}
5331
5332void kvm_get_segment(struct kvm_vcpu *vcpu,
5333 struct kvm_segment *var, int seg)
5334{
5335 kvm_x86_ops->get_segment(vcpu, var, seg);
5336}
5337
Paolo Bonzini54987b72014-09-02 13:23:06 +02005338gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5339 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005340{
5341 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005342
5343 BUG_ON(!mmu_is_nested(vcpu));
5344
5345 /* NPT walks are always user-walks */
5346 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005347 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005348
5349 return t_gpa;
5350}
5351
Avi Kivityab9ae312010-11-22 17:53:26 +02005352gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5353 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005354{
5355 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005356 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005357}
5358
Avi Kivityab9ae312010-11-22 17:53:26 +02005359 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5360 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005361{
5362 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5363 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005364 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005365}
5366
Avi Kivityab9ae312010-11-22 17:53:26 +02005367gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5368 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005369{
5370 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5371 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005372 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005373}
5374
5375/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005376gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5377 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005378{
Avi Kivityab9ae312010-11-22 17:53:26 +02005379 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005380}
5381
5382static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5383 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005384 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005385{
5386 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005387 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005388
5389 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005390 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005391 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005392 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005393 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005394 int ret;
5395
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005396 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005397 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005398 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5399 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005400 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005401 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005402 goto out;
5403 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005404
Izik Eidus77c20022008-12-29 01:42:19 +02005405 bytes -= toread;
5406 data += toread;
5407 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005408 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005409out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005410 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005411}
Izik Eidus77c20022008-12-29 01:42:19 +02005412
Gleb Natapov1871c602010-02-10 14:21:32 +02005413/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005414static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5415 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005416 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005417{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005418 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005419 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005420 unsigned offset;
5421 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005422
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005423 /* Inline kvm_read_guest_virt_helper for speed. */
5424 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5425 exception);
5426 if (unlikely(gpa == UNMAPPED_GVA))
5427 return X86EMUL_PROPAGATE_FAULT;
5428
5429 offset = addr & (PAGE_SIZE-1);
5430 if (WARN_ON(offset + bytes > PAGE_SIZE))
5431 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005432 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5433 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005434 if (unlikely(ret < 0))
5435 return X86EMUL_IO_NEEDED;
5436
5437 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005438}
5439
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005440int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005441 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005442 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005443{
5444 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005445
Paolo Bonzini353c0952019-01-29 18:41:16 +01005446 /*
5447 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5448 * is returned, but our callers are not ready for that and they blindly
5449 * call kvm_inject_page_fault. Ensure that they at least do not leak
5450 * uninitialized kernel stack memory into cr2 and error code.
5451 */
5452 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005453 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005454 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005455}
Nadav Har'El064aea72011-05-25 23:04:56 +03005456EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005457
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005458static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5459 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005460 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005461{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005462 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005463 u32 access = 0;
5464
5465 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5466 access |= PFERR_USER_MASK;
5467
5468 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005469}
5470
Radim Krčmář7a036a62015-10-30 16:36:24 +01005471static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5472 unsigned long addr, void *val, unsigned int bytes)
5473{
5474 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5475 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5476
5477 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5478}
5479
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005480static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5481 struct kvm_vcpu *vcpu, u32 access,
5482 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005483{
5484 void *data = val;
5485 int r = X86EMUL_CONTINUE;
5486
5487 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005488 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005489 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005490 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005491 unsigned offset = addr & (PAGE_SIZE-1);
5492 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5493 int ret;
5494
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005495 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005496 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005497 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005498 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005499 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005500 goto out;
5501 }
5502
5503 bytes -= towrite;
5504 data += towrite;
5505 addr += towrite;
5506 }
5507out:
5508 return r;
5509}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005510
5511static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005512 unsigned int bytes, struct x86_exception *exception,
5513 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005514{
5515 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005516 u32 access = PFERR_WRITE_MASK;
5517
5518 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5519 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005520
5521 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005522 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005523}
5524
5525int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5526 unsigned int bytes, struct x86_exception *exception)
5527{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005528 /* kvm_write_guest_virt_system can pull in tons of pages. */
5529 vcpu->arch.l1tf_flush_l1d = true;
5530
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005531 /*
5532 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5533 * is returned, but our callers are not ready for that and they blindly
5534 * call kvm_inject_page_fault. Ensure that they at least do not leak
5535 * uninitialized kernel stack memory into cr2 and error code.
5536 */
5537 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005538 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5539 PFERR_WRITE_MASK, exception);
5540}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005541EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005542
Wanpeng Li082d06e2018-04-03 16:28:48 -07005543int handle_ud(struct kvm_vcpu *vcpu)
5544{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005545 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005546 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005547 char sig[5]; /* ud2; .ascii "kvm" */
5548 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005549
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005550 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005551 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5552 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005553 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005554 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005555 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005556 }
5557
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005558 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005559}
5560EXPORT_SYMBOL_GPL(handle_ud);
5561
Tom Lendacky0f89b202016-12-14 14:59:23 -05005562static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5563 gpa_t gpa, bool write)
5564{
5565 /* For APIC access vmexit */
5566 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5567 return 1;
5568
5569 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5570 trace_vcpu_match_mmio(gva, gpa, write, true);
5571 return 1;
5572 }
5573
5574 return 0;
5575}
5576
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005577static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5578 gpa_t *gpa, struct x86_exception *exception,
5579 bool write)
5580{
Avi Kivity97d64b72012-09-12 14:52:00 +03005581 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5582 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005583
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005584 /*
5585 * currently PKRU is only applied to ept enabled guest so
5586 * there is no pkey in EPT page table for L1 guest or EPT
5587 * shadow page table for L2 guest.
5588 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005589 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005590 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005591 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005592 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5593 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005594 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005595 return 1;
5596 }
5597
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005598 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5599
5600 if (*gpa == UNMAPPED_GVA)
5601 return -1;
5602
Tom Lendacky0f89b202016-12-14 14:59:23 -05005603 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005604}
5605
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005606int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005607 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005608{
5609 int ret;
5610
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005611 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005612 if (ret < 0)
5613 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005614 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005615 return 1;
5616}
5617
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005618struct read_write_emulator_ops {
5619 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5620 int bytes);
5621 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5622 void *val, int bytes);
5623 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5624 int bytes, void *val);
5625 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5626 void *val, int bytes);
5627 bool write;
5628};
5629
5630static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5631{
5632 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005633 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005634 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005635 vcpu->mmio_read_completed = 0;
5636 return 1;
5637 }
5638
5639 return 0;
5640}
5641
5642static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5643 void *val, int bytes)
5644{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005645 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005646}
5647
5648static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5649 void *val, int bytes)
5650{
5651 return emulator_write_phys(vcpu, gpa, val, bytes);
5652}
5653
5654static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5655{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005656 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005657 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5658}
5659
5660static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5661 void *val, int bytes)
5662{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005663 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005664 return X86EMUL_IO_NEEDED;
5665}
5666
5667static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5668 void *val, int bytes)
5669{
Avi Kivityf78146b2012-04-18 19:22:47 +03005670 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5671
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005672 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005673 return X86EMUL_CONTINUE;
5674}
5675
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005676static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005677 .read_write_prepare = read_prepare,
5678 .read_write_emulate = read_emulate,
5679 .read_write_mmio = vcpu_mmio_read,
5680 .read_write_exit_mmio = read_exit_mmio,
5681};
5682
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005683static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005684 .read_write_emulate = write_emulate,
5685 .read_write_mmio = write_mmio,
5686 .read_write_exit_mmio = write_exit_mmio,
5687 .write = true,
5688};
5689
Xiao Guangrong22388a32011-07-13 14:32:31 +08005690static int emulator_read_write_onepage(unsigned long addr, void *val,
5691 unsigned int bytes,
5692 struct x86_exception *exception,
5693 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005694 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005695{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005696 gpa_t gpa;
5697 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005698 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005699 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005700 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005701
5702 /*
5703 * If the exit was due to a NPF we may already have a GPA.
5704 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5705 * Note, this cannot be used on string operations since string
5706 * operation using rep will only have the initial GPA from the NPF
5707 * occurred.
5708 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005709 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5710 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5711 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005712 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5713 } else {
5714 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5715 if (ret < 0)
5716 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005717 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005718
Brijesh Singh618232e2017-08-17 18:36:57 +02005719 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005720 return X86EMUL_CONTINUE;
5721
Carsten Ottebbd9b642007-10-30 18:44:21 +01005722 /*
5723 * Is this MMIO handled locally?
5724 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005725 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005726 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005727 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005728
Avi Kivity70252a12010-01-19 12:51:22 +02005729 gpa += handled;
5730 bytes -= handled;
5731 val += handled;
5732
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005733 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5734 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5735 frag->gpa = gpa;
5736 frag->data = val;
5737 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005738 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005739}
5740
Xiubo Li52eb5a62015-03-13 17:39:45 +08005741static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5742 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005743 void *val, unsigned int bytes,
5744 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005745 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005746{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005747 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005748 gpa_t gpa;
5749 int rc;
5750
5751 if (ops->read_write_prepare &&
5752 ops->read_write_prepare(vcpu, val, bytes))
5753 return X86EMUL_CONTINUE;
5754
5755 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005756
Carsten Ottebbd9b642007-10-30 18:44:21 +01005757 /* Crossing a page boundary? */
5758 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005759 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005760
5761 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005762 rc = emulator_read_write_onepage(addr, val, now, exception,
5763 vcpu, ops);
5764
Carsten Ottebbd9b642007-10-30 18:44:21 +01005765 if (rc != X86EMUL_CONTINUE)
5766 return rc;
5767 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005768 if (ctxt->mode != X86EMUL_MODE_PROT64)
5769 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005770 val += now;
5771 bytes -= now;
5772 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005773
Avi Kivityf78146b2012-04-18 19:22:47 +03005774 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5775 vcpu, ops);
5776 if (rc != X86EMUL_CONTINUE)
5777 return rc;
5778
5779 if (!vcpu->mmio_nr_fragments)
5780 return rc;
5781
5782 gpa = vcpu->mmio_fragments[0].gpa;
5783
5784 vcpu->mmio_needed = 1;
5785 vcpu->mmio_cur_fragment = 0;
5786
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005787 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005788 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5789 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5790 vcpu->run->mmio.phys_addr = gpa;
5791
5792 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005793}
5794
5795static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5796 unsigned long addr,
5797 void *val,
5798 unsigned int bytes,
5799 struct x86_exception *exception)
5800{
5801 return emulator_read_write(ctxt, addr, val, bytes,
5802 exception, &read_emultor);
5803}
5804
Xiubo Li52eb5a62015-03-13 17:39:45 +08005805static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005806 unsigned long addr,
5807 const void *val,
5808 unsigned int bytes,
5809 struct x86_exception *exception)
5810{
5811 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5812 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005813}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005814
Avi Kivitydaea3e72010-03-15 13:59:54 +02005815#define CMPXCHG_TYPE(t, ptr, old, new) \
5816 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5817
5818#ifdef CONFIG_X86_64
5819# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5820#else
5821# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005822 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005823#endif
5824
Avi Kivity0f65dd72011-04-20 13:37:53 +03005825static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5826 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005827 const void *old,
5828 const void *new,
5829 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005830 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005831{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005832 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005833 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005834 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005835 char *kaddr;
5836 bool exchanged;
5837
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005838 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005839 if (bytes > 8 || (bytes & (bytes - 1)))
5840 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005841
Avi Kivitydaea3e72010-03-15 13:59:54 +02005842 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005843
Avi Kivitydaea3e72010-03-15 13:59:54 +02005844 if (gpa == UNMAPPED_GVA ||
5845 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5846 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005847
Avi Kivitydaea3e72010-03-15 13:59:54 +02005848 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5849 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005850
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005851 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005852 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005853
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005854 kaddr = map.hva + offset_in_page(gpa);
5855
Avi Kivitydaea3e72010-03-15 13:59:54 +02005856 switch (bytes) {
5857 case 1:
5858 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5859 break;
5860 case 2:
5861 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5862 break;
5863 case 4:
5864 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5865 break;
5866 case 8:
5867 exchanged = CMPXCHG64(kaddr, old, new);
5868 break;
5869 default:
5870 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005871 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005872
5873 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005874
5875 if (!exchanged)
5876 return X86EMUL_CMPXCHG_FAILED;
5877
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005878 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005879
5880 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005881
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005882emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005883 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005884
Avi Kivity0f65dd72011-04-20 13:37:53 +03005885 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005886}
5887
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005888static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5889{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005890 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005891
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005892 for (i = 0; i < vcpu->arch.pio.count; i++) {
5893 if (vcpu->arch.pio.in)
5894 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5895 vcpu->arch.pio.size, pd);
5896 else
5897 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5898 vcpu->arch.pio.port, vcpu->arch.pio.size,
5899 pd);
5900 if (r)
5901 break;
5902 pd += vcpu->arch.pio.size;
5903 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005904 return r;
5905}
5906
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005907static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5908 unsigned short port, void *val,
5909 unsigned int count, bool in)
5910{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005911 vcpu->arch.pio.port = port;
5912 vcpu->arch.pio.in = in;
5913 vcpu->arch.pio.count = count;
5914 vcpu->arch.pio.size = size;
5915
5916 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5917 vcpu->arch.pio.count = 0;
5918 return 1;
5919 }
5920
5921 vcpu->run->exit_reason = KVM_EXIT_IO;
5922 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5923 vcpu->run->io.size = size;
5924 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5925 vcpu->run->io.count = count;
5926 vcpu->run->io.port = port;
5927
5928 return 0;
5929}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005930
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005931static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
5932 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005933{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005934 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005935
Gleb Natapov79729952010-03-18 15:20:24 +02005936 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005937 goto data_avail;
5938
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005939 memset(vcpu->arch.pio_data, 0, size * count);
5940
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005941 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5942 if (ret) {
5943data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005944 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005945 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005946 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005947 return 1;
5948 }
5949
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005950 return 0;
5951}
5952
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005953static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5954 int size, unsigned short port, void *val,
5955 unsigned int count)
5956{
5957 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
5958
5959}
5960
5961static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
5962 unsigned short port, const void *val,
5963 unsigned int count)
5964{
5965 memcpy(vcpu->arch.pio_data, val, size * count);
5966 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
5967 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
5968}
5969
Avi Kivityca1d4a92011-04-20 13:37:53 +03005970static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5971 int size, unsigned short port,
5972 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005973{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005974 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005975}
5976
Carsten Ottebbd9b642007-10-30 18:44:21 +01005977static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5978{
5979 return kvm_x86_ops->get_segment_base(vcpu, seg);
5980}
5981
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005982static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005983{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005984 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005985}
5986
Jiang Biaoae6a2372016-11-07 08:54:51 +08005987static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005988{
5989 if (!need_emulate_wbinvd(vcpu))
5990 return X86EMUL_CONTINUE;
5991
5992 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01005993 int cpu = get_cpu();
5994
5995 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005996 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
5997 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005998 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005999 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006000 } else
6001 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006002 return X86EMUL_CONTINUE;
6003}
Joel Schopp5cb56052015-03-02 13:43:31 -06006004
6005int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6006{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006007 kvm_emulate_wbinvd_noskip(vcpu);
6008 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006009}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006010EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6011
Joel Schopp5cb56052015-03-02 13:43:31 -06006012
6013
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006014static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6015{
Joel Schopp5cb56052015-03-02 13:43:31 -06006016 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006017}
6018
Xiubo Li52eb5a62015-03-13 17:39:45 +08006019static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6020 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006021{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006022 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006023}
6024
Xiubo Li52eb5a62015-03-13 17:39:45 +08006025static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6026 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006027{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006028
Avi Kivity717746e2011-04-20 13:37:53 +03006029 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006030}
6031
Gleb Natapov52a46612010-03-18 15:20:03 +02006032static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6033{
6034 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6035}
6036
Avi Kivity717746e2011-04-20 13:37:53 +03006037static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006038{
Avi Kivity717746e2011-04-20 13:37:53 +03006039 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006040 unsigned long value;
6041
6042 switch (cr) {
6043 case 0:
6044 value = kvm_read_cr0(vcpu);
6045 break;
6046 case 2:
6047 value = vcpu->arch.cr2;
6048 break;
6049 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006050 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006051 break;
6052 case 4:
6053 value = kvm_read_cr4(vcpu);
6054 break;
6055 case 8:
6056 value = kvm_get_cr8(vcpu);
6057 break;
6058 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006059 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006060 return 0;
6061 }
6062
6063 return value;
6064}
6065
Avi Kivity717746e2011-04-20 13:37:53 +03006066static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006067{
Avi Kivity717746e2011-04-20 13:37:53 +03006068 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006069 int res = 0;
6070
Gleb Natapov52a46612010-03-18 15:20:03 +02006071 switch (cr) {
6072 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006073 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006074 break;
6075 case 2:
6076 vcpu->arch.cr2 = val;
6077 break;
6078 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006079 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006080 break;
6081 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006082 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006083 break;
6084 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006085 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006086 break;
6087 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006088 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006089 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006090 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006091
6092 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006093}
6094
Avi Kivity717746e2011-04-20 13:37:53 +03006095static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006096{
Avi Kivity717746e2011-04-20 13:37:53 +03006097 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006098}
6099
Avi Kivity4bff1e862011-04-20 13:37:53 +03006100static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006101{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006102 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006103}
6104
Avi Kivity4bff1e862011-04-20 13:37:53 +03006105static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006106{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006107 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006108}
6109
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006110static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6111{
6112 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
6113}
6114
6115static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6116{
6117 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
6118}
6119
Avi Kivity4bff1e862011-04-20 13:37:53 +03006120static unsigned long emulator_get_cached_segment_base(
6121 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006122{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006123 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006124}
6125
Avi Kivity1aa36612011-04-27 13:20:30 +03006126static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6127 struct desc_struct *desc, u32 *base3,
6128 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006129{
6130 struct kvm_segment var;
6131
Avi Kivity4bff1e862011-04-20 13:37:53 +03006132 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006133 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006134
Gleb Natapov378a8b02013-01-21 15:36:48 +02006135 if (var.unusable) {
6136 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006137 if (base3)
6138 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006139 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006140 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006141
6142 if (var.g)
6143 var.limit >>= 12;
6144 set_desc_limit(desc, var.limit);
6145 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006146#ifdef CONFIG_X86_64
6147 if (base3)
6148 *base3 = var.base >> 32;
6149#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006150 desc->type = var.type;
6151 desc->s = var.s;
6152 desc->dpl = var.dpl;
6153 desc->p = var.present;
6154 desc->avl = var.avl;
6155 desc->l = var.l;
6156 desc->d = var.db;
6157 desc->g = var.g;
6158
6159 return true;
6160}
6161
Avi Kivity1aa36612011-04-27 13:20:30 +03006162static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6163 struct desc_struct *desc, u32 base3,
6164 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006165{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006166 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006167 struct kvm_segment var;
6168
Avi Kivity1aa36612011-04-27 13:20:30 +03006169 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006170 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006171#ifdef CONFIG_X86_64
6172 var.base |= ((u64)base3) << 32;
6173#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006174 var.limit = get_desc_limit(desc);
6175 if (desc->g)
6176 var.limit = (var.limit << 12) | 0xfff;
6177 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006178 var.dpl = desc->dpl;
6179 var.db = desc->d;
6180 var.s = desc->s;
6181 var.l = desc->l;
6182 var.g = desc->g;
6183 var.avl = desc->avl;
6184 var.present = desc->p;
6185 var.unusable = !var.present;
6186 var.padding = 0;
6187
6188 kvm_set_segment(vcpu, &var, seg);
6189 return;
6190}
6191
Avi Kivity717746e2011-04-20 13:37:53 +03006192static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6193 u32 msr_index, u64 *pdata)
6194{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006195 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006196}
6197
6198static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6199 u32 msr_index, u64 data)
6200{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006201 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006202}
6203
Paolo Bonzini64d60672015-05-07 11:36:11 +02006204static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6205{
6206 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6207
6208 return vcpu->arch.smbase;
6209}
6210
6211static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6212{
6213 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6214
6215 vcpu->arch.smbase = smbase;
6216}
6217
Nadav Amit67f4d422014-06-02 18:34:09 +03006218static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6219 u32 pmc)
6220{
Like Xu98ff80f2019-10-27 18:52:40 +08006221 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006222}
6223
Avi Kivity222d21a2011-11-10 14:57:30 +02006224static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6225 u32 pmc, u64 *pdata)
6226{
Wei Huangc6702c92015-06-19 13:44:45 +02006227 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006228}
6229
Avi Kivity6c3287f2011-04-20 15:43:05 +03006230static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6231{
6232 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6233}
6234
Avi Kivity29535382011-04-20 13:37:53 +03006235static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006236 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006237 enum x86_intercept_stage stage)
6238{
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006239 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage,
6240 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006241}
6242
Yu Zhange911eb32017-08-24 20:27:52 +08006243static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08006244 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
6245 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006246{
Sean Christophersonf91af512020-03-04 17:34:37 -08006247 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006248}
6249
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006250static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6251{
6252 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6253}
6254
6255static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6256{
6257 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6258}
6259
6260static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6261{
6262 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6263}
6264
Avi Kivitydd856ef2012-08-27 23:46:17 +03006265static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6266{
6267 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6268}
6269
6270static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6271{
6272 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6273}
6274
Nadav Amit801806d2015-01-26 09:32:23 +02006275static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6276{
6277 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
6278}
6279
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006280static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6281{
6282 return emul_to_vcpu(ctxt)->arch.hflags;
6283}
6284
6285static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6286{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006287 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006288}
6289
Sean Christophersoned193212019-04-02 08:03:09 -07006290static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6291 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006292{
Sean Christophersoned193212019-04-02 08:03:09 -07006293 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006294}
6295
Sean Christophersonc5833c72019-04-02 08:03:10 -07006296static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6297{
6298 kvm_smm_changed(emul_to_vcpu(ctxt));
6299}
6300
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006301static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6302{
6303 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6304}
6305
Mathias Krause0225fb52012-08-30 01:30:16 +02006306static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006307 .read_gpr = emulator_read_gpr,
6308 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006309 .read_std = emulator_read_std,
6310 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006311 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006312 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006313 .read_emulated = emulator_read_emulated,
6314 .write_emulated = emulator_write_emulated,
6315 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006316 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006317 .pio_in_emulated = emulator_pio_in_emulated,
6318 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006319 .get_segment = emulator_get_segment,
6320 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006321 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006322 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006323 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006324 .set_gdt = emulator_set_gdt,
6325 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006326 .get_cr = emulator_get_cr,
6327 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006328 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006329 .get_dr = emulator_get_dr,
6330 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006331 .get_smbase = emulator_get_smbase,
6332 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006333 .set_msr = emulator_set_msr,
6334 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006335 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006336 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006337 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006338 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006339 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006340 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006341 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006342 .guest_has_long_mode = emulator_guest_has_long_mode,
6343 .guest_has_movbe = emulator_guest_has_movbe,
6344 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006345 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006346 .get_hflags = emulator_get_hflags,
6347 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006348 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006349 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006350 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006351};
6352
Gleb Natapov95cb2292010-04-28 19:15:43 +03006353static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6354{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006355 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006356 /*
6357 * an sti; sti; sequence only disable interrupts for the first
6358 * instruction. So, if the last instruction, be it emulated or
6359 * not, left the system with the INT_STI flag enabled, it
6360 * means that the last instruction is an sti. We should not
6361 * leave the flag on in this case. The same goes for mov ss
6362 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006363 if (int_shadow & mask)
6364 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006365 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03006366 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006367 if (!mask)
6368 kvm_make_request(KVM_REQ_EVENT, vcpu);
6369 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006370}
6371
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006372static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006373{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006374 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006375 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006376 return kvm_propagate_fault(vcpu, &ctxt->exception);
6377
6378 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006379 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6380 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006381 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006382 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006383 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006384}
6385
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006386static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6387{
6388 struct x86_emulate_ctxt *ctxt;
6389
6390 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6391 if (!ctxt) {
6392 pr_err("kvm: failed to allocate vcpu's emulator\n");
6393 return NULL;
6394 }
6395
6396 ctxt->vcpu = vcpu;
6397 ctxt->ops = &emulate_ops;
6398 vcpu->arch.emulate_ctxt = ctxt;
6399
6400 return ctxt;
6401}
6402
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006403static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6404{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006405 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006406 int cs_db, cs_l;
6407
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006408 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6409
Sean Christopherson744e6992020-02-18 15:03:09 -08006410 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006411 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006412 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6413
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006414 ctxt->eip = kvm_rip_read(vcpu);
6415 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6416 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006417 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006418 cs_db ? X86EMUL_MODE_PROT32 :
6419 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006420 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006421 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6422 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006423
Avi Kivitydd856ef2012-08-27 23:46:17 +03006424 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006425 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006426}
6427
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006428void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006429{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006430 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006431 int ret;
6432
6433 init_emulate_ctxt(vcpu);
6434
Avi Kivity9dac77f2011-06-01 15:34:25 +03006435 ctxt->op_bytes = 2;
6436 ctxt->ad_bytes = 2;
6437 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006438 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006439
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006440 if (ret != X86EMUL_CONTINUE) {
6441 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6442 } else {
6443 ctxt->eip = ctxt->_eip;
6444 kvm_rip_write(vcpu, ctxt->eip);
6445 kvm_set_rflags(vcpu, ctxt->eflags);
6446 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006447}
6448EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6449
Liran Alone2366172018-03-12 13:12:49 +02006450static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006451{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006452 ++vcpu->stat.insn_emulation_fail;
6453 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006454
Sean Christopherson42cbf062019-08-27 14:40:31 -07006455 if (emulation_type & EMULTYPE_VMWARE_GP) {
6456 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006457 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006458 }
Liran Alone2366172018-03-12 13:12:49 +02006459
Sean Christopherson738fece2019-08-27 14:40:34 -07006460 if (emulation_type & EMULTYPE_SKIP) {
6461 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6462 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6463 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006464 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006465 }
6466
Sean Christopherson22da61c2019-08-27 14:40:28 -07006467 kvm_queue_exception(vcpu, UD_VECTOR);
6468
Nadav Amita2b9e6c2014-09-17 02:50:50 +03006469 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006470 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6471 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6472 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006473 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006474 }
Liran Alone2366172018-03-12 13:12:49 +02006475
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006476 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006477}
6478
Sean Christopherson736c2912019-12-06 15:57:14 -08006479static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006480 bool write_fault_to_shadow_pgtable,
6481 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006482{
Sean Christopherson736c2912019-12-06 15:57:14 -08006483 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006484 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006485
Sean Christopherson92daa482020-02-18 15:03:08 -08006486 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006487 return false;
6488
Sean Christopherson92daa482020-02-18 15:03:08 -08006489 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6490 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006491 return false;
6492
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006493 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006494 /*
6495 * Write permission should be allowed since only
6496 * write access need to be emulated.
6497 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006498 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006499
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006500 /*
6501 * If the mapping is invalid in guest, let cpu retry
6502 * it to generate fault.
6503 */
6504 if (gpa == UNMAPPED_GVA)
6505 return true;
6506 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006507
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006508 /*
6509 * Do not retry the unhandleable instruction if it faults on the
6510 * readonly host memory, otherwise it will goto a infinite loop:
6511 * retry instruction -> write #PF -> emulation fail -> retry
6512 * instruction -> ...
6513 */
6514 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006515
6516 /*
6517 * If the instruction failed on the error pfn, it can not be fixed,
6518 * report the error to userspace.
6519 */
6520 if (is_error_noslot_pfn(pfn))
6521 return false;
6522
6523 kvm_release_pfn_clean(pfn);
6524
6525 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006526 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006527 unsigned int indirect_shadow_pages;
6528
6529 spin_lock(&vcpu->kvm->mmu_lock);
6530 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6531 spin_unlock(&vcpu->kvm->mmu_lock);
6532
6533 if (indirect_shadow_pages)
6534 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6535
Gleb Natapova6f177e2010-07-08 12:41:12 +03006536 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006537 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006538
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006539 /*
6540 * if emulation was due to access to shadowed page table
6541 * and it failed try to unshadow page and re-enter the
6542 * guest to let CPU execute the instruction.
6543 */
6544 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006545
6546 /*
6547 * If the access faults on its page table, it can not
6548 * be fixed by unprotecting shadow page and it should
6549 * be reported to userspace.
6550 */
6551 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006552}
6553
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006554static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006555 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006556{
6557 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006558 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006559
6560 last_retry_eip = vcpu->arch.last_retry_eip;
6561 last_retry_addr = vcpu->arch.last_retry_addr;
6562
6563 /*
6564 * If the emulation is caused by #PF and it is non-page_table
6565 * writing instruction, it means the VM-EXIT is caused by shadow
6566 * page protected, we can zap the shadow page and retry this
6567 * instruction directly.
6568 *
6569 * Note: if the guest uses a non-page-table modifying instruction
6570 * on the PDE that points to the instruction, then we will unmap
6571 * the instruction and go to an infinite loop. So, we cache the
6572 * last retried eip and the last fault address, if we meet the eip
6573 * and the address again, we can break out of the potential infinite
6574 * loop.
6575 */
6576 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6577
Sean Christopherson92daa482020-02-18 15:03:08 -08006578 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006579 return false;
6580
Sean Christopherson92daa482020-02-18 15:03:08 -08006581 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6582 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006583 return false;
6584
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006585 if (x86_page_table_writing_insn(ctxt))
6586 return false;
6587
Sean Christopherson736c2912019-12-06 15:57:14 -08006588 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006589 return false;
6590
6591 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006592 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006593
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006594 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006595 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006596
Xiao Guangrong22368022013-01-13 23:44:12 +08006597 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006598
6599 return true;
6600}
6601
Gleb Natapov716d51a2012-09-03 15:24:26 +03006602static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6603static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6604
Paolo Bonzini64d60672015-05-07 11:36:11 +02006605static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006606{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006607 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006608 /* This is a good place to trace that we are exiting SMM. */
6609 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6610
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006611 /* Process a latched INIT or SMI, if any. */
6612 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006613 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006614
6615 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006616}
6617
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006618static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6619 unsigned long *db)
6620{
6621 u32 dr6 = 0;
6622 int i;
6623 u32 enable, rwlen;
6624
6625 enable = dr7;
6626 rwlen = dr7 >> 16;
6627 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6628 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6629 dr6 |= (1 << i);
6630 return dr6;
6631}
6632
Sean Christopherson120c2c42019-08-27 14:40:29 -07006633static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006634{
6635 struct kvm_run *kvm_run = vcpu->run;
6636
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006637 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6638 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6639 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6640 kvm_run->debug.arch.exception = DB_VECTOR;
6641 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006642 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006643 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006644 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006645 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006646}
6647
Kyle Huey6affcbe2016-11-29 12:40:40 -08006648int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6649{
6650 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006651 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006652
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006653 r = kvm_x86_ops->skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006654 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006655 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006656
6657 /*
6658 * rflags is the old, "raw" value of the flags. The new value has
6659 * not been saved yet.
6660 *
6661 * This is correct even for TF set by the guest, because "the
6662 * processor will not generate this exception after the instruction
6663 * that sets the TF flag".
6664 */
6665 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006666 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006667 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006668}
6669EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6670
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006671static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6672{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006673 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6674 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006675 struct kvm_run *kvm_run = vcpu->run;
6676 unsigned long eip = kvm_get_linear_rip(vcpu);
6677 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006678 vcpu->arch.guest_debug_dr7,
6679 vcpu->arch.eff_db);
6680
6681 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006682 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006683 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006684 kvm_run->debug.arch.exception = DB_VECTOR;
6685 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006686 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006687 return true;
6688 }
6689 }
6690
Nadav Amit4161a562014-07-17 01:19:31 +03006691 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6692 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006693 unsigned long eip = kvm_get_linear_rip(vcpu);
6694 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006695 vcpu->arch.dr7,
6696 vcpu->arch.db);
6697
6698 if (dr6 != 0) {
Liran Alon1fc5d1942019-06-06 01:54:47 +03006699 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006700 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006701 kvm_queue_exception(vcpu, DB_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006702 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006703 return true;
6704 }
6705 }
6706
6707 return false;
6708}
6709
Liran Alon04789b62018-03-12 13:12:50 +02006710static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6711{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006712 switch (ctxt->opcode_len) {
6713 case 1:
6714 switch (ctxt->b) {
6715 case 0xe4: /* IN */
6716 case 0xe5:
6717 case 0xec:
6718 case 0xed:
6719 case 0xe6: /* OUT */
6720 case 0xe7:
6721 case 0xee:
6722 case 0xef:
6723 case 0x6c: /* INS */
6724 case 0x6d:
6725 case 0x6e: /* OUTS */
6726 case 0x6f:
6727 return true;
6728 }
6729 break;
6730 case 2:
6731 switch (ctxt->b) {
6732 case 0x33: /* RDPMC */
6733 return true;
6734 }
6735 break;
Liran Alon04789b62018-03-12 13:12:50 +02006736 }
6737
6738 return false;
6739}
6740
Sean Christopherson736c2912019-12-06 15:57:14 -08006741int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6742 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006743{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006744 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006745 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006746 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006747 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006748
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006749 vcpu->arch.l1tf_flush_l1d = true;
6750
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006751 /*
6752 * Clear write_fault_to_shadow_pgtable here to ensure it is
6753 * never reused.
6754 */
6755 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006756 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006757
Sheng Yang571008d2008-01-02 14:49:22 +08006758 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006759 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006760
6761 /*
6762 * We will reenter on the same instruction since
6763 * we do not set complete_userspace_io. This does not
6764 * handle watchpoints yet, those would be handled in
6765 * the emulate_ops.
6766 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006767 if (!(emulation_type & EMULTYPE_SKIP) &&
6768 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006769 return r;
6770
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006771 ctxt->interruptibility = 0;
6772 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006773 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006774 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006775
Borislav Petkovb51e9742013-09-22 16:44:52 +02006776 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006777
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006778 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006779
Avi Kivitye46479f2010-04-11 13:05:16 +03006780 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006781 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006782 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006783 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006784 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6785 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006786 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006787 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006788 if (reexecute_instruction(vcpu, cr2_or_gpa,
6789 write_fault_to_spt,
6790 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006791 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006792 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006793 /*
6794 * #UD should result in just EMULATION_FAILED, and trap-like
6795 * exception should not be encountered during decode.
6796 */
6797 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6798 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006799 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006800 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006801 }
Liran Alone2366172018-03-12 13:12:49 +02006802 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006803 }
6804 }
6805
Sean Christopherson42cbf062019-08-27 14:40:31 -07006806 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6807 !is_vmware_backdoor_opcode(ctxt)) {
6808 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006809 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006810 }
Liran Alon04789b62018-03-12 13:12:50 +02006811
Sean Christopherson1957aa62019-08-27 14:40:39 -07006812 /*
6813 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6814 * for kvm_skip_emulated_instruction(). The caller is responsible for
6815 * updating interruptibility state and injecting single-step #DBs.
6816 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006817 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006818 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006819 if (ctxt->eflags & X86_EFLAGS_RF)
6820 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006821 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006822 }
6823
Sean Christopherson736c2912019-12-06 15:57:14 -08006824 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006825 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006826
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006827 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006828 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006829 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6830 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006831 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006832 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006833
Gleb Natapov5cd21912010-03-18 15:20:26 +02006834restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006835 if (emulation_type & EMULTYPE_PF) {
6836 /* Save the faulting GPA (cr2) in the address field */
6837 ctxt->exception.address = cr2_or_gpa;
6838
6839 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6840 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006841 ctxt->gpa_available = true;
6842 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006843 }
6844 } else {
6845 /* Sanitize the address out of an abundance of paranoia. */
6846 ctxt->exception.address = 0;
6847 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006848
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006849 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006850
Joerg Roedel775fde82011-04-04 12:39:24 +02006851 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006852 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006853
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006854 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006855 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006856 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006857 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006858
Liran Alone2366172018-03-12 13:12:49 +02006859 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006860 }
6861
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006862 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006863 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006864 if (inject_emulated_exception(vcpu))
6865 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006866 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006867 if (!vcpu->arch.pio.in) {
6868 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006869 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006870 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006871 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006872 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6873 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006874 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006875 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006876 ++vcpu->stat.mmio_exits;
6877
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006878 if (!vcpu->mmio_is_write)
6879 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006880 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006881 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006882 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006883 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006884 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006885 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006886
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006887 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006888 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006889 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006890 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006891 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006892 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6893 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006894 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006895 r = kvm_vcpu_do_singlestep(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006896 if (kvm_x86_ops->update_emulated_instruction)
6897 kvm_x86_ops->update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006898 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006899 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006900
6901 /*
6902 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6903 * do nothing, and it will be requested again as soon as
6904 * the shadow expires. But we still need to check here,
6905 * because POPF has no interrupt shadow.
6906 */
6907 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6908 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006909 } else
6910 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006911
Gleb Natapove85d28f2010-07-29 15:11:52 +03006912 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006913}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006914
6915int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6916{
6917 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6918}
6919EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6920
6921int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6922 void *insn, int insn_len)
6923{
6924 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6925}
6926EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006927
Sean Christopherson8764ed52019-04-29 07:04:15 -07006928static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6929{
6930 vcpu->arch.pio.count = 0;
6931 return 1;
6932}
6933
Sean Christopherson45def772019-03-11 20:01:05 -07006934static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6935{
6936 vcpu->arch.pio.count = 0;
6937
6938 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6939 return 1;
6940
6941 return kvm_skip_emulated_instruction(vcpu);
6942}
6943
Sean Christophersondca7f122018-03-08 08:57:27 -08006944static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6945 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006946{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006947 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006948 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
6949
Sean Christopherson8764ed52019-04-29 07:04:15 -07006950 if (ret)
6951 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006952
Sean Christopherson8764ed52019-04-29 07:04:15 -07006953 /*
6954 * Workaround userspace that relies on old KVM behavior of %rip being
6955 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6956 */
6957 if (port == 0x7e &&
6958 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6959 vcpu->arch.complete_userspace_io =
6960 complete_fast_pio_out_port_0x7e;
6961 kvm_skip_emulated_instruction(vcpu);
6962 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006963 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6964 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6965 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006966 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006967}
Carsten Ottede7d7892007-10-30 18:44:25 +01006968
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006969static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6970{
6971 unsigned long val;
6972
6973 /* We should only ever be called with arch.pio.count equal to 1 */
6974 BUG_ON(vcpu->arch.pio.count != 1);
6975
Sean Christopherson45def772019-03-11 20:01:05 -07006976 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
6977 vcpu->arch.pio.count = 0;
6978 return 1;
6979 }
6980
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006981 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006982 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006983
6984 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006985 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006986 * the copy and tracing
6987 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006988 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07006989 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006990
Sean Christopherson45def772019-03-11 20:01:05 -07006991 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006992}
6993
Sean Christophersondca7f122018-03-08 08:57:27 -08006994static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
6995 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006996{
6997 unsigned long val;
6998 int ret;
6999
7000 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007001 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007002
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007003 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007004 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007005 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007006 return ret;
7007 }
7008
Sean Christopherson45def772019-03-11 20:01:05 -07007009 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007010 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7011
7012 return 0;
7013}
Sean Christophersondca7f122018-03-08 08:57:27 -08007014
7015int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7016{
Sean Christopherson45def772019-03-11 20:01:05 -07007017 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007018
Sean Christophersondca7f122018-03-08 08:57:27 -08007019 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007020 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007021 else
Sean Christopherson45def772019-03-11 20:01:05 -07007022 ret = kvm_fast_pio_out(vcpu, size, port);
7023 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007024}
7025EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007026
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007027static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007028{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007029 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007030 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007031}
7032
7033static void tsc_khz_changed(void *data)
7034{
7035 struct cpufreq_freqs *freq = data;
7036 unsigned long khz = 0;
7037
7038 if (data)
7039 khz = freq->new;
7040 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7041 khz = cpufreq_quick_get(raw_smp_processor_id());
7042 if (!khz)
7043 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007044 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007045}
7046
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007047#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007048static void kvm_hyperv_tsc_notifier(void)
7049{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007050 struct kvm *kvm;
7051 struct kvm_vcpu *vcpu;
7052 int cpu;
7053
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007054 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007055 list_for_each_entry(kvm, &vm_list, vm_list)
7056 kvm_make_mclock_inprogress_request(kvm);
7057
7058 hyperv_stop_tsc_emulation();
7059
7060 /* TSC frequency always matches when on Hyper-V */
7061 for_each_present_cpu(cpu)
7062 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7063 kvm_max_guest_tsc_khz = tsc_khz;
7064
7065 list_for_each_entry(kvm, &vm_list, vm_list) {
7066 struct kvm_arch *ka = &kvm->arch;
7067
7068 spin_lock(&ka->pvclock_gtod_sync_lock);
7069
7070 pvclock_update_vm_gtod_copy(kvm);
7071
7072 kvm_for_each_vcpu(cpu, vcpu, kvm)
7073 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7074
7075 kvm_for_each_vcpu(cpu, vcpu, kvm)
7076 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7077
7078 spin_unlock(&ka->pvclock_gtod_sync_lock);
7079 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007080 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007081}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007082#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007083
Viresh Kumardf240142019-04-29 15:03:58 +05307084static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007085{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007086 struct kvm *kvm;
7087 struct kvm_vcpu *vcpu;
7088 int i, send_ipi = 0;
7089
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007090 /*
7091 * We allow guests to temporarily run on slowing clocks,
7092 * provided we notify them after, or to run on accelerating
7093 * clocks, provided we notify them before. Thus time never
7094 * goes backwards.
7095 *
7096 * However, we have a problem. We can't atomically update
7097 * the frequency of a given CPU from this function; it is
7098 * merely a notifier, which can be called from any CPU.
7099 * Changing the TSC frequency at arbitrary points in time
7100 * requires a recomputation of local variables related to
7101 * the TSC for each VCPU. We must flag these local variables
7102 * to be updated and be sure the update takes place with the
7103 * new frequency before any guests proceed.
7104 *
7105 * Unfortunately, the combination of hotplug CPU and frequency
7106 * change creates an intractable locking scenario; the order
7107 * of when these callouts happen is undefined with respect to
7108 * CPU hotplug, and they can race with each other. As such,
7109 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7110 * undefined; you can actually have a CPU frequency change take
7111 * place in between the computation of X and the setting of the
7112 * variable. To protect against this problem, all updates of
7113 * the per_cpu tsc_khz variable are done in an interrupt
7114 * protected IPI, and all callers wishing to update the value
7115 * must wait for a synchronous IPI to complete (which is trivial
7116 * if the caller is on the CPU already). This establishes the
7117 * necessary total order on variable updates.
7118 *
7119 * Note that because a guest time update may take place
7120 * anytime after the setting of the VCPU's request bit, the
7121 * correct TSC value must be set before the request. However,
7122 * to ensure the update actually makes it to any guest which
7123 * starts running in hardware virtualization between the set
7124 * and the acquisition of the spinlock, we must also ping the
7125 * CPU after setting the request bit.
7126 *
7127 */
7128
Viresh Kumardf240142019-04-29 15:03:58 +05307129 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007130
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007131 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007132 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007133 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307134 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007135 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007136 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007137 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007138 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007139 }
7140 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007141 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007142
7143 if (freq->old < freq->new && send_ipi) {
7144 /*
7145 * We upscale the frequency. Must make the guest
7146 * doesn't see old kvmclock values while running with
7147 * the new frequency, otherwise we risk the guest sees
7148 * time go backwards.
7149 *
7150 * In case we update the frequency for another cpu
7151 * (which might be in guest context) send an interrupt
7152 * to kick the cpu out of guest context. Next time
7153 * guest context is entered kvmclock will be updated,
7154 * so the guest will not see stale values.
7155 */
Viresh Kumardf240142019-04-29 15:03:58 +05307156 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007157 }
Viresh Kumardf240142019-04-29 15:03:58 +05307158}
7159
7160static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7161 void *data)
7162{
7163 struct cpufreq_freqs *freq = data;
7164 int cpu;
7165
7166 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7167 return 0;
7168 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7169 return 0;
7170
7171 for_each_cpu(cpu, freq->policy->cpus)
7172 __kvmclock_cpufreq_notifier(freq, cpu);
7173
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007174 return 0;
7175}
7176
7177static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007178 .notifier_call = kvmclock_cpufreq_notifier
7179};
7180
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007181static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007182{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007183 tsc_khz_changed(NULL);
7184 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007185}
7186
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007187static void kvm_timer_init(void)
7188{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007189 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307190
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007191 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007192#ifdef CONFIG_CPU_FREQ
7193 struct cpufreq_policy policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007194 int cpu;
7195
Zachary Amsdenc2855452010-09-18 14:38:15 -10007196 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02007197 cpu = get_cpu();
7198 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007199 if (policy.cpuinfo.max_freq)
7200 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02007201 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007202#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007203 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7204 CPUFREQ_TRANSITION_NOTIFIER);
7205 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307206
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007207 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007208 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007209}
7210
Andi Kleendd60d212017-07-25 17:20:32 -07007211DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7212EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007213
Gleb Natapovf5132b02011-11-10 14:57:22 +02007214int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007215{
Alex,Shi086c9852011-10-20 15:34:01 +08007216 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007217}
7218
7219static int kvm_is_user_mode(void)
7220{
7221 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007222
Alex,Shi086c9852011-10-20 15:34:01 +08007223 if (__this_cpu_read(current_vcpu))
7224 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007225
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007226 return user_mode != 0;
7227}
7228
7229static unsigned long kvm_get_guest_ip(void)
7230{
7231 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007232
Alex,Shi086c9852011-10-20 15:34:01 +08007233 if (__this_cpu_read(current_vcpu))
7234 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007235
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007236 return ip;
7237}
7238
Luwei Kang8479e042019-02-18 19:26:07 -05007239static void kvm_handle_intel_pt_intr(void)
7240{
7241 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7242
7243 kvm_make_request(KVM_REQ_PMI, vcpu);
7244 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7245 (unsigned long *)&vcpu->arch.pmu.global_status);
7246}
7247
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007248static struct perf_guest_info_callbacks kvm_guest_cbs = {
7249 .is_in_guest = kvm_is_in_guest,
7250 .is_user_mode = kvm_is_user_mode,
7251 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007252 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007253};
7254
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007255#ifdef CONFIG_X86_64
7256static void pvclock_gtod_update_fn(struct work_struct *work)
7257{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007258 struct kvm *kvm;
7259
7260 struct kvm_vcpu *vcpu;
7261 int i;
7262
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007263 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007264 list_for_each_entry(kvm, &vm_list, vm_list)
7265 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007266 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007267 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007268 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007269}
7270
7271static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7272
7273/*
7274 * Notification about pvclock gtod data update.
7275 */
7276static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7277 void *priv)
7278{
7279 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7280 struct timekeeper *tk = priv;
7281
7282 update_pvclock_gtod(tk);
7283
7284 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007285 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007286 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007287 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007288 atomic_read(&kvm_guest_has_master_clock) != 0)
7289 queue_work(system_long_wq, &pvclock_gtod_work);
7290
7291 return 0;
7292}
7293
7294static struct notifier_block pvclock_gtod_notifier = {
7295 .notifier_call = pvclock_gtod_notify,
7296};
7297#endif
7298
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007299int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007300{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007301 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02007302 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007303
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007304 if (kvm_x86_ops) {
7305 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007306 r = -EEXIST;
7307 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007308 }
7309
7310 if (!ops->cpu_has_kvm_support()) {
7311 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007312 r = -EOPNOTSUPP;
7313 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007314 }
7315 if (ops->disabled_by_bios()) {
7316 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007317 r = -EOPNOTSUPP;
7318 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007319 }
7320
Marc Orrb666a4b2018-11-06 14:53:56 -08007321 /*
7322 * KVM explicitly assumes that the guest has an FPU and
7323 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7324 * vCPU's FPU state as a fxregs_state struct.
7325 */
7326 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7327 printk(KERN_ERR "kvm: inadequate fpu\n");
7328 r = -EOPNOTSUPP;
7329 goto out;
7330 }
7331
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007332 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007333 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007334 __alignof__(struct fpu), SLAB_ACCOUNT,
7335 NULL);
7336 if (!x86_fpu_cache) {
7337 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7338 goto out;
7339 }
7340
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007341 x86_emulator_cache = kvm_alloc_emulator_cache();
7342 if (!x86_emulator_cache) {
7343 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7344 goto out_free_x86_fpu_cache;
7345 }
7346
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007347 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7348 if (!shared_msrs) {
7349 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007350 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007351 }
7352
Avi Kivity97db56c2008-01-13 13:23:56 +02007353 r = kvm_mmu_module_init();
7354 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007355 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007356
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007357 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01007358
Sheng Yang7b523452008-04-25 21:13:50 +08007359 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007360 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007361 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007362 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007363
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007364 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7365
Sean Christophersoncfc48182020-03-02 15:56:23 -08007366 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007367 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007368 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7369 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007370
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007371 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007372 if (pi_inject_timer == -1)
7373 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007374#ifdef CONFIG_X86_64
7375 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007376
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007377 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007378 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007379#endif
7380
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007381 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007382
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007383out_free_percpu:
7384 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007385out_free_x86_emulator_cache:
7386 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007387out_free_x86_fpu_cache:
7388 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007389out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007390 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007391}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007392
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007393void kvm_arch_exit(void)
7394{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007395#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007396 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007397 clear_hv_tscchange_cb();
7398#endif
David Matlackcef84c32016-12-16 14:30:36 -08007399 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007400 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7401
Jan Kiszka888d2562009-04-17 19:24:58 +02007402 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7403 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7404 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007405 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007406#ifdef CONFIG_X86_64
7407 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7408#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007409 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007410 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007411 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007412 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007413}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007414
Joel Schopp5cb56052015-03-02 13:43:31 -06007415int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007416{
7417 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007418 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007419 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007420 return 1;
7421 } else {
7422 vcpu->run->exit_reason = KVM_EXIT_HLT;
7423 return 0;
7424 }
7425}
Joel Schopp5cb56052015-03-02 13:43:31 -06007426EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7427
7428int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7429{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007430 int ret = kvm_skip_emulated_instruction(vcpu);
7431 /*
7432 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7433 * KVM_EXIT_DEBUG here.
7434 */
7435 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007436}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007437EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7438
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007439#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007440static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7441 unsigned long clock_type)
7442{
7443 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007444 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007445 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007446 int ret;
7447
7448 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7449 return -KVM_EOPNOTSUPP;
7450
7451 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7452 return -KVM_EOPNOTSUPP;
7453
7454 clock_pairing.sec = ts.tv_sec;
7455 clock_pairing.nsec = ts.tv_nsec;
7456 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7457 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007458 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007459
7460 ret = 0;
7461 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7462 sizeof(struct kvm_clock_pairing)))
7463 ret = -KVM_EFAULT;
7464
7465 return ret;
7466}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007467#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007468
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307469/*
7470 * kvm_pv_kick_cpu_op: Kick a vcpu.
7471 *
7472 * @apicid - apicid of vcpu to be kicked.
7473 */
7474static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7475{
Raghavendra K T24d21662013-08-26 14:18:35 +05307476 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307477
Peter Xu150a84f2019-12-04 20:07:21 +01007478 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007479 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007480 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307481 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007482 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307483
Raghavendra K T24d21662013-08-26 14:18:35 +05307484 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007485 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307486}
7487
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007488bool kvm_apicv_activated(struct kvm *kvm)
7489{
7490 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7491}
7492EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7493
7494void kvm_apicv_init(struct kvm *kvm, bool enable)
7495{
7496 if (enable)
7497 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7498 &kvm->arch.apicv_inhibit_reasons);
7499 else
7500 set_bit(APICV_INHIBIT_REASON_DISABLE,
7501 &kvm->arch.apicv_inhibit_reasons);
7502}
7503EXPORT_SYMBOL_GPL(kvm_apicv_init);
7504
Wanpeng Li71506292019-06-11 20:23:49 +08007505static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7506{
7507 struct kvm_vcpu *target = NULL;
7508 struct kvm_apic_map *map;
7509
7510 rcu_read_lock();
7511 map = rcu_dereference(kvm->arch.apic_map);
7512
7513 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7514 target = map->phys_map[dest_id]->vcpu;
7515
7516 rcu_read_unlock();
7517
Wanpeng Li266e85a2019-07-24 17:43:13 +08007518 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007519 kvm_vcpu_yield_to(target);
7520}
7521
Hollis Blanchard8776e512007-10-31 17:24:24 -05007522int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7523{
7524 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007525 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007526
Radim Krčmář696ca772018-05-24 17:50:56 +02007527 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7528 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007529
Sean Christophersonde3cd112019-04-30 10:36:17 -07007530 nr = kvm_rax_read(vcpu);
7531 a0 = kvm_rbx_read(vcpu);
7532 a1 = kvm_rcx_read(vcpu);
7533 a2 = kvm_rdx_read(vcpu);
7534 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007535
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007536 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007537
Nadav Amita449c7a2014-06-18 17:19:24 +03007538 op_64_bit = is_64_bit_mode(vcpu);
7539 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007540 nr &= 0xFFFFFFFF;
7541 a0 &= 0xFFFFFFFF;
7542 a1 &= 0xFFFFFFFF;
7543 a2 &= 0xFFFFFFFF;
7544 a3 &= 0xFFFFFFFF;
7545 }
7546
Jan Kiszka07708c42009-08-03 18:43:28 +02007547 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
7548 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007549 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007550 }
7551
Hollis Blanchard8776e512007-10-31 17:24:24 -05007552 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007553 case KVM_HC_VAPIC_POLL_IRQ:
7554 ret = 0;
7555 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307556 case KVM_HC_KICK_CPU:
7557 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007558 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307559 ret = 0;
7560 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007561#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007562 case KVM_HC_CLOCK_PAIRING:
7563 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7564 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007565#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007566 case KVM_HC_SEND_IPI:
7567 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7568 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007569 case KVM_HC_SCHED_YIELD:
7570 kvm_sched_yield(vcpu->kvm, a0);
7571 ret = 0;
7572 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007573 default:
7574 ret = -KVM_ENOSYS;
7575 break;
7576 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007577out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007578 if (!op_64_bit)
7579 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007580 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007581
Amit Shahf11c3a82008-02-21 01:00:30 +05307582 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007583 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007584}
7585EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7586
Jan Kiszkab6785de2012-09-20 07:43:17 +02007587static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007588{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007589 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007590 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007591 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007592
Hollis Blanchard8776e512007-10-31 17:24:24 -05007593 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007594
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007595 return emulator_write_emulated(ctxt, rip, instruction, 3,
7596 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007597}
7598
Avi Kivity851ba692009-08-24 11:10:17 +03007599static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007600{
Matt Gingell782d4222015-11-16 15:26:00 -08007601 return vcpu->run->request_interrupt_window &&
7602 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007603}
7604
Avi Kivity851ba692009-08-24 11:10:17 +03007605static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007606{
Avi Kivity851ba692009-08-24 11:10:17 +03007607 struct kvm_run *kvm_run = vcpu->run;
7608
Jan Kiszka91586a32009-10-05 13:07:21 +02007609 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007610 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007611 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007612 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007613 kvm_run->ready_for_interrupt_injection =
7614 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007615 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007616}
7617
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007618static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7619{
7620 int max_irr, tpr;
7621
7622 if (!kvm_x86_ops->update_cr8_intercept)
7623 return;
7624
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007625 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007626 return;
7627
Andrey Smetanind62caab2015-11-10 15:36:33 +03007628 if (vcpu->arch.apicv_active)
7629 return;
7630
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007631 if (!vcpu->arch.apic->vapic_addr)
7632 max_irr = kvm_lapic_find_highest_irr(vcpu);
7633 else
7634 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007635
7636 if (max_irr != -1)
7637 max_irr >>= 4;
7638
7639 tpr = kvm_lapic_get_cr8(vcpu);
7640
7641 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
7642}
7643
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007644static int inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007645{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007646 int r;
7647
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007648 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007649
Liran Alon1a680e32018-03-23 03:01:33 +03007650 if (vcpu->arch.exception.injected)
7651 kvm_x86_ops->queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007652 /*
Liran Alona042c262018-03-23 03:01:32 +03007653 * Do not inject an NMI or interrupt if there is a pending
7654 * exception. Exceptions and interrupts are recognized at
7655 * instruction boundaries, i.e. the start of an instruction.
7656 * Trap-like exceptions, e.g. #DB, have higher priority than
7657 * NMIs and interrupts, i.e. traps are recognized before an
7658 * NMI/interrupt that's pending on the same instruction.
7659 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7660 * priority, but are only generated (pended) during instruction
7661 * execution, i.e. a pending fault-like exception means the
7662 * fault occurred on the *previous* instruction and must be
7663 * serviced prior to recognizing any new events in order to
7664 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007665 */
Liran Alon1a680e32018-03-23 03:01:33 +03007666 else if (!vcpu->arch.exception.pending) {
7667 if (vcpu->arch.nmi_injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007668 kvm_x86_ops->set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007669 else if (vcpu->arch.interrupt.injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007670 kvm_x86_ops->set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007671 }
7672
Liran Alon1a680e32018-03-23 03:01:33 +03007673 /*
7674 * Call check_nested_events() even if we reinjected a previous event
7675 * in order for caller to determine if it should require immediate-exit
7676 * from L2 to L1 due to pending L1 events which require exit
7677 * from L2 to L1.
7678 */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007679 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007680 r = kvm_x86_ops->check_nested_events(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007681 if (r != 0)
7682 return r;
7683 }
7684
7685 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007686 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007687 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7688 vcpu->arch.exception.has_error_code,
7689 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007690
Liran Alon1a680e32018-03-23 03:01:33 +03007691 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007692 vcpu->arch.exception.pending = false;
7693 vcpu->arch.exception.injected = true;
7694
Nadav Amitd6e8c852014-07-24 14:51:24 +03007695 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7696 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7697 X86_EFLAGS_RF);
7698
Jim Mattsonf10c7292018-10-16 14:29:23 -07007699 if (vcpu->arch.exception.nr == DB_VECTOR) {
7700 /*
7701 * This code assumes that nSVM doesn't use
7702 * check_nested_events(). If it does, the
7703 * DR6/DR7 changes should happen before L1
7704 * gets a #VMEXIT for an intercepted #DB in
7705 * L2. (Under VMX, on the other hand, the
7706 * DR6/DR7 changes should not happen in the
7707 * event of a VM-exit to L1 for an intercepted
7708 * #DB in L2.)
7709 */
7710 kvm_deliver_exception_payload(vcpu);
7711 if (vcpu->arch.dr7 & DR7_GD) {
7712 vcpu->arch.dr7 &= ~DR7_GD;
7713 kvm_update_dr7(vcpu);
7714 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007715 }
7716
Wanpeng Licfcd20e2017-07-13 18:30:39 -07007717 kvm_x86_ops->queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007718 }
7719
7720 /* Don't consider new event if we re-injected an event */
7721 if (kvm_event_needs_reinjection(vcpu))
7722 return 0;
7723
7724 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
7725 kvm_x86_ops->smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007726 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007727 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007728 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007729 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007730 --vcpu->arch.nmi_pending;
7731 vcpu->arch.nmi_injected = true;
7732 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007733 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007734 /*
7735 * Because interrupts can be injected asynchronously, we are
7736 * calling check_nested_events again here to avoid a race condition.
7737 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7738 * proposal and current concerns. Perhaps we should be setting
7739 * KVM_REQ_EVENT only on certain events and not unconditionally?
7740 */
7741 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007742 r = kvm_x86_ops->check_nested_events(vcpu);
Bandan Das9242b5b2014-07-08 00:30:23 -04007743 if (r != 0)
7744 return r;
7745 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007746 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007747 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7748 false);
7749 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007750 }
7751 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007752
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007753 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007754}
7755
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007756static void process_nmi(struct kvm_vcpu *vcpu)
7757{
7758 unsigned limit = 2;
7759
7760 /*
7761 * x86 is limited to one NMI running, and one NMI pending after it.
7762 * If an NMI is already in progress, limit further NMIs to just one.
7763 * Otherwise, allow two (and we'll inject the first one immediately).
7764 */
7765 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7766 limit = 1;
7767
7768 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7769 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7770 kvm_make_request(KVM_REQ_EVENT, vcpu);
7771}
7772
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007773static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007774{
7775 u32 flags = 0;
7776 flags |= seg->g << 23;
7777 flags |= seg->db << 22;
7778 flags |= seg->l << 21;
7779 flags |= seg->avl << 20;
7780 flags |= seg->present << 15;
7781 flags |= seg->dpl << 13;
7782 flags |= seg->s << 12;
7783 flags |= seg->type << 8;
7784 return flags;
7785}
7786
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007787static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007788{
7789 struct kvm_segment seg;
7790 int offset;
7791
7792 kvm_get_segment(vcpu, &seg, n);
7793 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7794
7795 if (n < 3)
7796 offset = 0x7f84 + n * 12;
7797 else
7798 offset = 0x7f2c + (n - 3) * 12;
7799
7800 put_smstate(u32, buf, offset + 8, seg.base);
7801 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007802 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007803}
7804
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007805#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007806static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007807{
7808 struct kvm_segment seg;
7809 int offset;
7810 u16 flags;
7811
7812 kvm_get_segment(vcpu, &seg, n);
7813 offset = 0x7e00 + n * 16;
7814
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007815 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007816 put_smstate(u16, buf, offset, seg.selector);
7817 put_smstate(u16, buf, offset + 2, flags);
7818 put_smstate(u32, buf, offset + 4, seg.limit);
7819 put_smstate(u64, buf, offset + 8, seg.base);
7820}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007821#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007822
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007823static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007824{
7825 struct desc_ptr dt;
7826 struct kvm_segment seg;
7827 unsigned long val;
7828 int i;
7829
7830 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7831 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7832 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7833 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7834
7835 for (i = 0; i < 8; i++)
7836 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7837
7838 kvm_get_dr(vcpu, 6, &val);
7839 put_smstate(u32, buf, 0x7fcc, (u32)val);
7840 kvm_get_dr(vcpu, 7, &val);
7841 put_smstate(u32, buf, 0x7fc8, (u32)val);
7842
7843 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7844 put_smstate(u32, buf, 0x7fc4, seg.selector);
7845 put_smstate(u32, buf, 0x7f64, seg.base);
7846 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007847 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007848
7849 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7850 put_smstate(u32, buf, 0x7fc0, seg.selector);
7851 put_smstate(u32, buf, 0x7f80, seg.base);
7852 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007853 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007854
7855 kvm_x86_ops->get_gdt(vcpu, &dt);
7856 put_smstate(u32, buf, 0x7f74, dt.address);
7857 put_smstate(u32, buf, 0x7f70, dt.size);
7858
7859 kvm_x86_ops->get_idt(vcpu, &dt);
7860 put_smstate(u32, buf, 0x7f58, dt.address);
7861 put_smstate(u32, buf, 0x7f54, dt.size);
7862
7863 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007864 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007865
7866 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7867
7868 /* revision id */
7869 put_smstate(u32, buf, 0x7efc, 0x00020000);
7870 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7871}
7872
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007873#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007874static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007875{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007876 struct desc_ptr dt;
7877 struct kvm_segment seg;
7878 unsigned long val;
7879 int i;
7880
7881 for (i = 0; i < 16; i++)
7882 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7883
7884 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7885 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7886
7887 kvm_get_dr(vcpu, 6, &val);
7888 put_smstate(u64, buf, 0x7f68, val);
7889 kvm_get_dr(vcpu, 7, &val);
7890 put_smstate(u64, buf, 0x7f60, val);
7891
7892 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7893 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7894 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7895
7896 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7897
7898 /* revision id */
7899 put_smstate(u32, buf, 0x7efc, 0x00020064);
7900
7901 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7902
7903 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7904 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007905 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007906 put_smstate(u32, buf, 0x7e94, seg.limit);
7907 put_smstate(u64, buf, 0x7e98, seg.base);
7908
7909 kvm_x86_ops->get_idt(vcpu, &dt);
7910 put_smstate(u32, buf, 0x7e84, dt.size);
7911 put_smstate(u64, buf, 0x7e88, dt.address);
7912
7913 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7914 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007915 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007916 put_smstate(u32, buf, 0x7e74, seg.limit);
7917 put_smstate(u64, buf, 0x7e78, seg.base);
7918
7919 kvm_x86_ops->get_gdt(vcpu, &dt);
7920 put_smstate(u32, buf, 0x7e64, dt.size);
7921 put_smstate(u64, buf, 0x7e68, dt.address);
7922
7923 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007924 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007925}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007926#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007927
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007928static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007929{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007930 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007931 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007932 char buf[512];
7933 u32 cr0;
7934
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007935 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007936 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007937#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007938 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007939 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007940 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007941#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007942 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007943
Ladi Prosek0234bf82017-10-11 16:54:40 +02007944 /*
7945 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7946 * vCPU state (e.g. leave guest mode) after we've saved the state into
7947 * the SMM state-save area.
7948 */
7949 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7950
7951 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007952 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007953
7954 if (kvm_x86_ops->get_nmi_mask(vcpu))
7955 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7956 else
7957 kvm_x86_ops->set_nmi_mask(vcpu, true);
7958
7959 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7960 kvm_rip_write(vcpu, 0x8000);
7961
7962 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7963 kvm_x86_ops->set_cr0(vcpu, cr0);
7964 vcpu->arch.cr0 = cr0;
7965
7966 kvm_x86_ops->set_cr4(vcpu, 0);
7967
Paolo Bonzini18c36262015-08-07 12:27:54 +02007968 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7969 dt.address = dt.size = 0;
7970 kvm_x86_ops->set_idt(vcpu, &dt);
7971
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007972 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7973
7974 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7975 cs.base = vcpu->arch.smbase;
7976
7977 ds.selector = 0;
7978 ds.base = 0;
7979
7980 cs.limit = ds.limit = 0xffffffff;
7981 cs.type = ds.type = 0x3;
7982 cs.dpl = ds.dpl = 0;
7983 cs.db = ds.db = 0;
7984 cs.s = ds.s = 1;
7985 cs.l = ds.l = 0;
7986 cs.g = ds.g = 1;
7987 cs.avl = ds.avl = 0;
7988 cs.present = ds.present = 1;
7989 cs.unusable = ds.unusable = 0;
7990 cs.padding = ds.padding = 0;
7991
7992 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7993 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7994 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7995 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7996 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
7997 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
7998
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007999#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008000 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008001 kvm_x86_ops->set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008002#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008003
8004 kvm_update_cpuid(vcpu);
8005 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008006}
8007
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008008static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008009{
8010 vcpu->arch.smi_pending = true;
8011 kvm_make_request(KVM_REQ_EVENT, vcpu);
8012}
8013
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008014void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8015 unsigned long *vcpu_bitmap)
8016{
8017 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008018
8019 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8020
Mao Wenandb5a95e2019-11-19 11:06:40 +08008021 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
8022 vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008023
8024 free_cpumask_var(cpus);
8025}
8026
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008027void kvm_make_scan_ioapic_request(struct kvm *kvm)
8028{
8029 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8030}
8031
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008032void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8033{
8034 if (!lapic_in_kernel(vcpu))
8035 return;
8036
8037 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8038 kvm_apic_update_apicv(vcpu);
8039 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
8040}
8041EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8042
8043/*
8044 * NOTE: Do not hold any lock prior to calling this.
8045 *
8046 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8047 * locked, because it calls __x86_set_memory_region() which does
8048 * synchronize_srcu(&kvm->srcu).
8049 */
8050void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8051{
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008052 unsigned long old, new, expected;
8053
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008054 if (!kvm_x86_ops->check_apicv_inhibit_reasons ||
8055 !kvm_x86_ops->check_apicv_inhibit_reasons(bit))
8056 return;
8057
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008058 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
8059 do {
8060 expected = new = old;
8061 if (activate)
8062 __clear_bit(bit, &new);
8063 else
8064 __set_bit(bit, &new);
8065 if (new == old)
8066 break;
8067 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
8068 } while (old != expected);
8069
8070 if (!!old == !!new)
8071 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008072
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008073 trace_kvm_apicv_update_request(activate, bit);
Suravee Suthikulpanit2de9d0c2019-11-14 14:15:11 -06008074 if (kvm_x86_ops->pre_update_apicv_exec_ctrl)
8075 kvm_x86_ops->pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008076 kvm_make_all_cpus_request(kvm, KVM_REQ_APICV_UPDATE);
8077}
8078EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8079
Yang Zhang3d81bc72013-04-11 19:25:13 +08008080static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008081{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008082 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008083 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008084
Andrey Smetanin63086302015-11-10 15:36:32 +03008085 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008086
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008087 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008088 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008089 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008090 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03008091 kvm_x86_ops->sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008092 if (ioapic_in_kernel(vcpu->kvm))
8093 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008094 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008095
8096 if (is_guest_mode(vcpu))
8097 vcpu->arch.load_eoi_exitmap_pending = true;
8098 else
8099 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8100}
8101
8102static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8103{
8104 u64 eoi_exit_bitmap[4];
8105
8106 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8107 return;
8108
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008109 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8110 vcpu_to_synic(vcpu)->vec_bitmap, 256);
8111 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008112}
8113
Michal Hocko93065ac2018-08-21 21:52:33 -07008114int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8115 unsigned long start, unsigned long end,
8116 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008117{
8118 unsigned long apic_address;
8119
8120 /*
8121 * The physical address of apic access page is stored in the VMCS.
8122 * Update it when it becomes invalid.
8123 */
8124 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8125 if (start <= apic_address && apic_address < end)
8126 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07008127
8128 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01008129}
8130
Tang Chen4256f432014-09-24 15:57:54 +08008131void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8132{
Tang Chenc24ae0d2014-09-24 15:57:58 +08008133 struct page *page = NULL;
8134
Paolo Bonzini35754c92015-07-29 12:05:37 +02008135 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008136 return;
8137
Tang Chen4256f432014-09-24 15:57:54 +08008138 if (!kvm_x86_ops->set_apic_access_page_addr)
8139 return;
8140
Tang Chenc24ae0d2014-09-24 15:57:58 +08008141 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02008142 if (is_error_page(page))
8143 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08008144 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
8145
8146 /*
8147 * Do not pin apic access page in memory, the MMU notifier
8148 * will call us again if it is migrated or swapped out.
8149 */
8150 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08008151}
Tang Chen4256f432014-09-24 15:57:54 +08008152
Sean Christophersond264ee02018-08-27 15:21:12 -07008153void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8154{
8155 smp_send_reschedule(vcpu->cpu);
8156}
8157EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8158
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008159/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008160 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008161 * exiting to the userspace. Otherwise, the value will be returned to the
8162 * userspace.
8163 */
Avi Kivity851ba692009-08-24 11:10:17 +03008164static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008165{
8166 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008167 bool req_int_win =
8168 dm_request_for_irq_injection(vcpu) &&
8169 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008170 enum exit_fastpath_completion exit_fastpath = EXIT_FASTPATH_NONE;
Matt Gingell62a193e2015-11-16 15:26:07 -08008171
Jan Kiszka730dca42013-04-28 10:50:52 +02008172 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008173
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008174 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008175 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
8176 if (unlikely(!kvm_x86_ops->get_vmcs12_pages(vcpu))) {
8177 r = 0;
8178 goto out;
8179 }
8180 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008181 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008182 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008183 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008184 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008185 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8186 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03008187 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8188 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008189 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8190 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008191 if (unlikely(r))
8192 goto out;
8193 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008194 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008195 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05008196 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
8197 kvm_mmu_load_pgd(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008198 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008199 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008200 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008201 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008202 r = 0;
8203 goto out;
8204 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008205 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008206 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008207 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008208 r = 0;
8209 goto out;
8210 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008211 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8212 /* Page is swapped out. Do synthetic halt */
8213 vcpu->arch.apf.halted = true;
8214 r = 1;
8215 goto out;
8216 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008217 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8218 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008219 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8220 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008221 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8222 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008223 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008224 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008225 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008226 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008227 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8228 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8229 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008230 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008231 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8232 vcpu->run->eoi.vector =
8233 vcpu->arch.pending_ioapic_eoi;
8234 r = 0;
8235 goto out;
8236 }
8237 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008238 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8239 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008240 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8241 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008242 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8243 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008244 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8245 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8246 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8247 r = 0;
8248 goto out;
8249 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008250 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8251 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8252 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8253 r = 0;
8254 goto out;
8255 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008256 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8257 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8258 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8259 r = 0;
8260 goto out;
8261 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008262
8263 /*
8264 * KVM_REQ_HV_STIMER has to be processed after
8265 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8266 * depend on the guest clock being up-to-date
8267 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008268 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8269 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008270 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8271 kvm_vcpu_update_apicv(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008272 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008273
Avi Kivityb463a6f2010-07-20 15:06:17 +03008274 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008275 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008276 kvm_apic_accept_events(vcpu);
8277 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8278 r = 1;
8279 goto out;
8280 }
8281
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008282 if (inject_pending_event(vcpu) != 0)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008283 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008284 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008285 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008286 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008287 * SMIs have three cases:
8288 * 1) They can be nested, and then there is nothing to
8289 * do here because RSM will cause a vmexit anyway.
8290 * 2) There is an ISA-specific reason why SMI cannot be
8291 * injected, and the moment when this changes can be
8292 * intercepted.
8293 * 3) Or the SMI can be pending because
8294 * inject_pending_event has completed the injection
8295 * of an IRQ or NMI from the previous vmexit, and
8296 * then we request an immediate exit to inject the
8297 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008298 */
8299 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008300 if (!kvm_x86_ops->enable_smi_window(vcpu))
8301 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008302 if (vcpu->arch.nmi_pending)
8303 kvm_x86_ops->enable_nmi_window(vcpu);
8304 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
8305 kvm_x86_ops->enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008306 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008307 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008308
Avi Kivity3842d132010-07-27 12:30:24 +03008309 if (kvm_lapic_enabled(vcpu)) {
8310 update_cr8_intercept(vcpu);
8311 kvm_lapic_sync_to_vapic(vcpu);
8312 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008313 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008314
Avi Kivityd8368af2012-05-14 18:07:56 +03008315 r = kvm_mmu_reload(vcpu);
8316 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008317 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008318 }
8319
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008320 preempt_disable();
8321
8322 kvm_x86_ops->prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008323
8324 /*
8325 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8326 * IPI are then delayed after guest entry, which ensures that they
8327 * result in virtual interrupt delivery.
8328 */
8329 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008330 vcpu->mode = IN_GUEST_MODE;
8331
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008332 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8333
Lan Tianyu0f127d12016-03-13 11:10:29 +08008334 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008335 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008336 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008337 *
Luwei Kang81b01662019-01-31 16:52:02 +08008338 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008339 * pairs with the memory barrier implicit in pi_test_and_set_on
8340 * (see vmx_deliver_posted_interrupt).
8341 *
8342 * 3) This also orders the write to mode from any reads to the page
8343 * tables done while the VCPU is running. Please see the comment
8344 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008345 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008346 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008347
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008348 /*
8349 * This handles the case where a posted interrupt was
8350 * notified with kvm_vcpu_kick.
8351 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008352 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
8353 kvm_x86_ops->sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008354
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008355 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008356 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008357 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008358 smp_wmb();
8359 local_irq_enable();
8360 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008361 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008362 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008363 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008364 }
8365
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008366 if (req_immediate_exit) {
8367 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersond264ee02018-08-27 15:21:12 -07008368 kvm_x86_ops->request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008369 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008370
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008371 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008372 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008373
Sean Christopherson2620fe22020-01-17 11:30:51 -08008374 fpregs_assert_state_consistent();
8375 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8376 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008377
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008378 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008379 set_debugreg(0, 7);
8380 set_debugreg(vcpu->arch.eff_db[0], 0);
8381 set_debugreg(vcpu->arch.eff_db[1], 1);
8382 set_debugreg(vcpu->arch.eff_db[2], 2);
8383 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008384 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008385 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008386 }
8387
Avi Kivity851ba692009-08-24 11:10:17 +03008388 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008389
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008390 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008391 * Do this here before restoring debug registers on the host. And
8392 * since we do this before handling the vmexit, a DR access vmexit
8393 * can (a) read the correct value of the debug registers, (b) set
8394 * KVM_DEBUGREG_WONT_EXIT again.
8395 */
8396 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008397 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
8398 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008399 kvm_update_dr0123(vcpu);
8400 kvm_update_dr6(vcpu);
8401 kvm_update_dr7(vcpu);
8402 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008403 }
8404
8405 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008406 * If the guest has used debug registers, at least dr7
8407 * will be disabled while returning to the host.
8408 * If we don't have active breakpoints in the host, we don't
8409 * care about the messed up debug address registers. But if
8410 * we have some of them active, restore the old state.
8411 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008412 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008413 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008414
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008415 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008416
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008417 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008418 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008419
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008420 kvm_x86_ops->handle_exit_irqoff(vcpu, &exit_fastpath);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008421
Sean Christophersond7a08882019-07-10 09:07:34 -07008422 /*
8423 * Consume any pending interrupts, including the possible source of
8424 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8425 * An instruction is required after local_irq_enable() to fully unblock
8426 * interrupts on processors that implement an interrupt shadow, the
8427 * stat.exits increment will do nicely.
8428 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008429 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008430 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008431 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008432 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008433 kvm_after_interrupt(vcpu);
8434
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008435 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008436 if (lapic_in_kernel(vcpu)) {
8437 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8438 if (delta != S64_MIN) {
8439 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8440 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8441 }
8442 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008443
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008444 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008445 preempt_enable();
8446
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008447 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008448
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008449 /*
8450 * Profile KVM exit RIPs:
8451 */
8452 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008453 unsigned long rip = kvm_rip_read(vcpu);
8454 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008455 }
8456
Zachary Amsdencc578282012-02-03 15:43:50 -02008457 if (unlikely(vcpu->arch.tsc_always_catchup))
8458 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008459
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008460 if (vcpu->arch.apic_attention)
8461 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008462
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008463 r = kvm_x86_ops->handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008464 return r;
8465
8466cancel_injection:
8467 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008468 if (unlikely(vcpu->arch.apic_attention))
8469 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008470out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008471 return r;
8472}
8473
Paolo Bonzini362c6982015-02-06 12:48:04 +01008474static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8475{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008476 if (!kvm_arch_vcpu_runnable(vcpu) &&
8477 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008478 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8479 kvm_vcpu_block(vcpu);
8480 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008481
8482 if (kvm_x86_ops->post_block)
8483 kvm_x86_ops->post_block(vcpu);
8484
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008485 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8486 return 1;
8487 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008488
Paolo Bonzini362c6982015-02-06 12:48:04 +01008489 kvm_apic_accept_events(vcpu);
8490 switch(vcpu->arch.mp_state) {
8491 case KVM_MP_STATE_HALTED:
8492 vcpu->arch.pv.pv_unhalted = false;
8493 vcpu->arch.mp_state =
8494 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008495 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008496 case KVM_MP_STATE_RUNNABLE:
8497 vcpu->arch.apf.halted = false;
8498 break;
8499 case KVM_MP_STATE_INIT_RECEIVED:
8500 break;
8501 default:
8502 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008503 }
8504 return 1;
8505}
8506
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008507static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8508{
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008509 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008510 kvm_x86_ops->check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008511
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008512 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8513 !vcpu->arch.apf.halted);
8514}
8515
Paolo Bonzini362c6982015-02-06 12:48:04 +01008516static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008517{
8518 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008519 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008520
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008521 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008522 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008523
Paolo Bonzini362c6982015-02-06 12:48:04 +01008524 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008525 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008526 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008527 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008528 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008529 }
8530
Gleb Natapov09cec752009-03-23 15:11:44 +02008531 if (r <= 0)
8532 break;
8533
Radim Krčmář72875d82017-04-26 22:32:19 +02008534 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008535 if (kvm_cpu_has_pending_timer(vcpu))
8536 kvm_inject_pending_timer_irqs(vcpu);
8537
Matt Gingell782d4222015-11-16 15:26:00 -08008538 if (dm_request_for_irq_injection(vcpu) &&
8539 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008540 r = 0;
8541 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008542 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008543 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008544 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008545
8546 kvm_check_async_pf_completion(vcpu);
8547
Gleb Natapov09cec752009-03-23 15:11:44 +02008548 if (signal_pending(current)) {
8549 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008550 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008551 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008552 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008553 }
8554 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008555 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008556 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008557 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008558 }
8559 }
8560
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008561 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008562
8563 return r;
8564}
8565
Gleb Natapov716d51a2012-09-03 15:24:26 +03008566static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8567{
8568 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008569
Gleb Natapov716d51a2012-09-03 15:24:26 +03008570 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008571 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008572 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008573 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008574}
8575
8576static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8577{
8578 BUG_ON(!vcpu->arch.pio.count);
8579
8580 return complete_emulated_io(vcpu);
8581}
8582
Avi Kivityf78146b2012-04-18 19:22:47 +03008583/*
8584 * Implements the following, as a state machine:
8585 *
8586 * read:
8587 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008588 * for each mmio piece in the fragment
8589 * write gpa, len
8590 * exit
8591 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008592 * execute insn
8593 *
8594 * write:
8595 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008596 * for each mmio piece in the fragment
8597 * write gpa, len
8598 * copy data
8599 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008600 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008601static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008602{
8603 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008604 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008605 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008606
Gleb Natapov716d51a2012-09-03 15:24:26 +03008607 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008608
Gleb Natapov716d51a2012-09-03 15:24:26 +03008609 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008610 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8611 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008612 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008613 memcpy(frag->data, run->mmio.data, len);
8614
8615 if (frag->len <= 8) {
8616 /* Switch to the next fragment. */
8617 frag++;
8618 vcpu->mmio_cur_fragment++;
8619 } else {
8620 /* Go forward to the next mmio piece. */
8621 frag->data += len;
8622 frag->gpa += len;
8623 frag->len -= len;
8624 }
8625
Andrew Honiga08d3b32014-02-27 19:35:14 +01008626 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008627 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008628
8629 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008630 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008631 return 1;
8632 vcpu->mmio_read_completed = 1;
8633 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008634 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008635
Gleb Natapov716d51a2012-09-03 15:24:26 +03008636 run->exit_reason = KVM_EXIT_MMIO;
8637 run->mmio.phys_addr = frag->gpa;
8638 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008639 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8640 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008641 run->mmio.is_write = vcpu->mmio_is_write;
8642 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8643 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008644}
8645
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008646static void kvm_save_current_fpu(struct fpu *fpu)
8647{
8648 /*
8649 * If the target FPU state is not resident in the CPU registers, just
8650 * memcpy() from current, else save CPU state directly to the target.
8651 */
8652 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8653 memcpy(&fpu->state, &current->thread.fpu.state,
8654 fpu_kernel_xstate_size);
8655 else
8656 copy_fpregs_to_fpstate(fpu);
8657}
8658
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008659/* Swap (qemu) user FPU context for the guest FPU context. */
8660static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8661{
Rik van Riel5f409e22019-04-03 18:41:52 +02008662 fpregs_lock();
8663
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008664 kvm_save_current_fpu(vcpu->arch.user_fpu);
8665
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008666 /* PKRU is separately restored in kvm_x86_ops->run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008667 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008668 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008669
8670 fpregs_mark_activate();
8671 fpregs_unlock();
8672
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008673 trace_kvm_fpu(1);
8674}
8675
8676/* When vcpu_run ends, restore user space FPU context. */
8677static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8678{
Rik van Riel5f409e22019-04-03 18:41:52 +02008679 fpregs_lock();
8680
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008681 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8682
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008683 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008684
8685 fpregs_mark_activate();
8686 fpregs_unlock();
8687
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008688 ++vcpu->stat.fpu_reload;
8689 trace_kvm_fpu(0);
8690}
8691
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008692int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8693{
8694 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008695
Christoffer Dallaccb7572017-12-04 21:35:25 +01008696 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008697 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008698 kvm_load_guest_fpu(vcpu);
8699
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008700 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008701 if (kvm_run->immediate_exit) {
8702 r = -EINTR;
8703 goto out;
8704 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008705 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008706 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008707 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008708 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008709 if (signal_pending(current)) {
8710 r = -EINTR;
8711 vcpu->run->exit_reason = KVM_EXIT_INTR;
8712 ++vcpu->stat.signal_exits;
8713 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008714 goto out;
8715 }
8716
Ken Hofsass01643c52018-01-31 16:03:36 -08008717 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8718 r = -EINVAL;
8719 goto out;
8720 }
8721
8722 if (vcpu->run->kvm_dirty_regs) {
8723 r = sync_regs(vcpu);
8724 if (r != 0)
8725 goto out;
8726 }
8727
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008728 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008729 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008730 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8731 r = -EINVAL;
8732 goto out;
8733 }
8734 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008735
Gleb Natapov716d51a2012-09-03 15:24:26 +03008736 if (unlikely(vcpu->arch.complete_userspace_io)) {
8737 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8738 vcpu->arch.complete_userspace_io = NULL;
8739 r = cui(vcpu);
8740 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008741 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008742 } else
8743 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008744
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008745 if (kvm_run->immediate_exit)
8746 r = -EINTR;
8747 else
8748 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008749
8750out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008751 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008752 if (vcpu->run->kvm_valid_regs)
8753 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008754 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008755 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008756
Christoffer Dallaccb7572017-12-04 21:35:25 +01008757 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008758 return r;
8759}
8760
Ken Hofsass01643c52018-01-31 16:03:36 -08008761static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008762{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008763 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8764 /*
8765 * We are here if userspace calls get_regs() in the middle of
8766 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008767 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008768 * that usually, but some bad designed PV devices (vmware
8769 * backdoor interface) need this to work
8770 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008771 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008772 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8773 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008774 regs->rax = kvm_rax_read(vcpu);
8775 regs->rbx = kvm_rbx_read(vcpu);
8776 regs->rcx = kvm_rcx_read(vcpu);
8777 regs->rdx = kvm_rdx_read(vcpu);
8778 regs->rsi = kvm_rsi_read(vcpu);
8779 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008780 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008781 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008782#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008783 regs->r8 = kvm_r8_read(vcpu);
8784 regs->r9 = kvm_r9_read(vcpu);
8785 regs->r10 = kvm_r10_read(vcpu);
8786 regs->r11 = kvm_r11_read(vcpu);
8787 regs->r12 = kvm_r12_read(vcpu);
8788 regs->r13 = kvm_r13_read(vcpu);
8789 regs->r14 = kvm_r14_read(vcpu);
8790 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008791#endif
8792
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008793 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008794 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008795}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008796
Ken Hofsass01643c52018-01-31 16:03:36 -08008797int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8798{
8799 vcpu_load(vcpu);
8800 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008801 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008802 return 0;
8803}
8804
Ken Hofsass01643c52018-01-31 16:03:36 -08008805static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008806{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008807 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8808 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8809
Sean Christophersonde3cd112019-04-30 10:36:17 -07008810 kvm_rax_write(vcpu, regs->rax);
8811 kvm_rbx_write(vcpu, regs->rbx);
8812 kvm_rcx_write(vcpu, regs->rcx);
8813 kvm_rdx_write(vcpu, regs->rdx);
8814 kvm_rsi_write(vcpu, regs->rsi);
8815 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008816 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008817 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008818#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008819 kvm_r8_write(vcpu, regs->r8);
8820 kvm_r9_write(vcpu, regs->r9);
8821 kvm_r10_write(vcpu, regs->r10);
8822 kvm_r11_write(vcpu, regs->r11);
8823 kvm_r12_write(vcpu, regs->r12);
8824 kvm_r13_write(vcpu, regs->r13);
8825 kvm_r14_write(vcpu, regs->r14);
8826 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008827#endif
8828
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008829 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008830 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008831
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008832 vcpu->arch.exception.pending = false;
8833
Avi Kivity3842d132010-07-27 12:30:24 +03008834 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008835}
Avi Kivity3842d132010-07-27 12:30:24 +03008836
Ken Hofsass01643c52018-01-31 16:03:36 -08008837int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8838{
8839 vcpu_load(vcpu);
8840 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008841 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008842 return 0;
8843}
8844
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008845void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8846{
8847 struct kvm_segment cs;
8848
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008849 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008850 *db = cs.db;
8851 *l = cs.l;
8852}
8853EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8854
Ken Hofsass01643c52018-01-31 16:03:36 -08008855static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008856{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008857 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008858
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008859 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8860 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8861 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8862 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8863 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8864 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008865
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008866 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8867 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008868
8869 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008870 sregs->idt.limit = dt.size;
8871 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008872 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008873 sregs->gdt.limit = dt.size;
8874 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008875
Avi Kivity4d4ec082009-12-29 18:07:30 +02008876 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008877 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008878 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008879 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008880 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008881 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008882 sregs->apic_base = kvm_get_apic_base(vcpu);
8883
Jordan Borgner0e96f312018-10-28 12:58:28 +00008884 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008885
Liran Alon04140b42018-03-23 03:01:31 +03008886 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008887 set_bit(vcpu->arch.interrupt.nr,
8888 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008889}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008890
Ken Hofsass01643c52018-01-31 16:03:36 -08008891int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8892 struct kvm_sregs *sregs)
8893{
8894 vcpu_load(vcpu);
8895 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008896 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008897 return 0;
8898}
8899
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008900int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8901 struct kvm_mp_state *mp_state)
8902{
Christoffer Dallfd232562017-12-04 21:35:30 +01008903 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008904 if (kvm_mpx_supported())
8905 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008906
Jan Kiszka66450a22013-03-13 12:42:34 +01008907 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308908 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8909 vcpu->arch.pv.pv_unhalted)
8910 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8911 else
8912 mp_state->mp_state = vcpu->arch.mp_state;
8913
Sean Christophersonf958bd22019-12-09 12:19:31 -08008914 if (kvm_mpx_supported())
8915 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008916 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008917 return 0;
8918}
8919
8920int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8921 struct kvm_mp_state *mp_state)
8922{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008923 int ret = -EINVAL;
8924
8925 vcpu_load(vcpu);
8926
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008927 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008928 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008929 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008930
Liran Alon27cbe7d2019-11-11 11:16:40 +02008931 /*
8932 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
8933 * INIT state; latched init should be reported using
8934 * KVM_SET_VCPU_EVENTS, so reject it here.
8935 */
8936 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01008937 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8938 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008939 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008940
Jan Kiszka66450a22013-03-13 12:42:34 +01008941 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8942 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8943 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8944 } else
8945 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008946 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008947
8948 ret = 0;
8949out:
8950 vcpu_put(vcpu);
8951 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008952}
8953
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008954int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8955 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008956{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008957 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008958 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008959
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008960 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008961
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008962 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008963 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07008964 if (ret) {
8965 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8966 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
8967 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008968 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07008969 }
Gleb Natapovc6975182010-02-18 12:15:01 +02008970
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008971 kvm_rip_write(vcpu, ctxt->eip);
8972 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008973 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02008974}
8975EXPORT_SYMBOL_GPL(kvm_task_switch);
8976
Peng Hao3140c152018-04-02 09:15:32 +08008977static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05008978{
Tianyu Lan37b95952018-01-16 17:34:07 +08008979 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05008980 /*
8981 * When EFER.LME and CR0.PG are set, the processor is in
8982 * 64-bit mode (though maybe in a 32-bit code segment).
8983 * CR4.PAE and EFER.LMA must be set.
8984 */
Tianyu Lan37b95952018-01-16 17:34:07 +08008985 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05008986 || !(sregs->efer & EFER_LMA))
8987 return -EINVAL;
8988 } else {
8989 /*
8990 * Not in 64-bit mode: EFER.LMA is clear and the code
8991 * segment cannot be 64-bit.
8992 */
8993 if (sregs->efer & EFER_LMA || sregs->cs.l)
8994 return -EINVAL;
8995 }
8996
Wanpeng Li3ca94192019-09-18 17:50:10 +08008997 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05008998}
8999
Ken Hofsass01643c52018-01-31 16:03:36 -08009000static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009001{
Jan Kiszka58cb6282014-01-24 16:48:44 +01009002 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009003 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05009004 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009005 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02009006 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009007 int ret = -EINVAL;
9008
Lan Tianyuf2981032017-12-14 03:01:52 -05009009 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009010 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009011
Jim Mattsond3802282017-08-10 10:14:13 -07009012 apic_base_msr.data = sregs->apic_base;
9013 apic_base_msr.host_initiated = true;
9014 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009015 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009016
Gleb Natapov89a27f42010-02-16 10:51:48 +02009017 dt.size = sregs->idt.limit;
9018 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009019 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009020 dt.size = sregs->gdt.limit;
9021 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009022 kvm_x86_ops->set_gdt(vcpu, &dt);
9023
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009024 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009025 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009026 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009027 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009028
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009029 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009030
Avi Kivityf6801df2010-01-21 15:31:50 +02009031 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009032 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009033
Avi Kivity4d4ec082009-12-29 18:07:30 +02009034 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009035 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009036 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009037
Avi Kivityfc78f512009-12-07 12:16:48 +02009038 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009039 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9040 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009041 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009042 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009043 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009044
9045 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009046 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009047 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009048 mmu_reset_needed = 1;
9049 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009050 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009051
9052 if (mmu_reset_needed)
9053 kvm_mmu_reset_context(vcpu);
9054
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009055 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009056 pending_vec = find_first_bit(
9057 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9058 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009059 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009060 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009061 }
9062
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009063 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9064 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9065 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9066 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9067 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9068 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009069
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009070 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9071 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009072
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009073 update_cr8_intercept(vcpu);
9074
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009075 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009076 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009077 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009078 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009079 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9080
Avi Kivity3842d132010-07-27 12:30:24 +03009081 kvm_make_request(KVM_REQ_EVENT, vcpu);
9082
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009083 ret = 0;
9084out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009085 return ret;
9086}
9087
9088int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9089 struct kvm_sregs *sregs)
9090{
9091 int ret;
9092
9093 vcpu_load(vcpu);
9094 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009095 vcpu_put(vcpu);
9096 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009097}
9098
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009099int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9100 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009101{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009102 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009103 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009104
Christoffer Dall66b56562017-12-04 21:35:33 +01009105 vcpu_load(vcpu);
9106
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009107 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9108 r = -EBUSY;
9109 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009110 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009111 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9112 kvm_queue_exception(vcpu, DB_VECTOR);
9113 else
9114 kvm_queue_exception(vcpu, BP_VECTOR);
9115 }
9116
Jan Kiszka91586a32009-10-05 13:07:21 +02009117 /*
9118 * Read rflags as long as potentially injected trace flags are still
9119 * filtered out.
9120 */
9121 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009122
9123 vcpu->guest_debug = dbg->control;
9124 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9125 vcpu->guest_debug = 0;
9126
9127 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009128 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9129 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009130 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009131 } else {
9132 for (i = 0; i < KVM_NR_DB_REGS; i++)
9133 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009134 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009135 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009136
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009137 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9138 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9139 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009140
Jan Kiszka91586a32009-10-05 13:07:21 +02009141 /*
9142 * Trigger an rflags update that will inject or remove the trace
9143 * flags.
9144 */
9145 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009146
Paolo Bonzinia96036b2015-11-10 11:55:36 +01009147 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009148
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009149 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009150
Avi Kivity2122ff52010-05-13 11:25:04 +03009151out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009152 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009153 return r;
9154}
9155
9156/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009157 * Translate a guest virtual address to a guest physical address.
9158 */
9159int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9160 struct kvm_translation *tr)
9161{
9162 unsigned long vaddr = tr->linear_address;
9163 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009164 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009165
Christoffer Dall1da5b612017-12-04 21:35:32 +01009166 vcpu_load(vcpu);
9167
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009168 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009169 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009170 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009171 tr->physical_address = gpa;
9172 tr->valid = gpa != UNMAPPED_GVA;
9173 tr->writeable = 1;
9174 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009175
Christoffer Dall1da5b612017-12-04 21:35:32 +01009176 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009177 return 0;
9178}
9179
Hollis Blanchardd0752062007-10-31 17:24:25 -05009180int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9181{
Christoffer Dall13931232017-12-04 21:35:34 +01009182 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009183
Christoffer Dall13931232017-12-04 21:35:34 +01009184 vcpu_load(vcpu);
9185
Marc Orrb666a4b2018-11-06 14:53:56 -08009186 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009187 memcpy(fpu->fpr, fxsave->st_space, 128);
9188 fpu->fcw = fxsave->cwd;
9189 fpu->fsw = fxsave->swd;
9190 fpu->ftwx = fxsave->twd;
9191 fpu->last_opcode = fxsave->fop;
9192 fpu->last_ip = fxsave->rip;
9193 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009194 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009195
Christoffer Dall13931232017-12-04 21:35:34 +01009196 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009197 return 0;
9198}
9199
9200int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9201{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009202 struct fxregs_state *fxsave;
9203
9204 vcpu_load(vcpu);
9205
Marc Orrb666a4b2018-11-06 14:53:56 -08009206 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009207
Hollis Blanchardd0752062007-10-31 17:24:25 -05009208 memcpy(fxsave->st_space, fpu->fpr, 128);
9209 fxsave->cwd = fpu->fcw;
9210 fxsave->swd = fpu->fsw;
9211 fxsave->twd = fpu->ftwx;
9212 fxsave->fop = fpu->last_opcode;
9213 fxsave->rip = fpu->last_ip;
9214 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009215 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009216
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009217 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009218 return 0;
9219}
9220
Ken Hofsass01643c52018-01-31 16:03:36 -08009221static void store_regs(struct kvm_vcpu *vcpu)
9222{
9223 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9224
9225 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9226 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9227
9228 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9229 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9230
9231 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9232 kvm_vcpu_ioctl_x86_get_vcpu_events(
9233 vcpu, &vcpu->run->s.regs.events);
9234}
9235
9236static int sync_regs(struct kvm_vcpu *vcpu)
9237{
9238 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9239 return -EINVAL;
9240
9241 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9242 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9243 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9244 }
9245 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9246 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9247 return -EINVAL;
9248 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9249 }
9250 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9251 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9252 vcpu, &vcpu->run->s.regs.events))
9253 return -EINVAL;
9254 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9255 }
9256
9257 return 0;
9258}
9259
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009260static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009261{
Marc Orrb666a4b2018-11-06 14:53:56 -08009262 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009263 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009264 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009265 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009266
Dexuan Cui2acf9232010-06-10 11:27:12 +08009267 /*
9268 * Ensure guest xcr0 is valid for loading
9269 */
Dave Hansend91cab72015-09-02 16:31:26 -07009270 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009271
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009272 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009273}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009274
Sean Christopherson897cc382019-12-18 13:55:09 -08009275int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009276{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009277 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009278 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9279 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009280
Sean Christopherson897cc382019-12-18 13:55:09 -08009281 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009282}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009283
Sean Christophersone529ef62019-12-18 13:55:15 -08009284int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009285{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009286 struct page *page;
9287 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009288
Sean Christopherson95a0d012019-12-18 13:55:23 -08009289 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9290 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9291 else
9292 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9293
9294 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9295
9296 r = kvm_mmu_create(vcpu);
9297 if (r < 0)
9298 return r;
9299
9300 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009301 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9302 if (r < 0)
9303 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009304 if (kvm_apicv_activated(vcpu->kvm))
9305 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009306 } else
9307 static_key_slow_inc(&kvm_no_apic_vcpu);
9308
9309 r = -ENOMEM;
9310
9311 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9312 if (!page)
9313 goto fail_free_lapic;
9314 vcpu->arch.pio_data = page_address(page);
9315
9316 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9317 GFP_KERNEL_ACCOUNT);
9318 if (!vcpu->arch.mce_banks)
9319 goto fail_free_pio_data;
9320 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9321
9322 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9323 GFP_KERNEL_ACCOUNT))
9324 goto fail_free_mce_banks;
9325
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009326 if (!alloc_emulate_ctxt(vcpu))
9327 goto free_wbinvd_dirty_mask;
9328
Sean Christopherson95a0d012019-12-18 13:55:23 -08009329 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9330 GFP_KERNEL_ACCOUNT);
9331 if (!vcpu->arch.user_fpu) {
9332 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009333 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009334 }
9335
9336 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9337 GFP_KERNEL_ACCOUNT);
9338 if (!vcpu->arch.guest_fpu) {
9339 pr_err("kvm: failed to allocate vcpu's fpu\n");
9340 goto free_user_fpu;
9341 }
9342 fx_init(vcpu);
9343
9344 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
9345
9346 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9347
9348 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9349
9350 kvm_async_pf_hash_reset(vcpu);
9351 kvm_pmu_init(vcpu);
9352
9353 vcpu->arch.pending_external_vector = -1;
9354 vcpu->arch.preempted_in_kernel = false;
9355
9356 kvm_hv_vcpu_init(vcpu);
9357
9358 r = kvm_x86_ops->vcpu_create(vcpu);
9359 if (r)
9360 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009361
Sean Christopherson0cf91352019-03-07 15:43:02 -08009362 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009363 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009364 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009365 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009366 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009367 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009368 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009369 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009370
9371free_guest_fpu:
9372 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9373free_user_fpu:
9374 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009375free_emulate_ctxt:
9376 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009377free_wbinvd_dirty_mask:
9378 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9379fail_free_mce_banks:
9380 kfree(vcpu->arch.mce_banks);
9381fail_free_pio_data:
9382 free_page((unsigned long)vcpu->arch.pio_data);
9383fail_free_lapic:
9384 kvm_free_lapic(vcpu);
9385fail_mmu_destroy:
9386 kvm_mmu_destroy(vcpu);
9387 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009388}
9389
Dominik Dingel31928aa2014-12-04 15:47:07 +01009390void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009391{
Will Auld8fe8ab42012-11-29 12:42:12 -08009392 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009393 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009394
Roman Kagand3457c82017-07-14 17:13:20 +03009395 kvm_hv_vcpu_postcreate(vcpu);
9396
Christoffer Dallec7660c2017-12-04 21:35:23 +01009397 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009398 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009399 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009400 msr.data = 0x0;
9401 msr.index = MSR_IA32_TSC;
9402 msr.host_initiated = true;
9403 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009404 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009405
9406 /* poll control enabled by default */
9407 vcpu->arch.msr_kvm_poll_control = 1;
9408
Christoffer Dallec7660c2017-12-04 21:35:23 +01009409 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009410
Wanpeng Lib34de572020-02-28 11:18:41 +08009411 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9412 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9413 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009414}
9415
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009416void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009417{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009418 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009419 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009420
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009421 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9422
Sean Christopherson50b143e2019-12-18 13:55:07 -08009423 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009424
9425 kvm_x86_ops->vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009426
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009427 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009428 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9429 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9430 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009431
9432 kvm_hv_vcpu_uninit(vcpu);
9433 kvm_pmu_destroy(vcpu);
9434 kfree(vcpu->arch.mce_banks);
9435 kvm_free_lapic(vcpu);
9436 idx = srcu_read_lock(&vcpu->kvm->srcu);
9437 kvm_mmu_destroy(vcpu);
9438 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9439 free_page((unsigned long)vcpu->arch.pio_data);
9440 if (!lapic_in_kernel(vcpu))
9441 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009442}
9443
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009444void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009445{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009446 kvm_lapic_reset(vcpu, init_event);
9447
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009448 vcpu->arch.hflags = 0;
9449
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009450 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009451 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009452 atomic_set(&vcpu->arch.nmi_queued, 0);
9453 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009454 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009455 kvm_clear_interrupt_queue(vcpu);
9456 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009457
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009458 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009459 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009460 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01009461 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009462 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009463 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009464
Nadav Amit11190222015-04-02 03:10:38 +03009465 vcpu->arch.cr2 = 0;
9466
Avi Kivity3842d132010-07-27 12:30:24 +03009467 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009468 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009469 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009470
Glauber Costa12f9a482011-02-01 14:16:40 -05009471 kvmclock_reset(vcpu);
9472
Gleb Natapovaf585b92010-10-14 11:22:46 +02009473 kvm_clear_async_pf_completion_queue(vcpu);
9474 kvm_async_pf_hash_reset(vcpu);
9475 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009476
Wanpeng Lia554d202017-10-11 05:10:19 -07009477 if (kvm_mpx_supported()) {
9478 void *mpx_state_buffer;
9479
9480 /*
9481 * To avoid have the INIT path from kvm_apic_has_events() that be
9482 * called with loaded FPU and does not let userspace fix the state.
9483 */
Rik van Rielf775b132017-11-14 16:54:23 -05009484 if (init_event)
9485 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009486 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009487 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009488 if (mpx_state_buffer)
9489 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009490 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009491 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009492 if (mpx_state_buffer)
9493 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009494 if (init_event)
9495 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009496 }
9497
Paolo Bonzini64d60672015-05-07 11:36:11 +02009498 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009499 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009500 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009501
Kyle Hueydb2336a2017-03-20 01:16:28 -07009502 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009503
9504 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009505 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009506
Julian Stecklina66f7b722012-12-05 15:26:19 +01009507 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9508 vcpu->arch.regs_avail = ~0;
9509 vcpu->arch.regs_dirty = ~0;
9510
Wanpeng Lia554d202017-10-11 05:10:19 -07009511 vcpu->arch.ia32_xss = 0;
9512
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009513 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009514}
9515
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009516void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009517{
9518 struct kvm_segment cs;
9519
9520 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9521 cs.selector = vector << 8;
9522 cs.base = vector << 12;
9523 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9524 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009525}
9526
Radim Krčmář13a34e02014-08-28 15:13:03 +02009527int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009528{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009529 struct kvm *kvm;
9530 struct kvm_vcpu *vcpu;
9531 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009532 int ret;
9533 u64 local_tsc;
9534 u64 max_tsc = 0;
9535 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009536
9537 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009538 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009539 if (ret != 0)
9540 return ret;
9541
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009542 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009543 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009544 list_for_each_entry(kvm, &vm_list, vm_list) {
9545 kvm_for_each_vcpu(i, vcpu, kvm) {
9546 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009547 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009548 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9549 backwards_tsc = true;
9550 if (vcpu->arch.last_host_tsc > max_tsc)
9551 max_tsc = vcpu->arch.last_host_tsc;
9552 }
9553 }
9554 }
9555
9556 /*
9557 * Sometimes, even reliable TSCs go backwards. This happens on
9558 * platforms that reset TSC during suspend or hibernate actions, but
9559 * maintain synchronization. We must compensate. Fortunately, we can
9560 * detect that condition here, which happens early in CPU bringup,
9561 * before any KVM threads can be running. Unfortunately, we can't
9562 * bring the TSCs fully up to date with real time, as we aren't yet far
9563 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009564 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009565 * variables that haven't been updated yet.
9566 *
9567 * So we simply find the maximum observed TSC above, then record the
9568 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9569 * the adjustment will be applied. Note that we accumulate
9570 * adjustments, in case multiple suspend cycles happen before some VCPU
9571 * gets a chance to run again. In the event that no KVM threads get a
9572 * chance to run, we will miss the entire elapsed period, as we'll have
9573 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9574 * loose cycle time. This isn't too big a deal, since the loss will be
9575 * uniform across all VCPUs (not to mention the scenario is extremely
9576 * unlikely). It is possible that a second hibernate recovery happens
9577 * much faster than a first, causing the observed TSC here to be
9578 * smaller; this would require additional padding adjustment, which is
9579 * why we set last_host_tsc to the local tsc observed here.
9580 *
9581 * N.B. - this code below runs only on platforms with reliable TSC,
9582 * as that is the only way backwards_tsc is set above. Also note
9583 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9584 * have the same delta_cyc adjustment applied if backwards_tsc
9585 * is detected. Note further, this adjustment is only done once,
9586 * as we reset last_host_tsc on all VCPUs to stop this from being
9587 * called multiple times (one for each physical CPU bringup).
9588 *
Guo Chao4a969982012-06-28 15:17:27 +08009589 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009590 * will be compensated by the logic in vcpu_load, which sets the TSC to
9591 * catchup mode. This will catchup all VCPUs to real time, but cannot
9592 * guarantee that they stay in perfect synchronization.
9593 */
9594 if (backwards_tsc) {
9595 u64 delta_cyc = max_tsc - local_tsc;
9596 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009597 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009598 kvm_for_each_vcpu(i, vcpu, kvm) {
9599 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9600 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009601 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009602 }
9603
9604 /*
9605 * We have to disable TSC offset matching.. if you were
9606 * booting a VM while issuing an S4 host suspend....
9607 * you may have some problem. Solving this issue is
9608 * left as an exercise to the reader.
9609 */
9610 kvm->arch.last_tsc_nsec = 0;
9611 kvm->arch.last_tsc_write = 0;
9612 }
9613
9614 }
9615 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009616}
9617
Radim Krčmář13a34e02014-08-28 15:13:03 +02009618void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009619{
Radim Krčmář13a34e02014-08-28 15:13:03 +02009620 kvm_x86_ops->hardware_disable();
9621 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009622}
9623
9624int kvm_arch_hardware_setup(void)
9625{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009626 int r;
9627
Sean Christopherson91661982020-03-02 15:57:06 -08009628 rdmsrl_safe(MSR_EFER, &host_efer);
9629
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009630 if (boot_cpu_has(X86_FEATURE_XSAVES))
9631 rdmsrl(MSR_IA32_XSS, host_xss);
9632
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009633 r = kvm_x86_ops->hardware_setup();
9634 if (r != 0)
9635 return r;
9636
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009637 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
9638 supported_xss = 0;
9639
Sean Christophersonb11306b2019-12-10 14:44:13 -08009640 cr4_reserved_bits = kvm_host_cr4_reserved_bits(&boot_cpu_data);
9641
Haozhong Zhang35181e82015-10-20 15:39:03 +08009642 if (kvm_has_tsc_control) {
9643 /*
9644 * Make sure the user can only configure tsc_khz values that
9645 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009646 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009647 * be 1 on all machines.
9648 */
9649 u64 max = min(0x7fffffffULL,
9650 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9651 kvm_max_guest_tsc_khz = max;
9652
Haozhong Zhangad7218832015-10-20 15:39:02 +08009653 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009654 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009655
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009656 kvm_init_msr_list();
9657 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009658}
9659
9660void kvm_arch_hardware_unsetup(void)
9661{
9662 kvm_x86_ops->hardware_unsetup();
9663}
9664
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009665int kvm_arch_check_processor_compat(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009666{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009667 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
9668
9669 WARN_ON(!irqs_disabled());
9670
9671 if (kvm_host_cr4_reserved_bits(c) != cr4_reserved_bits)
9672 return -EIO;
9673
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009674 return kvm_x86_ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009675}
9676
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009677bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9678{
9679 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9680}
9681EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9682
9683bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9684{
9685 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9686}
9687
Gleb Natapov54e98182012-08-05 15:58:32 +03009688struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009689EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009690
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009691void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9692{
Like Xub35e5542019-10-27 18:52:43 +08009693 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9694
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009695 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009696 if (pmu->version && unlikely(pmu->event_count)) {
9697 pmu->need_cleanup = true;
9698 kvm_make_request(KVM_REQ_PMU, vcpu);
9699 }
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009700 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009701}
9702
Sean Christopherson562b6b02020-01-26 16:41:13 -08009703void kvm_arch_free_vm(struct kvm *kvm)
9704{
9705 kfree(kvm->arch.hyperv.hv_pa_pg);
9706 vfree(kvm);
9707}
9708
9709
Carsten Ottee08b9632012-01-04 10:25:20 +01009710int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009711{
Carsten Ottee08b9632012-01-04 10:25:20 +01009712 if (type)
9713 return -EINVAL;
9714
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009715 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009716 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009717 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009718 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009719 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009720 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009721
Sheng Yang5550af42008-10-15 20:15:06 +08009722 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9723 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009724 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9725 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9726 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009727
Jan Kiszka038f8c12011-02-04 10:49:11 +01009728 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009729 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009730 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9731
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009732 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009733 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009734
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009735 kvm->arch.guest_can_read_msr_platform_info = true;
9736
Andrew Jones7e44e442014-02-28 12:52:54 +01009737 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009738 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009739
Roman Kagancbc02362018-02-01 16:48:31 +03009740 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009741 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009742 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009743
Sean Christopherson92735b12019-08-02 15:06:17 -07009744 return kvm_x86_ops->vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009745}
9746
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009747int kvm_arch_post_init_vm(struct kvm *kvm)
9748{
9749 return kvm_mmu_post_init_vm(kvm);
9750}
9751
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009752static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9753{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009754 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009755 kvm_mmu_unload(vcpu);
9756 vcpu_put(vcpu);
9757}
9758
9759static void kvm_free_vcpus(struct kvm *kvm)
9760{
9761 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009762 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009763
9764 /*
9765 * Unpin any mmu pages first.
9766 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009767 kvm_for_each_vcpu(i, vcpu, kvm) {
9768 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009769 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009770 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009771 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009772 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009773
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009774 mutex_lock(&kvm->lock);
9775 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9776 kvm->vcpus[i] = NULL;
9777
9778 atomic_set(&kvm->online_vcpus, 0);
9779 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009780}
9781
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009782void kvm_arch_sync_events(struct kvm *kvm)
9783{
Andrew Jones332967a2014-02-28 12:52:55 +01009784 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009785 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009786 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009787}
9788
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009789int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009790{
9791 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009792 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009793 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009794 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009795
9796 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009797 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9798 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009799
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009800 slot = id_to_memslot(slots, id);
9801 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009802 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009803 return -EEXIST;
9804
9805 /*
9806 * MAP_SHARED to prevent internal slot pages from being moved
9807 * by fork()/COW.
9808 */
9809 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9810 MAP_SHARED | MAP_ANONYMOUS, 0);
9811 if (IS_ERR((void *)hva))
9812 return PTR_ERR((void *)hva);
9813 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009814 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009815 return 0;
9816
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009817 /*
9818 * Stuff a non-canonical value to catch use-after-delete. This
9819 * ends up being 0 on 32-bit KVM, but there's no better
9820 * alternative.
9821 */
9822 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009823 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009824 }
9825
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009826 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009827 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009828
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009829 m.slot = id | (i << 16);
9830 m.flags = 0;
9831 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009832 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009833 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009834 r = __kvm_set_memory_region(kvm, &m);
9835 if (r < 0)
9836 return r;
9837 }
9838
Eric Biggers103c7632018-01-31 17:30:21 -08009839 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009840 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009841
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009842 return 0;
9843}
9844EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9845
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009846void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9847{
9848 kvm_mmu_pre_destroy_vm(kvm);
9849}
9850
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009851void kvm_arch_destroy_vm(struct kvm *kvm)
9852{
Andrew Honig27469d22013-04-18 09:38:14 -07009853 if (current->mm == kvm->mm) {
9854 /*
9855 * Free memory regions allocated on behalf of userspace,
9856 * unless the the memory map has changed due to process exit
9857 * or fd copying.
9858 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009859 mutex_lock(&kvm->slots_lock);
9860 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9861 0, 0);
9862 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
9863 0, 0);
9864 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
9865 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -07009866 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05009867 if (kvm_x86_ops->vm_destroy)
9868 kvm_x86_ops->vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009869 kvm_pic_destroy(kvm);
9870 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009871 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009872 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009873 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009874 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009875 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009876 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009877}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009878
Sean Christophersone96c81e2020-02-18 13:07:27 -08009879void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009880{
9881 int i;
9882
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009883 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -08009884 kvfree(slot->arch.rmap[i]);
9885 slot->arch.rmap[i] = NULL;
9886
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009887 if (i == 0)
9888 continue;
9889
Sean Christophersone96c81e2020-02-18 13:07:27 -08009890 kvfree(slot->arch.lpage_info[i - 1]);
9891 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009892 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009893
Sean Christophersone96c81e2020-02-18 13:07:27 -08009894 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009895}
9896
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009897static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
9898 unsigned long npages)
9899{
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009900 int i;
9901
Sean Christophersonedd4fa32020-02-18 13:07:15 -08009902 /*
9903 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
9904 * old arrays will be freed by __kvm_set_memory_region() if installing
9905 * the new memslot is successful.
9906 */
9907 memset(&slot->arch, 0, sizeof(slot->arch));
9908
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009909 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009910 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009911 unsigned long ugfn;
9912 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009913 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009914
9915 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9916 slot->base_gfn, level) + 1;
9917
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009918 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009919 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009920 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009921 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009922 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009923 if (i == 0)
9924 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009925
Ben Gardon254272c2019-02-11 11:02:50 -08009926 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009927 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009928 goto out_free;
9929
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009930 slot->arch.lpage_info[i - 1] = linfo;
9931
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009932 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009933 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009934 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009935 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009936 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9937 /*
9938 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -08009939 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009940 */
Sean Christopherson600087b2020-03-02 15:57:05 -08009941 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009942 unsigned long j;
9943
9944 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009945 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009946 }
9947 }
9948
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009949 if (kvm_page_track_create_memslot(slot, npages))
9950 goto out_free;
9951
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009952 return 0;
9953
9954out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009955 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009956 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009957 slot->arch.rmap[i] = NULL;
9958 if (i == 0)
9959 continue;
9960
Thomas Huth548ef282015-02-24 21:29:25 +01009961 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009962 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009963 }
9964 return -ENOMEM;
9965}
9966
Sean Christopherson15248252019-02-05 12:54:17 -08009967void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009968{
Boris Ostrovsky91724812019-12-05 01:30:51 +00009969 struct kvm_vcpu *vcpu;
9970 int i;
9971
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009972 /*
9973 * memslots->generation has been incremented.
9974 * mmio generation may have reached its maximum value.
9975 */
Sean Christopherson15248252019-02-05 12:54:17 -08009976 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +00009977
9978 /* Force re-initialization of steal_time cache */
9979 kvm_for_each_vcpu(i, vcpu, kvm)
9980 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009981}
9982
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009983int kvm_arch_prepare_memory_region(struct kvm *kvm,
9984 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009985 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09009986 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08009987{
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009988 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
9989 return kvm_alloc_memslot_metadata(memslot,
9990 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009991 return 0;
9992}
9993
Kai Huang88178fd2015-01-28 10:54:27 +08009994static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
9995 struct kvm_memory_slot *new)
9996{
9997 /* Still write protect RO slot */
9998 if (new->flags & KVM_MEM_READONLY) {
Jay Zhou3c9bd402020-02-27 09:32:27 +08009999 kvm_mmu_slot_remove_write_access(kvm, new, PT_PAGE_TABLE_LEVEL);
Kai Huang88178fd2015-01-28 10:54:27 +080010000 return;
10001 }
10002
10003 /*
10004 * Call kvm_x86_ops dirty logging hooks when they are valid.
10005 *
10006 * kvm_x86_ops->slot_disable_log_dirty is called when:
10007 *
10008 * - KVM_MR_CREATE with dirty logging is disabled
10009 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
10010 *
10011 * The reason is, in case of PML, we need to set D-bit for any slots
10012 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +080010013 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +080010014 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +080010015 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +080010016 * logging disabled for memory slots.
10017 *
10018 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
10019 * to dirty logging mode.
10020 *
10021 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
10022 *
10023 * In case of write protect:
10024 *
10025 * Write protect all pages for dirty logging.
10026 *
10027 * All the sptes including the large sptes which point to this
10028 * slot are set to readonly. We can not create any new large
10029 * spte on this slot until the end of the logging.
10030 *
10031 * See the comments in fast_page_fault().
10032 */
10033 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Jay Zhou3c9bd402020-02-27 09:32:27 +080010034 if (kvm_x86_ops->slot_enable_log_dirty) {
Kai Huang88178fd2015-01-28 10:54:27 +080010035 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010036 } else {
10037 int level =
10038 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
10039 PT_DIRECTORY_LEVEL : PT_PAGE_TABLE_LEVEL;
10040
10041 /*
10042 * If we're with initial-all-set, we don't need
10043 * to write protect any small page because
10044 * they're reported as dirty already. However
10045 * we still need to write-protect huge pages
10046 * so that the page split can happen lazily on
10047 * the first write to the huge page.
10048 */
10049 kvm_mmu_slot_remove_write_access(kvm, new, level);
10050 }
Kai Huang88178fd2015-01-28 10:54:27 +080010051 } else {
10052 if (kvm_x86_ops->slot_disable_log_dirty)
10053 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
10054 }
10055}
10056
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010057void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010058 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010059 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010060 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010061 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010062{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010063 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010064 kvm_mmu_change_mmu_pages(kvm,
10065 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010066
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010067 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010068 * Dirty logging tracks sptes in 4k granularity, meaning that large
10069 * sptes have to be split. If live migration is successful, the guest
10070 * in the source machine will be destroyed and large sptes will be
10071 * created in the destination. However, if the guest continues to run
10072 * in the source machine (for example if live migration fails), small
10073 * sptes will remain around and cause bad performance.
10074 *
10075 * Scan sptes if dirty logging has been stopped, dropping those
10076 * which can be collapsed into a single large-page spte. Later
10077 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -070010078 *
10079 * There is no need to do this in any of the following cases:
10080 * CREATE: No dirty mappings will already exist.
10081 * MOVE/DELETE: The old mappings will already have been cleaned up by
10082 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010083 */
Sean Christopherson319109a2019-09-11 12:19:52 -070010084 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010085 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10086 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10087 kvm_mmu_zap_collapsible_sptes(kvm, new);
10088
10089 /*
Kai Huang88178fd2015-01-28 10:54:27 +080010090 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +080010091 *
Kai Huang88178fd2015-01-28 10:54:27 +080010092 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
10093 * been zapped so no dirty logging staff is needed for old slot. For
10094 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
10095 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010096 *
10097 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010098 */
Kai Huang88178fd2015-01-28 10:54:27 +080010099 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010100 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Sean Christopherson21198842020-02-18 13:07:25 -080010101
10102 /* Free the arrays associated with the old memslot. */
10103 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010104 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010105}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010106
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010107void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010108{
Sean Christopherson7390de12019-02-05 13:01:31 -080010109 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010110}
10111
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010112void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10113 struct kvm_memory_slot *slot)
10114{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010115 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010116}
10117
Liran Alone6c67d82018-09-04 10:56:52 +030010118static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10119{
10120 return (is_guest_mode(vcpu) &&
10121 kvm_x86_ops->guest_apic_has_interrupt &&
10122 kvm_x86_ops->guest_apic_has_interrupt(vcpu));
10123}
10124
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010125static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10126{
10127 if (!list_empty_careful(&vcpu->async_pf.done))
10128 return true;
10129
10130 if (kvm_apic_has_events(vcpu))
10131 return true;
10132
10133 if (vcpu->arch.pv.pv_unhalted)
10134 return true;
10135
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010136 if (vcpu->arch.exception.pending)
10137 return true;
10138
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010139 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10140 (vcpu->arch.nmi_pending &&
10141 kvm_x86_ops->nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010142 return true;
10143
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010144 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
10145 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010146 return true;
10147
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010148 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010149 (kvm_cpu_has_interrupt(vcpu) ||
10150 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010151 return true;
10152
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010153 if (kvm_hv_has_stimer_pending(vcpu))
10154 return true;
10155
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010156 return false;
10157}
10158
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010159int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10160{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010161 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010162}
Zhang Xiantao57361992007-12-17 14:21:40 +080010163
Wanpeng Li17e433b2019-08-05 10:03:19 +080010164bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10165{
10166 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10167 return true;
10168
10169 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10170 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10171 kvm_test_request(KVM_REQ_EVENT, vcpu))
10172 return true;
10173
10174 if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu))
10175 return true;
10176
10177 return false;
10178}
10179
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010180bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10181{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010182 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010183}
10184
Christoffer Dallb6d33832012-03-08 16:44:24 -050010185int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010186{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010187 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010188}
Gleb Natapov78646122009-03-23 12:12:11 +020010189
10190int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10191{
10192 return kvm_x86_ops->interrupt_allowed(vcpu);
10193}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010194
Nadav Amit82b32772014-11-02 11:54:45 +020010195unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10196{
10197 if (is_64_bit_mode(vcpu))
10198 return kvm_rip_read(vcpu);
10199 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10200 kvm_rip_read(vcpu));
10201}
10202EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10203
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010204bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10205{
Nadav Amit82b32772014-11-02 11:54:45 +020010206 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010207}
10208EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10209
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010210unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10211{
10212 unsigned long rflags;
10213
10214 rflags = kvm_x86_ops->get_rflags(vcpu);
10215 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010216 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010217 return rflags;
10218}
10219EXPORT_SYMBOL_GPL(kvm_get_rflags);
10220
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010221static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010222{
10223 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010224 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010225 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010226 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010227}
10228
10229void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10230{
10231 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010232 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010233}
10234EXPORT_SYMBOL_GPL(kvm_set_rflags);
10235
Gleb Natapov56028d02010-10-17 18:13:42 +020010236void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10237{
10238 int r;
10239
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010240 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010241 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010242 return;
10243
10244 r = kvm_mmu_reload(vcpu);
10245 if (unlikely(r))
10246 return;
10247
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010248 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010249 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010250 return;
10251
Sean Christopherson7a026742020-02-06 14:14:34 -080010252 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010253}
10254
Gleb Natapovaf585b92010-10-14 11:22:46 +020010255static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10256{
10257 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10258}
10259
10260static inline u32 kvm_async_pf_next_probe(u32 key)
10261{
10262 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
10263}
10264
10265static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10266{
10267 u32 key = kvm_async_pf_hash_fn(gfn);
10268
10269 while (vcpu->arch.apf.gfns[key] != ~0)
10270 key = kvm_async_pf_next_probe(key);
10271
10272 vcpu->arch.apf.gfns[key] = gfn;
10273}
10274
10275static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10276{
10277 int i;
10278 u32 key = kvm_async_pf_hash_fn(gfn);
10279
10280 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010281 (vcpu->arch.apf.gfns[key] != gfn &&
10282 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010283 key = kvm_async_pf_next_probe(key);
10284
10285 return key;
10286}
10287
10288bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10289{
10290 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10291}
10292
10293static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10294{
10295 u32 i, j, k;
10296
10297 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
10298 while (true) {
10299 vcpu->arch.apf.gfns[i] = ~0;
10300 do {
10301 j = kvm_async_pf_next_probe(j);
10302 if (vcpu->arch.apf.gfns[j] == ~0)
10303 return;
10304 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10305 /*
10306 * k lies cyclically in ]i,j]
10307 * | i.k.j |
10308 * |....j i.k.| or |.k..j i...|
10309 */
10310 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10311 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10312 i = j;
10313 }
10314}
10315
Gleb Natapov7c907052010-10-14 11:22:53 +020010316static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10317{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010318
10319 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10320 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010321}
10322
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010323static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10324{
10325
10326 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10327 sizeof(u32));
10328}
10329
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010330static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10331{
10332 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10333 return false;
10334
10335 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10336 (vcpu->arch.apf.send_user_only &&
10337 kvm_x86_ops->get_cpl(vcpu) == 0))
10338 return false;
10339
10340 return true;
10341}
10342
10343bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10344{
10345 if (unlikely(!lapic_in_kernel(vcpu) ||
10346 kvm_event_needs_reinjection(vcpu) ||
10347 vcpu->arch.exception.pending))
10348 return false;
10349
10350 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10351 return false;
10352
10353 /*
10354 * If interrupts are off we cannot even use an artificial
10355 * halt state.
10356 */
10357 return kvm_x86_ops->interrupt_allowed(vcpu);
10358}
10359
Gleb Natapovaf585b92010-10-14 11:22:46 +020010360void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10361 struct kvm_async_pf *work)
10362{
Avi Kivity6389ee92010-11-29 16:12:30 +020010363 struct x86_exception fault;
10364
Sean Christopherson736c2912019-12-06 15:57:14 -080010365 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010366 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010367
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010368 if (kvm_can_deliver_async_pf(vcpu) &&
10369 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010370 fault.vector = PF_VECTOR;
10371 fault.error_code_valid = true;
10372 fault.error_code = 0;
10373 fault.nested_page_fault = false;
10374 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010375 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010376 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010377 } else {
10378 /*
10379 * It is not possible to deliver a paravirtualized asynchronous
10380 * page fault, but putting the guest in an artificial halt state
10381 * can be beneficial nevertheless: if an interrupt arrives, we
10382 * can deliver it timely and perhaps the guest will schedule
10383 * another process. When the instruction that triggered a page
10384 * fault is retried, hopefully the page will be ready in the host.
10385 */
10386 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010387 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010388}
10389
10390void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10391 struct kvm_async_pf *work)
10392{
Avi Kivity6389ee92010-11-29 16:12:30 +020010393 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010394 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010395
chai wenf2e10662013-10-14 22:22:33 +080010396 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010397 work->arch.token = ~0; /* broadcast wakeup */
10398 else
10399 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010400 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010401
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010402 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10403 !apf_get_user(vcpu, &val)) {
10404 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10405 vcpu->arch.exception.pending &&
10406 vcpu->arch.exception.nr == PF_VECTOR &&
10407 !apf_put_user(vcpu, 0)) {
10408 vcpu->arch.exception.injected = false;
10409 vcpu->arch.exception.pending = false;
10410 vcpu->arch.exception.nr = 0;
10411 vcpu->arch.exception.has_error_code = false;
10412 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010413 vcpu->arch.exception.has_payload = false;
10414 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010415 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10416 fault.vector = PF_VECTOR;
10417 fault.error_code_valid = true;
10418 fault.error_code = 0;
10419 fault.nested_page_fault = false;
10420 fault.address = work->arch.token;
10421 fault.async_page_fault = true;
10422 kvm_inject_page_fault(vcpu, &fault);
10423 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010424 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010425 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010426 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010427}
10428
10429bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10430{
10431 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10432 return true;
10433 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010434 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010435}
10436
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010437void kvm_arch_start_assignment(struct kvm *kvm)
10438{
10439 atomic_inc(&kvm->arch.assigned_device_count);
10440}
10441EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10442
10443void kvm_arch_end_assignment(struct kvm *kvm)
10444{
10445 atomic_dec(&kvm->arch.assigned_device_count);
10446}
10447EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10448
10449bool kvm_arch_has_assigned_device(struct kvm *kvm)
10450{
10451 return atomic_read(&kvm->arch.assigned_device_count);
10452}
10453EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10454
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010455void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10456{
10457 atomic_inc(&kvm->arch.noncoherent_dma_count);
10458}
10459EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10460
10461void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10462{
10463 atomic_dec(&kvm->arch.noncoherent_dma_count);
10464}
10465EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10466
10467bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10468{
10469 return atomic_read(&kvm->arch.noncoherent_dma_count);
10470}
10471EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10472
Alex Williamson14717e22016-05-05 11:58:35 -060010473bool kvm_arch_has_irq_bypass(void)
10474{
Sean Christopherson92735b12019-08-02 15:06:17 -070010475 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010476}
10477
Feng Wu87276882015-09-18 22:29:40 +080010478int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10479 struct irq_bypass_producer *prod)
10480{
10481 struct kvm_kernel_irqfd *irqfd =
10482 container_of(cons, struct kvm_kernel_irqfd, consumer);
10483
Alex Williamson14717e22016-05-05 11:58:35 -060010484 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010485
Alex Williamson14717e22016-05-05 11:58:35 -060010486 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
10487 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010488}
10489
10490void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10491 struct irq_bypass_producer *prod)
10492{
10493 int ret;
10494 struct kvm_kernel_irqfd *irqfd =
10495 container_of(cons, struct kvm_kernel_irqfd, consumer);
10496
Feng Wu87276882015-09-18 22:29:40 +080010497 WARN_ON(irqfd->producer != prod);
10498 irqfd->producer = NULL;
10499
10500 /*
10501 * When producer of consumer is unregistered, we change back to
10502 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010503 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010504 * int this case doesn't want to receive the interrupts.
10505 */
10506 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
10507 if (ret)
10508 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10509 " fails: %d\n", irqfd->consumer.token, ret);
10510}
10511
10512int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10513 uint32_t guest_irq, bool set)
10514{
Feng Wu87276882015-09-18 22:29:40 +080010515 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
10516}
10517
Feng Wu520040142016-01-25 16:53:33 +080010518bool kvm_vector_hashing_enabled(void)
10519{
10520 return vector_hashing;
10521}
Feng Wu520040142016-01-25 16:53:33 +080010522
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010523bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10524{
10525 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10526}
10527EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10528
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010529u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10530{
10531 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10532
10533 /* The STIBP bit doesn't fault even if it's not advertised */
10534 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10535 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10536 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10537 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10538 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10539 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10540
10541 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10542 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10543 bits &= ~SPEC_CTRL_SSBD;
10544 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10545 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10546 bits &= ~SPEC_CTRL_SSBD;
10547
10548 return bits;
10549}
10550EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010551
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010552EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010553EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010554EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10555EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10556EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10557EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010558EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010559EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010560EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010561EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010562EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010563EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010564EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010565EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010566EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010567EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010568EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010569EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010570EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10571EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050010572EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010573EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);