blob: d2f1b4746903e8bdc230a9ac0f08e332334a466d [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;
193
Hollis Blanchard417bc302007-10-31 17:24:23 -0500194struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200195 { "pf_fixed", VCPU_STAT(pf_fixed) },
196 { "pf_guest", VCPU_STAT(pf_guest) },
197 { "tlb_flush", VCPU_STAT(tlb_flush) },
198 { "invlpg", VCPU_STAT(invlpg) },
199 { "exits", VCPU_STAT(exits) },
200 { "io_exits", VCPU_STAT(io_exits) },
201 { "mmio_exits", VCPU_STAT(mmio_exits) },
202 { "signal_exits", VCPU_STAT(signal_exits) },
203 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800204 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200205 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100206 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200207 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +0200208 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200209 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530210 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200211 { "request_irq", VCPU_STAT(request_irq_exits) },
212 { "irq_exits", VCPU_STAT(irq_exits) },
213 { "host_state_reload", VCPU_STAT(host_state_reload) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200214 { "fpu_reload", VCPU_STAT(fpu_reload) },
215 { "insn_emulation", VCPU_STAT(insn_emulation) },
216 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300217 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200218 { "nmi_injections", VCPU_STAT(nmi_injections) },
Paolo Bonzini0f1e2612016-12-17 16:05:19 +0100219 { "req_event", VCPU_STAT(req_event) },
Paolo Bonzinic595cee2018-07-02 13:07:14 +0200220 { "l1d_flush", VCPU_STAT(l1d_flush) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200221 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
222 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
223 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
224 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
225 { "mmu_flooded", VM_STAT(mmu_flooded) },
226 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200227 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300228 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200229 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Paolo Bonzini833b45d2019-09-30 18:48:44 +0200230 { "largepages", VM_STAT(lpages, .mode = 0444) },
Paolo Bonzinib8e8c832019-11-04 12:22:02 +0100231 { "nx_largepages_splitted", VM_STAT(nx_lpage_splits, .mode = 0444) },
David Matlackf3414bc2016-12-20 15:25:57 -0800232 { "max_mmu_page_hash_collisions",
233 VM_STAT(max_mmu_page_hash_collisions) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500234 { NULL }
235};
236
Dexuan Cui2acf9232010-06-10 11:27:12 +0800237u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800238u64 __read_mostly supported_xcr0;
239EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800240
Marc Orrb666a4b2018-11-06 14:53:56 -0800241struct kmem_cache *x86_fpu_cache;
242EXPORT_SYMBOL_GPL(x86_fpu_cache);
243
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800244static struct kmem_cache *x86_emulator_cache;
245
246static struct kmem_cache *kvm_alloc_emulator_cache(void)
247{
Sean Christopherson06add252020-02-18 15:29:50 -0800248 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
249 unsigned int size = sizeof(struct x86_emulate_ctxt);
250
251 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800252 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800253 SLAB_ACCOUNT, useroffset,
254 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800255}
256
Jan Kiszkab6785de2012-09-20 07:43:17 +0200257static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300258
Gleb Natapovaf585b92010-10-14 11:22:46 +0200259static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
260{
261 int i;
262 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
263 vcpu->arch.apf.gfns[i] = ~0;
264}
265
Avi Kivity18863bd2009-09-07 11:12:18 +0300266static void kvm_on_user_return(struct user_return_notifier *urn)
267{
268 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300269 struct kvm_shared_msrs *locals
270 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800271 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700272 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300273
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700274 /*
275 * Disabling irqs at this point since the following code could be
276 * interrupted and executed through kvm_arch_hardware_disable()
277 */
278 local_irq_save(flags);
279 if (locals->registered) {
280 locals->registered = false;
281 user_return_notifier_unregister(urn);
282 }
283 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300284 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800285 values = &locals->values[slot];
286 if (values->host != values->curr) {
287 wrmsrl(shared_msrs_global.msrs[slot], values->host);
288 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300289 }
290 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300291}
292
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800293void kvm_define_shared_msr(unsigned slot, u32 msr)
294{
Nadav Amit0123be42014-07-24 15:06:56 +0300295 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200296 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300297 if (slot >= shared_msrs_global.nr)
298 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300299}
300EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
301
302static void kvm_shared_msr_cpu_online(void)
303{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800304 unsigned int cpu = smp_processor_id();
305 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
306 u64 value;
307 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300308
Sean Christopherson05c19c22019-11-22 12:04:50 -0800309 for (i = 0; i < shared_msrs_global.nr; ++i) {
310 rdmsrl_safe(shared_msrs_global.msrs[i], &value);
311 smsr->values[i].host = value;
312 smsr->values[i].curr = value;
313 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300314}
315
Andy Honig8b3c3102014-08-27 11:16:44 -0700316int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300317{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200318 unsigned int cpu = smp_processor_id();
319 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700320 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300321
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500322 value = (value & mask) | (smsr->values[slot].host & ~mask);
323 if (value == smsr->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700324 return 0;
Andy Honig8b3c3102014-08-27 11:16:44 -0700325 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
326 if (err)
327 return 1;
328
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500329 smsr->values[slot].curr = value;
Avi Kivity18863bd2009-09-07 11:12:18 +0300330 if (!smsr->registered) {
331 smsr->urn.on_user_return = kvm_on_user_return;
332 user_return_notifier_register(&smsr->urn);
333 smsr->registered = true;
334 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700335 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300336}
337EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
338
Radim Krčmář13a34e02014-08-28 15:13:03 +0200339static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200340{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200341 unsigned int cpu = smp_processor_id();
342 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200343
344 if (smsr->registered)
345 kvm_on_user_return(&smsr->urn);
346}
347
Carsten Otte6866b832007-10-29 16:09:10 +0100348u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
349{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300350 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100351}
352EXPORT_SYMBOL_GPL(kvm_get_apic_base);
353
Jim Mattson58871642018-05-09 16:56:04 -0400354enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
355{
356 return kvm_apic_mode(kvm_get_apic_base(vcpu));
357}
358EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
359
Jan Kiszka58cb6282014-01-24 16:48:44 +0100360int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100361{
Jim Mattson58871642018-05-09 16:56:04 -0400362 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
363 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200364 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
365 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100366
Jim Mattson58871642018-05-09 16:56:04 -0400367 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700368 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400369 if (!msr_info->host_initiated) {
370 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
371 return 1;
372 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
373 return 1;
374 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100375
376 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800377 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100378 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100379}
380EXPORT_SYMBOL_GPL(kvm_set_apic_base);
381
Andi Kleen2605fc22014-05-02 00:44:37 +0200382asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000383{
384 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200385 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000386}
387EXPORT_SYMBOL_GPL(kvm_spurious_fault);
388
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200389#define EXCPT_BENIGN 0
390#define EXCPT_CONTRIBUTORY 1
391#define EXCPT_PF 2
392
393static int exception_class(int vector)
394{
395 switch (vector) {
396 case PF_VECTOR:
397 return EXCPT_PF;
398 case DE_VECTOR:
399 case TS_VECTOR:
400 case NP_VECTOR:
401 case SS_VECTOR:
402 case GP_VECTOR:
403 return EXCPT_CONTRIBUTORY;
404 default:
405 break;
406 }
407 return EXCPT_BENIGN;
408}
409
Nadav Amitd6e8c852014-07-24 14:51:24 +0300410#define EXCPT_FAULT 0
411#define EXCPT_TRAP 1
412#define EXCPT_ABORT 2
413#define EXCPT_INTERRUPT 3
414
415static int exception_type(int vector)
416{
417 unsigned int mask;
418
419 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
420 return EXCPT_INTERRUPT;
421
422 mask = 1 << vector;
423
424 /* #DB is trap, as instruction watchpoints are handled elsewhere */
425 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
426 return EXCPT_TRAP;
427
428 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
429 return EXCPT_ABORT;
430
431 /* Reserved exceptions will result in fault */
432 return EXCPT_FAULT;
433}
434
Jim Mattsonda998b42018-10-16 14:29:22 -0700435void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
436{
437 unsigned nr = vcpu->arch.exception.nr;
438 bool has_payload = vcpu->arch.exception.has_payload;
439 unsigned long payload = vcpu->arch.exception.payload;
440
441 if (!has_payload)
442 return;
443
444 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700445 case DB_VECTOR:
446 /*
447 * "Certain debug exceptions may clear bit 0-3. The
448 * remaining contents of the DR6 register are never
449 * cleared by the processor".
450 */
451 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
452 /*
453 * DR6.RTM is set by all #DB exceptions that don't clear it.
454 */
455 vcpu->arch.dr6 |= DR6_RTM;
456 vcpu->arch.dr6 |= payload;
457 /*
458 * Bit 16 should be set in the payload whenever the #DB
459 * exception should clear DR6.RTM. This makes the payload
460 * compatible with the pending debug exceptions under VMX.
461 * Though not currently documented in the SDM, this also
462 * makes the payload compatible with the exit qualification
463 * for #DB exceptions under VMX.
464 */
465 vcpu->arch.dr6 ^= payload & DR6_RTM;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800466
467 /*
468 * The #DB payload is defined as compatible with the 'pending
469 * debug exceptions' field under VMX, not DR6. While bit 12 is
470 * defined in the 'pending debug exceptions' field (enabled
471 * breakpoint), it is reserved and must be zero in DR6.
472 */
473 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700474 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700475 case PF_VECTOR:
476 vcpu->arch.cr2 = payload;
477 break;
478 }
479
480 vcpu->arch.exception.has_payload = false;
481 vcpu->arch.exception.payload = 0;
482}
483EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
484
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200485static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200486 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700487 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200488{
489 u32 prev_nr;
490 int class1, class2;
491
Avi Kivity3842d132010-07-27 12:30:24 +0300492 kvm_make_request(KVM_REQ_EVENT, vcpu);
493
Wanpeng Li664f8e22017-08-24 03:35:09 -0700494 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200495 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200496 if (has_error && !is_protmode(vcpu))
497 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700498 if (reinject) {
499 /*
500 * On vmentry, vcpu->arch.exception.pending is only
501 * true if an event injection was blocked by
502 * nested_run_pending. In that case, however,
503 * vcpu_enter_guest requests an immediate exit,
504 * and the guest shouldn't proceed far enough to
505 * need reinjection.
506 */
507 WARN_ON_ONCE(vcpu->arch.exception.pending);
508 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700509 if (WARN_ON_ONCE(has_payload)) {
510 /*
511 * A reinjected event has already
512 * delivered its payload.
513 */
514 has_payload = false;
515 payload = 0;
516 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700517 } else {
518 vcpu->arch.exception.pending = true;
519 vcpu->arch.exception.injected = false;
520 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200521 vcpu->arch.exception.has_error_code = has_error;
522 vcpu->arch.exception.nr = nr;
523 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700524 vcpu->arch.exception.has_payload = has_payload;
525 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800526 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700527 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200528 return;
529 }
530
531 /* to check exception */
532 prev_nr = vcpu->arch.exception.nr;
533 if (prev_nr == DF_VECTOR) {
534 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300535 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200536 return;
537 }
538 class1 = exception_class(prev_nr);
539 class2 = exception_class(nr);
540 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
541 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700542 /*
543 * Generate double fault per SDM Table 5-5. Set
544 * exception.pending = true so that the double fault
545 * can trigger a nested vmexit.
546 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200547 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700548 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200549 vcpu->arch.exception.has_error_code = true;
550 vcpu->arch.exception.nr = DF_VECTOR;
551 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700552 vcpu->arch.exception.has_payload = false;
553 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200554 } else
555 /* replace previous exception with a new one in a hope
556 that instruction re-execution will regenerate lost
557 exception */
558 goto queue;
559}
560
Avi Kivity298101d2007-11-25 13:41:11 +0200561void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
562{
Jim Mattson91e86d22018-10-16 14:29:21 -0700563 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200564}
565EXPORT_SYMBOL_GPL(kvm_queue_exception);
566
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200567void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
568{
Jim Mattson91e86d22018-10-16 14:29:21 -0700569 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200570}
571EXPORT_SYMBOL_GPL(kvm_requeue_exception);
572
Jim Mattsonf10c7292018-10-16 14:29:23 -0700573static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
574 unsigned long payload)
575{
576 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
577}
578
Jim Mattsonda998b42018-10-16 14:29:22 -0700579static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
580 u32 error_code, unsigned long payload)
581{
582 kvm_multiple_exception(vcpu, nr, true, error_code,
583 true, payload, false);
584}
585
Kyle Huey6affcbe2016-11-29 12:40:40 -0800586int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200587{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100588 if (err)
589 kvm_inject_gp(vcpu, 0);
590 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800591 return kvm_skip_emulated_instruction(vcpu);
592
593 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100594}
595EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200596
Avi Kivity6389ee92010-11-29 16:12:30 +0200597void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200598{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200599 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700600 vcpu->arch.exception.nested_apf =
601 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700602 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700603 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700604 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
605 } else {
606 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
607 fault->address);
608 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200609}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300610EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200611
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200612static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200613{
Avi Kivity6389ee92010-11-29 16:12:30 +0200614 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
615 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200616 else
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +0200617 vcpu->arch.mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200618
619 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200620}
621
Sheng Yang3419ffc2008-05-15 09:52:48 +0800622void kvm_inject_nmi(struct kvm_vcpu *vcpu)
623{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300624 atomic_inc(&vcpu->arch.nmi_queued);
625 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800626}
627EXPORT_SYMBOL_GPL(kvm_inject_nmi);
628
Avi Kivity298101d2007-11-25 13:41:11 +0200629void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
630{
Jim Mattson91e86d22018-10-16 14:29:21 -0700631 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200632}
633EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
634
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200635void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
636{
Jim Mattson91e86d22018-10-16 14:29:21 -0700637 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200638}
639EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
640
Carsten Ottea03490e2007-10-29 16:09:35 +0100641/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300642 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
643 * a #GP and return false.
644 */
645bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200646{
Avi Kivity0a79b002009-09-01 12:03:25 +0300647 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
648 return true;
649 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
650 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100651}
Avi Kivity0a79b002009-09-01 12:03:25 +0300652EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100653
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300654bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
655{
656 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
657 return true;
658
659 kvm_queue_exception(vcpu, UD_VECTOR);
660 return false;
661}
662EXPORT_SYMBOL_GPL(kvm_require_dr);
663
Carsten Ottea03490e2007-10-29 16:09:35 +0100664/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200665 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200666 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200667 * can read from guest physical or from the guest's guest physical memory.
668 */
669int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
670 gfn_t ngfn, void *data, int offset, int len,
671 u32 access)
672{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200673 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200674 gfn_t real_gfn;
675 gpa_t ngpa;
676
677 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200678 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200679 if (real_gfn == UNMAPPED_GVA)
680 return -EFAULT;
681
682 real_gfn = gpa_to_gfn(real_gfn);
683
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200684 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200685}
686EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
687
Fengguang Wu69b00492015-01-19 22:33:39 +0800688static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200689 void *data, int offset, int len, u32 access)
690{
691 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
692 data, offset, len, access);
693}
694
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700695static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
696{
697 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
698 rsvd_bits(1, 2);
699}
700
Joerg Roedelec92fe42010-09-10 17:30:51 +0200701/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700702 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100703 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200704int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100705{
706 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
707 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
708 int i;
709 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200710 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100711
Joerg Roedelff03a072010-09-10 17:30:57 +0200712 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
713 offset * sizeof(u64), sizeof(pdpte),
714 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100715 if (ret < 0) {
716 ret = 0;
717 goto out;
718 }
719 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400720 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700721 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100722 ret = 0;
723 goto out;
724 }
725 }
726 ret = 1;
727
Joerg Roedelff03a072010-09-10 17:30:57 +0200728 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700729 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
730
Carsten Ottea03490e2007-10-29 16:09:35 +0100731out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100732
733 return ret;
734}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100735EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100736
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100737bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200738{
Joerg Roedelff03a072010-09-10 17:30:57 +0200739 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200740 int offset;
741 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200742 int r;
743
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200744 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200745 return false;
746
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700747 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300748 return true;
749
Paolo Bonzinia5121772017-07-24 18:54:38 +0200750 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
751 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200752 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
753 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200754 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800755 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200756
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800757 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200758}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100759EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200760
Avi Kivity49a9b072010-06-10 17:02:14 +0300761int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100762{
Sheng Yangaad82702010-05-12 16:40:42 +0800763 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800764 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800765
Avi Kivityf9a48e62010-01-06 19:10:22 +0200766 cr0 |= X86_CR0_ET;
767
Gleb Natapovab344822010-01-21 15:28:46 +0200768#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300769 if (cr0 & 0xffffffff00000000UL)
770 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200771#endif
772
773 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100774
Gleb Natapov0f122442010-04-28 19:15:31 +0300775 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
776 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100777
Gleb Natapov0f122442010-04-28 19:15:31 +0300778 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
779 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100780
781 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
782#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200783 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100784 int cs_db, cs_l;
785
Gleb Natapov0f122442010-04-28 19:15:31 +0300786 if (!is_pae(vcpu))
787 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100788 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300789 if (cs_l)
790 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100791 } else
792#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200793 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200794 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300795 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100796 }
797
Mao, Junjiead756a12012-07-02 01:18:48 +0000798 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
799 return 1;
800
Carsten Ottea03490e2007-10-29 16:09:35 +0100801 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100802
Lai Jiangshand170c412011-02-21 11:21:30 +0800803 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800804 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800805 kvm_async_pf_hash_reset(vcpu);
806 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800807
Sheng Yangaad82702010-05-12 16:40:42 +0800808 if ((cr0 ^ old_cr0) & update_bits)
809 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800810
Laszlo Ersek879ae182015-11-04 12:54:41 +0100811 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
812 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
813 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800814 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
815
Gleb Natapov0f122442010-04-28 19:15:31 +0300816 return 0;
817}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200818EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100819
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200820void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100821{
Avi Kivity49a9b072010-06-10 17:02:14 +0300822 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100823}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200824EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100825
Aaron Lewis139a12c2019-10-21 16:30:25 -0700826void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300827{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700828 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300829
Aaron Lewis139a12c2019-10-21 16:30:25 -0700830 if (vcpu->arch.xcr0 != host_xcr0)
831 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
832
833 if (vcpu->arch.xsaves_enabled &&
834 vcpu->arch.ia32_xss != host_xss)
835 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
836 }
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300837}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700838EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
839
840void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
841{
842 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
843
844 if (vcpu->arch.xcr0 != host_xcr0)
845 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
846
847 if (vcpu->arch.xsaves_enabled &&
848 vcpu->arch.ia32_xss != host_xss)
849 wrmsrl(MSR_IA32_XSS, host_xss);
850 }
851
852}
853EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300854
Fengguang Wu69b00492015-01-19 22:33:39 +0800855static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800856{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000857 u64 xcr0 = xcr;
858 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200859 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800860
861 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
862 if (index != XCR_XFEATURE_ENABLED_MASK)
863 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700864 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800865 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700866 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800867 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200868
869 /*
870 * Do not allow the guest to set bits that we do not support
871 * saving. However, xcr0 bit 0 is always set, even if the
872 * emulated CPU does not support XSAVE (see fx_init).
873 */
Dave Hansend91cab72015-09-02 16:31:26 -0700874 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200875 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800876 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200877
Dave Hansend91cab72015-09-02 16:31:26 -0700878 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
879 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000880 return 1;
881
Dave Hansend91cab72015-09-02 16:31:26 -0700882 if (xcr0 & XFEATURE_MASK_AVX512) {
883 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800884 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700885 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800886 return 1;
887 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800888 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000889
Dave Hansend91cab72015-09-02 16:31:26 -0700890 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000891 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800892 return 0;
893}
894
895int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
896{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000897 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
898 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800899 kvm_inject_gp(vcpu, 0);
900 return 1;
901 }
902 return 0;
903}
904EXPORT_SYMBOL_GPL(kvm_set_xcr);
905
Sean Christopherson345599f2019-12-10 14:44:16 -0800906#define __cr4_reserved_bits(__cpu_has, __c) \
907({ \
908 u64 __reserved_bits = CR4_RESERVED_BITS; \
909 \
910 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
911 __reserved_bits |= X86_CR4_OSXSAVE; \
912 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
913 __reserved_bits |= X86_CR4_SMEP; \
914 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
915 __reserved_bits |= X86_CR4_SMAP; \
916 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
917 __reserved_bits |= X86_CR4_FSGSBASE; \
918 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
919 __reserved_bits |= X86_CR4_PKE; \
920 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
921 __reserved_bits |= X86_CR4_LA57; \
Sean Christophersond76c7fb2020-01-28 15:53:44 -0800922 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
923 __reserved_bits |= X86_CR4_UMIP; \
Sean Christopherson345599f2019-12-10 14:44:16 -0800924 __reserved_bits; \
925})
926
Sean Christophersonb11306b2019-12-10 14:44:13 -0800927static u64 kvm_host_cr4_reserved_bits(struct cpuinfo_x86 *c)
928{
Sean Christopherson345599f2019-12-10 14:44:16 -0800929 u64 reserved_bits = __cr4_reserved_bits(cpu_has, c);
Sean Christophersonb11306b2019-12-10 14:44:13 -0800930
Sean Christophersonc10398b2020-03-02 15:56:46 -0800931 if (kvm_cpu_cap_has(X86_FEATURE_LA57))
Sean Christopherson345599f2019-12-10 14:44:16 -0800932 reserved_bits &= ~X86_CR4_LA57;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800933
Sean Christopherson90d2f602020-03-02 15:56:47 -0800934 if (kvm_cpu_cap_has(X86_FEATURE_UMIP))
Sean Christopherson345599f2019-12-10 14:44:16 -0800935 reserved_bits &= ~X86_CR4_UMIP;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800936
937 return reserved_bits;
938}
939
Wanpeng Li3ca94192019-09-18 17:50:10 +0800940static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
941{
Sean Christophersonb11306b2019-12-10 14:44:13 -0800942 if (cr4 & cr4_reserved_bits)
Wanpeng Li3ca94192019-09-18 17:50:10 +0800943 return -EINVAL;
944
Sean Christopherson345599f2019-12-10 14:44:16 -0800945 if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu))
Wanpeng Li3ca94192019-09-18 17:50:10 +0800946 return -EINVAL;
947
948 return 0;
949}
950
Avi Kivitya83b29c2010-06-10 17:02:15 +0300951int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100952{
Avi Kivityfc78f512009-12-07 12:16:48 +0200953 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800954 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800955 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800956
Wanpeng Li3ca94192019-09-18 17:50:10 +0800957 if (kvm_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200958 return 1;
959
Carsten Ottea03490e2007-10-29 16:09:35 +0100960 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300961 if (!(cr4 & X86_CR4_PAE))
962 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300963 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
964 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200965 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
966 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300967 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100968
Mao, Junjiead756a12012-07-02 01:18:48 +0000969 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200970 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000971 return 1;
972
973 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
974 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
975 return 1;
976 }
977
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300978 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300979 return 1;
980
Mao, Junjiead756a12012-07-02 01:18:48 +0000981 if (((cr4 ^ old_cr4) & pdptr_bits) ||
982 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800983 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300984
Huaitong Hanb9baba82016-03-22 16:51:21 +0800985 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200986 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800987
Gleb Natapov0f122442010-04-28 19:15:31 +0300988 return 0;
989}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200990EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100991
Avi Kivity23902182010-06-10 17:02:16 +0300992int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100993{
Junaid Shahidade61e22018-06-27 14:59:15 -0700994 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +0100995#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -0700996 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
997
Junaid Shahidade61e22018-06-27 14:59:15 -0700998 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -0700999 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1000 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001001 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001002#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001003
Avi Kivity9f8fe502010-12-05 17:30:00 +02001004 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001005 if (!skip_tlb_flush) {
1006 kvm_mmu_sync_roots(vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -07001007 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001008 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001009 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001010 }
1011
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001012 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -07001013 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001014 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02001015 else if (is_pae_paging(vcpu) &&
1016 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001017 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001018
Junaid Shahidade61e22018-06-27 14:59:15 -07001019 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001020 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001021 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001022
Gleb Natapov0f122442010-04-28 19:15:31 +03001023 return 0;
1024}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001025EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001026
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001027int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001028{
Gleb Natapov0f122442010-04-28 19:15:31 +03001029 if (cr8 & CR8_RESERVED_BITS)
1030 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001031 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001032 kvm_lapic_set_tpr(vcpu, cr8);
1033 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001034 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001035 return 0;
1036}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001037EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001038
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001039unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001040{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001041 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001042 return kvm_lapic_get_cr8(vcpu);
1043 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001044 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001045}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001046EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001047
Nadav Amitae561ed2015-04-02 03:10:37 +03001048static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1049{
1050 int i;
1051
1052 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1053 for (i = 0; i < KVM_NR_DB_REGS; i++)
1054 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1055 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1056 }
1057}
1058
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001059static void kvm_update_dr6(struct kvm_vcpu *vcpu)
1060{
1061 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1062 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
1063}
1064
Jan Kiszkac8639012012-09-21 05:42:55 +02001065static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1066{
1067 unsigned long dr7;
1068
1069 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1070 dr7 = vcpu->arch.guest_debug_dr7;
1071 else
1072 dr7 = vcpu->arch.dr7;
1073 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001074 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1075 if (dr7 & DR7_BP_EN_MASK)
1076 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001077}
1078
Nadav Amit6f43ed02014-07-15 17:37:46 +03001079static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1080{
1081 u64 fixed = DR6_FIXED_1;
1082
Radim Krčmářd6321d42017-08-05 00:12:49 +02001083 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001084 fixed |= DR6_RTM;
1085 return fixed;
1086}
1087
Gleb Natapov338dbc92010-04-28 19:15:32 +03001088static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001089{
Marios Pomonisea740052019-12-11 12:47:52 -08001090 size_t size = ARRAY_SIZE(vcpu->arch.db);
1091
Gleb Natapov020df072010-04-13 10:05:23 +03001092 switch (dr) {
1093 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001094 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001095 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1096 vcpu->arch.eff_db[dr] = val;
1097 break;
1098 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001099 /* fall through */
1100 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001101 if (val & 0xffffffff00000000ULL)
1102 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001103 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001104 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001105 break;
1106 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001107 /* fall through */
1108 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001109 if (!kvm_dr7_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001110 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001111 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001112 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001113 break;
1114 }
1115
1116 return 0;
1117}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001118
1119int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1120{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001121 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001122 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001123 return 1;
1124 }
1125 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001126}
Gleb Natapov020df072010-04-13 10:05:23 +03001127EXPORT_SYMBOL_GPL(kvm_set_dr);
1128
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001129int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001130{
Marios Pomonisea740052019-12-11 12:47:52 -08001131 size_t size = ARRAY_SIZE(vcpu->arch.db);
1132
Gleb Natapov020df072010-04-13 10:05:23 +03001133 switch (dr) {
1134 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001135 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001136 break;
1137 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001138 /* fall through */
1139 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001140 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1141 *val = vcpu->arch.dr6;
1142 else
1143 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001144 break;
1145 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001146 /* fall through */
1147 default: /* 7 */
1148 *val = vcpu->arch.dr7;
1149 break;
1150 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001151 return 0;
1152}
Gleb Natapov020df072010-04-13 10:05:23 +03001153EXPORT_SYMBOL_GPL(kvm_get_dr);
1154
Avi Kivity022cd0e2011-11-10 14:57:23 +02001155bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1156{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001157 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001158 u64 data;
1159 int err;
1160
Wei Huangc6702c92015-06-19 13:44:45 +02001161 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001162 if (err)
1163 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001164 kvm_rax_write(vcpu, (u32)data);
1165 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001166 return err;
1167}
1168EXPORT_SYMBOL_GPL(kvm_rdpmc);
1169
Carsten Otte043405e2007-10-10 17:16:19 +02001170/*
1171 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1172 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1173 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001174 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1175 * extract the supported MSRs from the related const lists.
1176 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001177 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001178 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001179 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001180 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001181
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001182static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001183 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001184 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001185#ifdef CONFIG_X86_64
1186 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1187#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001188 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001189 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001190 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001191 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1192 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1193 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1194 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1195 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1196 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001197 MSR_IA32_UMWAIT_CONTROL,
1198
Jim Mattsone2ada662019-08-21 11:20:04 -07001199 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1200 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1201 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1202 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1203 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1204 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1205 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1206 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1207 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1208 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1209 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1210 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1211 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001212 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1213 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1214 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1215 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1216 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1217 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1218 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1219 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1220 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001221};
1222
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001223static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001224static unsigned num_msrs_to_save;
1225
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001226static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001227 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1228 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1229 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1230 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001231 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001232 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1233 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001234 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001235 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001236 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001237 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001238 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001239 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001240 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1241 HV_X64_MSR_TSC_EMULATION_STATUS,
1242
1243 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001244 MSR_KVM_PV_EOI_EN,
1245
Will Auldba904632012-11-29 12:42:50 -08001246 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001247 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001248 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001249 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001250 MSR_IA32_MCG_STATUS,
1251 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001252 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001253 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001254 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001255 MSR_PLATFORM_INFO,
1256 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001257 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001258 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001259 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001260
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001261 /*
1262 * The following list leaves out MSRs whose values are determined
1263 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1264 * We always support the "true" VMX control MSRs, even if the host
1265 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001266 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001267 */
1268 MSR_IA32_VMX_BASIC,
1269 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1270 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1271 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1272 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1273 MSR_IA32_VMX_MISC,
1274 MSR_IA32_VMX_CR0_FIXED0,
1275 MSR_IA32_VMX_CR4_FIXED0,
1276 MSR_IA32_VMX_VMCS_ENUM,
1277 MSR_IA32_VMX_PROCBASED_CTLS2,
1278 MSR_IA32_VMX_EPT_VPID_CAP,
1279 MSR_IA32_VMX_VMFUNC,
1280
Borislav Petkov191c8132019-04-18 18:32:50 +02001281 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001282 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001283};
1284
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001285static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001286static unsigned num_emulated_msrs;
1287
Tom Lendacky801e4592018-02-21 13:39:51 -06001288/*
1289 * List of msr numbers which are used to expose MSR-based features that
1290 * can be used by a hypervisor to validate requested CPU features.
1291 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001292static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001293 MSR_IA32_VMX_BASIC,
1294 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1295 MSR_IA32_VMX_PINBASED_CTLS,
1296 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1297 MSR_IA32_VMX_PROCBASED_CTLS,
1298 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1299 MSR_IA32_VMX_EXIT_CTLS,
1300 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1301 MSR_IA32_VMX_ENTRY_CTLS,
1302 MSR_IA32_VMX_MISC,
1303 MSR_IA32_VMX_CR0_FIXED0,
1304 MSR_IA32_VMX_CR0_FIXED1,
1305 MSR_IA32_VMX_CR4_FIXED0,
1306 MSR_IA32_VMX_CR4_FIXED1,
1307 MSR_IA32_VMX_VMCS_ENUM,
1308 MSR_IA32_VMX_PROCBASED_CTLS2,
1309 MSR_IA32_VMX_EPT_VPID_CAP,
1310 MSR_IA32_VMX_VMFUNC,
1311
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001312 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001313 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001314 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001315};
1316
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001317static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001318static unsigned int num_msr_based_features;
1319
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001320static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001321{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001322 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001323
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001324 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1325 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001326
1327 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001328 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1329 * the nested hypervisor runs with NX huge pages. If it is not,
1330 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1331 * L1 guests, so it need not worry about its own (L2) guests.
1332 */
1333 data |= ARCH_CAP_PSCHANGE_MC_NO;
1334
1335 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001336 * If we're doing cache flushes (either "always" or "cond")
1337 * we will do one whenever the guest does a vmlaunch/vmresume.
1338 * If an outer hypervisor is doing the cache flush for us
1339 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1340 * capability to the guest too, and if EPT is disabled we're not
1341 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1342 * require a nested hypervisor to do a flush of its own.
1343 */
1344 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1345 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1346
Paolo Bonzini0c549142019-08-19 17:24:07 +02001347 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1348 data |= ARCH_CAP_RDCL_NO;
1349 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1350 data |= ARCH_CAP_SSB_NO;
1351 if (!boot_cpu_has_bug(X86_BUG_MDS))
1352 data |= ARCH_CAP_MDS_NO;
1353
Pawan Guptae1d38b62019-10-23 12:23:33 +02001354 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001355 * On TAA affected systems:
1356 * - nothing to do if TSX is disabled on the host.
1357 * - we emulate TSX_CTRL if present on the host.
1358 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001359 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001360 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001361 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001362 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1363 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001364
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001365 return data;
1366}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001367
Wanpeng Li66421c12018-02-28 14:03:30 +08001368static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1369{
1370 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001371 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001372 msr->data = kvm_get_arch_capabilities();
1373 break;
1374 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001375 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001376 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001377 default:
1378 if (kvm_x86_ops->get_msr_feature(msr))
1379 return 1;
1380 }
1381 return 0;
1382}
1383
Tom Lendacky801e4592018-02-21 13:39:51 -06001384static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1385{
1386 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001387 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001388
1389 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001390 r = kvm_get_msr_feature(&msr);
1391 if (r)
1392 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001393
1394 *data = msr.data;
1395
1396 return 0;
1397}
1398
Sean Christopherson11988492019-04-02 08:19:15 -07001399static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1400{
1401 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1402 return false;
1403
1404 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1405 return false;
1406
Sean Christopherson0a629562019-04-02 08:19:16 -07001407 if (efer & (EFER_LME | EFER_LMA) &&
1408 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1409 return false;
1410
1411 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1412 return false;
1413
Sean Christopherson11988492019-04-02 08:19:15 -07001414 return true;
1415
1416}
Jan Kiszka384bb782013-04-20 10:52:36 +02001417bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001418{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001419 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001420 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001421
Sean Christopherson11988492019-04-02 08:19:15 -07001422 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001423}
1424EXPORT_SYMBOL_GPL(kvm_valid_efer);
1425
Sean Christopherson11988492019-04-02 08:19:15 -07001426static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001427{
1428 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001429 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001430
Sean Christopherson11988492019-04-02 08:19:15 -07001431 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001432 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001433
Sean Christopherson11988492019-04-02 08:19:15 -07001434 if (!msr_info->host_initiated) {
1435 if (!__kvm_valid_efer(vcpu, efer))
1436 return 1;
1437
1438 if (is_paging(vcpu) &&
1439 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1440 return 1;
1441 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001442
Carsten Otte15c4a642007-10-30 18:44:17 +01001443 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001444 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001445
Sheng Yanga3d204e2010-05-12 16:40:40 +08001446 kvm_x86_ops->set_efer(vcpu, efer);
1447
Sheng Yangaad82702010-05-12 16:40:42 +08001448 /* Update reserved bits */
1449 if ((efer ^ old_efer) & EFER_NX)
1450 kvm_mmu_reset_context(vcpu);
1451
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001452 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001453}
1454
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001455void kvm_enable_efer_bits(u64 mask)
1456{
1457 efer_reserved_bits &= ~mask;
1458}
1459EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1460
Carsten Otte15c4a642007-10-30 18:44:17 +01001461/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001462 * Write @data into the MSR specified by @index. Select MSR specific fault
1463 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001464 * Returns 0 on success, non-0 otherwise.
1465 * Assumes vcpu_load() was already called.
1466 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001467static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1468 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001469{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001470 struct msr_data msr;
1471
1472 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001473 case MSR_FS_BASE:
1474 case MSR_GS_BASE:
1475 case MSR_KERNEL_GS_BASE:
1476 case MSR_CSTAR:
1477 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001478 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001479 return 1;
1480 break;
1481 case MSR_IA32_SYSENTER_EIP:
1482 case MSR_IA32_SYSENTER_ESP:
1483 /*
1484 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1485 * non-canonical address is written on Intel but not on
1486 * AMD (which ignores the top 32-bits, because it does
1487 * not implement 64-bit SYSENTER).
1488 *
1489 * 64-bit code should hence be able to write a non-canonical
1490 * value on AMD. Making the address canonical ensures that
1491 * vmentry does not fail on Intel after writing a non-canonical
1492 * value, and that something deterministic happens if the guest
1493 * invokes 64-bit SYSENTER.
1494 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001495 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001496 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001497
1498 msr.data = data;
1499 msr.index = index;
1500 msr.host_initiated = host_initiated;
1501
1502 return kvm_x86_ops->set_msr(vcpu, &msr);
1503}
1504
1505/*
1506 * Read the MSR specified by @index into @data. Select MSR specific fault
1507 * checks are bypassed if @host_initiated is %true.
1508 * Returns 0 on success, non-0 otherwise.
1509 * Assumes vcpu_load() was already called.
1510 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001511int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1512 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001513{
1514 struct msr_data msr;
1515 int ret;
1516
1517 msr.index = index;
1518 msr.host_initiated = host_initiated;
1519
1520 ret = kvm_x86_ops->get_msr(vcpu, &msr);
1521 if (!ret)
1522 *data = msr.data;
1523 return ret;
1524}
1525
1526int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1527{
1528 return __kvm_get_msr(vcpu, index, data, false);
1529}
1530EXPORT_SYMBOL_GPL(kvm_get_msr);
1531
1532int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1533{
1534 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001535}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001536EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001537
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001538int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1539{
1540 u32 ecx = kvm_rcx_read(vcpu);
1541 u64 data;
1542
1543 if (kvm_get_msr(vcpu, ecx, &data)) {
1544 trace_kvm_msr_read_ex(ecx);
1545 kvm_inject_gp(vcpu, 0);
1546 return 1;
1547 }
1548
1549 trace_kvm_msr_read(ecx, data);
1550
1551 kvm_rax_write(vcpu, data & -1u);
1552 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1553 return kvm_skip_emulated_instruction(vcpu);
1554}
1555EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1556
1557int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1558{
1559 u32 ecx = kvm_rcx_read(vcpu);
1560 u64 data = kvm_read_edx_eax(vcpu);
1561
1562 if (kvm_set_msr(vcpu, ecx, data)) {
1563 trace_kvm_msr_write_ex(ecx, data);
1564 kvm_inject_gp(vcpu, 0);
1565 return 1;
1566 }
1567
1568 trace_kvm_msr_write(ecx, data);
1569 return kvm_skip_emulated_instruction(vcpu);
1570}
1571EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1572
Carsten Otte313a3dc2007-10-11 19:16:52 +02001573/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001574 * The fast path for frequent and performance sensitive wrmsr emulation,
1575 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1576 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1577 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1578 * other cases which must be called after interrupts are enabled on the host.
1579 */
1580static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1581{
1582 if (lapic_in_kernel(vcpu) && apic_x2apic_mode(vcpu->arch.apic) &&
1583 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
1584 ((data & APIC_MODE_MASK) == APIC_DM_FIXED)) {
1585
1586 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
1587 return kvm_lapic_reg_write(vcpu->arch.apic, APIC_ICR, (u32)data);
1588 }
1589
1590 return 1;
1591}
1592
1593enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
1594{
1595 u32 msr = kvm_rcx_read(vcpu);
1596 u64 data = kvm_read_edx_eax(vcpu);
1597 int ret = 0;
1598
1599 switch (msr) {
1600 case APIC_BASE_MSR + (APIC_ICR >> 4):
1601 ret = handle_fastpath_set_x2apic_icr_irqoff(vcpu, data);
1602 break;
1603 default:
1604 return EXIT_FASTPATH_NONE;
1605 }
1606
1607 if (!ret) {
1608 trace_kvm_msr_write(msr, data);
1609 return EXIT_FASTPATH_SKIP_EMUL_INS;
1610 }
1611
1612 return EXIT_FASTPATH_NONE;
1613}
1614EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1615
1616/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001617 * Adapt set_msr() to msr_io()'s calling convention
1618 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001619static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1620{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001621 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001622}
1623
Carsten Otte313a3dc2007-10-11 19:16:52 +02001624static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1625{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001626 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001627}
1628
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001629#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001630struct pvclock_clock {
1631 int vclock_mode;
1632 u64 cycle_last;
1633 u64 mask;
1634 u32 mult;
1635 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001636 u64 base_cycles;
1637 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001638};
1639
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001640struct pvclock_gtod_data {
1641 seqcount_t seq;
1642
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001643 struct pvclock_clock clock; /* extract of a clocksource struct */
1644 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001645
Paolo Bonzini917f9472020-01-22 14:32:20 +01001646 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001647 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001648};
1649
1650static struct pvclock_gtod_data pvclock_gtod_data;
1651
1652static void update_pvclock_gtod(struct timekeeper *tk)
1653{
1654 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1655
1656 write_seqcount_begin(&vdata->seq);
1657
1658 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001659 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1660 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1661 vdata->clock.mask = tk->tkr_mono.mask;
1662 vdata->clock.mult = tk->tkr_mono.mult;
1663 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001664 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1665 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001666
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001667 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->archdata.vclock_mode;
1668 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1669 vdata->raw_clock.mask = tk->tkr_raw.mask;
1670 vdata->raw_clock.mult = tk->tkr_raw.mult;
1671 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001672 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1673 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001674
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001675 vdata->wall_time_sec = tk->xtime_sec;
1676
Paolo Bonzini917f9472020-01-22 14:32:20 +01001677 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001678
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001679 write_seqcount_end(&vdata->seq);
1680}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001681
1682static s64 get_kvmclock_base_ns(void)
1683{
1684 /* Count up from boot time, but with the frequency of the raw clock. */
1685 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1686}
1687#else
1688static s64 get_kvmclock_base_ns(void)
1689{
1690 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1691 return ktime_get_boottime_ns();
1692}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001693#endif
1694
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001695void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1696{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001697 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001698 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001699}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001700
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001701static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1702{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001703 int version;
1704 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001705 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001706 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001707
1708 if (!wall_clock)
1709 return;
1710
Avi Kivity9ed3c442010-05-04 15:00:37 +03001711 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1712 if (r)
1713 return;
1714
1715 if (version & 1)
1716 ++version; /* first time write, random junk */
1717
1718 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001719
Nicholas Krause1dab1342015-12-30 13:08:46 -05001720 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1721 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001722
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001723 /*
1724 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001725 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001726 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001727 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001728 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001729
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001730 wc.nsec = do_div(wall_nsec, 1000000000);
1731 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001732 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001733
1734 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1735
1736 version++;
1737 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001738}
1739
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001740static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1741{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001742 do_shl32_div32(dividend, divisor);
1743 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001744}
1745
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001746static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001747 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001748{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001749 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001750 int32_t shift = 0;
1751 uint64_t tps64;
1752 uint32_t tps32;
1753
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001754 tps64 = base_hz;
1755 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001756 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001757 tps64 >>= 1;
1758 shift--;
1759 }
1760
1761 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001762 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1763 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001764 scaled64 >>= 1;
1765 else
1766 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001767 shift++;
1768 }
1769
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001770 *pshift = shift;
1771 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001772}
1773
Marcelo Tosattid8281992012-11-27 23:29:01 -02001774#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001775static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001776#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001777
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001778static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001779static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001780
Zachary Amsdencc578282012-02-03 15:43:50 -02001781static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001782{
Zachary Amsdencc578282012-02-03 15:43:50 -02001783 u64 v = (u64)khz * (1000000 + ppm);
1784 do_div(v, 1000000);
1785 return v;
1786}
1787
Haozhong Zhang381d5852015-10-20 15:39:04 +08001788static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1789{
1790 u64 ratio;
1791
1792 /* Guest TSC same frequency as host TSC? */
1793 if (!scale) {
1794 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1795 return 0;
1796 }
1797
1798 /* TSC scaling supported? */
1799 if (!kvm_has_tsc_control) {
1800 if (user_tsc_khz > tsc_khz) {
1801 vcpu->arch.tsc_catchup = 1;
1802 vcpu->arch.tsc_always_catchup = 1;
1803 return 0;
1804 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001805 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001806 return -1;
1807 }
1808 }
1809
1810 /* TSC scaling required - calculate ratio */
1811 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1812 user_tsc_khz, tsc_khz);
1813
1814 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001815 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1816 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001817 return -1;
1818 }
1819
1820 vcpu->arch.tsc_scaling_ratio = ratio;
1821 return 0;
1822}
1823
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001824static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001825{
1826 u32 thresh_lo, thresh_hi;
1827 int use_scaling = 0;
1828
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001829 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001830 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001831 /* set tsc_scaling_ratio to a safe value */
1832 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001833 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001834 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001835
Zachary Amsdenc2855452010-09-18 14:38:15 -10001836 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001837 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001838 &vcpu->arch.virtual_tsc_shift,
1839 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001840 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001841
1842 /*
1843 * Compute the variation in TSC rate which is acceptable
1844 * within the range of tolerance and decide if the
1845 * rate being applied is within that bounds of the hardware
1846 * rate. If so, no scaling or compensation need be done.
1847 */
1848 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1849 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001850 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1851 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 -02001852 use_scaling = 1;
1853 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001854 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001855}
1856
1857static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1858{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001859 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001860 vcpu->arch.virtual_tsc_mult,
1861 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001862 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001863 return tsc;
1864}
1865
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001866static inline int gtod_is_based_on_tsc(int mode)
1867{
1868 return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK;
1869}
1870
Fengguang Wu69b00492015-01-19 22:33:39 +08001871static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001872{
1873#ifdef CONFIG_X86_64
1874 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001875 struct kvm_arch *ka = &vcpu->kvm->arch;
1876 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1877
1878 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1879 atomic_read(&vcpu->kvm->online_vcpus));
1880
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001881 /*
1882 * Once the masterclock is enabled, always perform request in
1883 * order to update it.
1884 *
1885 * In order to enable masterclock, the host clocksource must be TSC
1886 * and the vcpus need to have matched TSCs. When that happens,
1887 * perform request to enable masterclock.
1888 */
1889 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001890 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001891 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1892
1893 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1894 atomic_read(&vcpu->kvm->online_vcpus),
1895 ka->use_master_clock, gtod->clock.vclock_mode);
1896#endif
1897}
1898
Will Auldba904632012-11-29 12:42:50 -08001899static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1900{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001901 u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001902 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1903}
1904
Haozhong Zhang35181e82015-10-20 15:39:03 +08001905/*
1906 * Multiply tsc by a fixed point number represented by ratio.
1907 *
1908 * The most significant 64-N bits (mult) of ratio represent the
1909 * integral part of the fixed point number; the remaining N bits
1910 * (frac) represent the fractional part, ie. ratio represents a fixed
1911 * point number (mult + frac * 2^(-N)).
1912 *
1913 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1914 */
1915static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1916{
1917 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1918}
1919
1920u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1921{
1922 u64 _tsc = tsc;
1923 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1924
1925 if (ratio != kvm_default_tsc_scaling_ratio)
1926 _tsc = __scale_tsc(ratio, tsc);
1927
1928 return _tsc;
1929}
1930EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1931
Haozhong Zhang07c14192015-10-20 15:39:05 +08001932static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1933{
1934 u64 tsc;
1935
1936 tsc = kvm_scale_tsc(vcpu, rdtsc());
1937
1938 return target_tsc - tsc;
1939}
1940
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001941u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1942{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001943 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1944
1945 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001946}
1947EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1948
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001949static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1950{
Leonid Shatz326e7422018-11-06 12:14:25 +02001951 vcpu->arch.tsc_offset = kvm_x86_ops->write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001952}
1953
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001954static inline bool kvm_check_tsc_unstable(void)
1955{
1956#ifdef CONFIG_X86_64
1957 /*
1958 * TSC is marked unstable when we're running on Hyper-V,
1959 * 'TSC page' clocksource is good.
1960 */
1961 if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK)
1962 return false;
1963#endif
1964 return check_tsc_unstable();
1965}
1966
Will Auld8fe8ab42012-11-29 12:42:12 -08001967void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001968{
1969 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001970 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001971 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001972 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001973 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001974 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001975 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001976
Jan Kiszka038f8c12011-02-04 10:49:11 +01001977 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001978 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001979 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001980 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001981
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001982 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03001983 if (data == 0 && msr->host_initiated) {
1984 /*
1985 * detection of vcpu initialization -- need to sync
1986 * with other vCPUs. This particularly helps to keep
1987 * kvm_clock stable after CPU hotplug
1988 */
1989 synchronizing = true;
1990 } else {
1991 u64 tsc_exp = kvm->arch.last_tsc_write +
1992 nsec_to_cycles(vcpu, elapsed);
1993 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
1994 /*
1995 * Special case: TSC write with a small delta (1 second)
1996 * of virtual cycle time against real time is
1997 * interpreted as an attempt to synchronize the CPU.
1998 */
1999 synchronizing = data < tsc_exp + tsc_hz &&
2000 data + tsc_hz > tsc_exp;
2001 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002002 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002003
2004 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002005 * For a reliable TSC, we can match TSC offsets, and for an unstable
2006 * TSC, we add elapsed time in this computation. We could let the
2007 * compensation code attempt to catch up if we fall behind, but
2008 * it's better to try to match offsets from the beginning.
2009 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002010 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002011 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002012 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002013 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002014 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002015 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002016 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002017 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002018 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002019 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002020 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002021 } else {
2022 /*
2023 * We split periods of matched TSC writes into generations.
2024 * For each generation, we track the original measured
2025 * nanosecond time, offset, and write, so if TSCs are in
2026 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002027 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002028 *
2029 * These values are tracked in kvm->arch.cur_xxx variables.
2030 */
2031 kvm->arch.cur_tsc_generation++;
2032 kvm->arch.cur_tsc_nsec = ns;
2033 kvm->arch.cur_tsc_write = data;
2034 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002035 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002036 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002037
2038 /*
2039 * We also track th most recent recorded KHZ, write and time to
2040 * allow the matching interval to be extended at each write.
2041 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002042 kvm->arch.last_tsc_nsec = ns;
2043 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002044 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002045
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002046 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002047
2048 /* Keep track of which generation this VCPU has synchronized to */
2049 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2050 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2051 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2052
Radim Krčmářd6321d42017-08-05 00:12:49 +02002053 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08002054 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002055
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002056 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002057 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002058
2059 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002060 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002061 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002062 } else if (!already_matched) {
2063 kvm->arch.nr_vcpus_matched_tsc++;
2064 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002065
2066 kvm_track_tsc_matching(vcpu);
2067 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002068}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002069
Zachary Amsden99e3e302010-08-19 22:07:17 -10002070EXPORT_SYMBOL_GPL(kvm_write_tsc);
2071
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002072static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2073 s64 adjustment)
2074{
Leonid Shatz326e7422018-11-06 12:14:25 +02002075 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
2076 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002077}
2078
2079static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2080{
2081 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2082 WARN_ON(adjustment < 0);
2083 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002084 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002085}
2086
Marcelo Tosattid8281992012-11-27 23:29:01 -02002087#ifdef CONFIG_X86_64
2088
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002089static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002090{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002091 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002092 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002093
2094 if (likely(ret >= last))
2095 return ret;
2096
2097 /*
2098 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002099 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002100 * very likely) and there's a data dependence, so force GCC
2101 * to generate a branch instead. I don't barrier() because
2102 * we don't actually need a barrier, and if this function
2103 * ever gets inlined it will generate worse code.
2104 */
2105 asm volatile ("");
2106 return last;
2107}
2108
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002109static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2110 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002111{
2112 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002113 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002114
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002115 switch (clock->vclock_mode) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002116 case VCLOCK_HVCLOCK:
2117 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2118 tsc_timestamp);
2119 if (tsc_pg_val != U64_MAX) {
2120 /* TSC page valid */
2121 *mode = VCLOCK_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002122 v = (tsc_pg_val - clock->cycle_last) &
2123 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002124 } else {
2125 /* TSC page invalid */
2126 *mode = VCLOCK_NONE;
2127 }
2128 break;
2129 case VCLOCK_TSC:
2130 *mode = VCLOCK_TSC;
2131 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002132 v = (*tsc_timestamp - clock->cycle_last) &
2133 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002134 break;
2135 default:
2136 *mode = VCLOCK_NONE;
2137 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002138
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002139 if (*mode == VCLOCK_NONE)
2140 *tsc_timestamp = v = 0;
2141
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002142 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002143}
2144
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002145static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002146{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002147 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002148 unsigned long seq;
2149 int mode;
2150 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002151
Marcelo Tosattid8281992012-11-27 23:29:01 -02002152 do {
2153 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002154 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002155 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002156 ns >>= gtod->raw_clock.shift;
2157 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002158 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002159 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002160
2161 return mode;
2162}
2163
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002164static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002165{
2166 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2167 unsigned long seq;
2168 int mode;
2169 u64 ns;
2170
2171 do {
2172 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002173 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002174 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002175 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002176 ns >>= gtod->clock.shift;
2177 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2178
2179 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2180 ts->tv_nsec = ns;
2181
2182 return mode;
2183}
2184
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002185/* returns true if host is using TSC based clocksource */
2186static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002187{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002188 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002189 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002190 return false;
2191
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002192 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002193 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002194}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002195
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002196/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002197static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002198 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002199{
2200 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002201 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002202 return false;
2203
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002204 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002205}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002206#endif
2207
2208/*
2209 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002210 * Assuming a stable TSC across physical CPUS, and a stable TSC
2211 * across virtual CPUs, the following condition is possible.
2212 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002213 * CPUs at the next numbered event.
2214 *
2215 * "timespecX" represents host monotonic time. "tscX" represents
2216 * RDTSC value.
2217 *
2218 * VCPU0 on CPU0 | VCPU1 on CPU1
2219 *
2220 * 1. read timespec0,tsc0
2221 * 2. | timespec1 = timespec0 + N
2222 * | tsc1 = tsc0 + M
2223 * 3. transition to guest | transition to guest
2224 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2225 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2226 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2227 *
2228 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2229 *
2230 * - ret0 < ret1
2231 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2232 * ...
2233 * - 0 < N - M => M < N
2234 *
2235 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2236 * always the case (the difference between two distinct xtime instances
2237 * might be smaller then the difference between corresponding TSC reads,
2238 * when updating guest vcpus pvclock areas).
2239 *
2240 * To avoid that problem, do not allow visibility of distinct
2241 * system_timestamp/tsc_timestamp values simultaneously: use a master
2242 * copy of host monotonic time values. Update that master copy
2243 * in lockstep.
2244 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002245 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002246 *
2247 */
2248
2249static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2250{
2251#ifdef CONFIG_X86_64
2252 struct kvm_arch *ka = &kvm->arch;
2253 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002254 bool host_tsc_clocksource, vcpus_matched;
2255
2256 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2257 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002258
2259 /*
2260 * If the host uses TSC clock, then passthrough TSC as stable
2261 * to the guest.
2262 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002263 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002264 &ka->master_kernel_ns,
2265 &ka->master_cycle_now);
2266
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002267 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002268 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002269 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002270
Marcelo Tosattid8281992012-11-27 23:29:01 -02002271 if (ka->use_master_clock)
2272 atomic_set(&kvm_guest_has_master_clock, 1);
2273
2274 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002275 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2276 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002277#endif
2278}
2279
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002280void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2281{
2282 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2283}
2284
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002285static void kvm_gen_update_masterclock(struct kvm *kvm)
2286{
2287#ifdef CONFIG_X86_64
2288 int i;
2289 struct kvm_vcpu *vcpu;
2290 struct kvm_arch *ka = &kvm->arch;
2291
2292 spin_lock(&ka->pvclock_gtod_sync_lock);
2293 kvm_make_mclock_inprogress_request(kvm);
2294 /* no guest entries from this point */
2295 pvclock_update_vm_gtod_copy(kvm);
2296
2297 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002298 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002299
2300 /* guest entries allowed */
2301 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002302 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002303
2304 spin_unlock(&ka->pvclock_gtod_sync_lock);
2305#endif
2306}
2307
Marcelo Tosattie891a322017-04-17 12:51:37 -03002308u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002309{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002310 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002311 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002312 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002313
Paolo Bonzini8b953442016-11-16 18:31:30 +01002314 spin_lock(&ka->pvclock_gtod_sync_lock);
2315 if (!ka->use_master_clock) {
2316 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002317 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002318 }
2319
Paolo Bonzini8b953442016-11-16 18:31:30 +01002320 hv_clock.tsc_timestamp = ka->master_cycle_now;
2321 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2322 spin_unlock(&ka->pvclock_gtod_sync_lock);
2323
Wanpeng Lie2c22062017-05-11 18:12:05 -07002324 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2325 get_cpu();
2326
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002327 if (__this_cpu_read(cpu_tsc_khz)) {
2328 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2329 &hv_clock.tsc_shift,
2330 &hv_clock.tsc_to_system_mul);
2331 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2332 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002333 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002334
2335 put_cpu();
2336
2337 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002338}
2339
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002340static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2341{
2342 struct kvm_vcpu_arch *vcpu = &v->arch;
2343 struct pvclock_vcpu_time_info guest_hv_clock;
2344
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002345 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002346 &guest_hv_clock, sizeof(guest_hv_clock))))
2347 return;
2348
2349 /* This VCPU is paused, but it's legal for a guest to read another
2350 * VCPU's kvmclock, so we really have to follow the specification where
2351 * it says that version is odd if data is being modified, and even after
2352 * it is consistent.
2353 *
2354 * Version field updates must be kept separate. This is because
2355 * kvm_write_guest_cached might use a "rep movs" instruction, and
2356 * writes within a string instruction are weakly ordered. So there
2357 * are three writes overall.
2358 *
2359 * As a small optimization, only write the version field in the first
2360 * and third write. The vcpu->pv_time cache is still valid, because the
2361 * version field is the first in the struct.
2362 */
2363 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2364
Liran Alon51c4b8b2017-11-05 16:11:30 +02002365 if (guest_hv_clock.version & 1)
2366 ++guest_hv_clock.version; /* first time write, random junk */
2367
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002368 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002369 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2370 &vcpu->hv_clock,
2371 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002372
2373 smp_wmb();
2374
2375 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2376 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2377
2378 if (vcpu->pvclock_set_guest_stopped_request) {
2379 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2380 vcpu->pvclock_set_guest_stopped_request = false;
2381 }
2382
2383 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2384
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002385 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2386 &vcpu->hv_clock,
2387 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002388
2389 smp_wmb();
2390
2391 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002392 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2393 &vcpu->hv_clock,
2394 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002395}
2396
Zachary Amsden34c238a2010-09-18 14:38:14 -10002397static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002398{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002399 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002400 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002401 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002402 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002403 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002404 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002405 bool use_master_clock;
2406
2407 kernel_ns = 0;
2408 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002409
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002410 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002411 * If the host uses TSC clock, then passthrough TSC as stable
2412 * to the guest.
2413 */
2414 spin_lock(&ka->pvclock_gtod_sync_lock);
2415 use_master_clock = ka->use_master_clock;
2416 if (use_master_clock) {
2417 host_tsc = ka->master_cycle_now;
2418 kernel_ns = ka->master_kernel_ns;
2419 }
2420 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002421
2422 /* Keep irq disabled to prevent changes to the clock */
2423 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002424 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2425 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002426 local_irq_restore(flags);
2427 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2428 return 1;
2429 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002430 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002431 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002432 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002433 }
2434
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002435 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002436
2437 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002438 * We may have to catch up the TSC to match elapsed wall clock
2439 * time for two reasons, even if kvmclock is used.
2440 * 1) CPU could have been running below the maximum TSC rate
2441 * 2) Broken TSC compensation resets the base at each VCPU
2442 * entry to avoid unknown leaps of TSC even when running
2443 * again on the same CPU. This may cause apparent elapsed
2444 * time to disappear, and the guest to stand still or run
2445 * very slowly.
2446 */
2447 if (vcpu->tsc_catchup) {
2448 u64 tsc = compute_guest_tsc(v, kernel_ns);
2449 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002450 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002451 tsc_timestamp = tsc;
2452 }
2453 }
2454
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002455 local_irq_restore(flags);
2456
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002457 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002458
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002459 if (kvm_has_tsc_control)
2460 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2461
2462 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002463 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002464 &vcpu->hv_clock.tsc_shift,
2465 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002466 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002467 }
2468
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002469 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002470 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002471 vcpu->last_guest_tsc = tsc_timestamp;
Paolo Bonzini9446e6f2020-02-12 13:27:10 +01002472 WARN_ON((s64)vcpu->hv_clock.system_time < 0);
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002473
Marcelo Tosattid8281992012-11-27 23:29:01 -02002474 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002475 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002476 if (use_master_clock)
2477 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2478
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002479 vcpu->hv_clock.flags = pvclock_flags;
2480
Paolo Bonzini095cf552016-02-08 12:54:12 +01002481 if (vcpu->pv_time_enabled)
2482 kvm_setup_pvclock_page(v);
2483 if (v == kvm_get_vcpu(v->kvm, 0))
2484 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002485 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002486}
2487
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002488/*
2489 * kvmclock updates which are isolated to a given vcpu, such as
2490 * vcpu->cpu migration, should not allow system_timestamp from
2491 * the rest of the vcpus to remain static. Otherwise ntp frequency
2492 * correction applies to one vcpu's system_timestamp but not
2493 * the others.
2494 *
2495 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002496 * We need to rate-limit these requests though, as they can
2497 * considerably slow guests that have a large number of vcpus.
2498 * The time for a remote vcpu to update its kvmclock is bound
2499 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002500 */
2501
Andrew Jones7e44e442014-02-28 12:52:54 +01002502#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2503
2504static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002505{
2506 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002507 struct delayed_work *dwork = to_delayed_work(work);
2508 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2509 kvmclock_update_work);
2510 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002511 struct kvm_vcpu *vcpu;
2512
2513 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002514 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002515 kvm_vcpu_kick(vcpu);
2516 }
2517}
2518
Andrew Jones7e44e442014-02-28 12:52:54 +01002519static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2520{
2521 struct kvm *kvm = v->kvm;
2522
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002523 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002524 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2525 KVMCLOCK_UPDATE_DELAY);
2526}
2527
Andrew Jones332967a2014-02-28 12:52:55 +01002528#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2529
2530static void kvmclock_sync_fn(struct work_struct *work)
2531{
2532 struct delayed_work *dwork = to_delayed_work(work);
2533 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2534 kvmclock_sync_work);
2535 struct kvm *kvm = container_of(ka, struct kvm, arch);
2536
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002537 if (!kvmclock_periodic_sync)
2538 return;
2539
Andrew Jones332967a2014-02-28 12:52:55 +01002540 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2541 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2542 KVMCLOCK_SYNC_PERIOD);
2543}
2544
Borislav Petkov191c8132019-04-18 18:32:50 +02002545/*
2546 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2547 */
2548static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2549{
2550 /* McStatusWrEn enabled? */
2551 if (guest_cpuid_is_amd(vcpu))
2552 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2553
2554 return false;
2555}
2556
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002557static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002558{
2559 u64 mcg_cap = vcpu->arch.mcg_cap;
2560 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002561 u32 msr = msr_info->index;
2562 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002563
2564 switch (msr) {
2565 case MSR_IA32_MCG_STATUS:
2566 vcpu->arch.mcg_status = data;
2567 break;
2568 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002569 if (!(mcg_cap & MCG_CTL_P) &&
2570 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002571 return 1;
2572 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002573 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002574 vcpu->arch.mcg_ctl = data;
2575 break;
2576 default:
2577 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002578 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002579 u32 offset = array_index_nospec(
2580 msr - MSR_IA32_MC0_CTL,
2581 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2582
Andre Przywara114be422010-03-24 17:46:42 +01002583 /* only 0 or all 1s can be written to IA32_MCi_CTL
2584 * some Linux kernels though clear bit 10 in bank 4 to
2585 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2586 * this to avoid an uncatched #GP in the guest
2587 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002588 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002589 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002590 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002591
2592 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002593 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002594 (offset & 0x3) == 1 && data != 0) {
2595 if (!can_set_mci_status(vcpu))
2596 return -1;
2597 }
2598
Huang Ying890ca9a2009-05-11 16:48:15 +08002599 vcpu->arch.mce_banks[offset] = data;
2600 break;
2601 }
2602 return 1;
2603 }
2604 return 0;
2605}
2606
Ed Swierkffde22a2009-10-15 15:21:43 -07002607static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2608{
2609 struct kvm *kvm = vcpu->kvm;
2610 int lm = is_long_mode(vcpu);
2611 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2612 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2613 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2614 : kvm->arch.xen_hvm_config.blob_size_32;
2615 u32 page_num = data & ~PAGE_MASK;
2616 u64 page_addr = data & PAGE_MASK;
2617 u8 *page;
2618 int r;
2619
2620 r = -E2BIG;
2621 if (page_num >= blob_size)
2622 goto out;
2623 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002624 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2625 if (IS_ERR(page)) {
2626 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002627 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002628 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002629 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002630 goto out_free;
2631 r = 0;
2632out_free:
2633 kfree(page);
2634out:
2635 return r;
2636}
2637
Gleb Natapov344d9582010-10-14 11:22:50 +02002638static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2639{
2640 gpa_t gpa = data & ~0x3f;
2641
Wanpeng Li52a5c152017-07-13 18:30:42 -07002642 /* Bits 3:5 are reserved, Should be zero */
2643 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002644 return 1;
2645
2646 vcpu->arch.apf.msr_val = data;
2647
2648 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2649 kvm_clear_async_pf_completion_queue(vcpu);
2650 kvm_async_pf_hash_reset(vcpu);
2651 return 0;
2652 }
2653
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002654 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002655 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002656 return 1;
2657
Gleb Natapov6adba522010-10-14 11:22:55 +02002658 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002659 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002660 kvm_async_pf_wakeup_all(vcpu);
2661 return 0;
2662}
2663
Glauber Costa12f9a482011-02-01 14:16:40 -05002664static void kvmclock_reset(struct kvm_vcpu *vcpu)
2665{
Andy Honig0b794592013-02-20 14:48:10 -08002666 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002667 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002668}
2669
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002670static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2671{
2672 ++vcpu->stat.tlb_flush;
2673 kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa);
2674}
2675
Glauber Costac9aaa892011-07-11 15:28:14 -04002676static void record_steal_time(struct kvm_vcpu *vcpu)
2677{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002678 struct kvm_host_map map;
2679 struct kvm_steal_time *st;
2680
Glauber Costac9aaa892011-07-11 15:28:14 -04002681 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2682 return;
2683
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002684 /* -EAGAIN is returned in atomic context so we can just return. */
2685 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2686 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002687 return;
2688
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002689 st = map.hva +
2690 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2691
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002692 /*
2693 * Doing a TLB flush here, on the guest's behalf, can avoid
2694 * expensive IPIs.
2695 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002696 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002697 st->preempted & KVM_VCPU_FLUSH_TLB);
2698 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002699 kvm_vcpu_flush_tlb(vcpu, false);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002700
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002701 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002702
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002703 if (st->version & 1)
2704 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002705
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002706 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002707
2708 smp_wmb();
2709
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002710 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002711 vcpu->arch.st.last_steal;
2712 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002713
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002714 smp_wmb();
2715
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002716 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002717
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002718 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002719}
2720
Will Auld8fe8ab42012-11-29 12:42:12 -08002721int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002722{
Gleb Natapov57537852012-01-15 14:17:22 +02002723 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002724 u32 msr = msr_info->index;
2725 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002726
Carsten Otte15c4a642007-10-30 18:44:17 +01002727 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002728 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002729 case MSR_IA32_UCODE_WRITE:
2730 case MSR_VM_HSAVE_PA:
2731 case MSR_AMD64_PATCH_LOADER:
2732 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002733 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002734 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002735 break;
2736
Wanpeng Li518e7b92018-02-28 14:03:31 +08002737 case MSR_IA32_UCODE_REV:
2738 if (msr_info->host_initiated)
2739 vcpu->arch.microcode_version = data;
2740 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002741 case MSR_IA32_ARCH_CAPABILITIES:
2742 if (!msr_info->host_initiated)
2743 return 1;
2744 vcpu->arch.arch_capabilities = data;
2745 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002746 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002747 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002748 case MSR_K7_HWCR:
2749 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002750 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002751 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002752
2753 /* Handle McStatusWrEn */
2754 if (data == BIT_ULL(18)) {
2755 vcpu->arch.msr_hwcr = data;
2756 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002757 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2758 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002759 return 1;
2760 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002761 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002762 case MSR_FAM10H_MMIO_CONF_BASE:
2763 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002764 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2765 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002766 return 1;
2767 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002768 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002769 case MSR_IA32_DEBUGCTLMSR:
2770 if (!data) {
2771 /* We support the non-activated case already */
2772 break;
2773 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2774 /* Values other than LBR and BTF are vendor-specific,
2775 thus reserved and should throw a #GP */
2776 return 1;
2777 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002778 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2779 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002780 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002781 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002782 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002783 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002784 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002785 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2786 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002787 case MSR_IA32_TSCDEADLINE:
2788 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2789 break;
Will Auldba904632012-11-29 12:42:50 -08002790 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002791 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002792 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002793 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002794 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002795 }
2796 vcpu->arch.ia32_tsc_adjust_msr = data;
2797 }
2798 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002799 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002800 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2801 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2802 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2803 return 1;
2804 vcpu->arch.ia32_misc_enable_msr = data;
2805 kvm_update_cpuid(vcpu);
2806 } else {
2807 vcpu->arch.ia32_misc_enable_msr = data;
2808 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002809 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002810 case MSR_IA32_SMBASE:
2811 if (!msr_info->host_initiated)
2812 return 1;
2813 vcpu->arch.smbase = data;
2814 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002815 case MSR_IA32_POWER_CTL:
2816 vcpu->arch.msr_ia32_power_ctl = data;
2817 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002818 case MSR_IA32_TSC:
2819 kvm_write_tsc(vcpu, msr_info);
2820 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002821 case MSR_IA32_XSS:
2822 if (!msr_info->host_initiated &&
2823 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2824 return 1;
2825 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08002826 * KVM supports exposing PT to the guest, but does not support
2827 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
2828 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002829 */
2830 if (data != 0)
2831 return 1;
2832 vcpu->arch.ia32_xss = data;
2833 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002834 case MSR_SMI_COUNT:
2835 if (!msr_info->host_initiated)
2836 return 1;
2837 vcpu->arch.smi_count = data;
2838 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002839 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002840 case MSR_KVM_WALL_CLOCK:
2841 vcpu->kvm->arch.wall_clock = data;
2842 kvm_write_wall_clock(vcpu->kvm, data);
2843 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002844 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002845 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002846 struct kvm_arch *ka = &vcpu->kvm->arch;
2847
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002848 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2849 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2850
2851 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002852 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002853
2854 ka->boot_vcpu_runs_old_kvmclock = tmp;
2855 }
2856
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002857 vcpu->arch.time = data;
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002858 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002859
2860 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002861 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002862 if (!(data & 1))
2863 break;
2864
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002865 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002866 &vcpu->arch.pv_time, data & ~1ULL,
2867 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002868 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002869
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002870 break;
2871 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002872 case MSR_KVM_ASYNC_PF_EN:
2873 if (kvm_pv_enable_async_pf(vcpu, data))
2874 return 1;
2875 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002876 case MSR_KVM_STEAL_TIME:
2877
2878 if (unlikely(!sched_info_on()))
2879 return 1;
2880
2881 if (data & KVM_STEAL_RESERVED_MASK)
2882 return 1;
2883
Glauber Costac9aaa892011-07-11 15:28:14 -04002884 vcpu->arch.st.msr_val = data;
2885
2886 if (!(data & KVM_MSR_ENABLED))
2887 break;
2888
Glauber Costac9aaa892011-07-11 15:28:14 -04002889 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2890
2891 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002892 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002893 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002894 return 1;
2895 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002896
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002897 case MSR_KVM_POLL_CONTROL:
2898 /* only enable bit supported */
2899 if (data & (-1ULL << 1))
2900 return 1;
2901
2902 vcpu->arch.msr_kvm_poll_control = data;
2903 break;
2904
Huang Ying890ca9a2009-05-11 16:48:15 +08002905 case MSR_IA32_MCG_CTL:
2906 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002907 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002908 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002909
Wei Huang6912ac32015-06-12 01:34:56 -04002910 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2911 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2912 pr = true; /* fall through */
2913 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2914 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002915 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002916 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002917
2918 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002919 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2920 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002921 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002922 case MSR_K7_CLK_CTL:
2923 /*
2924 * Ignore all writes to this no longer documented MSR.
2925 * Writes are only relevant for old K7 processors,
2926 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002927 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002928 * affected processor models on the command line, hence
2929 * the need to ignore the workaround.
2930 */
2931 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002932 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002933 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2934 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002935 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002936 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2937 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2938 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002939 return kvm_hv_set_msr_common(vcpu, msr, data,
2940 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002941 case MSR_IA32_BBL_CR_CTL3:
2942 /* Drop writes to this legacy MSR -- see rdmsr
2943 * counterpart for further detail.
2944 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002945 if (report_ignored_msrs)
2946 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2947 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002948 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002949 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002950 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002951 return 1;
2952 vcpu->arch.osvw.length = data;
2953 break;
2954 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002955 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002956 return 1;
2957 vcpu->arch.osvw.status = data;
2958 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002959 case MSR_PLATFORM_INFO:
2960 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002961 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2962 cpuid_fault_enabled(vcpu)))
2963 return 1;
2964 vcpu->arch.msr_platform_info = data;
2965 break;
2966 case MSR_MISC_FEATURES_ENABLES:
2967 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2968 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2969 !supports_cpuid_fault(vcpu)))
2970 return 1;
2971 vcpu->arch.msr_misc_features_enables = data;
2972 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002973 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002974 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2975 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002976 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002977 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002978 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002979 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002980 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002981 return 1;
2982 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002983 if (report_ignored_msrs)
2984 vcpu_unimpl(vcpu,
2985 "ignored wrmsr: 0x%x data 0x%llx\n",
2986 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002987 break;
2988 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002989 }
2990 return 0;
2991}
2992EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2993
Paolo Bonzini44883f02018-07-26 13:01:52 +02002994static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002995{
2996 u64 data;
2997 u64 mcg_cap = vcpu->arch.mcg_cap;
2998 unsigned bank_num = mcg_cap & 0xff;
2999
3000 switch (msr) {
3001 case MSR_IA32_P5_MC_ADDR:
3002 case MSR_IA32_P5_MC_TYPE:
3003 data = 0;
3004 break;
3005 case MSR_IA32_MCG_CAP:
3006 data = vcpu->arch.mcg_cap;
3007 break;
3008 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003009 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003010 return 1;
3011 data = vcpu->arch.mcg_ctl;
3012 break;
3013 case MSR_IA32_MCG_STATUS:
3014 data = vcpu->arch.mcg_status;
3015 break;
3016 default:
3017 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003018 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003019 u32 offset = array_index_nospec(
3020 msr - MSR_IA32_MC0_CTL,
3021 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3022
Huang Ying890ca9a2009-05-11 16:48:15 +08003023 data = vcpu->arch.mce_banks[offset];
3024 break;
3025 }
3026 return 1;
3027 }
3028 *pdata = data;
3029 return 0;
3030}
3031
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003032int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003033{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003034 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003035 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003036 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003037 case MSR_IA32_DEBUGCTLMSR:
3038 case MSR_IA32_LASTBRANCHFROMIP:
3039 case MSR_IA32_LASTBRANCHTOIP:
3040 case MSR_IA32_LASTINTFROMIP:
3041 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303042 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003043 case MSR_K8_TSEG_ADDR:
3044 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003045 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003046 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003047 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003048 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003049 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003050 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003051 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003052 case MSR_F15H_EX_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003053 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003054 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003055 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003056 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3057 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3058 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3059 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003060 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003061 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
3062 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003063 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003064 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003065 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003066 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003067 case MSR_IA32_ARCH_CAPABILITIES:
3068 if (!msr_info->host_initiated &&
3069 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3070 return 1;
3071 msr_info->data = vcpu->arch.arch_capabilities;
3072 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003073 case MSR_IA32_POWER_CTL:
3074 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3075 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003076 case MSR_IA32_TSC:
3077 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3078 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003079 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003080 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003081 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003082 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003083 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003084 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003085 /*
3086 * MSR_EBC_FREQUENCY_ID
3087 * Conservative value valid for even the basic CPU models.
3088 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3089 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3090 * and 266MHz for model 3, or 4. Set Core Clock
3091 * Frequency to System Bus Frequency Ratio to 1 (bits
3092 * 31:24) even though these are only valid for CPU
3093 * models > 2, however guests may end up dividing or
3094 * multiplying by zero otherwise.
3095 */
3096 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003097 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003098 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003099 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003100 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003101 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003102 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003103 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003104 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003105 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003106 break;
Will Auldba904632012-11-29 12:42:50 -08003107 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003108 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003109 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003110 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003111 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003112 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003113 case MSR_IA32_SMBASE:
3114 if (!msr_info->host_initiated)
3115 return 1;
3116 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003117 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003118 case MSR_SMI_COUNT:
3119 msr_info->data = vcpu->arch.smi_count;
3120 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003121 case MSR_IA32_PERF_STATUS:
3122 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003123 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003124 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003125 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003126 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003127 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003128 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003129 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003130 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003131 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003132 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003133 break;
3134 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003135 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003136 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003137 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003138 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003139 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003140 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003141 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003142 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003143 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003144 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003145 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003146 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003147 case MSR_KVM_POLL_CONTROL:
3148 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3149 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003150 case MSR_IA32_P5_MC_ADDR:
3151 case MSR_IA32_P5_MC_TYPE:
3152 case MSR_IA32_MCG_CAP:
3153 case MSR_IA32_MCG_CTL:
3154 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003155 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003156 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3157 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003158 case MSR_IA32_XSS:
3159 if (!msr_info->host_initiated &&
3160 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3161 return 1;
3162 msr_info->data = vcpu->arch.ia32_xss;
3163 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003164 case MSR_K7_CLK_CTL:
3165 /*
3166 * Provide expected ramp-up count for K7. All other
3167 * are set to zero, indicating minimum divisors for
3168 * every field.
3169 *
3170 * This prevents guest kernels on AMD host with CPU
3171 * type 6, model 8 and higher from exploding due to
3172 * the rdmsr failing.
3173 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003174 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003175 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003176 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003177 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3178 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003179 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003180 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3181 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3182 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003183 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003184 msr_info->index, &msr_info->data,
3185 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003186 case MSR_IA32_BBL_CR_CTL3:
3187 /* This legacy MSR exists but isn't fully documented in current
3188 * silicon. It is however accessed by winxp in very narrow
3189 * scenarios where it sets bit #19, itself documented as
3190 * a "reserved" bit. Best effort attempt to source coherent
3191 * read data here should the balance of the register be
3192 * interpreted by the guest:
3193 *
3194 * L2 cache control register 3: 64GB range, 256KB size,
3195 * enabled, latency 0x1, configured
3196 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003197 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003198 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003199 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003200 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003201 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003202 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003203 break;
3204 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003205 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003206 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003207 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003208 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003209 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003210 if (!msr_info->host_initiated &&
3211 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3212 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003213 msr_info->data = vcpu->arch.msr_platform_info;
3214 break;
3215 case MSR_MISC_FEATURES_ENABLES:
3216 msr_info->data = vcpu->arch.msr_misc_features_enables;
3217 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003218 case MSR_K7_HWCR:
3219 msr_info->data = vcpu->arch.msr_hwcr;
3220 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003221 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003222 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003223 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003224 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003225 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3226 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003227 return 1;
3228 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003229 if (report_ignored_msrs)
3230 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3231 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003232 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003233 }
3234 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003235 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003236 return 0;
3237}
3238EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3239
Carsten Otte313a3dc2007-10-11 19:16:52 +02003240/*
3241 * Read or write a bunch of msrs. All parameters are kernel addresses.
3242 *
3243 * @return number of msrs set successfully.
3244 */
3245static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3246 struct kvm_msr_entry *entries,
3247 int (*do_msr)(struct kvm_vcpu *vcpu,
3248 unsigned index, u64 *data))
3249{
Tom Lendacky801e4592018-02-21 13:39:51 -06003250 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003251
Carsten Otte313a3dc2007-10-11 19:16:52 +02003252 for (i = 0; i < msrs->nmsrs; ++i)
3253 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3254 break;
3255
Carsten Otte313a3dc2007-10-11 19:16:52 +02003256 return i;
3257}
3258
3259/*
3260 * Read or write a bunch of msrs. Parameters are user addresses.
3261 *
3262 * @return number of msrs set successfully.
3263 */
3264static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3265 int (*do_msr)(struct kvm_vcpu *vcpu,
3266 unsigned index, u64 *data),
3267 int writeback)
3268{
3269 struct kvm_msrs msrs;
3270 struct kvm_msr_entry *entries;
3271 int r, n;
3272 unsigned size;
3273
3274 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003275 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003276 goto out;
3277
3278 r = -E2BIG;
3279 if (msrs.nmsrs >= MAX_IO_MSRS)
3280 goto out;
3281
Carsten Otte313a3dc2007-10-11 19:16:52 +02003282 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003283 entries = memdup_user(user_msrs->entries, size);
3284 if (IS_ERR(entries)) {
3285 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003286 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003287 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003288
3289 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3290 if (r < 0)
3291 goto out_free;
3292
3293 r = -EFAULT;
3294 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3295 goto out_free;
3296
3297 r = n;
3298
3299out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003300 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003301out:
3302 return r;
3303}
3304
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003305static inline bool kvm_can_mwait_in_guest(void)
3306{
3307 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003308 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3309 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003310}
3311
Alexander Graf784aa3d2014-07-14 18:27:35 +02003312int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003313{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003314 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003315
3316 switch (ext) {
3317 case KVM_CAP_IRQCHIP:
3318 case KVM_CAP_HLT:
3319 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003320 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003321 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003322 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003323 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003324 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003325 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003326 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003327 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003328 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003329 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003330 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003331 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003332 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003333 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003334 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003335 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003336 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003337 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003338 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003339 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003340 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003341 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003342 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003343 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003344 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003345 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003346 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003347 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003348 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003349 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003350 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003351 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003352 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003353 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003354 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003355 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003356 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003357 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003358 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003359 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003360 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003361 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003362 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003363 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003364 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003365 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003366 case KVM_CAP_EXCEPTION_PAYLOAD:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003367 r = 1;
3368 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003369 case KVM_CAP_SYNC_REGS:
3370 r = KVM_SYNC_X86_VALID_FIELDS;
3371 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003372 case KVM_CAP_ADJUST_CLOCK:
3373 r = KVM_CLOCK_TSC_STABLE;
3374 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003375 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003376 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3377 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003378 if(kvm_can_mwait_in_guest())
3379 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003380 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003381 case KVM_CAP_X86_SMM:
3382 /* SMBASE is usually relocated above 1M on modern chipsets,
3383 * and SMM handlers might indeed rely on 4G segment limits,
3384 * so do not report SMM to be available if real mode is
3385 * emulated via vm86 mode. Still, do not go to great lengths
3386 * to avoid userspace's usage of the feature, because it is a
3387 * fringe case that is not enabled except via specific settings
3388 * of the module parameters.
3389 */
Tom Lendackybc226f02018-05-10 22:06:39 +02003390 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003391 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003392 case KVM_CAP_VAPIC:
3393 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
3394 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003395 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003396 r = KVM_SOFT_MAX_VCPUS;
3397 break;
3398 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003399 r = KVM_MAX_VCPUS;
3400 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003401 case KVM_CAP_MAX_VCPU_ID:
3402 r = KVM_MAX_VCPU_ID;
3403 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003404 case KVM_CAP_PV_MMU: /* obsolete */
3405 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003406 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003407 case KVM_CAP_MCE:
3408 r = KVM_MAX_MCE_BANKS;
3409 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003410 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003411 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003412 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003413 case KVM_CAP_TSC_CONTROL:
3414 r = kvm_has_tsc_control;
3415 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003416 case KVM_CAP_X2APIC_API:
3417 r = KVM_X2APIC_API_VALID_FLAGS;
3418 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003419 case KVM_CAP_NESTED_STATE:
3420 r = kvm_x86_ops->get_nested_state ?
Hariprasad Kelambe43c442019-04-06 15:06:58 +05303421 kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003422 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003423 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003424 r = kvm_x86_ops->enable_direct_tlbflush != NULL;
3425 break;
3426 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
3427 r = kvm_x86_ops->nested_enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003428 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003429 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003430 break;
3431 }
3432 return r;
3433
3434}
3435
Carsten Otte043405e2007-10-10 17:16:19 +02003436long kvm_arch_dev_ioctl(struct file *filp,
3437 unsigned int ioctl, unsigned long arg)
3438{
3439 void __user *argp = (void __user *)arg;
3440 long r;
3441
3442 switch (ioctl) {
3443 case KVM_GET_MSR_INDEX_LIST: {
3444 struct kvm_msr_list __user *user_msr_list = argp;
3445 struct kvm_msr_list msr_list;
3446 unsigned n;
3447
3448 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003449 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003450 goto out;
3451 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003452 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003453 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003454 goto out;
3455 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003456 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003457 goto out;
3458 r = -EFAULT;
3459 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3460 num_msrs_to_save * sizeof(u32)))
3461 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003462 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003463 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003464 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003465 goto out;
3466 r = 0;
3467 break;
3468 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003469 case KVM_GET_SUPPORTED_CPUID:
3470 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003471 struct kvm_cpuid2 __user *cpuid_arg = argp;
3472 struct kvm_cpuid2 cpuid;
3473
3474 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003475 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003476 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003477
3478 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3479 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003480 if (r)
3481 goto out;
3482
3483 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003484 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003485 goto out;
3486 r = 0;
3487 break;
3488 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003489 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
Huang Ying890ca9a2009-05-11 16:48:15 +08003490 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003491 if (copy_to_user(argp, &kvm_mce_cap_supported,
3492 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003493 goto out;
3494 r = 0;
3495 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003496 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3497 struct kvm_msr_list __user *user_msr_list = argp;
3498 struct kvm_msr_list msr_list;
3499 unsigned int n;
3500
3501 r = -EFAULT;
3502 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3503 goto out;
3504 n = msr_list.nmsrs;
3505 msr_list.nmsrs = num_msr_based_features;
3506 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3507 goto out;
3508 r = -E2BIG;
3509 if (n < msr_list.nmsrs)
3510 goto out;
3511 r = -EFAULT;
3512 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3513 num_msr_based_features * sizeof(u32)))
3514 goto out;
3515 r = 0;
3516 break;
3517 }
3518 case KVM_GET_MSRS:
3519 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3520 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003521 }
Carsten Otte043405e2007-10-10 17:16:19 +02003522 default:
3523 r = -EINVAL;
3524 }
3525out:
3526 return r;
3527}
3528
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003529static void wbinvd_ipi(void *garbage)
3530{
3531 wbinvd();
3532}
3533
3534static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3535{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003536 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003537}
3538
Carsten Otte313a3dc2007-10-11 19:16:52 +02003539void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3540{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003541 /* Address WBINVD may be executed by guest */
3542 if (need_emulate_wbinvd(vcpu)) {
3543 if (kvm_x86_ops->has_wbinvd_exit())
3544 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3545 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3546 smp_call_function_single(vcpu->cpu,
3547 wbinvd_ipi, NULL, 1);
3548 }
3549
Carsten Otte313a3dc2007-10-11 19:16:52 +02003550 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003551
3552 /* Apply any externally detected TSC adjustments (due to suspend) */
3553 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3554 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3555 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003556 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003557 }
3558
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003559 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003560 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003561 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003562 if (tsc_delta < 0)
3563 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003564
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003565 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003566 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003567 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003568 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003569 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003570 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003571
3572 if (kvm_lapic_hv_timer_in_use(vcpu))
3573 kvm_lapic_restart_hv_timer(vcpu);
3574
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003575 /*
3576 * On a host with synchronized TSC, there is no need to update
3577 * kvmclock on vcpu->cpu migration
3578 */
3579 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03003580 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003581 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003582 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003583 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003584 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003585
Glauber Costac9aaa892011-07-11 15:28:14 -04003586 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003587}
3588
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003589static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3590{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003591 struct kvm_host_map map;
3592 struct kvm_steal_time *st;
3593
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003594 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3595 return;
3596
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003597 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003598 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003599
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003600 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3601 &vcpu->arch.st.cache, true))
3602 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003603
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003604 st = map.hva +
3605 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3606
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003607 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003608
3609 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003610}
3611
Carsten Otte313a3dc2007-10-11 19:16:52 +02003612void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3613{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003614 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003615
3616 if (vcpu->preempted)
3617 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3618
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003619 /*
3620 * Disable page faults because we're in atomic context here.
3621 * kvm_write_guest_offset_cached() would call might_fault()
3622 * that relies on pagefault_disable() to tell if there's a
3623 * bug. NOTE: the write to guest memory may not go through if
3624 * during postcopy live migration or if there's heavy guest
3625 * paging.
3626 */
3627 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003628 /*
3629 * kvm_memslots() will be called by
3630 * kvm_write_guest_offset_cached() so take the srcu lock.
3631 */
3632 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003633 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003634 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003635 pagefault_enable();
Avi Kivity02daab22009-12-30 12:40:26 +02003636 kvm_x86_ops->vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003637 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003638 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003639 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3640 * on every vmexit, but if not, we might have a stale dr6 from the
3641 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003642 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003643 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003644}
3645
Carsten Otte313a3dc2007-10-11 19:16:52 +02003646static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3647 struct kvm_lapic_state *s)
3648{
Liran Alonfa59cc02017-12-24 18:12:53 +02003649 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03003650 kvm_x86_ops->sync_pir_to_irr(vcpu);
3651
Radim Krčmářa92e2542016-07-12 22:09:22 +02003652 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003653}
3654
3655static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3656 struct kvm_lapic_state *s)
3657{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003658 int r;
3659
3660 r = kvm_apic_set_state(vcpu, s);
3661 if (r)
3662 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003663 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003664
3665 return 0;
3666}
3667
Matt Gingell127a4572015-11-17 17:32:05 +01003668static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3669{
3670 return (!lapic_in_kernel(vcpu) ||
3671 kvm_apic_accept_pic_intr(vcpu));
3672}
3673
Matt Gingell782d4222015-11-16 15:26:00 -08003674/*
3675 * if userspace requested an interrupt window, check that the
3676 * interrupt window is open.
3677 *
3678 * No need to exit to userspace if we already have an interrupt queued.
3679 */
3680static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3681{
3682 return kvm_arch_interrupt_allowed(vcpu) &&
3683 !kvm_cpu_has_interrupt(vcpu) &&
3684 !kvm_event_needs_reinjection(vcpu) &&
3685 kvm_cpu_accept_dm_intr(vcpu);
3686}
3687
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003688static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3689 struct kvm_interrupt *irq)
3690{
Chen Gang02cdb502013-02-27 11:33:25 +08003691 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003692 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003693
3694 if (!irqchip_in_kernel(vcpu->kvm)) {
3695 kvm_queue_interrupt(vcpu, irq->irq, false);
3696 kvm_make_request(KVM_REQ_EVENT, vcpu);
3697 return 0;
3698 }
3699
3700 /*
3701 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3702 * fail for in-kernel 8259.
3703 */
3704 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003705 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003706
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003707 if (vcpu->arch.pending_external_vector != -1)
3708 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003709
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003710 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003711 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003712 return 0;
3713}
3714
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003715static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3716{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003717 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003718
3719 return 0;
3720}
3721
Paolo Bonzinif0778252015-04-01 15:06:40 +02003722static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3723{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003724 kvm_make_request(KVM_REQ_SMI, vcpu);
3725
Paolo Bonzinif0778252015-04-01 15:06:40 +02003726 return 0;
3727}
3728
Avi Kivityb209749f2007-10-22 16:50:39 +02003729static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3730 struct kvm_tpr_access_ctl *tac)
3731{
3732 if (tac->flags)
3733 return -EINVAL;
3734 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3735 return 0;
3736}
3737
Huang Ying890ca9a2009-05-11 16:48:15 +08003738static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3739 u64 mcg_cap)
3740{
3741 int r;
3742 unsigned bank_num = mcg_cap & 0xff, bank;
3743
3744 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003745 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003746 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003747 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003748 goto out;
3749 r = 0;
3750 vcpu->arch.mcg_cap = mcg_cap;
3751 /* Init IA32_MCG_CTL to all 1s */
3752 if (mcg_cap & MCG_CTL_P)
3753 vcpu->arch.mcg_ctl = ~(u64)0;
3754 /* Init IA32_MCi_CTL to all 1s */
3755 for (bank = 0; bank < bank_num; bank++)
3756 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003757
Sean Christopherson92735b12019-08-02 15:06:17 -07003758 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003759out:
3760 return r;
3761}
3762
3763static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3764 struct kvm_x86_mce *mce)
3765{
3766 u64 mcg_cap = vcpu->arch.mcg_cap;
3767 unsigned bank_num = mcg_cap & 0xff;
3768 u64 *banks = vcpu->arch.mce_banks;
3769
3770 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3771 return -EINVAL;
3772 /*
3773 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3774 * reporting is disabled
3775 */
3776 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3777 vcpu->arch.mcg_ctl != ~(u64)0)
3778 return 0;
3779 banks += 4 * mce->bank;
3780 /*
3781 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3782 * reporting is disabled for the bank
3783 */
3784 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3785 return 0;
3786 if (mce->status & MCI_STATUS_UC) {
3787 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003788 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003789 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003790 return 0;
3791 }
3792 if (banks[1] & MCI_STATUS_VAL)
3793 mce->status |= MCI_STATUS_OVER;
3794 banks[2] = mce->addr;
3795 banks[3] = mce->misc;
3796 vcpu->arch.mcg_status = mce->mcg_status;
3797 banks[1] = mce->status;
3798 kvm_queue_exception(vcpu, MC_VECTOR);
3799 } else if (!(banks[1] & MCI_STATUS_VAL)
3800 || !(banks[1] & MCI_STATUS_UC)) {
3801 if (banks[1] & MCI_STATUS_VAL)
3802 mce->status |= MCI_STATUS_OVER;
3803 banks[2] = mce->addr;
3804 banks[3] = mce->misc;
3805 banks[1] = mce->status;
3806 } else
3807 banks[1] |= MCI_STATUS_OVER;
3808 return 0;
3809}
3810
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003811static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3812 struct kvm_vcpu_events *events)
3813{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003814 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003815
Wanpeng Li664f8e22017-08-24 03:35:09 -07003816 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003817 * In guest mode, payload delivery should be deferred,
3818 * so that the L1 hypervisor can intercept #PF before
3819 * CR2 is modified (or intercept #DB before DR6 is
3820 * modified under nVMX). Unless the per-VM capability,
3821 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3822 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3823 * opportunistically defer the exception payload, deliver it if the
3824 * capability hasn't been requested before processing a
3825 * KVM_GET_VCPU_EVENTS.
3826 */
3827 if (!vcpu->kvm->arch.exception_payload_enabled &&
3828 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3829 kvm_deliver_exception_payload(vcpu);
3830
3831 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003832 * The API doesn't provide the instruction length for software
3833 * exceptions, so don't report them. As long as the guest RIP
3834 * isn't advanced, we should expect to encounter the exception
3835 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003836 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003837 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3838 events->exception.injected = 0;
3839 events->exception.pending = 0;
3840 } else {
3841 events->exception.injected = vcpu->arch.exception.injected;
3842 events->exception.pending = vcpu->arch.exception.pending;
3843 /*
3844 * For ABI compatibility, deliberately conflate
3845 * pending and injected exceptions when
3846 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3847 */
3848 if (!vcpu->kvm->arch.exception_payload_enabled)
3849 events->exception.injected |=
3850 vcpu->arch.exception.pending;
3851 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003852 events->exception.nr = vcpu->arch.exception.nr;
3853 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3854 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003855 events->exception_has_payload = vcpu->arch.exception.has_payload;
3856 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003857
Jan Kiszka03b82a32010-02-15 10:45:41 +01003858 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003859 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003860 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003861 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003862 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003863
3864 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003865 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003866 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003867 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003868
Jan Kiszka66450a22013-03-13 12:42:34 +01003869 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003870
Paolo Bonzinif0778252015-04-01 15:06:40 +02003871 events->smi.smm = is_smm(vcpu);
3872 events->smi.pending = vcpu->arch.smi_pending;
3873 events->smi.smm_inside_nmi =
3874 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3875 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3876
Jan Kiszkadab4b912009-12-06 18:24:15 +01003877 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003878 | KVM_VCPUEVENT_VALID_SHADOW
3879 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003880 if (vcpu->kvm->arch.exception_payload_enabled)
3881 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3882
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003883 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003884}
3885
Sean Christophersonc5833c72019-04-02 08:03:10 -07003886static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003887
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003888static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3889 struct kvm_vcpu_events *events)
3890{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003891 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003892 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003893 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003894 | KVM_VCPUEVENT_VALID_SMM
3895 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003896 return -EINVAL;
3897
Jim Mattson59073aa2018-10-16 14:29:20 -07003898 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3899 if (!vcpu->kvm->arch.exception_payload_enabled)
3900 return -EINVAL;
3901 if (events->exception.pending)
3902 events->exception.injected = 0;
3903 else
3904 events->exception_has_payload = 0;
3905 } else {
3906 events->exception.pending = 0;
3907 events->exception_has_payload = 0;
3908 }
3909
3910 if ((events->exception.injected || events->exception.pending) &&
3911 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003912 return -EINVAL;
3913
David Hildenbrand28bf2882017-03-23 11:46:03 +01003914 /* INITs are latched while in SMM */
3915 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3916 (events->smi.smm || events->smi.pending) &&
3917 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3918 return -EINVAL;
3919
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003920 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003921 vcpu->arch.exception.injected = events->exception.injected;
3922 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003923 vcpu->arch.exception.nr = events->exception.nr;
3924 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3925 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003926 vcpu->arch.exception.has_payload = events->exception_has_payload;
3927 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003928
Liran Alon04140b42018-03-23 03:01:31 +03003929 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003930 vcpu->arch.interrupt.nr = events->interrupt.nr;
3931 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003932 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3933 kvm_x86_ops->set_interrupt_shadow(vcpu,
3934 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003935
3936 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003937 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3938 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003939 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3940
Jan Kiszka66450a22013-03-13 12:42:34 +01003941 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003942 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003943 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003944
Paolo Bonzinif0778252015-04-01 15:06:40 +02003945 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003946 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3947 if (events->smi.smm)
3948 vcpu->arch.hflags |= HF_SMM_MASK;
3949 else
3950 vcpu->arch.hflags &= ~HF_SMM_MASK;
3951 kvm_smm_changed(vcpu);
3952 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003953
Paolo Bonzinif0778252015-04-01 15:06:40 +02003954 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003955
3956 if (events->smi.smm) {
3957 if (events->smi.smm_inside_nmi)
3958 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003959 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003960 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02003961 }
3962
3963 if (lapic_in_kernel(vcpu)) {
3964 if (events->smi.latched_init)
3965 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3966 else
3967 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02003968 }
3969 }
3970
Avi Kivity3842d132010-07-27 12:30:24 +03003971 kvm_make_request(KVM_REQ_EVENT, vcpu);
3972
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003973 return 0;
3974}
3975
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003976static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3977 struct kvm_debugregs *dbgregs)
3978{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003979 unsigned long val;
3980
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003981 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003982 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003983 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003984 dbgregs->dr7 = vcpu->arch.dr7;
3985 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003986 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003987}
3988
3989static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3990 struct kvm_debugregs *dbgregs)
3991{
3992 if (dbgregs->flags)
3993 return -EINVAL;
3994
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02003995 if (dbgregs->dr6 & ~0xffffffffull)
3996 return -EINVAL;
3997 if (dbgregs->dr7 & ~0xffffffffull)
3998 return -EINVAL;
3999
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004000 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004001 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004002 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004003 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004004 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004005 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004006
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004007 return 0;
4008}
4009
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004010#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4011
4012static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4013{
Marc Orrb666a4b2018-11-06 14:53:56 -08004014 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004015 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004016 u64 valid;
4017
4018 /*
4019 * Copy legacy XSAVE area, to avoid complications with CPUID
4020 * leaves 0 and 1 in the loop below.
4021 */
4022 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4023
4024 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004025 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004026 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4027
4028 /*
4029 * Copy each region from the possibly compacted offset to the
4030 * non-compacted offset.
4031 */
Dave Hansend91cab72015-09-02 16:31:26 -07004032 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004033 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004034 u64 xfeature_mask = valid & -valid;
4035 int xfeature_nr = fls64(xfeature_mask) - 1;
4036 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004037
4038 if (src) {
4039 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004040 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004041 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004042 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004043 memcpy(dest + offset, &vcpu->arch.pkru,
4044 sizeof(vcpu->arch.pkru));
4045 else
4046 memcpy(dest + offset, src, size);
4047
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004048 }
4049
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004050 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004051 }
4052}
4053
4054static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4055{
Marc Orrb666a4b2018-11-06 14:53:56 -08004056 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004057 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4058 u64 valid;
4059
4060 /*
4061 * Copy legacy XSAVE area, to avoid complications with CPUID
4062 * leaves 0 and 1 in the loop below.
4063 */
4064 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4065
4066 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004067 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004068 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004069 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004070
4071 /*
4072 * Copy each region from the non-compacted offset to the
4073 * possibly compacted offset.
4074 */
Dave Hansend91cab72015-09-02 16:31:26 -07004075 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004076 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004077 u64 xfeature_mask = valid & -valid;
4078 int xfeature_nr = fls64(xfeature_mask) - 1;
4079 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004080
4081 if (dest) {
4082 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004083 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004084 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004085 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004086 memcpy(&vcpu->arch.pkru, src + offset,
4087 sizeof(vcpu->arch.pkru));
4088 else
4089 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004090 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004091
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004092 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004093 }
4094}
4095
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004096static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4097 struct kvm_xsave *guest_xsave)
4098{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004099 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004100 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4101 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004102 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004103 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004104 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004105 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004106 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004107 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004108 }
4109}
4110
Wanpeng Lia5758132017-05-11 02:58:55 -07004111#define XSAVE_MXCSR_OFFSET 24
4112
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004113static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4114 struct kvm_xsave *guest_xsave)
4115{
4116 u64 xstate_bv =
4117 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004118 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004119
Borislav Petkovd366bf72016-04-04 22:25:02 +02004120 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004121 /*
4122 * Here we allow setting states that are not present in
4123 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4124 * with old userspace.
4125 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004126 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004127 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004128 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004129 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004130 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4131 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004132 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004133 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004134 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004135 }
4136 return 0;
4137}
4138
4139static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4140 struct kvm_xcrs *guest_xcrs)
4141{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004142 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004143 guest_xcrs->nr_xcrs = 0;
4144 return;
4145 }
4146
4147 guest_xcrs->nr_xcrs = 1;
4148 guest_xcrs->flags = 0;
4149 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4150 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4151}
4152
4153static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4154 struct kvm_xcrs *guest_xcrs)
4155{
4156 int i, r = 0;
4157
Borislav Petkovd366bf72016-04-04 22:25:02 +02004158 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004159 return -EINVAL;
4160
4161 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4162 return -EINVAL;
4163
4164 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4165 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004166 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004167 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004168 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004169 break;
4170 }
4171 if (r)
4172 r = -EINVAL;
4173 return r;
4174}
4175
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004176/*
4177 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4178 * stopped by the hypervisor. This function will be called from the host only.
4179 * EINVAL is returned when the host attempts to set the flag for a guest that
4180 * does not support pv clocks.
4181 */
4182static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4183{
Andy Honig0b794592013-02-20 14:48:10 -08004184 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004185 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004186 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004187 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4188 return 0;
4189}
4190
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004191static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4192 struct kvm_enable_cap *cap)
4193{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004194 int r;
4195 uint16_t vmcs_version;
4196 void __user *user_ptr;
4197
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004198 if (cap->flags)
4199 return -EINVAL;
4200
4201 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004202 case KVM_CAP_HYPERV_SYNIC2:
4203 if (cap->args[0])
4204 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004205 /* fall through */
4206
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004207 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004208 if (!irqchip_in_kernel(vcpu->kvm))
4209 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004210 return kvm_hv_activate_synic(vcpu, cap->cap ==
4211 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004212 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christopherson51589172018-12-03 13:53:10 -08004213 if (!kvm_x86_ops->nested_enable_evmcs)
4214 return -ENOTTY;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004215 r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version);
4216 if (!r) {
4217 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4218 if (copy_to_user(user_ptr, &vmcs_version,
4219 sizeof(vmcs_version)))
4220 r = -EFAULT;
4221 }
4222 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004223 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
4224 if (!kvm_x86_ops->enable_direct_tlbflush)
4225 return -ENOTTY;
4226
4227 return kvm_x86_ops->enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004228
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004229 default:
4230 return -EINVAL;
4231 }
4232}
4233
Carsten Otte313a3dc2007-10-11 19:16:52 +02004234long kvm_arch_vcpu_ioctl(struct file *filp,
4235 unsigned int ioctl, unsigned long arg)
4236{
4237 struct kvm_vcpu *vcpu = filp->private_data;
4238 void __user *argp = (void __user *)arg;
4239 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004240 union {
4241 struct kvm_lapic_state *lapic;
4242 struct kvm_xsave *xsave;
4243 struct kvm_xcrs *xcrs;
4244 void *buffer;
4245 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004246
Christoffer Dall9b0624712017-12-04 21:35:36 +01004247 vcpu_load(vcpu);
4248
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004249 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004250 switch (ioctl) {
4251 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004252 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004253 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004254 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004255 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4256 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004257
Dave Hansenb772ff32008-08-11 10:01:47 -07004258 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004259 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004260 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004261 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004262 if (r)
4263 goto out;
4264 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004265 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004266 goto out;
4267 r = 0;
4268 break;
4269 }
4270 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004271 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004272 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004273 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004274 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004275 if (IS_ERR(u.lapic)) {
4276 r = PTR_ERR(u.lapic);
4277 goto out_nofree;
4278 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004279
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004280 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004281 break;
4282 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004283 case KVM_INTERRUPT: {
4284 struct kvm_interrupt irq;
4285
4286 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004287 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004288 goto out;
4289 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004290 break;
4291 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004292 case KVM_NMI: {
4293 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004294 break;
4295 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004296 case KVM_SMI: {
4297 r = kvm_vcpu_ioctl_smi(vcpu);
4298 break;
4299 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004300 case KVM_SET_CPUID: {
4301 struct kvm_cpuid __user *cpuid_arg = argp;
4302 struct kvm_cpuid cpuid;
4303
4304 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004305 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004306 goto out;
4307 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004308 break;
4309 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004310 case KVM_SET_CPUID2: {
4311 struct kvm_cpuid2 __user *cpuid_arg = argp;
4312 struct kvm_cpuid2 cpuid;
4313
4314 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004315 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004316 goto out;
4317 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004318 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004319 break;
4320 }
4321 case KVM_GET_CPUID2: {
4322 struct kvm_cpuid2 __user *cpuid_arg = argp;
4323 struct kvm_cpuid2 cpuid;
4324
4325 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004326 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004327 goto out;
4328 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004329 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004330 if (r)
4331 goto out;
4332 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004333 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004334 goto out;
4335 r = 0;
4336 break;
4337 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004338 case KVM_GET_MSRS: {
4339 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004340 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004341 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004342 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004343 }
4344 case KVM_SET_MSRS: {
4345 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004346 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004347 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004348 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004349 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004350 case KVM_TPR_ACCESS_REPORTING: {
4351 struct kvm_tpr_access_ctl tac;
4352
4353 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004354 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004355 goto out;
4356 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4357 if (r)
4358 goto out;
4359 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004360 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004361 goto out;
4362 r = 0;
4363 break;
4364 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004365 case KVM_SET_VAPIC_ADDR: {
4366 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004367 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004368
4369 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004370 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004371 goto out;
4372 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004373 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004374 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004375 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004376 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004377 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004378 break;
4379 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004380 case KVM_X86_SETUP_MCE: {
4381 u64 mcg_cap;
4382
4383 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004384 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004385 goto out;
4386 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4387 break;
4388 }
4389 case KVM_X86_SET_MCE: {
4390 struct kvm_x86_mce mce;
4391
4392 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004393 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004394 goto out;
4395 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4396 break;
4397 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004398 case KVM_GET_VCPU_EVENTS: {
4399 struct kvm_vcpu_events events;
4400
4401 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4402
4403 r = -EFAULT;
4404 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4405 break;
4406 r = 0;
4407 break;
4408 }
4409 case KVM_SET_VCPU_EVENTS: {
4410 struct kvm_vcpu_events events;
4411
4412 r = -EFAULT;
4413 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4414 break;
4415
4416 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4417 break;
4418 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004419 case KVM_GET_DEBUGREGS: {
4420 struct kvm_debugregs dbgregs;
4421
4422 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4423
4424 r = -EFAULT;
4425 if (copy_to_user(argp, &dbgregs,
4426 sizeof(struct kvm_debugregs)))
4427 break;
4428 r = 0;
4429 break;
4430 }
4431 case KVM_SET_DEBUGREGS: {
4432 struct kvm_debugregs dbgregs;
4433
4434 r = -EFAULT;
4435 if (copy_from_user(&dbgregs, argp,
4436 sizeof(struct kvm_debugregs)))
4437 break;
4438
4439 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4440 break;
4441 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004442 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004443 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004444 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004445 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004446 break;
4447
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004448 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004449
4450 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004451 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004452 break;
4453 r = 0;
4454 break;
4455 }
4456 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004457 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004458 if (IS_ERR(u.xsave)) {
4459 r = PTR_ERR(u.xsave);
4460 goto out_nofree;
4461 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004462
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004463 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004464 break;
4465 }
4466 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004467 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004468 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004469 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004470 break;
4471
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004472 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004473
4474 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004475 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004476 sizeof(struct kvm_xcrs)))
4477 break;
4478 r = 0;
4479 break;
4480 }
4481 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004482 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004483 if (IS_ERR(u.xcrs)) {
4484 r = PTR_ERR(u.xcrs);
4485 goto out_nofree;
4486 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004487
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004488 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004489 break;
4490 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004491 case KVM_SET_TSC_KHZ: {
4492 u32 user_tsc_khz;
4493
4494 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004495 user_tsc_khz = (u32)arg;
4496
4497 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4498 goto out;
4499
Zachary Amsdencc578282012-02-03 15:43:50 -02004500 if (user_tsc_khz == 0)
4501 user_tsc_khz = tsc_khz;
4502
Haozhong Zhang381d5852015-10-20 15:39:04 +08004503 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4504 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004505
Joerg Roedel92a1f122011-03-25 09:44:51 +01004506 goto out;
4507 }
4508 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004509 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004510 goto out;
4511 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004512 case KVM_KVMCLOCK_CTRL: {
4513 r = kvm_set_guest_paused(vcpu);
4514 goto out;
4515 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004516 case KVM_ENABLE_CAP: {
4517 struct kvm_enable_cap cap;
4518
4519 r = -EFAULT;
4520 if (copy_from_user(&cap, argp, sizeof(cap)))
4521 goto out;
4522 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4523 break;
4524 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004525 case KVM_GET_NESTED_STATE: {
4526 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4527 u32 user_data_size;
4528
4529 r = -EINVAL;
4530 if (!kvm_x86_ops->get_nested_state)
4531 break;
4532
4533 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004534 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004535 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004536 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004537
4538 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4539 user_data_size);
4540 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004541 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004542
4543 if (r > user_data_size) {
4544 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004545 r = -EFAULT;
4546 else
4547 r = -E2BIG;
4548 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004549 }
Liran Alon26b471c2018-09-16 14:28:20 +03004550
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004551 r = 0;
4552 break;
4553 }
4554 case KVM_SET_NESTED_STATE: {
4555 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4556 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004557 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004558
4559 r = -EINVAL;
4560 if (!kvm_x86_ops->set_nested_state)
4561 break;
4562
Liran Alon26b471c2018-09-16 14:28:20 +03004563 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004564 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004565 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004566
Liran Alon26b471c2018-09-16 14:28:20 +03004567 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004568 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004569 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004570
4571 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004572 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4573 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004574 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004575
4576 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004577 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4578 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004579 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004580
Sean Christophersonad5996d2019-11-22 08:58:18 -08004581 idx = srcu_read_lock(&vcpu->kvm->srcu);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004582 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004583 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004584 break;
4585 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004586 case KVM_GET_SUPPORTED_HV_CPUID: {
4587 struct kvm_cpuid2 __user *cpuid_arg = argp;
4588 struct kvm_cpuid2 cpuid;
4589
4590 r = -EFAULT;
4591 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4592 goto out;
4593
4594 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4595 cpuid_arg->entries);
4596 if (r)
4597 goto out;
4598
4599 r = -EFAULT;
4600 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4601 goto out;
4602 r = 0;
4603 break;
4604 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004605 default:
4606 r = -EINVAL;
4607 }
4608out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004609 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004610out_nofree:
4611 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004612 return r;
4613}
4614
Souptick Joarder1499fa82018-04-19 00:49:58 +05304615vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004616{
4617 return VM_FAULT_SIGBUS;
4618}
4619
Carsten Otte1fe779f2007-10-29 16:08:35 +01004620static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4621{
4622 int ret;
4623
4624 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004625 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004626 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4627 return ret;
4628}
4629
Sheng Yangb927a3c2009-07-21 10:42:48 +08004630static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4631 u64 ident_addr)
4632{
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004633 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004634}
4635
Carsten Otte1fe779f2007-10-29 16:08:35 +01004636static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004637 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004638{
4639 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4640 return -EINVAL;
4641
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004642 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004643
4644 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004645 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004646
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004647 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004648 return 0;
4649}
4650
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004651static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004652{
Dave Hansen39de71e2010-08-19 18:11:14 -07004653 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004654}
4655
Carsten Otte1fe779f2007-10-29 16:08:35 +01004656static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4657{
David Hildenbrand90bca052017-04-07 10:50:23 +02004658 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004659 int r;
4660
4661 r = 0;
4662 switch (chip->chip_id) {
4663 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004664 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004665 sizeof(struct kvm_pic_state));
4666 break;
4667 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004668 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004669 sizeof(struct kvm_pic_state));
4670 break;
4671 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004672 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004673 break;
4674 default:
4675 r = -EINVAL;
4676 break;
4677 }
4678 return r;
4679}
4680
4681static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4682{
David Hildenbrand90bca052017-04-07 10:50:23 +02004683 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004684 int r;
4685
4686 r = 0;
4687 switch (chip->chip_id) {
4688 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004689 spin_lock(&pic->lock);
4690 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004691 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004692 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004693 break;
4694 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004695 spin_lock(&pic->lock);
4696 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004697 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004698 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004699 break;
4700 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004701 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004702 break;
4703 default:
4704 r = -EINVAL;
4705 break;
4706 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004707 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004708 return r;
4709}
4710
Sheng Yange0f63cb2008-03-04 00:50:59 +08004711static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4712{
Radim Krčmář34f39412016-03-02 22:56:50 +01004713 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4714
4715 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4716
4717 mutex_lock(&kps->lock);
4718 memcpy(ps, &kps->channels, sizeof(*ps));
4719 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304720 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004721}
4722
4723static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4724{
Andrew Honig01856042015-11-18 14:50:23 -08004725 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004726 struct kvm_pit *pit = kvm->arch.vpit;
4727
4728 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004729 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004730 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004731 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4732 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304733 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004734}
4735
4736static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4737{
Beth Kone9f42752009-07-07 11:50:38 -04004738 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4739 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4740 sizeof(ps->channels));
4741 ps->flags = kvm->arch.vpit->pit_state.flags;
4742 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004743 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304744 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004745}
4746
4747static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4748{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304749 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004750 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004751 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004752 struct kvm_pit *pit = kvm->arch.vpit;
4753
4754 mutex_lock(&pit->pit_state.lock);
4755 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004756 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4757 if (!prev_legacy && cur_legacy)
4758 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004759 memcpy(&pit->pit_state.channels, &ps->channels,
4760 sizeof(pit->pit_state.channels));
4761 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004762 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004763 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004764 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004765 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304766 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004767}
4768
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004769static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4770 struct kvm_reinject_control *control)
4771{
Radim Krčmář71474e22016-03-02 22:56:45 +01004772 struct kvm_pit *pit = kvm->arch.vpit;
4773
Radim Krčmář71474e22016-03-02 22:56:45 +01004774 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4775 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4776 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4777 */
4778 mutex_lock(&pit->pit_state.lock);
4779 kvm_pit_set_reinject(pit, control->pit_reinject);
4780 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004781
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004782 return 0;
4783}
4784
Sean Christopherson0dff0842020-02-18 13:07:29 -08004785void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004786{
Kai Huang88178fd2015-01-28 10:54:27 +08004787 /*
4788 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4789 */
4790 if (kvm_x86_ops->flush_log_dirty)
4791 kvm_x86_ops->flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004792}
4793
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004794int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4795 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004796{
4797 if (!irqchip_in_kernel(kvm))
4798 return -ENXIO;
4799
4800 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004801 irq_event->irq, irq_event->level,
4802 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004803 return 0;
4804}
4805
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004806int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4807 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004808{
4809 int r;
4810
4811 if (cap->flags)
4812 return -EINVAL;
4813
4814 switch (cap->cap) {
4815 case KVM_CAP_DISABLE_QUIRKS:
4816 kvm->arch.disabled_quirks = cap->args[0];
4817 r = 0;
4818 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004819 case KVM_CAP_SPLIT_IRQCHIP: {
4820 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004821 r = -EINVAL;
4822 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4823 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004824 r = -EEXIST;
4825 if (irqchip_in_kernel(kvm))
4826 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004827 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004828 goto split_irqchip_unlock;
4829 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004830 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004831 goto split_irqchip_unlock;
4832 /* Pairs with irqchip_in_kernel. */
4833 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004834 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004835 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004836 r = 0;
4837split_irqchip_unlock:
4838 mutex_unlock(&kvm->lock);
4839 break;
4840 }
Radim Krčmář371313132016-07-12 22:09:27 +02004841 case KVM_CAP_X2APIC_API:
4842 r = -EINVAL;
4843 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4844 break;
4845
4846 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4847 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004848 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4849 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004850
4851 r = 0;
4852 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004853 case KVM_CAP_X86_DISABLE_EXITS:
4854 r = -EINVAL;
4855 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4856 break;
4857
4858 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4859 kvm_can_mwait_in_guest())
4860 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004861 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004862 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004863 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4864 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004865 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4866 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004867 r = 0;
4868 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004869 case KVM_CAP_MSR_PLATFORM_INFO:
4870 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4871 r = 0;
4872 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004873 case KVM_CAP_EXCEPTION_PAYLOAD:
4874 kvm->arch.exception_payload_enabled = cap->args[0];
4875 r = 0;
4876 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004877 default:
4878 r = -EINVAL;
4879 break;
4880 }
4881 return r;
4882}
4883
Carsten Otte1fe779f2007-10-29 16:08:35 +01004884long kvm_arch_vm_ioctl(struct file *filp,
4885 unsigned int ioctl, unsigned long arg)
4886{
4887 struct kvm *kvm = filp->private_data;
4888 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004889 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004890 /*
4891 * This union makes it completely explicit to gcc-3.x
4892 * that these two variables' stack usage should be
4893 * combined, not added together.
4894 */
4895 union {
4896 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004897 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004898 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004899 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004900
4901 switch (ioctl) {
4902 case KVM_SET_TSS_ADDR:
4903 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004904 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004905 case KVM_SET_IDENTITY_MAP_ADDR: {
4906 u64 ident_addr;
4907
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004908 mutex_lock(&kvm->lock);
4909 r = -EINVAL;
4910 if (kvm->created_vcpus)
4911 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004912 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004913 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004914 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004915 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004916set_identity_unlock:
4917 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004918 break;
4919 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004920 case KVM_SET_NR_MMU_PAGES:
4921 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004922 break;
4923 case KVM_GET_NR_MMU_PAGES:
4924 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4925 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004926 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004927 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004928
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004929 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004930 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004931 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004932
Avi Kivity3e515702012-03-05 14:23:29 +02004933 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004934 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004935 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004936
4937 r = kvm_pic_init(kvm);
4938 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004939 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004940
4941 r = kvm_ioapic_init(kvm);
4942 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004943 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004944 goto create_irqchip_unlock;
4945 }
4946
Avi Kivity399ec802008-11-19 13:58:46 +02004947 r = kvm_setup_default_irq_routing(kvm);
4948 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004949 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004950 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004951 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004952 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004953 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004954 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004955 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004956 create_irqchip_unlock:
4957 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004958 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004959 }
Sheng Yang78376992008-01-28 05:10:22 +08004960 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004961 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4962 goto create_pit;
4963 case KVM_CREATE_PIT2:
4964 r = -EFAULT;
4965 if (copy_from_user(&u.pit_config, argp,
4966 sizeof(struct kvm_pit_config)))
4967 goto out;
4968 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004969 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004970 r = -EEXIST;
4971 if (kvm->arch.vpit)
4972 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004973 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004974 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004975 if (kvm->arch.vpit)
4976 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004977 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004978 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004979 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004980 case KVM_GET_IRQCHIP: {
4981 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004982 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004983
Sasha Levinff5c2c02011-12-04 19:36:29 +02004984 chip = memdup_user(argp, sizeof(*chip));
4985 if (IS_ERR(chip)) {
4986 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004987 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004988 }
4989
Carsten Otte1fe779f2007-10-29 16:08:35 +01004990 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004991 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004992 goto get_irqchip_out;
4993 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
4994 if (r)
4995 goto get_irqchip_out;
4996 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004997 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07004998 goto get_irqchip_out;
4999 r = 0;
5000 get_irqchip_out:
5001 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005002 break;
5003 }
5004 case KVM_SET_IRQCHIP: {
5005 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005006 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005007
Sasha Levinff5c2c02011-12-04 19:36:29 +02005008 chip = memdup_user(argp, sizeof(*chip));
5009 if (IS_ERR(chip)) {
5010 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005011 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005012 }
5013
Carsten Otte1fe779f2007-10-29 16:08:35 +01005014 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005015 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005016 goto set_irqchip_out;
5017 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005018 set_irqchip_out:
5019 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005020 break;
5021 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005022 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005023 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005024 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005025 goto out;
5026 r = -ENXIO;
5027 if (!kvm->arch.vpit)
5028 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005029 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005030 if (r)
5031 goto out;
5032 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005033 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005034 goto out;
5035 r = 0;
5036 break;
5037 }
5038 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005039 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005040 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005041 goto out;
5042 r = -ENXIO;
5043 if (!kvm->arch.vpit)
5044 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005045 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005046 break;
5047 }
Beth Kone9f42752009-07-07 11:50:38 -04005048 case KVM_GET_PIT2: {
5049 r = -ENXIO;
5050 if (!kvm->arch.vpit)
5051 goto out;
5052 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5053 if (r)
5054 goto out;
5055 r = -EFAULT;
5056 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5057 goto out;
5058 r = 0;
5059 break;
5060 }
5061 case KVM_SET_PIT2: {
5062 r = -EFAULT;
5063 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5064 goto out;
5065 r = -ENXIO;
5066 if (!kvm->arch.vpit)
5067 goto out;
5068 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04005069 break;
5070 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005071 case KVM_REINJECT_CONTROL: {
5072 struct kvm_reinject_control control;
5073 r = -EFAULT;
5074 if (copy_from_user(&control, argp, sizeof(control)))
5075 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005076 r = -ENXIO;
5077 if (!kvm->arch.vpit)
5078 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005079 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005080 break;
5081 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005082 case KVM_SET_BOOT_CPU_ID:
5083 r = 0;
5084 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005085 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005086 r = -EBUSY;
5087 else
5088 kvm->arch.bsp_vcpu_id = arg;
5089 mutex_unlock(&kvm->lock);
5090 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005091 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005092 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005093 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005094 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005095 goto out;
5096 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005097 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005098 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005099 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005100 r = 0;
5101 break;
5102 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005103 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005104 struct kvm_clock_data user_ns;
5105 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005106
5107 r = -EFAULT;
5108 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5109 goto out;
5110
5111 r = -EINVAL;
5112 if (user_ns.flags)
5113 goto out;
5114
5115 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005116 /*
5117 * TODO: userspace has to take care of races with VCPU_RUN, so
5118 * kvm_gen_update_masterclock() can be cut down to locked
5119 * pvclock_update_vm_gtod_copy().
5120 */
5121 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005122 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005123 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005124 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005125 break;
5126 }
5127 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005128 struct kvm_clock_data user_ns;
5129 u64 now_ns;
5130
Marcelo Tosattie891a322017-04-17 12:51:37 -03005131 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005132 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005133 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005134 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005135
5136 r = -EFAULT;
5137 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5138 goto out;
5139 r = 0;
5140 break;
5141 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005142 case KVM_MEMORY_ENCRYPT_OP: {
5143 r = -ENOTTY;
5144 if (kvm_x86_ops->mem_enc_op)
5145 r = kvm_x86_ops->mem_enc_op(kvm, argp);
5146 break;
5147 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005148 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5149 struct kvm_enc_region region;
5150
5151 r = -EFAULT;
5152 if (copy_from_user(&region, argp, sizeof(region)))
5153 goto out;
5154
5155 r = -ENOTTY;
5156 if (kvm_x86_ops->mem_enc_reg_region)
5157 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
5158 break;
5159 }
5160 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5161 struct kvm_enc_region region;
5162
5163 r = -EFAULT;
5164 if (copy_from_user(&region, argp, sizeof(region)))
5165 goto out;
5166
5167 r = -ENOTTY;
5168 if (kvm_x86_ops->mem_enc_unreg_region)
5169 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
5170 break;
5171 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005172 case KVM_HYPERV_EVENTFD: {
5173 struct kvm_hyperv_eventfd hvevfd;
5174
5175 r = -EFAULT;
5176 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5177 goto out;
5178 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5179 break;
5180 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005181 case KVM_SET_PMU_EVENT_FILTER:
5182 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5183 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005184 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005185 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005186 }
5187out:
5188 return r;
5189}
5190
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005191static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005192{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005193 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005194 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005195 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005196
Jim Mattsone2ada662019-08-21 11:20:04 -07005197 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005198 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005199
5200 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005201
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005202 num_msrs_to_save = 0;
5203 num_emulated_msrs = 0;
5204 num_msr_based_features = 0;
5205
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005206 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5207 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005208 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005209
5210 /*
5211 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005212 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005213 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005214 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005215 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005216 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005217 continue;
5218 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005219 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005220 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005221 continue;
5222 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005223 case MSR_IA32_RTIT_CTL:
5224 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005225 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005226 continue;
5227 break;
5228 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005229 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005230 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5231 continue;
5232 break;
5233 case MSR_IA32_RTIT_OUTPUT_BASE:
5234 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005235 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005236 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5237 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5238 continue;
5239 break;
5240 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
Sean Christopherson7b874c22020-03-02 15:56:59 -08005241 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005242 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005243 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5244 continue;
5245 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005246 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005247 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005248 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5249 continue;
5250 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005251 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005252 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005253 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5254 continue;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005255 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005256 default:
5257 break;
5258 }
5259
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005260 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005261 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005262
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005263 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
5264 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005265 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005266
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005267 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005268 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005269
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005270 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005271 struct kvm_msr_entry msr;
5272
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005273 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005274 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005275 continue;
5276
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005277 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005278 }
Carsten Otte043405e2007-10-10 17:16:19 +02005279}
5280
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005281static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5282 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005283{
Avi Kivity70252a12010-01-19 12:51:22 +02005284 int handled = 0;
5285 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005286
Avi Kivity70252a12010-01-19 12:51:22 +02005287 do {
5288 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005289 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005290 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5291 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005292 break;
5293 handled += n;
5294 addr += n;
5295 len -= n;
5296 v += n;
5297 } while (len);
5298
5299 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005300}
5301
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005302static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005303{
Avi Kivity70252a12010-01-19 12:51:22 +02005304 int handled = 0;
5305 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005306
Avi Kivity70252a12010-01-19 12:51:22 +02005307 do {
5308 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005309 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005310 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5311 addr, n, v))
5312 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005313 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005314 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005315 handled += n;
5316 addr += n;
5317 len -= n;
5318 v += n;
5319 } while (len);
5320
5321 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005322}
5323
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005324static void kvm_set_segment(struct kvm_vcpu *vcpu,
5325 struct kvm_segment *var, int seg)
5326{
5327 kvm_x86_ops->set_segment(vcpu, var, seg);
5328}
5329
5330void kvm_get_segment(struct kvm_vcpu *vcpu,
5331 struct kvm_segment *var, int seg)
5332{
5333 kvm_x86_ops->get_segment(vcpu, var, seg);
5334}
5335
Paolo Bonzini54987b72014-09-02 13:23:06 +02005336gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5337 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005338{
5339 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005340
5341 BUG_ON(!mmu_is_nested(vcpu));
5342
5343 /* NPT walks are always user-walks */
5344 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005345 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005346
5347 return t_gpa;
5348}
5349
Avi Kivityab9ae312010-11-22 17:53:26 +02005350gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5351 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005352{
5353 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005354 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005355}
5356
Avi Kivityab9ae312010-11-22 17:53:26 +02005357 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5358 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005359{
5360 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5361 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005362 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005363}
5364
Avi Kivityab9ae312010-11-22 17:53:26 +02005365gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5366 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005367{
5368 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5369 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005370 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005371}
5372
5373/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005374gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5375 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005376{
Avi Kivityab9ae312010-11-22 17:53:26 +02005377 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005378}
5379
5380static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5381 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005382 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005383{
5384 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005385 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005386
5387 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005388 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005389 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005390 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005391 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005392 int ret;
5393
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005394 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005395 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005396 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5397 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005398 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005399 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005400 goto out;
5401 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005402
Izik Eidus77c20022008-12-29 01:42:19 +02005403 bytes -= toread;
5404 data += toread;
5405 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005406 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005407out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005408 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005409}
Izik Eidus77c20022008-12-29 01:42:19 +02005410
Gleb Natapov1871c602010-02-10 14:21:32 +02005411/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005412static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5413 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005414 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005415{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005416 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005417 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005418 unsigned offset;
5419 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005420
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005421 /* Inline kvm_read_guest_virt_helper for speed. */
5422 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5423 exception);
5424 if (unlikely(gpa == UNMAPPED_GVA))
5425 return X86EMUL_PROPAGATE_FAULT;
5426
5427 offset = addr & (PAGE_SIZE-1);
5428 if (WARN_ON(offset + bytes > PAGE_SIZE))
5429 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005430 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5431 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005432 if (unlikely(ret < 0))
5433 return X86EMUL_IO_NEEDED;
5434
5435 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005436}
5437
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005438int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005439 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005440 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005441{
5442 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005443
Paolo Bonzini353c0952019-01-29 18:41:16 +01005444 /*
5445 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5446 * is returned, but our callers are not ready for that and they blindly
5447 * call kvm_inject_page_fault. Ensure that they at least do not leak
5448 * uninitialized kernel stack memory into cr2 and error code.
5449 */
5450 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005451 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005452 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005453}
Nadav Har'El064aea72011-05-25 23:04:56 +03005454EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005455
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005456static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5457 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005458 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005459{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005460 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005461 u32 access = 0;
5462
5463 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5464 access |= PFERR_USER_MASK;
5465
5466 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005467}
5468
Radim Krčmář7a036a62015-10-30 16:36:24 +01005469static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5470 unsigned long addr, void *val, unsigned int bytes)
5471{
5472 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5473 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5474
5475 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5476}
5477
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005478static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5479 struct kvm_vcpu *vcpu, u32 access,
5480 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005481{
5482 void *data = val;
5483 int r = X86EMUL_CONTINUE;
5484
5485 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005486 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005487 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005488 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005489 unsigned offset = addr & (PAGE_SIZE-1);
5490 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5491 int ret;
5492
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005493 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005494 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005495 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005496 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005497 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005498 goto out;
5499 }
5500
5501 bytes -= towrite;
5502 data += towrite;
5503 addr += towrite;
5504 }
5505out:
5506 return r;
5507}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005508
5509static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005510 unsigned int bytes, struct x86_exception *exception,
5511 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005512{
5513 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005514 u32 access = PFERR_WRITE_MASK;
5515
5516 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5517 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005518
5519 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005520 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005521}
5522
5523int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5524 unsigned int bytes, struct x86_exception *exception)
5525{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005526 /* kvm_write_guest_virt_system can pull in tons of pages. */
5527 vcpu->arch.l1tf_flush_l1d = true;
5528
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005529 /*
5530 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5531 * is returned, but our callers are not ready for that and they blindly
5532 * call kvm_inject_page_fault. Ensure that they at least do not leak
5533 * uninitialized kernel stack memory into cr2 and error code.
5534 */
5535 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005536 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5537 PFERR_WRITE_MASK, exception);
5538}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005539EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005540
Wanpeng Li082d06e2018-04-03 16:28:48 -07005541int handle_ud(struct kvm_vcpu *vcpu)
5542{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005543 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005544 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005545 char sig[5]; /* ud2; .ascii "kvm" */
5546 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005547
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005548 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005549 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5550 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005551 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005552 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005553 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005554 }
5555
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005556 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005557}
5558EXPORT_SYMBOL_GPL(handle_ud);
5559
Tom Lendacky0f89b202016-12-14 14:59:23 -05005560static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5561 gpa_t gpa, bool write)
5562{
5563 /* For APIC access vmexit */
5564 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5565 return 1;
5566
5567 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5568 trace_vcpu_match_mmio(gva, gpa, write, true);
5569 return 1;
5570 }
5571
5572 return 0;
5573}
5574
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005575static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5576 gpa_t *gpa, struct x86_exception *exception,
5577 bool write)
5578{
Avi Kivity97d64b72012-09-12 14:52:00 +03005579 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5580 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005581
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005582 /*
5583 * currently PKRU is only applied to ept enabled guest so
5584 * there is no pkey in EPT page table for L1 guest or EPT
5585 * shadow page table for L2 guest.
5586 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005587 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005588 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005589 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005590 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5591 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005592 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005593 return 1;
5594 }
5595
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005596 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5597
5598 if (*gpa == UNMAPPED_GVA)
5599 return -1;
5600
Tom Lendacky0f89b202016-12-14 14:59:23 -05005601 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005602}
5603
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005604int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005605 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005606{
5607 int ret;
5608
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005609 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005610 if (ret < 0)
5611 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005612 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005613 return 1;
5614}
5615
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005616struct read_write_emulator_ops {
5617 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5618 int bytes);
5619 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5620 void *val, int bytes);
5621 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5622 int bytes, void *val);
5623 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5624 void *val, int bytes);
5625 bool write;
5626};
5627
5628static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5629{
5630 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005631 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005632 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005633 vcpu->mmio_read_completed = 0;
5634 return 1;
5635 }
5636
5637 return 0;
5638}
5639
5640static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5641 void *val, int bytes)
5642{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005643 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005644}
5645
5646static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5647 void *val, int bytes)
5648{
5649 return emulator_write_phys(vcpu, gpa, val, bytes);
5650}
5651
5652static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5653{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005654 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005655 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5656}
5657
5658static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5659 void *val, int bytes)
5660{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005661 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005662 return X86EMUL_IO_NEEDED;
5663}
5664
5665static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5666 void *val, int bytes)
5667{
Avi Kivityf78146b2012-04-18 19:22:47 +03005668 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5669
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005670 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005671 return X86EMUL_CONTINUE;
5672}
5673
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005674static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005675 .read_write_prepare = read_prepare,
5676 .read_write_emulate = read_emulate,
5677 .read_write_mmio = vcpu_mmio_read,
5678 .read_write_exit_mmio = read_exit_mmio,
5679};
5680
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005681static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005682 .read_write_emulate = write_emulate,
5683 .read_write_mmio = write_mmio,
5684 .read_write_exit_mmio = write_exit_mmio,
5685 .write = true,
5686};
5687
Xiao Guangrong22388a32011-07-13 14:32:31 +08005688static int emulator_read_write_onepage(unsigned long addr, void *val,
5689 unsigned int bytes,
5690 struct x86_exception *exception,
5691 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005692 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005693{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005694 gpa_t gpa;
5695 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005696 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005697 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005698 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005699
5700 /*
5701 * If the exit was due to a NPF we may already have a GPA.
5702 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5703 * Note, this cannot be used on string operations since string
5704 * operation using rep will only have the initial GPA from the NPF
5705 * occurred.
5706 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005707 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5708 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5709 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005710 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5711 } else {
5712 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5713 if (ret < 0)
5714 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005715 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005716
Brijesh Singh618232e2017-08-17 18:36:57 +02005717 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005718 return X86EMUL_CONTINUE;
5719
Carsten Ottebbd9b642007-10-30 18:44:21 +01005720 /*
5721 * Is this MMIO handled locally?
5722 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005723 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005724 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005725 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005726
Avi Kivity70252a12010-01-19 12:51:22 +02005727 gpa += handled;
5728 bytes -= handled;
5729 val += handled;
5730
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005731 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5732 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5733 frag->gpa = gpa;
5734 frag->data = val;
5735 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005736 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005737}
5738
Xiubo Li52eb5a62015-03-13 17:39:45 +08005739static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5740 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005741 void *val, unsigned int bytes,
5742 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005743 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005744{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005745 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005746 gpa_t gpa;
5747 int rc;
5748
5749 if (ops->read_write_prepare &&
5750 ops->read_write_prepare(vcpu, val, bytes))
5751 return X86EMUL_CONTINUE;
5752
5753 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005754
Carsten Ottebbd9b642007-10-30 18:44:21 +01005755 /* Crossing a page boundary? */
5756 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005757 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005758
5759 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005760 rc = emulator_read_write_onepage(addr, val, now, exception,
5761 vcpu, ops);
5762
Carsten Ottebbd9b642007-10-30 18:44:21 +01005763 if (rc != X86EMUL_CONTINUE)
5764 return rc;
5765 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005766 if (ctxt->mode != X86EMUL_MODE_PROT64)
5767 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005768 val += now;
5769 bytes -= now;
5770 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005771
Avi Kivityf78146b2012-04-18 19:22:47 +03005772 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5773 vcpu, ops);
5774 if (rc != X86EMUL_CONTINUE)
5775 return rc;
5776
5777 if (!vcpu->mmio_nr_fragments)
5778 return rc;
5779
5780 gpa = vcpu->mmio_fragments[0].gpa;
5781
5782 vcpu->mmio_needed = 1;
5783 vcpu->mmio_cur_fragment = 0;
5784
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005785 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005786 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5787 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5788 vcpu->run->mmio.phys_addr = gpa;
5789
5790 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005791}
5792
5793static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5794 unsigned long addr,
5795 void *val,
5796 unsigned int bytes,
5797 struct x86_exception *exception)
5798{
5799 return emulator_read_write(ctxt, addr, val, bytes,
5800 exception, &read_emultor);
5801}
5802
Xiubo Li52eb5a62015-03-13 17:39:45 +08005803static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005804 unsigned long addr,
5805 const void *val,
5806 unsigned int bytes,
5807 struct x86_exception *exception)
5808{
5809 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5810 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005811}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005812
Avi Kivitydaea3e72010-03-15 13:59:54 +02005813#define CMPXCHG_TYPE(t, ptr, old, new) \
5814 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5815
5816#ifdef CONFIG_X86_64
5817# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5818#else
5819# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005820 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005821#endif
5822
Avi Kivity0f65dd72011-04-20 13:37:53 +03005823static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5824 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005825 const void *old,
5826 const void *new,
5827 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005828 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005829{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005830 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005831 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005832 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005833 char *kaddr;
5834 bool exchanged;
5835
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005836 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005837 if (bytes > 8 || (bytes & (bytes - 1)))
5838 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005839
Avi Kivitydaea3e72010-03-15 13:59:54 +02005840 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005841
Avi Kivitydaea3e72010-03-15 13:59:54 +02005842 if (gpa == UNMAPPED_GVA ||
5843 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5844 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005845
Avi Kivitydaea3e72010-03-15 13:59:54 +02005846 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5847 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005848
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005849 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005850 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005851
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005852 kaddr = map.hva + offset_in_page(gpa);
5853
Avi Kivitydaea3e72010-03-15 13:59:54 +02005854 switch (bytes) {
5855 case 1:
5856 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5857 break;
5858 case 2:
5859 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5860 break;
5861 case 4:
5862 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5863 break;
5864 case 8:
5865 exchanged = CMPXCHG64(kaddr, old, new);
5866 break;
5867 default:
5868 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005869 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005870
5871 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005872
5873 if (!exchanged)
5874 return X86EMUL_CMPXCHG_FAILED;
5875
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005876 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005877
5878 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005879
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005880emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005881 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005882
Avi Kivity0f65dd72011-04-20 13:37:53 +03005883 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005884}
5885
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005886static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5887{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005888 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005889
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005890 for (i = 0; i < vcpu->arch.pio.count; i++) {
5891 if (vcpu->arch.pio.in)
5892 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5893 vcpu->arch.pio.size, pd);
5894 else
5895 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5896 vcpu->arch.pio.port, vcpu->arch.pio.size,
5897 pd);
5898 if (r)
5899 break;
5900 pd += vcpu->arch.pio.size;
5901 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005902 return r;
5903}
5904
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005905static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5906 unsigned short port, void *val,
5907 unsigned int count, bool in)
5908{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005909 vcpu->arch.pio.port = port;
5910 vcpu->arch.pio.in = in;
5911 vcpu->arch.pio.count = count;
5912 vcpu->arch.pio.size = size;
5913
5914 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5915 vcpu->arch.pio.count = 0;
5916 return 1;
5917 }
5918
5919 vcpu->run->exit_reason = KVM_EXIT_IO;
5920 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5921 vcpu->run->io.size = size;
5922 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5923 vcpu->run->io.count = count;
5924 vcpu->run->io.port = port;
5925
5926 return 0;
5927}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005928
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005929static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
5930 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005931{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005932 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005933
Gleb Natapov79729952010-03-18 15:20:24 +02005934 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005935 goto data_avail;
5936
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005937 memset(vcpu->arch.pio_data, 0, size * count);
5938
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005939 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5940 if (ret) {
5941data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005942 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005943 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005944 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005945 return 1;
5946 }
5947
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005948 return 0;
5949}
5950
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005951static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5952 int size, unsigned short port, void *val,
5953 unsigned int count)
5954{
5955 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
5956
5957}
5958
5959static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
5960 unsigned short port, const void *val,
5961 unsigned int count)
5962{
5963 memcpy(vcpu->arch.pio_data, val, size * count);
5964 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
5965 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
5966}
5967
Avi Kivityca1d4a92011-04-20 13:37:53 +03005968static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5969 int size, unsigned short port,
5970 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005971{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005972 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005973}
5974
Carsten Ottebbd9b642007-10-30 18:44:21 +01005975static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5976{
5977 return kvm_x86_ops->get_segment_base(vcpu, seg);
5978}
5979
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005980static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005981{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005982 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005983}
5984
Jiang Biaoae6a2372016-11-07 08:54:51 +08005985static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005986{
5987 if (!need_emulate_wbinvd(vcpu))
5988 return X86EMUL_CONTINUE;
5989
5990 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01005991 int cpu = get_cpu();
5992
5993 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005994 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
5995 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005996 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005997 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005998 } else
5999 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006000 return X86EMUL_CONTINUE;
6001}
Joel Schopp5cb56052015-03-02 13:43:31 -06006002
6003int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6004{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006005 kvm_emulate_wbinvd_noskip(vcpu);
6006 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006007}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006008EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6009
Joel Schopp5cb56052015-03-02 13:43:31 -06006010
6011
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006012static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6013{
Joel Schopp5cb56052015-03-02 13:43:31 -06006014 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006015}
6016
Xiubo Li52eb5a62015-03-13 17:39:45 +08006017static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6018 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006019{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006020 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006021}
6022
Xiubo Li52eb5a62015-03-13 17:39:45 +08006023static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6024 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006025{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006026
Avi Kivity717746e2011-04-20 13:37:53 +03006027 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006028}
6029
Gleb Natapov52a46612010-03-18 15:20:03 +02006030static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6031{
6032 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6033}
6034
Avi Kivity717746e2011-04-20 13:37:53 +03006035static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006036{
Avi Kivity717746e2011-04-20 13:37:53 +03006037 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006038 unsigned long value;
6039
6040 switch (cr) {
6041 case 0:
6042 value = kvm_read_cr0(vcpu);
6043 break;
6044 case 2:
6045 value = vcpu->arch.cr2;
6046 break;
6047 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006048 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006049 break;
6050 case 4:
6051 value = kvm_read_cr4(vcpu);
6052 break;
6053 case 8:
6054 value = kvm_get_cr8(vcpu);
6055 break;
6056 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006057 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006058 return 0;
6059 }
6060
6061 return value;
6062}
6063
Avi Kivity717746e2011-04-20 13:37:53 +03006064static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006065{
Avi Kivity717746e2011-04-20 13:37:53 +03006066 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006067 int res = 0;
6068
Gleb Natapov52a46612010-03-18 15:20:03 +02006069 switch (cr) {
6070 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006071 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006072 break;
6073 case 2:
6074 vcpu->arch.cr2 = val;
6075 break;
6076 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006077 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006078 break;
6079 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006080 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006081 break;
6082 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006083 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006084 break;
6085 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006086 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006087 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006088 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006089
6090 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006091}
6092
Avi Kivity717746e2011-04-20 13:37:53 +03006093static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006094{
Avi Kivity717746e2011-04-20 13:37:53 +03006095 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006096}
6097
Avi Kivity4bff1e862011-04-20 13:37:53 +03006098static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006099{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006100 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006101}
6102
Avi Kivity4bff1e862011-04-20 13:37:53 +03006103static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006104{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006105 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006106}
6107
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006108static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6109{
6110 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
6111}
6112
6113static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6114{
6115 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
6116}
6117
Avi Kivity4bff1e862011-04-20 13:37:53 +03006118static unsigned long emulator_get_cached_segment_base(
6119 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006120{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006121 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006122}
6123
Avi Kivity1aa36612011-04-27 13:20:30 +03006124static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6125 struct desc_struct *desc, u32 *base3,
6126 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006127{
6128 struct kvm_segment var;
6129
Avi Kivity4bff1e862011-04-20 13:37:53 +03006130 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006131 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006132
Gleb Natapov378a8b02013-01-21 15:36:48 +02006133 if (var.unusable) {
6134 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006135 if (base3)
6136 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006137 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006138 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006139
6140 if (var.g)
6141 var.limit >>= 12;
6142 set_desc_limit(desc, var.limit);
6143 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006144#ifdef CONFIG_X86_64
6145 if (base3)
6146 *base3 = var.base >> 32;
6147#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006148 desc->type = var.type;
6149 desc->s = var.s;
6150 desc->dpl = var.dpl;
6151 desc->p = var.present;
6152 desc->avl = var.avl;
6153 desc->l = var.l;
6154 desc->d = var.db;
6155 desc->g = var.g;
6156
6157 return true;
6158}
6159
Avi Kivity1aa36612011-04-27 13:20:30 +03006160static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6161 struct desc_struct *desc, u32 base3,
6162 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006163{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006164 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006165 struct kvm_segment var;
6166
Avi Kivity1aa36612011-04-27 13:20:30 +03006167 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006168 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006169#ifdef CONFIG_X86_64
6170 var.base |= ((u64)base3) << 32;
6171#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006172 var.limit = get_desc_limit(desc);
6173 if (desc->g)
6174 var.limit = (var.limit << 12) | 0xfff;
6175 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006176 var.dpl = desc->dpl;
6177 var.db = desc->d;
6178 var.s = desc->s;
6179 var.l = desc->l;
6180 var.g = desc->g;
6181 var.avl = desc->avl;
6182 var.present = desc->p;
6183 var.unusable = !var.present;
6184 var.padding = 0;
6185
6186 kvm_set_segment(vcpu, &var, seg);
6187 return;
6188}
6189
Avi Kivity717746e2011-04-20 13:37:53 +03006190static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6191 u32 msr_index, u64 *pdata)
6192{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006193 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006194}
6195
6196static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6197 u32 msr_index, u64 data)
6198{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006199 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006200}
6201
Paolo Bonzini64d60672015-05-07 11:36:11 +02006202static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6203{
6204 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6205
6206 return vcpu->arch.smbase;
6207}
6208
6209static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6210{
6211 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6212
6213 vcpu->arch.smbase = smbase;
6214}
6215
Nadav Amit67f4d422014-06-02 18:34:09 +03006216static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6217 u32 pmc)
6218{
Like Xu98ff80f2019-10-27 18:52:40 +08006219 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006220}
6221
Avi Kivity222d21a2011-11-10 14:57:30 +02006222static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6223 u32 pmc, u64 *pdata)
6224{
Wei Huangc6702c92015-06-19 13:44:45 +02006225 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006226}
6227
Avi Kivity6c3287f2011-04-20 15:43:05 +03006228static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6229{
6230 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6231}
6232
Avi Kivity29535382011-04-20 13:37:53 +03006233static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006234 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006235 enum x86_intercept_stage stage)
6236{
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006237 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage,
6238 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006239}
6240
Yu Zhange911eb32017-08-24 20:27:52 +08006241static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
6242 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006243{
Yu Zhange911eb32017-08-24 20:27:52 +08006244 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, check_limit);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006245}
6246
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006247static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6248{
6249 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6250}
6251
6252static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6253{
6254 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6255}
6256
6257static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6258{
6259 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6260}
6261
Avi Kivitydd856ef2012-08-27 23:46:17 +03006262static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6263{
6264 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6265}
6266
6267static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6268{
6269 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6270}
6271
Nadav Amit801806d2015-01-26 09:32:23 +02006272static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6273{
6274 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
6275}
6276
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006277static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6278{
6279 return emul_to_vcpu(ctxt)->arch.hflags;
6280}
6281
6282static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6283{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006284 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006285}
6286
Sean Christophersoned193212019-04-02 08:03:09 -07006287static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6288 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006289{
Sean Christophersoned193212019-04-02 08:03:09 -07006290 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006291}
6292
Sean Christophersonc5833c72019-04-02 08:03:10 -07006293static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6294{
6295 kvm_smm_changed(emul_to_vcpu(ctxt));
6296}
6297
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006298static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6299{
6300 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6301}
6302
Mathias Krause0225fb52012-08-30 01:30:16 +02006303static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006304 .read_gpr = emulator_read_gpr,
6305 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006306 .read_std = emulator_read_std,
6307 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006308 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006309 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006310 .read_emulated = emulator_read_emulated,
6311 .write_emulated = emulator_write_emulated,
6312 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006313 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006314 .pio_in_emulated = emulator_pio_in_emulated,
6315 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006316 .get_segment = emulator_get_segment,
6317 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006318 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006319 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006320 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006321 .set_gdt = emulator_set_gdt,
6322 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006323 .get_cr = emulator_get_cr,
6324 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006325 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006326 .get_dr = emulator_get_dr,
6327 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006328 .get_smbase = emulator_get_smbase,
6329 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006330 .set_msr = emulator_set_msr,
6331 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006332 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006333 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006334 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006335 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006336 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006337 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006338 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006339 .guest_has_long_mode = emulator_guest_has_long_mode,
6340 .guest_has_movbe = emulator_guest_has_movbe,
6341 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006342 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006343 .get_hflags = emulator_get_hflags,
6344 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006345 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006346 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006347 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006348};
6349
Gleb Natapov95cb2292010-04-28 19:15:43 +03006350static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6351{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006352 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006353 /*
6354 * an sti; sti; sequence only disable interrupts for the first
6355 * instruction. So, if the last instruction, be it emulated or
6356 * not, left the system with the INT_STI flag enabled, it
6357 * means that the last instruction is an sti. We should not
6358 * leave the flag on in this case. The same goes for mov ss
6359 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006360 if (int_shadow & mask)
6361 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006362 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03006363 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006364 if (!mask)
6365 kvm_make_request(KVM_REQ_EVENT, vcpu);
6366 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006367}
6368
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006369static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006370{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006371 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006372 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006373 return kvm_propagate_fault(vcpu, &ctxt->exception);
6374
6375 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006376 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6377 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006378 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006379 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006380 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006381}
6382
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006383static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6384{
6385 struct x86_emulate_ctxt *ctxt;
6386
6387 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6388 if (!ctxt) {
6389 pr_err("kvm: failed to allocate vcpu's emulator\n");
6390 return NULL;
6391 }
6392
6393 ctxt->vcpu = vcpu;
6394 ctxt->ops = &emulate_ops;
6395 vcpu->arch.emulate_ctxt = ctxt;
6396
6397 return ctxt;
6398}
6399
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006400static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6401{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006402 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006403 int cs_db, cs_l;
6404
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006405 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6406
Sean Christopherson744e6992020-02-18 15:03:09 -08006407 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006408 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006409 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6410
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006411 ctxt->eip = kvm_rip_read(vcpu);
6412 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6413 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006414 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006415 cs_db ? X86EMUL_MODE_PROT32 :
6416 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006417 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006418 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6419 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006420
Avi Kivitydd856ef2012-08-27 23:46:17 +03006421 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006422 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006423}
6424
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006425void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006426{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006427 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006428 int ret;
6429
6430 init_emulate_ctxt(vcpu);
6431
Avi Kivity9dac77f2011-06-01 15:34:25 +03006432 ctxt->op_bytes = 2;
6433 ctxt->ad_bytes = 2;
6434 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006435 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006436
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006437 if (ret != X86EMUL_CONTINUE) {
6438 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6439 } else {
6440 ctxt->eip = ctxt->_eip;
6441 kvm_rip_write(vcpu, ctxt->eip);
6442 kvm_set_rflags(vcpu, ctxt->eflags);
6443 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006444}
6445EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6446
Liran Alone2366172018-03-12 13:12:49 +02006447static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006448{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006449 ++vcpu->stat.insn_emulation_fail;
6450 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006451
Sean Christopherson42cbf062019-08-27 14:40:31 -07006452 if (emulation_type & EMULTYPE_VMWARE_GP) {
6453 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006454 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006455 }
Liran Alone2366172018-03-12 13:12:49 +02006456
Sean Christopherson738fece2019-08-27 14:40:34 -07006457 if (emulation_type & EMULTYPE_SKIP) {
6458 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6459 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6460 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006461 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006462 }
6463
Sean Christopherson22da61c2019-08-27 14:40:28 -07006464 kvm_queue_exception(vcpu, UD_VECTOR);
6465
Nadav Amita2b9e6c2014-09-17 02:50:50 +03006466 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006467 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6468 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6469 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006470 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006471 }
Liran Alone2366172018-03-12 13:12:49 +02006472
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006473 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006474}
6475
Sean Christopherson736c2912019-12-06 15:57:14 -08006476static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006477 bool write_fault_to_shadow_pgtable,
6478 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006479{
Sean Christopherson736c2912019-12-06 15:57:14 -08006480 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006481 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006482
Sean Christopherson92daa482020-02-18 15:03:08 -08006483 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006484 return false;
6485
Sean Christopherson92daa482020-02-18 15:03:08 -08006486 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6487 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006488 return false;
6489
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006490 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006491 /*
6492 * Write permission should be allowed since only
6493 * write access need to be emulated.
6494 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006495 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006496
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006497 /*
6498 * If the mapping is invalid in guest, let cpu retry
6499 * it to generate fault.
6500 */
6501 if (gpa == UNMAPPED_GVA)
6502 return true;
6503 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006504
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006505 /*
6506 * Do not retry the unhandleable instruction if it faults on the
6507 * readonly host memory, otherwise it will goto a infinite loop:
6508 * retry instruction -> write #PF -> emulation fail -> retry
6509 * instruction -> ...
6510 */
6511 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006512
6513 /*
6514 * If the instruction failed on the error pfn, it can not be fixed,
6515 * report the error to userspace.
6516 */
6517 if (is_error_noslot_pfn(pfn))
6518 return false;
6519
6520 kvm_release_pfn_clean(pfn);
6521
6522 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006523 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006524 unsigned int indirect_shadow_pages;
6525
6526 spin_lock(&vcpu->kvm->mmu_lock);
6527 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6528 spin_unlock(&vcpu->kvm->mmu_lock);
6529
6530 if (indirect_shadow_pages)
6531 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6532
Gleb Natapova6f177e2010-07-08 12:41:12 +03006533 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006534 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006535
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006536 /*
6537 * if emulation was due to access to shadowed page table
6538 * and it failed try to unshadow page and re-enter the
6539 * guest to let CPU execute the instruction.
6540 */
6541 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006542
6543 /*
6544 * If the access faults on its page table, it can not
6545 * be fixed by unprotecting shadow page and it should
6546 * be reported to userspace.
6547 */
6548 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006549}
6550
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006551static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006552 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006553{
6554 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006555 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006556
6557 last_retry_eip = vcpu->arch.last_retry_eip;
6558 last_retry_addr = vcpu->arch.last_retry_addr;
6559
6560 /*
6561 * If the emulation is caused by #PF and it is non-page_table
6562 * writing instruction, it means the VM-EXIT is caused by shadow
6563 * page protected, we can zap the shadow page and retry this
6564 * instruction directly.
6565 *
6566 * Note: if the guest uses a non-page-table modifying instruction
6567 * on the PDE that points to the instruction, then we will unmap
6568 * the instruction and go to an infinite loop. So, we cache the
6569 * last retried eip and the last fault address, if we meet the eip
6570 * and the address again, we can break out of the potential infinite
6571 * loop.
6572 */
6573 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6574
Sean Christopherson92daa482020-02-18 15:03:08 -08006575 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006576 return false;
6577
Sean Christopherson92daa482020-02-18 15:03:08 -08006578 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6579 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006580 return false;
6581
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006582 if (x86_page_table_writing_insn(ctxt))
6583 return false;
6584
Sean Christopherson736c2912019-12-06 15:57:14 -08006585 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006586 return false;
6587
6588 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006589 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006590
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006591 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006592 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006593
Xiao Guangrong22368022013-01-13 23:44:12 +08006594 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006595
6596 return true;
6597}
6598
Gleb Natapov716d51a2012-09-03 15:24:26 +03006599static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6600static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6601
Paolo Bonzini64d60672015-05-07 11:36:11 +02006602static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006603{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006604 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006605 /* This is a good place to trace that we are exiting SMM. */
6606 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6607
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006608 /* Process a latched INIT or SMI, if any. */
6609 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006610 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006611
6612 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006613}
6614
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006615static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6616 unsigned long *db)
6617{
6618 u32 dr6 = 0;
6619 int i;
6620 u32 enable, rwlen;
6621
6622 enable = dr7;
6623 rwlen = dr7 >> 16;
6624 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6625 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6626 dr6 |= (1 << i);
6627 return dr6;
6628}
6629
Sean Christopherson120c2c42019-08-27 14:40:29 -07006630static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006631{
6632 struct kvm_run *kvm_run = vcpu->run;
6633
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006634 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6635 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6636 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6637 kvm_run->debug.arch.exception = DB_VECTOR;
6638 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006639 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006640 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006641 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006642 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006643}
6644
Kyle Huey6affcbe2016-11-29 12:40:40 -08006645int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6646{
6647 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006648 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006649
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006650 r = kvm_x86_ops->skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006651 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006652 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006653
6654 /*
6655 * rflags is the old, "raw" value of the flags. The new value has
6656 * not been saved yet.
6657 *
6658 * This is correct even for TF set by the guest, because "the
6659 * processor will not generate this exception after the instruction
6660 * that sets the TF flag".
6661 */
6662 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006663 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006664 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006665}
6666EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6667
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006668static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6669{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006670 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6671 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006672 struct kvm_run *kvm_run = vcpu->run;
6673 unsigned long eip = kvm_get_linear_rip(vcpu);
6674 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006675 vcpu->arch.guest_debug_dr7,
6676 vcpu->arch.eff_db);
6677
6678 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006679 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006680 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006681 kvm_run->debug.arch.exception = DB_VECTOR;
6682 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006683 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006684 return true;
6685 }
6686 }
6687
Nadav Amit4161a562014-07-17 01:19:31 +03006688 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6689 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006690 unsigned long eip = kvm_get_linear_rip(vcpu);
6691 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006692 vcpu->arch.dr7,
6693 vcpu->arch.db);
6694
6695 if (dr6 != 0) {
Liran Alon1fc5d1942019-06-06 01:54:47 +03006696 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006697 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006698 kvm_queue_exception(vcpu, DB_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006699 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006700 return true;
6701 }
6702 }
6703
6704 return false;
6705}
6706
Liran Alon04789b62018-03-12 13:12:50 +02006707static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6708{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006709 switch (ctxt->opcode_len) {
6710 case 1:
6711 switch (ctxt->b) {
6712 case 0xe4: /* IN */
6713 case 0xe5:
6714 case 0xec:
6715 case 0xed:
6716 case 0xe6: /* OUT */
6717 case 0xe7:
6718 case 0xee:
6719 case 0xef:
6720 case 0x6c: /* INS */
6721 case 0x6d:
6722 case 0x6e: /* OUTS */
6723 case 0x6f:
6724 return true;
6725 }
6726 break;
6727 case 2:
6728 switch (ctxt->b) {
6729 case 0x33: /* RDPMC */
6730 return true;
6731 }
6732 break;
Liran Alon04789b62018-03-12 13:12:50 +02006733 }
6734
6735 return false;
6736}
6737
Sean Christopherson736c2912019-12-06 15:57:14 -08006738int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6739 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006740{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006741 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006742 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006743 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006744 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006745
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006746 vcpu->arch.l1tf_flush_l1d = true;
6747
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006748 /*
6749 * Clear write_fault_to_shadow_pgtable here to ensure it is
6750 * never reused.
6751 */
6752 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006753 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006754
Sheng Yang571008d2008-01-02 14:49:22 +08006755 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006756 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006757
6758 /*
6759 * We will reenter on the same instruction since
6760 * we do not set complete_userspace_io. This does not
6761 * handle watchpoints yet, those would be handled in
6762 * the emulate_ops.
6763 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006764 if (!(emulation_type & EMULTYPE_SKIP) &&
6765 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006766 return r;
6767
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006768 ctxt->interruptibility = 0;
6769 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006770 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006771 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006772
Borislav Petkovb51e9742013-09-22 16:44:52 +02006773 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006774
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006775 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006776
Avi Kivitye46479f2010-04-11 13:05:16 +03006777 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006778 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006779 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006780 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006781 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6782 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006783 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006784 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006785 if (reexecute_instruction(vcpu, cr2_or_gpa,
6786 write_fault_to_spt,
6787 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006788 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006789 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006790 /*
6791 * #UD should result in just EMULATION_FAILED, and trap-like
6792 * exception should not be encountered during decode.
6793 */
6794 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6795 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006796 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006797 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006798 }
Liran Alone2366172018-03-12 13:12:49 +02006799 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006800 }
6801 }
6802
Sean Christopherson42cbf062019-08-27 14:40:31 -07006803 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6804 !is_vmware_backdoor_opcode(ctxt)) {
6805 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006806 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006807 }
Liran Alon04789b62018-03-12 13:12:50 +02006808
Sean Christopherson1957aa62019-08-27 14:40:39 -07006809 /*
6810 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6811 * for kvm_skip_emulated_instruction(). The caller is responsible for
6812 * updating interruptibility state and injecting single-step #DBs.
6813 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006814 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006815 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006816 if (ctxt->eflags & X86_EFLAGS_RF)
6817 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006818 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006819 }
6820
Sean Christopherson736c2912019-12-06 15:57:14 -08006821 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006822 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006823
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006824 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006825 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006826 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6827 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006828 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006829 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006830
Gleb Natapov5cd21912010-03-18 15:20:26 +02006831restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006832 if (emulation_type & EMULTYPE_PF) {
6833 /* Save the faulting GPA (cr2) in the address field */
6834 ctxt->exception.address = cr2_or_gpa;
6835
6836 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6837 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006838 ctxt->gpa_available = true;
6839 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006840 }
6841 } else {
6842 /* Sanitize the address out of an abundance of paranoia. */
6843 ctxt->exception.address = 0;
6844 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006845
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006846 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006847
Joerg Roedel775fde82011-04-04 12:39:24 +02006848 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006849 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006850
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006851 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006852 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006853 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006854 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006855
Liran Alone2366172018-03-12 13:12:49 +02006856 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006857 }
6858
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006859 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006860 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006861 if (inject_emulated_exception(vcpu))
6862 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006863 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006864 if (!vcpu->arch.pio.in) {
6865 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006866 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006867 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006868 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006869 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6870 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006871 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006872 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006873 ++vcpu->stat.mmio_exits;
6874
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006875 if (!vcpu->mmio_is_write)
6876 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006877 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006878 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006879 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006880 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006881 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006882 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006883
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006884 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006885 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006886 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006887 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006888 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006889 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6890 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006891 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006892 r = kvm_vcpu_do_singlestep(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006893 if (kvm_x86_ops->update_emulated_instruction)
6894 kvm_x86_ops->update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006895 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006896 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006897
6898 /*
6899 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6900 * do nothing, and it will be requested again as soon as
6901 * the shadow expires. But we still need to check here,
6902 * because POPF has no interrupt shadow.
6903 */
6904 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6905 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006906 } else
6907 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006908
Gleb Natapove85d28f2010-07-29 15:11:52 +03006909 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006910}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006911
6912int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6913{
6914 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6915}
6916EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6917
6918int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6919 void *insn, int insn_len)
6920{
6921 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6922}
6923EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006924
Sean Christopherson8764ed52019-04-29 07:04:15 -07006925static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6926{
6927 vcpu->arch.pio.count = 0;
6928 return 1;
6929}
6930
Sean Christopherson45def772019-03-11 20:01:05 -07006931static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6932{
6933 vcpu->arch.pio.count = 0;
6934
6935 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6936 return 1;
6937
6938 return kvm_skip_emulated_instruction(vcpu);
6939}
6940
Sean Christophersondca7f122018-03-08 08:57:27 -08006941static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6942 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006943{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006944 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006945 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
6946
Sean Christopherson8764ed52019-04-29 07:04:15 -07006947 if (ret)
6948 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006949
Sean Christopherson8764ed52019-04-29 07:04:15 -07006950 /*
6951 * Workaround userspace that relies on old KVM behavior of %rip being
6952 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6953 */
6954 if (port == 0x7e &&
6955 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6956 vcpu->arch.complete_userspace_io =
6957 complete_fast_pio_out_port_0x7e;
6958 kvm_skip_emulated_instruction(vcpu);
6959 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006960 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6961 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6962 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006963 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006964}
Carsten Ottede7d7892007-10-30 18:44:25 +01006965
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006966static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6967{
6968 unsigned long val;
6969
6970 /* We should only ever be called with arch.pio.count equal to 1 */
6971 BUG_ON(vcpu->arch.pio.count != 1);
6972
Sean Christopherson45def772019-03-11 20:01:05 -07006973 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
6974 vcpu->arch.pio.count = 0;
6975 return 1;
6976 }
6977
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006978 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006979 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006980
6981 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006982 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006983 * the copy and tracing
6984 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006985 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07006986 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006987
Sean Christopherson45def772019-03-11 20:01:05 -07006988 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006989}
6990
Sean Christophersondca7f122018-03-08 08:57:27 -08006991static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
6992 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006993{
6994 unsigned long val;
6995 int ret;
6996
6997 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006998 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006999
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007000 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007001 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007002 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007003 return ret;
7004 }
7005
Sean Christopherson45def772019-03-11 20:01:05 -07007006 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007007 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7008
7009 return 0;
7010}
Sean Christophersondca7f122018-03-08 08:57:27 -08007011
7012int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7013{
Sean Christopherson45def772019-03-11 20:01:05 -07007014 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007015
Sean Christophersondca7f122018-03-08 08:57:27 -08007016 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007017 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007018 else
Sean Christopherson45def772019-03-11 20:01:05 -07007019 ret = kvm_fast_pio_out(vcpu, size, port);
7020 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007021}
7022EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007023
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007024static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007025{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007026 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007027 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007028}
7029
7030static void tsc_khz_changed(void *data)
7031{
7032 struct cpufreq_freqs *freq = data;
7033 unsigned long khz = 0;
7034
7035 if (data)
7036 khz = freq->new;
7037 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7038 khz = cpufreq_quick_get(raw_smp_processor_id());
7039 if (!khz)
7040 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007041 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007042}
7043
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007044#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007045static void kvm_hyperv_tsc_notifier(void)
7046{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007047 struct kvm *kvm;
7048 struct kvm_vcpu *vcpu;
7049 int cpu;
7050
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007051 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007052 list_for_each_entry(kvm, &vm_list, vm_list)
7053 kvm_make_mclock_inprogress_request(kvm);
7054
7055 hyperv_stop_tsc_emulation();
7056
7057 /* TSC frequency always matches when on Hyper-V */
7058 for_each_present_cpu(cpu)
7059 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7060 kvm_max_guest_tsc_khz = tsc_khz;
7061
7062 list_for_each_entry(kvm, &vm_list, vm_list) {
7063 struct kvm_arch *ka = &kvm->arch;
7064
7065 spin_lock(&ka->pvclock_gtod_sync_lock);
7066
7067 pvclock_update_vm_gtod_copy(kvm);
7068
7069 kvm_for_each_vcpu(cpu, vcpu, kvm)
7070 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7071
7072 kvm_for_each_vcpu(cpu, vcpu, kvm)
7073 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7074
7075 spin_unlock(&ka->pvclock_gtod_sync_lock);
7076 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007077 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007078}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007079#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007080
Viresh Kumardf240142019-04-29 15:03:58 +05307081static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007082{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007083 struct kvm *kvm;
7084 struct kvm_vcpu *vcpu;
7085 int i, send_ipi = 0;
7086
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007087 /*
7088 * We allow guests to temporarily run on slowing clocks,
7089 * provided we notify them after, or to run on accelerating
7090 * clocks, provided we notify them before. Thus time never
7091 * goes backwards.
7092 *
7093 * However, we have a problem. We can't atomically update
7094 * the frequency of a given CPU from this function; it is
7095 * merely a notifier, which can be called from any CPU.
7096 * Changing the TSC frequency at arbitrary points in time
7097 * requires a recomputation of local variables related to
7098 * the TSC for each VCPU. We must flag these local variables
7099 * to be updated and be sure the update takes place with the
7100 * new frequency before any guests proceed.
7101 *
7102 * Unfortunately, the combination of hotplug CPU and frequency
7103 * change creates an intractable locking scenario; the order
7104 * of when these callouts happen is undefined with respect to
7105 * CPU hotplug, and they can race with each other. As such,
7106 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7107 * undefined; you can actually have a CPU frequency change take
7108 * place in between the computation of X and the setting of the
7109 * variable. To protect against this problem, all updates of
7110 * the per_cpu tsc_khz variable are done in an interrupt
7111 * protected IPI, and all callers wishing to update the value
7112 * must wait for a synchronous IPI to complete (which is trivial
7113 * if the caller is on the CPU already). This establishes the
7114 * necessary total order on variable updates.
7115 *
7116 * Note that because a guest time update may take place
7117 * anytime after the setting of the VCPU's request bit, the
7118 * correct TSC value must be set before the request. However,
7119 * to ensure the update actually makes it to any guest which
7120 * starts running in hardware virtualization between the set
7121 * and the acquisition of the spinlock, we must also ping the
7122 * CPU after setting the request bit.
7123 *
7124 */
7125
Viresh Kumardf240142019-04-29 15:03:58 +05307126 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007127
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007128 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007129 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007130 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307131 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007132 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007133 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007134 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007135 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007136 }
7137 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007138 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007139
7140 if (freq->old < freq->new && send_ipi) {
7141 /*
7142 * We upscale the frequency. Must make the guest
7143 * doesn't see old kvmclock values while running with
7144 * the new frequency, otherwise we risk the guest sees
7145 * time go backwards.
7146 *
7147 * In case we update the frequency for another cpu
7148 * (which might be in guest context) send an interrupt
7149 * to kick the cpu out of guest context. Next time
7150 * guest context is entered kvmclock will be updated,
7151 * so the guest will not see stale values.
7152 */
Viresh Kumardf240142019-04-29 15:03:58 +05307153 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007154 }
Viresh Kumardf240142019-04-29 15:03:58 +05307155}
7156
7157static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7158 void *data)
7159{
7160 struct cpufreq_freqs *freq = data;
7161 int cpu;
7162
7163 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7164 return 0;
7165 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7166 return 0;
7167
7168 for_each_cpu(cpu, freq->policy->cpus)
7169 __kvmclock_cpufreq_notifier(freq, cpu);
7170
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007171 return 0;
7172}
7173
7174static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007175 .notifier_call = kvmclock_cpufreq_notifier
7176};
7177
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007178static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007179{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007180 tsc_khz_changed(NULL);
7181 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007182}
7183
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007184static void kvm_timer_init(void)
7185{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007186 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307187
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007188 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007189#ifdef CONFIG_CPU_FREQ
7190 struct cpufreq_policy policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007191 int cpu;
7192
Zachary Amsdenc2855452010-09-18 14:38:15 -10007193 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02007194 cpu = get_cpu();
7195 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007196 if (policy.cpuinfo.max_freq)
7197 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02007198 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007199#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007200 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7201 CPUFREQ_TRANSITION_NOTIFIER);
7202 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307203
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007204 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007205 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007206}
7207
Andi Kleendd60d212017-07-25 17:20:32 -07007208DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7209EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007210
Gleb Natapovf5132b02011-11-10 14:57:22 +02007211int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007212{
Alex,Shi086c9852011-10-20 15:34:01 +08007213 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007214}
7215
7216static int kvm_is_user_mode(void)
7217{
7218 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007219
Alex,Shi086c9852011-10-20 15:34:01 +08007220 if (__this_cpu_read(current_vcpu))
7221 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007222
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007223 return user_mode != 0;
7224}
7225
7226static unsigned long kvm_get_guest_ip(void)
7227{
7228 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007229
Alex,Shi086c9852011-10-20 15:34:01 +08007230 if (__this_cpu_read(current_vcpu))
7231 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007232
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007233 return ip;
7234}
7235
Luwei Kang8479e042019-02-18 19:26:07 -05007236static void kvm_handle_intel_pt_intr(void)
7237{
7238 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7239
7240 kvm_make_request(KVM_REQ_PMI, vcpu);
7241 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7242 (unsigned long *)&vcpu->arch.pmu.global_status);
7243}
7244
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007245static struct perf_guest_info_callbacks kvm_guest_cbs = {
7246 .is_in_guest = kvm_is_in_guest,
7247 .is_user_mode = kvm_is_user_mode,
7248 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007249 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007250};
7251
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007252#ifdef CONFIG_X86_64
7253static void pvclock_gtod_update_fn(struct work_struct *work)
7254{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007255 struct kvm *kvm;
7256
7257 struct kvm_vcpu *vcpu;
7258 int i;
7259
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007260 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007261 list_for_each_entry(kvm, &vm_list, vm_list)
7262 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007263 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007264 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007265 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007266}
7267
7268static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7269
7270/*
7271 * Notification about pvclock gtod data update.
7272 */
7273static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7274 void *priv)
7275{
7276 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7277 struct timekeeper *tk = priv;
7278
7279 update_pvclock_gtod(tk);
7280
7281 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007282 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007283 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007284 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007285 atomic_read(&kvm_guest_has_master_clock) != 0)
7286 queue_work(system_long_wq, &pvclock_gtod_work);
7287
7288 return 0;
7289}
7290
7291static struct notifier_block pvclock_gtod_notifier = {
7292 .notifier_call = pvclock_gtod_notify,
7293};
7294#endif
7295
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007296int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007297{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007298 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02007299 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007300
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007301 if (kvm_x86_ops) {
7302 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007303 r = -EEXIST;
7304 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007305 }
7306
7307 if (!ops->cpu_has_kvm_support()) {
7308 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007309 r = -EOPNOTSUPP;
7310 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007311 }
7312 if (ops->disabled_by_bios()) {
7313 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007314 r = -EOPNOTSUPP;
7315 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007316 }
7317
Marc Orrb666a4b2018-11-06 14:53:56 -08007318 /*
7319 * KVM explicitly assumes that the guest has an FPU and
7320 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7321 * vCPU's FPU state as a fxregs_state struct.
7322 */
7323 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7324 printk(KERN_ERR "kvm: inadequate fpu\n");
7325 r = -EOPNOTSUPP;
7326 goto out;
7327 }
7328
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007329 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007330 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007331 __alignof__(struct fpu), SLAB_ACCOUNT,
7332 NULL);
7333 if (!x86_fpu_cache) {
7334 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7335 goto out;
7336 }
7337
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007338 x86_emulator_cache = kvm_alloc_emulator_cache();
7339 if (!x86_emulator_cache) {
7340 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7341 goto out_free_x86_fpu_cache;
7342 }
7343
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007344 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7345 if (!shared_msrs) {
7346 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007347 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007348 }
7349
Avi Kivity97db56c2008-01-13 13:23:56 +02007350 r = kvm_mmu_module_init();
7351 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007352 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007353
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007354 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01007355
Sheng Yang7b523452008-04-25 21:13:50 +08007356 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007357 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007358 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007359 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007360
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007361 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7362
Sean Christophersoncfc48182020-03-02 15:56:23 -08007363 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007364 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007365 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7366 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007367
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007368 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007369 if (pi_inject_timer == -1)
7370 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007371#ifdef CONFIG_X86_64
7372 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007373
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007374 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007375 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007376#endif
7377
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007378 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007379
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007380out_free_percpu:
7381 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007382out_free_x86_emulator_cache:
7383 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007384out_free_x86_fpu_cache:
7385 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007386out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007387 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007388}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007389
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007390void kvm_arch_exit(void)
7391{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007392#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007393 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007394 clear_hv_tscchange_cb();
7395#endif
David Matlackcef84c32016-12-16 14:30:36 -08007396 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007397 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7398
Jan Kiszka888d2562009-04-17 19:24:58 +02007399 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7400 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7401 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007402 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007403#ifdef CONFIG_X86_64
7404 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7405#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007406 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007407 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007408 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007409 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007410}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007411
Joel Schopp5cb56052015-03-02 13:43:31 -06007412int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007413{
7414 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007415 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007416 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007417 return 1;
7418 } else {
7419 vcpu->run->exit_reason = KVM_EXIT_HLT;
7420 return 0;
7421 }
7422}
Joel Schopp5cb56052015-03-02 13:43:31 -06007423EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7424
7425int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7426{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007427 int ret = kvm_skip_emulated_instruction(vcpu);
7428 /*
7429 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7430 * KVM_EXIT_DEBUG here.
7431 */
7432 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007433}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007434EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7435
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007436#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007437static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7438 unsigned long clock_type)
7439{
7440 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007441 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007442 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007443 int ret;
7444
7445 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7446 return -KVM_EOPNOTSUPP;
7447
7448 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7449 return -KVM_EOPNOTSUPP;
7450
7451 clock_pairing.sec = ts.tv_sec;
7452 clock_pairing.nsec = ts.tv_nsec;
7453 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7454 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007455 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007456
7457 ret = 0;
7458 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7459 sizeof(struct kvm_clock_pairing)))
7460 ret = -KVM_EFAULT;
7461
7462 return ret;
7463}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007464#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007465
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307466/*
7467 * kvm_pv_kick_cpu_op: Kick a vcpu.
7468 *
7469 * @apicid - apicid of vcpu to be kicked.
7470 */
7471static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7472{
Raghavendra K T24d21662013-08-26 14:18:35 +05307473 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307474
Peter Xu150a84f2019-12-04 20:07:21 +01007475 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007476 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007477 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307478 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007479 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307480
Raghavendra K T24d21662013-08-26 14:18:35 +05307481 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007482 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307483}
7484
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007485bool kvm_apicv_activated(struct kvm *kvm)
7486{
7487 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7488}
7489EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7490
7491void kvm_apicv_init(struct kvm *kvm, bool enable)
7492{
7493 if (enable)
7494 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7495 &kvm->arch.apicv_inhibit_reasons);
7496 else
7497 set_bit(APICV_INHIBIT_REASON_DISABLE,
7498 &kvm->arch.apicv_inhibit_reasons);
7499}
7500EXPORT_SYMBOL_GPL(kvm_apicv_init);
7501
Wanpeng Li71506292019-06-11 20:23:49 +08007502static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7503{
7504 struct kvm_vcpu *target = NULL;
7505 struct kvm_apic_map *map;
7506
7507 rcu_read_lock();
7508 map = rcu_dereference(kvm->arch.apic_map);
7509
7510 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7511 target = map->phys_map[dest_id]->vcpu;
7512
7513 rcu_read_unlock();
7514
Wanpeng Li266e85a2019-07-24 17:43:13 +08007515 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007516 kvm_vcpu_yield_to(target);
7517}
7518
Hollis Blanchard8776e512007-10-31 17:24:24 -05007519int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7520{
7521 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007522 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007523
Radim Krčmář696ca772018-05-24 17:50:56 +02007524 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7525 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007526
Sean Christophersonde3cd112019-04-30 10:36:17 -07007527 nr = kvm_rax_read(vcpu);
7528 a0 = kvm_rbx_read(vcpu);
7529 a1 = kvm_rcx_read(vcpu);
7530 a2 = kvm_rdx_read(vcpu);
7531 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007532
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007533 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007534
Nadav Amita449c7a2014-06-18 17:19:24 +03007535 op_64_bit = is_64_bit_mode(vcpu);
7536 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007537 nr &= 0xFFFFFFFF;
7538 a0 &= 0xFFFFFFFF;
7539 a1 &= 0xFFFFFFFF;
7540 a2 &= 0xFFFFFFFF;
7541 a3 &= 0xFFFFFFFF;
7542 }
7543
Jan Kiszka07708c42009-08-03 18:43:28 +02007544 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
7545 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007546 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007547 }
7548
Hollis Blanchard8776e512007-10-31 17:24:24 -05007549 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007550 case KVM_HC_VAPIC_POLL_IRQ:
7551 ret = 0;
7552 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307553 case KVM_HC_KICK_CPU:
7554 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007555 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307556 ret = 0;
7557 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007558#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007559 case KVM_HC_CLOCK_PAIRING:
7560 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7561 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007562#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007563 case KVM_HC_SEND_IPI:
7564 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7565 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007566 case KVM_HC_SCHED_YIELD:
7567 kvm_sched_yield(vcpu->kvm, a0);
7568 ret = 0;
7569 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007570 default:
7571 ret = -KVM_ENOSYS;
7572 break;
7573 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007574out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007575 if (!op_64_bit)
7576 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007577 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007578
Amit Shahf11c3a82008-02-21 01:00:30 +05307579 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007580 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007581}
7582EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7583
Jan Kiszkab6785de2012-09-20 07:43:17 +02007584static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007585{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007586 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007587 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007588 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007589
Hollis Blanchard8776e512007-10-31 17:24:24 -05007590 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007591
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007592 return emulator_write_emulated(ctxt, rip, instruction, 3,
7593 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007594}
7595
Avi Kivity851ba692009-08-24 11:10:17 +03007596static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007597{
Matt Gingell782d4222015-11-16 15:26:00 -08007598 return vcpu->run->request_interrupt_window &&
7599 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007600}
7601
Avi Kivity851ba692009-08-24 11:10:17 +03007602static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007603{
Avi Kivity851ba692009-08-24 11:10:17 +03007604 struct kvm_run *kvm_run = vcpu->run;
7605
Jan Kiszka91586a32009-10-05 13:07:21 +02007606 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007607 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007608 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007609 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007610 kvm_run->ready_for_interrupt_injection =
7611 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007612 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007613}
7614
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007615static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7616{
7617 int max_irr, tpr;
7618
7619 if (!kvm_x86_ops->update_cr8_intercept)
7620 return;
7621
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007622 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007623 return;
7624
Andrey Smetanind62caab2015-11-10 15:36:33 +03007625 if (vcpu->arch.apicv_active)
7626 return;
7627
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007628 if (!vcpu->arch.apic->vapic_addr)
7629 max_irr = kvm_lapic_find_highest_irr(vcpu);
7630 else
7631 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007632
7633 if (max_irr != -1)
7634 max_irr >>= 4;
7635
7636 tpr = kvm_lapic_get_cr8(vcpu);
7637
7638 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
7639}
7640
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007641static int inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007642{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007643 int r;
7644
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007645 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007646
Liran Alon1a680e32018-03-23 03:01:33 +03007647 if (vcpu->arch.exception.injected)
7648 kvm_x86_ops->queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007649 /*
Liran Alona042c262018-03-23 03:01:32 +03007650 * Do not inject an NMI or interrupt if there is a pending
7651 * exception. Exceptions and interrupts are recognized at
7652 * instruction boundaries, i.e. the start of an instruction.
7653 * Trap-like exceptions, e.g. #DB, have higher priority than
7654 * NMIs and interrupts, i.e. traps are recognized before an
7655 * NMI/interrupt that's pending on the same instruction.
7656 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7657 * priority, but are only generated (pended) during instruction
7658 * execution, i.e. a pending fault-like exception means the
7659 * fault occurred on the *previous* instruction and must be
7660 * serviced prior to recognizing any new events in order to
7661 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007662 */
Liran Alon1a680e32018-03-23 03:01:33 +03007663 else if (!vcpu->arch.exception.pending) {
7664 if (vcpu->arch.nmi_injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007665 kvm_x86_ops->set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007666 else if (vcpu->arch.interrupt.injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007667 kvm_x86_ops->set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007668 }
7669
Liran Alon1a680e32018-03-23 03:01:33 +03007670 /*
7671 * Call check_nested_events() even if we reinjected a previous event
7672 * in order for caller to determine if it should require immediate-exit
7673 * from L2 to L1 due to pending L1 events which require exit
7674 * from L2 to L1.
7675 */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007676 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007677 r = kvm_x86_ops->check_nested_events(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007678 if (r != 0)
7679 return r;
7680 }
7681
7682 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007683 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007684 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7685 vcpu->arch.exception.has_error_code,
7686 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007687
Liran Alon1a680e32018-03-23 03:01:33 +03007688 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007689 vcpu->arch.exception.pending = false;
7690 vcpu->arch.exception.injected = true;
7691
Nadav Amitd6e8c852014-07-24 14:51:24 +03007692 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7693 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7694 X86_EFLAGS_RF);
7695
Jim Mattsonf10c7292018-10-16 14:29:23 -07007696 if (vcpu->arch.exception.nr == DB_VECTOR) {
7697 /*
7698 * This code assumes that nSVM doesn't use
7699 * check_nested_events(). If it does, the
7700 * DR6/DR7 changes should happen before L1
7701 * gets a #VMEXIT for an intercepted #DB in
7702 * L2. (Under VMX, on the other hand, the
7703 * DR6/DR7 changes should not happen in the
7704 * event of a VM-exit to L1 for an intercepted
7705 * #DB in L2.)
7706 */
7707 kvm_deliver_exception_payload(vcpu);
7708 if (vcpu->arch.dr7 & DR7_GD) {
7709 vcpu->arch.dr7 &= ~DR7_GD;
7710 kvm_update_dr7(vcpu);
7711 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007712 }
7713
Wanpeng Licfcd20e2017-07-13 18:30:39 -07007714 kvm_x86_ops->queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007715 }
7716
7717 /* Don't consider new event if we re-injected an event */
7718 if (kvm_event_needs_reinjection(vcpu))
7719 return 0;
7720
7721 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
7722 kvm_x86_ops->smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007723 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007724 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007725 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007726 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007727 --vcpu->arch.nmi_pending;
7728 vcpu->arch.nmi_injected = true;
7729 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007730 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007731 /*
7732 * Because interrupts can be injected asynchronously, we are
7733 * calling check_nested_events again here to avoid a race condition.
7734 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7735 * proposal and current concerns. Perhaps we should be setting
7736 * KVM_REQ_EVENT only on certain events and not unconditionally?
7737 */
7738 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007739 r = kvm_x86_ops->check_nested_events(vcpu);
Bandan Das9242b5b2014-07-08 00:30:23 -04007740 if (r != 0)
7741 return r;
7742 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007743 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007744 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7745 false);
7746 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007747 }
7748 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007749
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007750 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007751}
7752
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007753static void process_nmi(struct kvm_vcpu *vcpu)
7754{
7755 unsigned limit = 2;
7756
7757 /*
7758 * x86 is limited to one NMI running, and one NMI pending after it.
7759 * If an NMI is already in progress, limit further NMIs to just one.
7760 * Otherwise, allow two (and we'll inject the first one immediately).
7761 */
7762 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7763 limit = 1;
7764
7765 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7766 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7767 kvm_make_request(KVM_REQ_EVENT, vcpu);
7768}
7769
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007770static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007771{
7772 u32 flags = 0;
7773 flags |= seg->g << 23;
7774 flags |= seg->db << 22;
7775 flags |= seg->l << 21;
7776 flags |= seg->avl << 20;
7777 flags |= seg->present << 15;
7778 flags |= seg->dpl << 13;
7779 flags |= seg->s << 12;
7780 flags |= seg->type << 8;
7781 return flags;
7782}
7783
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007784static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007785{
7786 struct kvm_segment seg;
7787 int offset;
7788
7789 kvm_get_segment(vcpu, &seg, n);
7790 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7791
7792 if (n < 3)
7793 offset = 0x7f84 + n * 12;
7794 else
7795 offset = 0x7f2c + (n - 3) * 12;
7796
7797 put_smstate(u32, buf, offset + 8, seg.base);
7798 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007799 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007800}
7801
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007802#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007803static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007804{
7805 struct kvm_segment seg;
7806 int offset;
7807 u16 flags;
7808
7809 kvm_get_segment(vcpu, &seg, n);
7810 offset = 0x7e00 + n * 16;
7811
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007812 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007813 put_smstate(u16, buf, offset, seg.selector);
7814 put_smstate(u16, buf, offset + 2, flags);
7815 put_smstate(u32, buf, offset + 4, seg.limit);
7816 put_smstate(u64, buf, offset + 8, seg.base);
7817}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007818#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007819
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007820static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007821{
7822 struct desc_ptr dt;
7823 struct kvm_segment seg;
7824 unsigned long val;
7825 int i;
7826
7827 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7828 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7829 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7830 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7831
7832 for (i = 0; i < 8; i++)
7833 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7834
7835 kvm_get_dr(vcpu, 6, &val);
7836 put_smstate(u32, buf, 0x7fcc, (u32)val);
7837 kvm_get_dr(vcpu, 7, &val);
7838 put_smstate(u32, buf, 0x7fc8, (u32)val);
7839
7840 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7841 put_smstate(u32, buf, 0x7fc4, seg.selector);
7842 put_smstate(u32, buf, 0x7f64, seg.base);
7843 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007844 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007845
7846 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7847 put_smstate(u32, buf, 0x7fc0, seg.selector);
7848 put_smstate(u32, buf, 0x7f80, seg.base);
7849 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007850 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007851
7852 kvm_x86_ops->get_gdt(vcpu, &dt);
7853 put_smstate(u32, buf, 0x7f74, dt.address);
7854 put_smstate(u32, buf, 0x7f70, dt.size);
7855
7856 kvm_x86_ops->get_idt(vcpu, &dt);
7857 put_smstate(u32, buf, 0x7f58, dt.address);
7858 put_smstate(u32, buf, 0x7f54, dt.size);
7859
7860 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007861 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007862
7863 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7864
7865 /* revision id */
7866 put_smstate(u32, buf, 0x7efc, 0x00020000);
7867 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7868}
7869
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007870#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007871static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007872{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007873 struct desc_ptr dt;
7874 struct kvm_segment seg;
7875 unsigned long val;
7876 int i;
7877
7878 for (i = 0; i < 16; i++)
7879 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7880
7881 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7882 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7883
7884 kvm_get_dr(vcpu, 6, &val);
7885 put_smstate(u64, buf, 0x7f68, val);
7886 kvm_get_dr(vcpu, 7, &val);
7887 put_smstate(u64, buf, 0x7f60, val);
7888
7889 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7890 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7891 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7892
7893 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7894
7895 /* revision id */
7896 put_smstate(u32, buf, 0x7efc, 0x00020064);
7897
7898 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7899
7900 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7901 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007902 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007903 put_smstate(u32, buf, 0x7e94, seg.limit);
7904 put_smstate(u64, buf, 0x7e98, seg.base);
7905
7906 kvm_x86_ops->get_idt(vcpu, &dt);
7907 put_smstate(u32, buf, 0x7e84, dt.size);
7908 put_smstate(u64, buf, 0x7e88, dt.address);
7909
7910 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7911 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007912 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007913 put_smstate(u32, buf, 0x7e74, seg.limit);
7914 put_smstate(u64, buf, 0x7e78, seg.base);
7915
7916 kvm_x86_ops->get_gdt(vcpu, &dt);
7917 put_smstate(u32, buf, 0x7e64, dt.size);
7918 put_smstate(u64, buf, 0x7e68, dt.address);
7919
7920 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007921 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007922}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007923#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007924
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007925static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007926{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007927 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007928 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007929 char buf[512];
7930 u32 cr0;
7931
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007932 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007933 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007934#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007935 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007936 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007937 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007938#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007939 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007940
Ladi Prosek0234bf82017-10-11 16:54:40 +02007941 /*
7942 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7943 * vCPU state (e.g. leave guest mode) after we've saved the state into
7944 * the SMM state-save area.
7945 */
7946 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7947
7948 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007949 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007950
7951 if (kvm_x86_ops->get_nmi_mask(vcpu))
7952 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7953 else
7954 kvm_x86_ops->set_nmi_mask(vcpu, true);
7955
7956 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7957 kvm_rip_write(vcpu, 0x8000);
7958
7959 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7960 kvm_x86_ops->set_cr0(vcpu, cr0);
7961 vcpu->arch.cr0 = cr0;
7962
7963 kvm_x86_ops->set_cr4(vcpu, 0);
7964
Paolo Bonzini18c36262015-08-07 12:27:54 +02007965 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7966 dt.address = dt.size = 0;
7967 kvm_x86_ops->set_idt(vcpu, &dt);
7968
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007969 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7970
7971 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7972 cs.base = vcpu->arch.smbase;
7973
7974 ds.selector = 0;
7975 ds.base = 0;
7976
7977 cs.limit = ds.limit = 0xffffffff;
7978 cs.type = ds.type = 0x3;
7979 cs.dpl = ds.dpl = 0;
7980 cs.db = ds.db = 0;
7981 cs.s = ds.s = 1;
7982 cs.l = ds.l = 0;
7983 cs.g = ds.g = 1;
7984 cs.avl = ds.avl = 0;
7985 cs.present = ds.present = 1;
7986 cs.unusable = ds.unusable = 0;
7987 cs.padding = ds.padding = 0;
7988
7989 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7990 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7991 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7992 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7993 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
7994 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
7995
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007996#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007997 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007998 kvm_x86_ops->set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007999#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008000
8001 kvm_update_cpuid(vcpu);
8002 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008003}
8004
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008005static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008006{
8007 vcpu->arch.smi_pending = true;
8008 kvm_make_request(KVM_REQ_EVENT, vcpu);
8009}
8010
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008011void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8012 unsigned long *vcpu_bitmap)
8013{
8014 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008015
8016 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8017
Mao Wenandb5a95e2019-11-19 11:06:40 +08008018 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
8019 vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008020
8021 free_cpumask_var(cpus);
8022}
8023
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008024void kvm_make_scan_ioapic_request(struct kvm *kvm)
8025{
8026 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8027}
8028
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008029void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8030{
8031 if (!lapic_in_kernel(vcpu))
8032 return;
8033
8034 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8035 kvm_apic_update_apicv(vcpu);
8036 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
8037}
8038EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8039
8040/*
8041 * NOTE: Do not hold any lock prior to calling this.
8042 *
8043 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8044 * locked, because it calls __x86_set_memory_region() which does
8045 * synchronize_srcu(&kvm->srcu).
8046 */
8047void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8048{
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008049 if (!kvm_x86_ops->check_apicv_inhibit_reasons ||
8050 !kvm_x86_ops->check_apicv_inhibit_reasons(bit))
8051 return;
8052
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008053 if (activate) {
8054 if (!test_and_clear_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
8055 !kvm_apicv_activated(kvm))
8056 return;
8057 } else {
8058 if (test_and_set_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
8059 kvm_apicv_activated(kvm))
8060 return;
8061 }
8062
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008063 trace_kvm_apicv_update_request(activate, bit);
Suravee Suthikulpanit2de9d0c2019-11-14 14:15:11 -06008064 if (kvm_x86_ops->pre_update_apicv_exec_ctrl)
8065 kvm_x86_ops->pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008066 kvm_make_all_cpus_request(kvm, KVM_REQ_APICV_UPDATE);
8067}
8068EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8069
Yang Zhang3d81bc72013-04-11 19:25:13 +08008070static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008071{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008072 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008073 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008074
Andrey Smetanin63086302015-11-10 15:36:32 +03008075 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008076
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008077 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008078 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008079 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008080 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03008081 kvm_x86_ops->sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008082 if (ioapic_in_kernel(vcpu->kvm))
8083 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008084 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008085
8086 if (is_guest_mode(vcpu))
8087 vcpu->arch.load_eoi_exitmap_pending = true;
8088 else
8089 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8090}
8091
8092static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8093{
8094 u64 eoi_exit_bitmap[4];
8095
8096 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8097 return;
8098
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008099 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8100 vcpu_to_synic(vcpu)->vec_bitmap, 256);
8101 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008102}
8103
Michal Hocko93065ac2018-08-21 21:52:33 -07008104int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8105 unsigned long start, unsigned long end,
8106 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008107{
8108 unsigned long apic_address;
8109
8110 /*
8111 * The physical address of apic access page is stored in the VMCS.
8112 * Update it when it becomes invalid.
8113 */
8114 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8115 if (start <= apic_address && apic_address < end)
8116 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07008117
8118 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01008119}
8120
Tang Chen4256f432014-09-24 15:57:54 +08008121void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8122{
Tang Chenc24ae0d2014-09-24 15:57:58 +08008123 struct page *page = NULL;
8124
Paolo Bonzini35754c92015-07-29 12:05:37 +02008125 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008126 return;
8127
Tang Chen4256f432014-09-24 15:57:54 +08008128 if (!kvm_x86_ops->set_apic_access_page_addr)
8129 return;
8130
Tang Chenc24ae0d2014-09-24 15:57:58 +08008131 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02008132 if (is_error_page(page))
8133 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08008134 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
8135
8136 /*
8137 * Do not pin apic access page in memory, the MMU notifier
8138 * will call us again if it is migrated or swapped out.
8139 */
8140 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08008141}
Tang Chen4256f432014-09-24 15:57:54 +08008142
Sean Christophersond264ee02018-08-27 15:21:12 -07008143void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8144{
8145 smp_send_reschedule(vcpu->cpu);
8146}
8147EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8148
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008149/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008150 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008151 * exiting to the userspace. Otherwise, the value will be returned to the
8152 * userspace.
8153 */
Avi Kivity851ba692009-08-24 11:10:17 +03008154static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008155{
8156 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008157 bool req_int_win =
8158 dm_request_for_irq_injection(vcpu) &&
8159 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008160 enum exit_fastpath_completion exit_fastpath = EXIT_FASTPATH_NONE;
Matt Gingell62a193e2015-11-16 15:26:07 -08008161
Jan Kiszka730dca42013-04-28 10:50:52 +02008162 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008163
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008164 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008165 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
8166 if (unlikely(!kvm_x86_ops->get_vmcs12_pages(vcpu))) {
8167 r = 0;
8168 goto out;
8169 }
8170 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008171 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008172 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008173 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008174 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008175 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8176 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03008177 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8178 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008179 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8180 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008181 if (unlikely(r))
8182 goto out;
8183 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008184 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008185 kvm_mmu_sync_roots(vcpu);
Junaid Shahid6e427822018-06-27 14:59:08 -07008186 if (kvm_check_request(KVM_REQ_LOAD_CR3, vcpu))
8187 kvm_mmu_load_cr3(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008188 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008189 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008190 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008191 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008192 r = 0;
8193 goto out;
8194 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008195 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008196 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008197 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008198 r = 0;
8199 goto out;
8200 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008201 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8202 /* Page is swapped out. Do synthetic halt */
8203 vcpu->arch.apf.halted = true;
8204 r = 1;
8205 goto out;
8206 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008207 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8208 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008209 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8210 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008211 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8212 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008213 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008214 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008215 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008216 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008217 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8218 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8219 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008220 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008221 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8222 vcpu->run->eoi.vector =
8223 vcpu->arch.pending_ioapic_eoi;
8224 r = 0;
8225 goto out;
8226 }
8227 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008228 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8229 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008230 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8231 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008232 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8233 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008234 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8235 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8236 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8237 r = 0;
8238 goto out;
8239 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008240 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8241 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8242 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8243 r = 0;
8244 goto out;
8245 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008246 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8247 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8248 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8249 r = 0;
8250 goto out;
8251 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008252
8253 /*
8254 * KVM_REQ_HV_STIMER has to be processed after
8255 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8256 * depend on the guest clock being up-to-date
8257 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008258 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8259 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008260 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8261 kvm_vcpu_update_apicv(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008262 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008263
Avi Kivityb463a6f2010-07-20 15:06:17 +03008264 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008265 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008266 kvm_apic_accept_events(vcpu);
8267 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8268 r = 1;
8269 goto out;
8270 }
8271
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008272 if (inject_pending_event(vcpu) != 0)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008273 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008274 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008275 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008276 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008277 * SMIs have three cases:
8278 * 1) They can be nested, and then there is nothing to
8279 * do here because RSM will cause a vmexit anyway.
8280 * 2) There is an ISA-specific reason why SMI cannot be
8281 * injected, and the moment when this changes can be
8282 * intercepted.
8283 * 3) Or the SMI can be pending because
8284 * inject_pending_event has completed the injection
8285 * of an IRQ or NMI from the previous vmexit, and
8286 * then we request an immediate exit to inject the
8287 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008288 */
8289 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008290 if (!kvm_x86_ops->enable_smi_window(vcpu))
8291 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008292 if (vcpu->arch.nmi_pending)
8293 kvm_x86_ops->enable_nmi_window(vcpu);
8294 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
8295 kvm_x86_ops->enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008296 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008297 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008298
Avi Kivity3842d132010-07-27 12:30:24 +03008299 if (kvm_lapic_enabled(vcpu)) {
8300 update_cr8_intercept(vcpu);
8301 kvm_lapic_sync_to_vapic(vcpu);
8302 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008303 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008304
Avi Kivityd8368af2012-05-14 18:07:56 +03008305 r = kvm_mmu_reload(vcpu);
8306 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008307 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008308 }
8309
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008310 preempt_disable();
8311
8312 kvm_x86_ops->prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008313
8314 /*
8315 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8316 * IPI are then delayed after guest entry, which ensures that they
8317 * result in virtual interrupt delivery.
8318 */
8319 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008320 vcpu->mode = IN_GUEST_MODE;
8321
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008322 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8323
Lan Tianyu0f127d12016-03-13 11:10:29 +08008324 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008325 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008326 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008327 *
Luwei Kang81b01662019-01-31 16:52:02 +08008328 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008329 * pairs with the memory barrier implicit in pi_test_and_set_on
8330 * (see vmx_deliver_posted_interrupt).
8331 *
8332 * 3) This also orders the write to mode from any reads to the page
8333 * tables done while the VCPU is running. Please see the comment
8334 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008335 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008336 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008337
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008338 /*
8339 * This handles the case where a posted interrupt was
8340 * notified with kvm_vcpu_kick.
8341 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008342 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
8343 kvm_x86_ops->sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008344
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008345 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008346 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008347 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008348 smp_wmb();
8349 local_irq_enable();
8350 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008351 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008352 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008353 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008354 }
8355
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008356 if (req_immediate_exit) {
8357 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersond264ee02018-08-27 15:21:12 -07008358 kvm_x86_ops->request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008359 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008360
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008361 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008362 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008363
Sean Christopherson2620fe22020-01-17 11:30:51 -08008364 fpregs_assert_state_consistent();
8365 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8366 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008367
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008368 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008369 set_debugreg(0, 7);
8370 set_debugreg(vcpu->arch.eff_db[0], 0);
8371 set_debugreg(vcpu->arch.eff_db[1], 1);
8372 set_debugreg(vcpu->arch.eff_db[2], 2);
8373 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008374 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008375 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008376 }
8377
Avi Kivity851ba692009-08-24 11:10:17 +03008378 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008379
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008380 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008381 * Do this here before restoring debug registers on the host. And
8382 * since we do this before handling the vmexit, a DR access vmexit
8383 * can (a) read the correct value of the debug registers, (b) set
8384 * KVM_DEBUGREG_WONT_EXIT again.
8385 */
8386 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008387 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
8388 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008389 kvm_update_dr0123(vcpu);
8390 kvm_update_dr6(vcpu);
8391 kvm_update_dr7(vcpu);
8392 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008393 }
8394
8395 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008396 * If the guest has used debug registers, at least dr7
8397 * will be disabled while returning to the host.
8398 * If we don't have active breakpoints in the host, we don't
8399 * care about the messed up debug address registers. But if
8400 * we have some of them active, restore the old state.
8401 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008402 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008403 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008404
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008405 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008406
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008407 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008408 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008409
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008410 kvm_x86_ops->handle_exit_irqoff(vcpu, &exit_fastpath);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008411
Sean Christophersond7a08882019-07-10 09:07:34 -07008412 /*
8413 * Consume any pending interrupts, including the possible source of
8414 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8415 * An instruction is required after local_irq_enable() to fully unblock
8416 * interrupts on processors that implement an interrupt shadow, the
8417 * stat.exits increment will do nicely.
8418 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008419 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008420 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008421 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008422 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008423 kvm_after_interrupt(vcpu);
8424
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008425 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008426 if (lapic_in_kernel(vcpu)) {
8427 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8428 if (delta != S64_MIN) {
8429 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8430 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8431 }
8432 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008433
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008434 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008435 preempt_enable();
8436
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008437 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008438
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008439 /*
8440 * Profile KVM exit RIPs:
8441 */
8442 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008443 unsigned long rip = kvm_rip_read(vcpu);
8444 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008445 }
8446
Zachary Amsdencc578282012-02-03 15:43:50 -02008447 if (unlikely(vcpu->arch.tsc_always_catchup))
8448 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008449
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008450 if (vcpu->arch.apic_attention)
8451 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008452
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008453 r = kvm_x86_ops->handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008454 return r;
8455
8456cancel_injection:
8457 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008458 if (unlikely(vcpu->arch.apic_attention))
8459 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008460out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008461 return r;
8462}
8463
Paolo Bonzini362c6982015-02-06 12:48:04 +01008464static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8465{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008466 if (!kvm_arch_vcpu_runnable(vcpu) &&
8467 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008468 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8469 kvm_vcpu_block(vcpu);
8470 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008471
8472 if (kvm_x86_ops->post_block)
8473 kvm_x86_ops->post_block(vcpu);
8474
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008475 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8476 return 1;
8477 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008478
Paolo Bonzini362c6982015-02-06 12:48:04 +01008479 kvm_apic_accept_events(vcpu);
8480 switch(vcpu->arch.mp_state) {
8481 case KVM_MP_STATE_HALTED:
8482 vcpu->arch.pv.pv_unhalted = false;
8483 vcpu->arch.mp_state =
8484 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008485 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008486 case KVM_MP_STATE_RUNNABLE:
8487 vcpu->arch.apf.halted = false;
8488 break;
8489 case KVM_MP_STATE_INIT_RECEIVED:
8490 break;
8491 default:
8492 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008493 }
8494 return 1;
8495}
8496
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008497static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8498{
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008499 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008500 kvm_x86_ops->check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008501
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008502 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8503 !vcpu->arch.apf.halted);
8504}
8505
Paolo Bonzini362c6982015-02-06 12:48:04 +01008506static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008507{
8508 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008509 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008510
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008511 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008512 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008513
Paolo Bonzini362c6982015-02-06 12:48:04 +01008514 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008515 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008516 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008517 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008518 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008519 }
8520
Gleb Natapov09cec752009-03-23 15:11:44 +02008521 if (r <= 0)
8522 break;
8523
Radim Krčmář72875d82017-04-26 22:32:19 +02008524 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008525 if (kvm_cpu_has_pending_timer(vcpu))
8526 kvm_inject_pending_timer_irqs(vcpu);
8527
Matt Gingell782d4222015-11-16 15:26:00 -08008528 if (dm_request_for_irq_injection(vcpu) &&
8529 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008530 r = 0;
8531 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008532 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008533 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008534 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008535
8536 kvm_check_async_pf_completion(vcpu);
8537
Gleb Natapov09cec752009-03-23 15:11:44 +02008538 if (signal_pending(current)) {
8539 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008540 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008541 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008542 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008543 }
8544 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008545 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008546 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008547 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008548 }
8549 }
8550
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008551 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008552
8553 return r;
8554}
8555
Gleb Natapov716d51a2012-09-03 15:24:26 +03008556static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8557{
8558 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008559
Gleb Natapov716d51a2012-09-03 15:24:26 +03008560 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008561 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008562 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008563 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008564}
8565
8566static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8567{
8568 BUG_ON(!vcpu->arch.pio.count);
8569
8570 return complete_emulated_io(vcpu);
8571}
8572
Avi Kivityf78146b2012-04-18 19:22:47 +03008573/*
8574 * Implements the following, as a state machine:
8575 *
8576 * read:
8577 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008578 * for each mmio piece in the fragment
8579 * write gpa, len
8580 * exit
8581 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008582 * execute insn
8583 *
8584 * write:
8585 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008586 * for each mmio piece in the fragment
8587 * write gpa, len
8588 * copy data
8589 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008590 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008591static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008592{
8593 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008594 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008595 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008596
Gleb Natapov716d51a2012-09-03 15:24:26 +03008597 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008598
Gleb Natapov716d51a2012-09-03 15:24:26 +03008599 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008600 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8601 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008602 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008603 memcpy(frag->data, run->mmio.data, len);
8604
8605 if (frag->len <= 8) {
8606 /* Switch to the next fragment. */
8607 frag++;
8608 vcpu->mmio_cur_fragment++;
8609 } else {
8610 /* Go forward to the next mmio piece. */
8611 frag->data += len;
8612 frag->gpa += len;
8613 frag->len -= len;
8614 }
8615
Andrew Honiga08d3b32014-02-27 19:35:14 +01008616 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008617 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008618
8619 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008620 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008621 return 1;
8622 vcpu->mmio_read_completed = 1;
8623 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008624 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008625
Gleb Natapov716d51a2012-09-03 15:24:26 +03008626 run->exit_reason = KVM_EXIT_MMIO;
8627 run->mmio.phys_addr = frag->gpa;
8628 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008629 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8630 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008631 run->mmio.is_write = vcpu->mmio_is_write;
8632 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8633 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008634}
8635
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008636static void kvm_save_current_fpu(struct fpu *fpu)
8637{
8638 /*
8639 * If the target FPU state is not resident in the CPU registers, just
8640 * memcpy() from current, else save CPU state directly to the target.
8641 */
8642 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8643 memcpy(&fpu->state, &current->thread.fpu.state,
8644 fpu_kernel_xstate_size);
8645 else
8646 copy_fpregs_to_fpstate(fpu);
8647}
8648
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008649/* Swap (qemu) user FPU context for the guest FPU context. */
8650static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8651{
Rik van Riel5f409e22019-04-03 18:41:52 +02008652 fpregs_lock();
8653
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008654 kvm_save_current_fpu(vcpu->arch.user_fpu);
8655
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008656 /* PKRU is separately restored in kvm_x86_ops->run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008657 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008658 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008659
8660 fpregs_mark_activate();
8661 fpregs_unlock();
8662
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008663 trace_kvm_fpu(1);
8664}
8665
8666/* When vcpu_run ends, restore user space FPU context. */
8667static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8668{
Rik van Riel5f409e22019-04-03 18:41:52 +02008669 fpregs_lock();
8670
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008671 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8672
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008673 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008674
8675 fpregs_mark_activate();
8676 fpregs_unlock();
8677
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008678 ++vcpu->stat.fpu_reload;
8679 trace_kvm_fpu(0);
8680}
8681
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008682int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8683{
8684 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008685
Christoffer Dallaccb7572017-12-04 21:35:25 +01008686 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008687 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008688 kvm_load_guest_fpu(vcpu);
8689
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008690 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008691 if (kvm_run->immediate_exit) {
8692 r = -EINTR;
8693 goto out;
8694 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008695 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008696 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008697 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008698 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008699 if (signal_pending(current)) {
8700 r = -EINTR;
8701 vcpu->run->exit_reason = KVM_EXIT_INTR;
8702 ++vcpu->stat.signal_exits;
8703 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008704 goto out;
8705 }
8706
Ken Hofsass01643c52018-01-31 16:03:36 -08008707 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8708 r = -EINVAL;
8709 goto out;
8710 }
8711
8712 if (vcpu->run->kvm_dirty_regs) {
8713 r = sync_regs(vcpu);
8714 if (r != 0)
8715 goto out;
8716 }
8717
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008718 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008719 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008720 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8721 r = -EINVAL;
8722 goto out;
8723 }
8724 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008725
Gleb Natapov716d51a2012-09-03 15:24:26 +03008726 if (unlikely(vcpu->arch.complete_userspace_io)) {
8727 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8728 vcpu->arch.complete_userspace_io = NULL;
8729 r = cui(vcpu);
8730 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008731 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008732 } else
8733 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008734
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008735 if (kvm_run->immediate_exit)
8736 r = -EINTR;
8737 else
8738 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008739
8740out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008741 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008742 if (vcpu->run->kvm_valid_regs)
8743 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008744 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008745 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008746
Christoffer Dallaccb7572017-12-04 21:35:25 +01008747 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008748 return r;
8749}
8750
Ken Hofsass01643c52018-01-31 16:03:36 -08008751static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008752{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008753 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8754 /*
8755 * We are here if userspace calls get_regs() in the middle of
8756 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008757 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008758 * that usually, but some bad designed PV devices (vmware
8759 * backdoor interface) need this to work
8760 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008761 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008762 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8763 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008764 regs->rax = kvm_rax_read(vcpu);
8765 regs->rbx = kvm_rbx_read(vcpu);
8766 regs->rcx = kvm_rcx_read(vcpu);
8767 regs->rdx = kvm_rdx_read(vcpu);
8768 regs->rsi = kvm_rsi_read(vcpu);
8769 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008770 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008771 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008772#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008773 regs->r8 = kvm_r8_read(vcpu);
8774 regs->r9 = kvm_r9_read(vcpu);
8775 regs->r10 = kvm_r10_read(vcpu);
8776 regs->r11 = kvm_r11_read(vcpu);
8777 regs->r12 = kvm_r12_read(vcpu);
8778 regs->r13 = kvm_r13_read(vcpu);
8779 regs->r14 = kvm_r14_read(vcpu);
8780 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008781#endif
8782
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008783 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008784 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008785}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008786
Ken Hofsass01643c52018-01-31 16:03:36 -08008787int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8788{
8789 vcpu_load(vcpu);
8790 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008791 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008792 return 0;
8793}
8794
Ken Hofsass01643c52018-01-31 16:03:36 -08008795static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008796{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008797 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8798 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8799
Sean Christophersonde3cd112019-04-30 10:36:17 -07008800 kvm_rax_write(vcpu, regs->rax);
8801 kvm_rbx_write(vcpu, regs->rbx);
8802 kvm_rcx_write(vcpu, regs->rcx);
8803 kvm_rdx_write(vcpu, regs->rdx);
8804 kvm_rsi_write(vcpu, regs->rsi);
8805 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008806 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008807 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008808#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008809 kvm_r8_write(vcpu, regs->r8);
8810 kvm_r9_write(vcpu, regs->r9);
8811 kvm_r10_write(vcpu, regs->r10);
8812 kvm_r11_write(vcpu, regs->r11);
8813 kvm_r12_write(vcpu, regs->r12);
8814 kvm_r13_write(vcpu, regs->r13);
8815 kvm_r14_write(vcpu, regs->r14);
8816 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008817#endif
8818
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008819 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008820 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008821
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008822 vcpu->arch.exception.pending = false;
8823
Avi Kivity3842d132010-07-27 12:30:24 +03008824 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008825}
Avi Kivity3842d132010-07-27 12:30:24 +03008826
Ken Hofsass01643c52018-01-31 16:03:36 -08008827int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8828{
8829 vcpu_load(vcpu);
8830 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008831 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008832 return 0;
8833}
8834
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008835void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8836{
8837 struct kvm_segment cs;
8838
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008839 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008840 *db = cs.db;
8841 *l = cs.l;
8842}
8843EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8844
Ken Hofsass01643c52018-01-31 16:03:36 -08008845static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008846{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008847 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008848
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008849 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8850 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8851 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8852 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8853 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8854 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008855
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008856 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8857 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008858
8859 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008860 sregs->idt.limit = dt.size;
8861 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008862 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008863 sregs->gdt.limit = dt.size;
8864 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008865
Avi Kivity4d4ec082009-12-29 18:07:30 +02008866 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008867 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008868 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008869 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008870 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008871 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008872 sregs->apic_base = kvm_get_apic_base(vcpu);
8873
Jordan Borgner0e96f312018-10-28 12:58:28 +00008874 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008875
Liran Alon04140b42018-03-23 03:01:31 +03008876 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008877 set_bit(vcpu->arch.interrupt.nr,
8878 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008879}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008880
Ken Hofsass01643c52018-01-31 16:03:36 -08008881int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8882 struct kvm_sregs *sregs)
8883{
8884 vcpu_load(vcpu);
8885 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008886 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008887 return 0;
8888}
8889
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008890int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8891 struct kvm_mp_state *mp_state)
8892{
Christoffer Dallfd232562017-12-04 21:35:30 +01008893 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008894 if (kvm_mpx_supported())
8895 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008896
Jan Kiszka66450a22013-03-13 12:42:34 +01008897 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308898 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8899 vcpu->arch.pv.pv_unhalted)
8900 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8901 else
8902 mp_state->mp_state = vcpu->arch.mp_state;
8903
Sean Christophersonf958bd22019-12-09 12:19:31 -08008904 if (kvm_mpx_supported())
8905 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008906 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008907 return 0;
8908}
8909
8910int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8911 struct kvm_mp_state *mp_state)
8912{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008913 int ret = -EINVAL;
8914
8915 vcpu_load(vcpu);
8916
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008917 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008918 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008919 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008920
Liran Alon27cbe7d2019-11-11 11:16:40 +02008921 /*
8922 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
8923 * INIT state; latched init should be reported using
8924 * KVM_SET_VCPU_EVENTS, so reject it here.
8925 */
8926 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01008927 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8928 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008929 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008930
Jan Kiszka66450a22013-03-13 12:42:34 +01008931 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8932 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8933 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8934 } else
8935 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008936 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008937
8938 ret = 0;
8939out:
8940 vcpu_put(vcpu);
8941 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008942}
8943
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008944int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8945 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008946{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008947 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008948 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008949
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008950 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008951
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008952 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008953 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07008954 if (ret) {
8955 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8956 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
8957 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008958 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07008959 }
Gleb Natapovc6975182010-02-18 12:15:01 +02008960
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008961 kvm_rip_write(vcpu, ctxt->eip);
8962 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008963 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02008964}
8965EXPORT_SYMBOL_GPL(kvm_task_switch);
8966
Peng Hao3140c152018-04-02 09:15:32 +08008967static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05008968{
Tianyu Lan37b95952018-01-16 17:34:07 +08008969 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05008970 /*
8971 * When EFER.LME and CR0.PG are set, the processor is in
8972 * 64-bit mode (though maybe in a 32-bit code segment).
8973 * CR4.PAE and EFER.LMA must be set.
8974 */
Tianyu Lan37b95952018-01-16 17:34:07 +08008975 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05008976 || !(sregs->efer & EFER_LMA))
8977 return -EINVAL;
8978 } else {
8979 /*
8980 * Not in 64-bit mode: EFER.LMA is clear and the code
8981 * segment cannot be 64-bit.
8982 */
8983 if (sregs->efer & EFER_LMA || sregs->cs.l)
8984 return -EINVAL;
8985 }
8986
Wanpeng Li3ca94192019-09-18 17:50:10 +08008987 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05008988}
8989
Ken Hofsass01643c52018-01-31 16:03:36 -08008990static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008991{
Jan Kiszka58cb6282014-01-24 16:48:44 +01008992 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008993 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05008994 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008995 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02008996 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008997 int ret = -EINVAL;
8998
Lan Tianyuf2981032017-12-14 03:01:52 -05008999 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009000 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009001
Jim Mattsond3802282017-08-10 10:14:13 -07009002 apic_base_msr.data = sregs->apic_base;
9003 apic_base_msr.host_initiated = true;
9004 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009005 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009006
Gleb Natapov89a27f42010-02-16 10:51:48 +02009007 dt.size = sregs->idt.limit;
9008 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009009 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009010 dt.size = sregs->gdt.limit;
9011 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009012 kvm_x86_ops->set_gdt(vcpu, &dt);
9013
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009014 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009015 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009016 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009017 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009018
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009019 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009020
Avi Kivityf6801df2010-01-21 15:31:50 +02009021 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009022 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009023
Avi Kivity4d4ec082009-12-29 18:07:30 +02009024 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009025 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009026 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009027
Avi Kivityfc78f512009-12-07 12:16:48 +02009028 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009029 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9030 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009031 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009032 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009033 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009034
9035 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009036 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009037 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009038 mmu_reset_needed = 1;
9039 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009040 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009041
9042 if (mmu_reset_needed)
9043 kvm_mmu_reset_context(vcpu);
9044
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009045 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009046 pending_vec = find_first_bit(
9047 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9048 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009049 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009050 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009051 }
9052
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009053 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9054 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9055 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9056 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9057 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9058 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009059
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009060 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9061 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009062
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009063 update_cr8_intercept(vcpu);
9064
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009065 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009066 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009067 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009068 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009069 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9070
Avi Kivity3842d132010-07-27 12:30:24 +03009071 kvm_make_request(KVM_REQ_EVENT, vcpu);
9072
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009073 ret = 0;
9074out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009075 return ret;
9076}
9077
9078int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9079 struct kvm_sregs *sregs)
9080{
9081 int ret;
9082
9083 vcpu_load(vcpu);
9084 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009085 vcpu_put(vcpu);
9086 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009087}
9088
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009089int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9090 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009091{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009092 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009093 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009094
Christoffer Dall66b56562017-12-04 21:35:33 +01009095 vcpu_load(vcpu);
9096
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009097 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9098 r = -EBUSY;
9099 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009100 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009101 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9102 kvm_queue_exception(vcpu, DB_VECTOR);
9103 else
9104 kvm_queue_exception(vcpu, BP_VECTOR);
9105 }
9106
Jan Kiszka91586a32009-10-05 13:07:21 +02009107 /*
9108 * Read rflags as long as potentially injected trace flags are still
9109 * filtered out.
9110 */
9111 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009112
9113 vcpu->guest_debug = dbg->control;
9114 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9115 vcpu->guest_debug = 0;
9116
9117 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009118 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9119 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009120 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009121 } else {
9122 for (i = 0; i < KVM_NR_DB_REGS; i++)
9123 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009124 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009125 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009126
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009127 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9128 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9129 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009130
Jan Kiszka91586a32009-10-05 13:07:21 +02009131 /*
9132 * Trigger an rflags update that will inject or remove the trace
9133 * flags.
9134 */
9135 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009136
Paolo Bonzinia96036b2015-11-10 11:55:36 +01009137 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009138
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009139 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009140
Avi Kivity2122ff52010-05-13 11:25:04 +03009141out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009142 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009143 return r;
9144}
9145
9146/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009147 * Translate a guest virtual address to a guest physical address.
9148 */
9149int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9150 struct kvm_translation *tr)
9151{
9152 unsigned long vaddr = tr->linear_address;
9153 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009154 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009155
Christoffer Dall1da5b612017-12-04 21:35:32 +01009156 vcpu_load(vcpu);
9157
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009158 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009159 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009160 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009161 tr->physical_address = gpa;
9162 tr->valid = gpa != UNMAPPED_GVA;
9163 tr->writeable = 1;
9164 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009165
Christoffer Dall1da5b612017-12-04 21:35:32 +01009166 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009167 return 0;
9168}
9169
Hollis Blanchardd0752062007-10-31 17:24:25 -05009170int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9171{
Christoffer Dall13931232017-12-04 21:35:34 +01009172 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009173
Christoffer Dall13931232017-12-04 21:35:34 +01009174 vcpu_load(vcpu);
9175
Marc Orrb666a4b2018-11-06 14:53:56 -08009176 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009177 memcpy(fpu->fpr, fxsave->st_space, 128);
9178 fpu->fcw = fxsave->cwd;
9179 fpu->fsw = fxsave->swd;
9180 fpu->ftwx = fxsave->twd;
9181 fpu->last_opcode = fxsave->fop;
9182 fpu->last_ip = fxsave->rip;
9183 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009184 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009185
Christoffer Dall13931232017-12-04 21:35:34 +01009186 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009187 return 0;
9188}
9189
9190int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9191{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009192 struct fxregs_state *fxsave;
9193
9194 vcpu_load(vcpu);
9195
Marc Orrb666a4b2018-11-06 14:53:56 -08009196 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009197
Hollis Blanchardd0752062007-10-31 17:24:25 -05009198 memcpy(fxsave->st_space, fpu->fpr, 128);
9199 fxsave->cwd = fpu->fcw;
9200 fxsave->swd = fpu->fsw;
9201 fxsave->twd = fpu->ftwx;
9202 fxsave->fop = fpu->last_opcode;
9203 fxsave->rip = fpu->last_ip;
9204 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009205 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009206
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009207 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009208 return 0;
9209}
9210
Ken Hofsass01643c52018-01-31 16:03:36 -08009211static void store_regs(struct kvm_vcpu *vcpu)
9212{
9213 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9214
9215 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9216 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9217
9218 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9219 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9220
9221 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9222 kvm_vcpu_ioctl_x86_get_vcpu_events(
9223 vcpu, &vcpu->run->s.regs.events);
9224}
9225
9226static int sync_regs(struct kvm_vcpu *vcpu)
9227{
9228 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9229 return -EINVAL;
9230
9231 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9232 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9233 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9234 }
9235 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9236 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9237 return -EINVAL;
9238 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9239 }
9240 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9241 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9242 vcpu, &vcpu->run->s.regs.events))
9243 return -EINVAL;
9244 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9245 }
9246
9247 return 0;
9248}
9249
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009250static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009251{
Marc Orrb666a4b2018-11-06 14:53:56 -08009252 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009253 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009254 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009255 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009256
Dexuan Cui2acf9232010-06-10 11:27:12 +08009257 /*
9258 * Ensure guest xcr0 is valid for loading
9259 */
Dave Hansend91cab72015-09-02 16:31:26 -07009260 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009261
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009262 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009263}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009264
Sean Christopherson897cc382019-12-18 13:55:09 -08009265int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009266{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009267 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009268 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9269 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009270
Sean Christopherson897cc382019-12-18 13:55:09 -08009271 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009272}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009273
Sean Christophersone529ef62019-12-18 13:55:15 -08009274int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009275{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009276 struct page *page;
9277 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009278
Sean Christopherson95a0d012019-12-18 13:55:23 -08009279 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9280 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9281 else
9282 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9283
9284 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9285
9286 r = kvm_mmu_create(vcpu);
9287 if (r < 0)
9288 return r;
9289
9290 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009291 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9292 if (r < 0)
9293 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009294 if (kvm_apicv_activated(vcpu->kvm))
9295 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009296 } else
9297 static_key_slow_inc(&kvm_no_apic_vcpu);
9298
9299 r = -ENOMEM;
9300
9301 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9302 if (!page)
9303 goto fail_free_lapic;
9304 vcpu->arch.pio_data = page_address(page);
9305
9306 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9307 GFP_KERNEL_ACCOUNT);
9308 if (!vcpu->arch.mce_banks)
9309 goto fail_free_pio_data;
9310 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9311
9312 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9313 GFP_KERNEL_ACCOUNT))
9314 goto fail_free_mce_banks;
9315
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009316 if (!alloc_emulate_ctxt(vcpu))
9317 goto free_wbinvd_dirty_mask;
9318
Sean Christopherson95a0d012019-12-18 13:55:23 -08009319 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9320 GFP_KERNEL_ACCOUNT);
9321 if (!vcpu->arch.user_fpu) {
9322 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009323 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009324 }
9325
9326 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9327 GFP_KERNEL_ACCOUNT);
9328 if (!vcpu->arch.guest_fpu) {
9329 pr_err("kvm: failed to allocate vcpu's fpu\n");
9330 goto free_user_fpu;
9331 }
9332 fx_init(vcpu);
9333
9334 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
9335
9336 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9337
9338 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9339
9340 kvm_async_pf_hash_reset(vcpu);
9341 kvm_pmu_init(vcpu);
9342
9343 vcpu->arch.pending_external_vector = -1;
9344 vcpu->arch.preempted_in_kernel = false;
9345
9346 kvm_hv_vcpu_init(vcpu);
9347
9348 r = kvm_x86_ops->vcpu_create(vcpu);
9349 if (r)
9350 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009351
Sean Christopherson0cf91352019-03-07 15:43:02 -08009352 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009353 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009354 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009355 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009356 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009357 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009358 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009359 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009360
9361free_guest_fpu:
9362 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9363free_user_fpu:
9364 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009365free_emulate_ctxt:
9366 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009367free_wbinvd_dirty_mask:
9368 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9369fail_free_mce_banks:
9370 kfree(vcpu->arch.mce_banks);
9371fail_free_pio_data:
9372 free_page((unsigned long)vcpu->arch.pio_data);
9373fail_free_lapic:
9374 kvm_free_lapic(vcpu);
9375fail_mmu_destroy:
9376 kvm_mmu_destroy(vcpu);
9377 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009378}
9379
Dominik Dingel31928aa2014-12-04 15:47:07 +01009380void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009381{
Will Auld8fe8ab42012-11-29 12:42:12 -08009382 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009383 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009384
Roman Kagand3457c82017-07-14 17:13:20 +03009385 kvm_hv_vcpu_postcreate(vcpu);
9386
Christoffer Dallec7660c2017-12-04 21:35:23 +01009387 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009388 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009389 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009390 msr.data = 0x0;
9391 msr.index = MSR_IA32_TSC;
9392 msr.host_initiated = true;
9393 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009394 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009395
9396 /* poll control enabled by default */
9397 vcpu->arch.msr_kvm_poll_control = 1;
9398
Christoffer Dallec7660c2017-12-04 21:35:23 +01009399 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009400
Wanpeng Lib34de572020-02-28 11:18:41 +08009401 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9402 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9403 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009404}
9405
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009406void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009407{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009408 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009409 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009410
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009411 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9412
Sean Christopherson50b143e2019-12-18 13:55:07 -08009413 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009414
9415 kvm_x86_ops->vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009416
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009417 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009418 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9419 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9420 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009421
9422 kvm_hv_vcpu_uninit(vcpu);
9423 kvm_pmu_destroy(vcpu);
9424 kfree(vcpu->arch.mce_banks);
9425 kvm_free_lapic(vcpu);
9426 idx = srcu_read_lock(&vcpu->kvm->srcu);
9427 kvm_mmu_destroy(vcpu);
9428 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9429 free_page((unsigned long)vcpu->arch.pio_data);
9430 if (!lapic_in_kernel(vcpu))
9431 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009432}
9433
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009434void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009435{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009436 kvm_lapic_reset(vcpu, init_event);
9437
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009438 vcpu->arch.hflags = 0;
9439
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009440 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009441 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009442 atomic_set(&vcpu->arch.nmi_queued, 0);
9443 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009444 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009445 kvm_clear_interrupt_queue(vcpu);
9446 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009447
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009448 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009449 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009450 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01009451 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009452 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009453 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009454
Nadav Amit11190222015-04-02 03:10:38 +03009455 vcpu->arch.cr2 = 0;
9456
Avi Kivity3842d132010-07-27 12:30:24 +03009457 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009458 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009459 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009460
Glauber Costa12f9a482011-02-01 14:16:40 -05009461 kvmclock_reset(vcpu);
9462
Gleb Natapovaf585b92010-10-14 11:22:46 +02009463 kvm_clear_async_pf_completion_queue(vcpu);
9464 kvm_async_pf_hash_reset(vcpu);
9465 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009466
Wanpeng Lia554d202017-10-11 05:10:19 -07009467 if (kvm_mpx_supported()) {
9468 void *mpx_state_buffer;
9469
9470 /*
9471 * To avoid have the INIT path from kvm_apic_has_events() that be
9472 * called with loaded FPU and does not let userspace fix the state.
9473 */
Rik van Rielf775b132017-11-14 16:54:23 -05009474 if (init_event)
9475 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009476 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009477 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009478 if (mpx_state_buffer)
9479 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009480 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009481 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009482 if (mpx_state_buffer)
9483 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009484 if (init_event)
9485 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009486 }
9487
Paolo Bonzini64d60672015-05-07 11:36:11 +02009488 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009489 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009490 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009491
Kyle Hueydb2336a2017-03-20 01:16:28 -07009492 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009493
9494 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009495 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009496
Julian Stecklina66f7b722012-12-05 15:26:19 +01009497 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9498 vcpu->arch.regs_avail = ~0;
9499 vcpu->arch.regs_dirty = ~0;
9500
Wanpeng Lia554d202017-10-11 05:10:19 -07009501 vcpu->arch.ia32_xss = 0;
9502
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009503 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009504}
9505
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009506void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009507{
9508 struct kvm_segment cs;
9509
9510 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9511 cs.selector = vector << 8;
9512 cs.base = vector << 12;
9513 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9514 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009515}
9516
Radim Krčmář13a34e02014-08-28 15:13:03 +02009517int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009518{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009519 struct kvm *kvm;
9520 struct kvm_vcpu *vcpu;
9521 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009522 int ret;
9523 u64 local_tsc;
9524 u64 max_tsc = 0;
9525 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009526
9527 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009528 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009529 if (ret != 0)
9530 return ret;
9531
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009532 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009533 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009534 list_for_each_entry(kvm, &vm_list, vm_list) {
9535 kvm_for_each_vcpu(i, vcpu, kvm) {
9536 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009537 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009538 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9539 backwards_tsc = true;
9540 if (vcpu->arch.last_host_tsc > max_tsc)
9541 max_tsc = vcpu->arch.last_host_tsc;
9542 }
9543 }
9544 }
9545
9546 /*
9547 * Sometimes, even reliable TSCs go backwards. This happens on
9548 * platforms that reset TSC during suspend or hibernate actions, but
9549 * maintain synchronization. We must compensate. Fortunately, we can
9550 * detect that condition here, which happens early in CPU bringup,
9551 * before any KVM threads can be running. Unfortunately, we can't
9552 * bring the TSCs fully up to date with real time, as we aren't yet far
9553 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009554 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009555 * variables that haven't been updated yet.
9556 *
9557 * So we simply find the maximum observed TSC above, then record the
9558 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9559 * the adjustment will be applied. Note that we accumulate
9560 * adjustments, in case multiple suspend cycles happen before some VCPU
9561 * gets a chance to run again. In the event that no KVM threads get a
9562 * chance to run, we will miss the entire elapsed period, as we'll have
9563 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9564 * loose cycle time. This isn't too big a deal, since the loss will be
9565 * uniform across all VCPUs (not to mention the scenario is extremely
9566 * unlikely). It is possible that a second hibernate recovery happens
9567 * much faster than a first, causing the observed TSC here to be
9568 * smaller; this would require additional padding adjustment, which is
9569 * why we set last_host_tsc to the local tsc observed here.
9570 *
9571 * N.B. - this code below runs only on platforms with reliable TSC,
9572 * as that is the only way backwards_tsc is set above. Also note
9573 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9574 * have the same delta_cyc adjustment applied if backwards_tsc
9575 * is detected. Note further, this adjustment is only done once,
9576 * as we reset last_host_tsc on all VCPUs to stop this from being
9577 * called multiple times (one for each physical CPU bringup).
9578 *
Guo Chao4a969982012-06-28 15:17:27 +08009579 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009580 * will be compensated by the logic in vcpu_load, which sets the TSC to
9581 * catchup mode. This will catchup all VCPUs to real time, but cannot
9582 * guarantee that they stay in perfect synchronization.
9583 */
9584 if (backwards_tsc) {
9585 u64 delta_cyc = max_tsc - local_tsc;
9586 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009587 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009588 kvm_for_each_vcpu(i, vcpu, kvm) {
9589 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9590 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009591 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009592 }
9593
9594 /*
9595 * We have to disable TSC offset matching.. if you were
9596 * booting a VM while issuing an S4 host suspend....
9597 * you may have some problem. Solving this issue is
9598 * left as an exercise to the reader.
9599 */
9600 kvm->arch.last_tsc_nsec = 0;
9601 kvm->arch.last_tsc_write = 0;
9602 }
9603
9604 }
9605 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009606}
9607
Radim Krčmář13a34e02014-08-28 15:13:03 +02009608void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009609{
Radim Krčmář13a34e02014-08-28 15:13:03 +02009610 kvm_x86_ops->hardware_disable();
9611 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009612}
9613
9614int kvm_arch_hardware_setup(void)
9615{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009616 int r;
9617
Sean Christopherson91661982020-03-02 15:57:06 -08009618 rdmsrl_safe(MSR_EFER, &host_efer);
9619
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009620 r = kvm_x86_ops->hardware_setup();
9621 if (r != 0)
9622 return r;
9623
Sean Christophersonb11306b2019-12-10 14:44:13 -08009624 cr4_reserved_bits = kvm_host_cr4_reserved_bits(&boot_cpu_data);
9625
Haozhong Zhang35181e82015-10-20 15:39:03 +08009626 if (kvm_has_tsc_control) {
9627 /*
9628 * Make sure the user can only configure tsc_khz values that
9629 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009630 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009631 * be 1 on all machines.
9632 */
9633 u64 max = min(0x7fffffffULL,
9634 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9635 kvm_max_guest_tsc_khz = max;
9636
Haozhong Zhangad7218832015-10-20 15:39:02 +08009637 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009638 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009639
Aaron Lewis139a12c2019-10-21 16:30:25 -07009640 if (boot_cpu_has(X86_FEATURE_XSAVES))
9641 rdmsrl(MSR_IA32_XSS, host_xss);
9642
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009643 kvm_init_msr_list();
9644 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009645}
9646
9647void kvm_arch_hardware_unsetup(void)
9648{
9649 kvm_x86_ops->hardware_unsetup();
9650}
9651
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009652int kvm_arch_check_processor_compat(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009653{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009654 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
9655
9656 WARN_ON(!irqs_disabled());
9657
9658 if (kvm_host_cr4_reserved_bits(c) != cr4_reserved_bits)
9659 return -EIO;
9660
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009661 return kvm_x86_ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009662}
9663
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009664bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9665{
9666 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9667}
9668EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9669
9670bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9671{
9672 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9673}
9674
Gleb Natapov54e98182012-08-05 15:58:32 +03009675struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009676EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009677
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009678void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9679{
Like Xub35e5542019-10-27 18:52:43 +08009680 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9681
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009682 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009683 if (pmu->version && unlikely(pmu->event_count)) {
9684 pmu->need_cleanup = true;
9685 kvm_make_request(KVM_REQ_PMU, vcpu);
9686 }
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009687 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009688}
9689
Sean Christopherson562b6b02020-01-26 16:41:13 -08009690void kvm_arch_free_vm(struct kvm *kvm)
9691{
9692 kfree(kvm->arch.hyperv.hv_pa_pg);
9693 vfree(kvm);
9694}
9695
9696
Carsten Ottee08b9632012-01-04 10:25:20 +01009697int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009698{
Carsten Ottee08b9632012-01-04 10:25:20 +01009699 if (type)
9700 return -EINVAL;
9701
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009702 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009703 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009704 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009705 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009706 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009707 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009708
Sheng Yang5550af42008-10-15 20:15:06 +08009709 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9710 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009711 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9712 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9713 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009714
Jan Kiszka038f8c12011-02-04 10:49:11 +01009715 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009716 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009717 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9718
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009719 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009720 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009721
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009722 kvm->arch.guest_can_read_msr_platform_info = true;
9723
Andrew Jones7e44e442014-02-28 12:52:54 +01009724 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009725 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009726
Roman Kagancbc02362018-02-01 16:48:31 +03009727 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009728 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009729 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009730
Sean Christopherson92735b12019-08-02 15:06:17 -07009731 return kvm_x86_ops->vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009732}
9733
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009734int kvm_arch_post_init_vm(struct kvm *kvm)
9735{
9736 return kvm_mmu_post_init_vm(kvm);
9737}
9738
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009739static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9740{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009741 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009742 kvm_mmu_unload(vcpu);
9743 vcpu_put(vcpu);
9744}
9745
9746static void kvm_free_vcpus(struct kvm *kvm)
9747{
9748 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009749 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009750
9751 /*
9752 * Unpin any mmu pages first.
9753 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009754 kvm_for_each_vcpu(i, vcpu, kvm) {
9755 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009756 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009757 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009758 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009759 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009760
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009761 mutex_lock(&kvm->lock);
9762 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9763 kvm->vcpus[i] = NULL;
9764
9765 atomic_set(&kvm->online_vcpus, 0);
9766 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009767}
9768
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009769void kvm_arch_sync_events(struct kvm *kvm)
9770{
Andrew Jones332967a2014-02-28 12:52:55 +01009771 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009772 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009773 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009774}
9775
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009776int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009777{
9778 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009779 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009780 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009781 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009782
9783 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009784 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9785 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009786
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009787 slot = id_to_memslot(slots, id);
9788 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009789 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009790 return -EEXIST;
9791
9792 /*
9793 * MAP_SHARED to prevent internal slot pages from being moved
9794 * by fork()/COW.
9795 */
9796 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9797 MAP_SHARED | MAP_ANONYMOUS, 0);
9798 if (IS_ERR((void *)hva))
9799 return PTR_ERR((void *)hva);
9800 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009801 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009802 return 0;
9803
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009804 /*
9805 * Stuff a non-canonical value to catch use-after-delete. This
9806 * ends up being 0 on 32-bit KVM, but there's no better
9807 * alternative.
9808 */
9809 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009810 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009811 }
9812
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009813 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009814 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009815
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009816 m.slot = id | (i << 16);
9817 m.flags = 0;
9818 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009819 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009820 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009821 r = __kvm_set_memory_region(kvm, &m);
9822 if (r < 0)
9823 return r;
9824 }
9825
Eric Biggers103c7632018-01-31 17:30:21 -08009826 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009827 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009828
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009829 return 0;
9830}
9831EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9832
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009833void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9834{
9835 kvm_mmu_pre_destroy_vm(kvm);
9836}
9837
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009838void kvm_arch_destroy_vm(struct kvm *kvm)
9839{
Andrew Honig27469d22013-04-18 09:38:14 -07009840 if (current->mm == kvm->mm) {
9841 /*
9842 * Free memory regions allocated on behalf of userspace,
9843 * unless the the memory map has changed due to process exit
9844 * or fd copying.
9845 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009846 mutex_lock(&kvm->slots_lock);
9847 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9848 0, 0);
9849 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
9850 0, 0);
9851 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
9852 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -07009853 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05009854 if (kvm_x86_ops->vm_destroy)
9855 kvm_x86_ops->vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009856 kvm_pic_destroy(kvm);
9857 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009858 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009859 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009860 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009861 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009862 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009863 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009864}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009865
Sean Christophersone96c81e2020-02-18 13:07:27 -08009866void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009867{
9868 int i;
9869
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009870 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -08009871 kvfree(slot->arch.rmap[i]);
9872 slot->arch.rmap[i] = NULL;
9873
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009874 if (i == 0)
9875 continue;
9876
Sean Christophersone96c81e2020-02-18 13:07:27 -08009877 kvfree(slot->arch.lpage_info[i - 1]);
9878 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009879 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009880
Sean Christophersone96c81e2020-02-18 13:07:27 -08009881 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009882}
9883
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009884static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
9885 unsigned long npages)
9886{
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009887 int i;
9888
Sean Christophersonedd4fa32020-02-18 13:07:15 -08009889 /*
9890 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
9891 * old arrays will be freed by __kvm_set_memory_region() if installing
9892 * the new memslot is successful.
9893 */
9894 memset(&slot->arch, 0, sizeof(slot->arch));
9895
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009896 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009897 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009898 unsigned long ugfn;
9899 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009900 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009901
9902 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9903 slot->base_gfn, level) + 1;
9904
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009905 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009906 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009907 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009908 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009909 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009910 if (i == 0)
9911 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009912
Ben Gardon254272c2019-02-11 11:02:50 -08009913 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009914 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009915 goto out_free;
9916
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009917 slot->arch.lpage_info[i - 1] = linfo;
9918
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009919 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009920 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009921 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009922 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009923 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9924 /*
9925 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -08009926 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009927 */
Sean Christopherson600087b2020-03-02 15:57:05 -08009928 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009929 unsigned long j;
9930
9931 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009932 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009933 }
9934 }
9935
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009936 if (kvm_page_track_create_memslot(slot, npages))
9937 goto out_free;
9938
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009939 return 0;
9940
9941out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009942 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009943 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009944 slot->arch.rmap[i] = NULL;
9945 if (i == 0)
9946 continue;
9947
Thomas Huth548ef282015-02-24 21:29:25 +01009948 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009949 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009950 }
9951 return -ENOMEM;
9952}
9953
Sean Christopherson15248252019-02-05 12:54:17 -08009954void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009955{
Boris Ostrovsky91724812019-12-05 01:30:51 +00009956 struct kvm_vcpu *vcpu;
9957 int i;
9958
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009959 /*
9960 * memslots->generation has been incremented.
9961 * mmio generation may have reached its maximum value.
9962 */
Sean Christopherson15248252019-02-05 12:54:17 -08009963 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +00009964
9965 /* Force re-initialization of steal_time cache */
9966 kvm_for_each_vcpu(i, vcpu, kvm)
9967 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009968}
9969
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009970int kvm_arch_prepare_memory_region(struct kvm *kvm,
9971 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009972 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09009973 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08009974{
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009975 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
9976 return kvm_alloc_memslot_metadata(memslot,
9977 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009978 return 0;
9979}
9980
Kai Huang88178fd2015-01-28 10:54:27 +08009981static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
9982 struct kvm_memory_slot *new)
9983{
9984 /* Still write protect RO slot */
9985 if (new->flags & KVM_MEM_READONLY) {
Jay Zhou3c9bd402020-02-27 09:32:27 +08009986 kvm_mmu_slot_remove_write_access(kvm, new, PT_PAGE_TABLE_LEVEL);
Kai Huang88178fd2015-01-28 10:54:27 +08009987 return;
9988 }
9989
9990 /*
9991 * Call kvm_x86_ops dirty logging hooks when they are valid.
9992 *
9993 * kvm_x86_ops->slot_disable_log_dirty is called when:
9994 *
9995 * - KVM_MR_CREATE with dirty logging is disabled
9996 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
9997 *
9998 * The reason is, in case of PML, we need to set D-bit for any slots
9999 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +080010000 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +080010001 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +080010002 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +080010003 * logging disabled for memory slots.
10004 *
10005 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
10006 * to dirty logging mode.
10007 *
10008 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
10009 *
10010 * In case of write protect:
10011 *
10012 * Write protect all pages for dirty logging.
10013 *
10014 * All the sptes including the large sptes which point to this
10015 * slot are set to readonly. We can not create any new large
10016 * spte on this slot until the end of the logging.
10017 *
10018 * See the comments in fast_page_fault().
10019 */
10020 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Jay Zhou3c9bd402020-02-27 09:32:27 +080010021 if (kvm_x86_ops->slot_enable_log_dirty) {
Kai Huang88178fd2015-01-28 10:54:27 +080010022 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010023 } else {
10024 int level =
10025 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
10026 PT_DIRECTORY_LEVEL : PT_PAGE_TABLE_LEVEL;
10027
10028 /*
10029 * If we're with initial-all-set, we don't need
10030 * to write protect any small page because
10031 * they're reported as dirty already. However
10032 * we still need to write-protect huge pages
10033 * so that the page split can happen lazily on
10034 * the first write to the huge page.
10035 */
10036 kvm_mmu_slot_remove_write_access(kvm, new, level);
10037 }
Kai Huang88178fd2015-01-28 10:54:27 +080010038 } else {
10039 if (kvm_x86_ops->slot_disable_log_dirty)
10040 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
10041 }
10042}
10043
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010044void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010045 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010046 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010047 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010048 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010049{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010050 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010051 kvm_mmu_change_mmu_pages(kvm,
10052 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010053
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010054 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010055 * Dirty logging tracks sptes in 4k granularity, meaning that large
10056 * sptes have to be split. If live migration is successful, the guest
10057 * in the source machine will be destroyed and large sptes will be
10058 * created in the destination. However, if the guest continues to run
10059 * in the source machine (for example if live migration fails), small
10060 * sptes will remain around and cause bad performance.
10061 *
10062 * Scan sptes if dirty logging has been stopped, dropping those
10063 * which can be collapsed into a single large-page spte. Later
10064 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -070010065 *
10066 * There is no need to do this in any of the following cases:
10067 * CREATE: No dirty mappings will already exist.
10068 * MOVE/DELETE: The old mappings will already have been cleaned up by
10069 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010070 */
Sean Christopherson319109a2019-09-11 12:19:52 -070010071 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010072 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10073 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10074 kvm_mmu_zap_collapsible_sptes(kvm, new);
10075
10076 /*
Kai Huang88178fd2015-01-28 10:54:27 +080010077 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +080010078 *
Kai Huang88178fd2015-01-28 10:54:27 +080010079 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
10080 * been zapped so no dirty logging staff is needed for old slot. For
10081 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
10082 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010083 *
10084 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010085 */
Kai Huang88178fd2015-01-28 10:54:27 +080010086 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010087 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Sean Christopherson21198842020-02-18 13:07:25 -080010088
10089 /* Free the arrays associated with the old memslot. */
10090 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010091 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010092}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010093
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010094void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010095{
Sean Christopherson7390de12019-02-05 13:01:31 -080010096 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010097}
10098
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010099void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10100 struct kvm_memory_slot *slot)
10101{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010102 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010103}
10104
Liran Alone6c67d82018-09-04 10:56:52 +030010105static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10106{
10107 return (is_guest_mode(vcpu) &&
10108 kvm_x86_ops->guest_apic_has_interrupt &&
10109 kvm_x86_ops->guest_apic_has_interrupt(vcpu));
10110}
10111
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010112static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10113{
10114 if (!list_empty_careful(&vcpu->async_pf.done))
10115 return true;
10116
10117 if (kvm_apic_has_events(vcpu))
10118 return true;
10119
10120 if (vcpu->arch.pv.pv_unhalted)
10121 return true;
10122
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010123 if (vcpu->arch.exception.pending)
10124 return true;
10125
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010126 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10127 (vcpu->arch.nmi_pending &&
10128 kvm_x86_ops->nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010129 return true;
10130
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010131 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
10132 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010133 return true;
10134
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010135 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010136 (kvm_cpu_has_interrupt(vcpu) ||
10137 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010138 return true;
10139
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010140 if (kvm_hv_has_stimer_pending(vcpu))
10141 return true;
10142
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010143 return false;
10144}
10145
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010146int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10147{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010148 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010149}
Zhang Xiantao57361992007-12-17 14:21:40 +080010150
Wanpeng Li17e433b2019-08-05 10:03:19 +080010151bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10152{
10153 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10154 return true;
10155
10156 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10157 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10158 kvm_test_request(KVM_REQ_EVENT, vcpu))
10159 return true;
10160
10161 if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu))
10162 return true;
10163
10164 return false;
10165}
10166
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010167bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10168{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010169 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010170}
10171
Christoffer Dallb6d33832012-03-08 16:44:24 -050010172int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010173{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010174 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010175}
Gleb Natapov78646122009-03-23 12:12:11 +020010176
10177int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10178{
10179 return kvm_x86_ops->interrupt_allowed(vcpu);
10180}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010181
Nadav Amit82b32772014-11-02 11:54:45 +020010182unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10183{
10184 if (is_64_bit_mode(vcpu))
10185 return kvm_rip_read(vcpu);
10186 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10187 kvm_rip_read(vcpu));
10188}
10189EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10190
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010191bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10192{
Nadav Amit82b32772014-11-02 11:54:45 +020010193 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010194}
10195EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10196
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010197unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10198{
10199 unsigned long rflags;
10200
10201 rflags = kvm_x86_ops->get_rflags(vcpu);
10202 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010203 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010204 return rflags;
10205}
10206EXPORT_SYMBOL_GPL(kvm_get_rflags);
10207
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010208static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010209{
10210 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010211 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010212 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010213 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010214}
10215
10216void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10217{
10218 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010219 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010220}
10221EXPORT_SYMBOL_GPL(kvm_set_rflags);
10222
Gleb Natapov56028d02010-10-17 18:13:42 +020010223void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10224{
10225 int r;
10226
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010227 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010228 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010229 return;
10230
10231 r = kvm_mmu_reload(vcpu);
10232 if (unlikely(r))
10233 return;
10234
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010235 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010236 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010237 return;
10238
Sean Christopherson7a026742020-02-06 14:14:34 -080010239 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010240}
10241
Gleb Natapovaf585b92010-10-14 11:22:46 +020010242static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10243{
10244 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10245}
10246
10247static inline u32 kvm_async_pf_next_probe(u32 key)
10248{
10249 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
10250}
10251
10252static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10253{
10254 u32 key = kvm_async_pf_hash_fn(gfn);
10255
10256 while (vcpu->arch.apf.gfns[key] != ~0)
10257 key = kvm_async_pf_next_probe(key);
10258
10259 vcpu->arch.apf.gfns[key] = gfn;
10260}
10261
10262static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10263{
10264 int i;
10265 u32 key = kvm_async_pf_hash_fn(gfn);
10266
10267 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010268 (vcpu->arch.apf.gfns[key] != gfn &&
10269 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010270 key = kvm_async_pf_next_probe(key);
10271
10272 return key;
10273}
10274
10275bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10276{
10277 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10278}
10279
10280static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10281{
10282 u32 i, j, k;
10283
10284 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
10285 while (true) {
10286 vcpu->arch.apf.gfns[i] = ~0;
10287 do {
10288 j = kvm_async_pf_next_probe(j);
10289 if (vcpu->arch.apf.gfns[j] == ~0)
10290 return;
10291 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10292 /*
10293 * k lies cyclically in ]i,j]
10294 * | i.k.j |
10295 * |....j i.k.| or |.k..j i...|
10296 */
10297 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10298 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10299 i = j;
10300 }
10301}
10302
Gleb Natapov7c907052010-10-14 11:22:53 +020010303static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10304{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010305
10306 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10307 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010308}
10309
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010310static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10311{
10312
10313 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10314 sizeof(u32));
10315}
10316
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010317static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10318{
10319 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10320 return false;
10321
10322 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10323 (vcpu->arch.apf.send_user_only &&
10324 kvm_x86_ops->get_cpl(vcpu) == 0))
10325 return false;
10326
10327 return true;
10328}
10329
10330bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10331{
10332 if (unlikely(!lapic_in_kernel(vcpu) ||
10333 kvm_event_needs_reinjection(vcpu) ||
10334 vcpu->arch.exception.pending))
10335 return false;
10336
10337 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10338 return false;
10339
10340 /*
10341 * If interrupts are off we cannot even use an artificial
10342 * halt state.
10343 */
10344 return kvm_x86_ops->interrupt_allowed(vcpu);
10345}
10346
Gleb Natapovaf585b92010-10-14 11:22:46 +020010347void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10348 struct kvm_async_pf *work)
10349{
Avi Kivity6389ee92010-11-29 16:12:30 +020010350 struct x86_exception fault;
10351
Sean Christopherson736c2912019-12-06 15:57:14 -080010352 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010353 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010354
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010355 if (kvm_can_deliver_async_pf(vcpu) &&
10356 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010357 fault.vector = PF_VECTOR;
10358 fault.error_code_valid = true;
10359 fault.error_code = 0;
10360 fault.nested_page_fault = false;
10361 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010362 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010363 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010364 } else {
10365 /*
10366 * It is not possible to deliver a paravirtualized asynchronous
10367 * page fault, but putting the guest in an artificial halt state
10368 * can be beneficial nevertheless: if an interrupt arrives, we
10369 * can deliver it timely and perhaps the guest will schedule
10370 * another process. When the instruction that triggered a page
10371 * fault is retried, hopefully the page will be ready in the host.
10372 */
10373 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010374 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010375}
10376
10377void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10378 struct kvm_async_pf *work)
10379{
Avi Kivity6389ee92010-11-29 16:12:30 +020010380 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010381 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010382
chai wenf2e10662013-10-14 22:22:33 +080010383 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010384 work->arch.token = ~0; /* broadcast wakeup */
10385 else
10386 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010387 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010388
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010389 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10390 !apf_get_user(vcpu, &val)) {
10391 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10392 vcpu->arch.exception.pending &&
10393 vcpu->arch.exception.nr == PF_VECTOR &&
10394 !apf_put_user(vcpu, 0)) {
10395 vcpu->arch.exception.injected = false;
10396 vcpu->arch.exception.pending = false;
10397 vcpu->arch.exception.nr = 0;
10398 vcpu->arch.exception.has_error_code = false;
10399 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010400 vcpu->arch.exception.has_payload = false;
10401 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010402 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10403 fault.vector = PF_VECTOR;
10404 fault.error_code_valid = true;
10405 fault.error_code = 0;
10406 fault.nested_page_fault = false;
10407 fault.address = work->arch.token;
10408 fault.async_page_fault = true;
10409 kvm_inject_page_fault(vcpu, &fault);
10410 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010411 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010412 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010413 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010414}
10415
10416bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10417{
10418 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10419 return true;
10420 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010421 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010422}
10423
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010424void kvm_arch_start_assignment(struct kvm *kvm)
10425{
10426 atomic_inc(&kvm->arch.assigned_device_count);
10427}
10428EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10429
10430void kvm_arch_end_assignment(struct kvm *kvm)
10431{
10432 atomic_dec(&kvm->arch.assigned_device_count);
10433}
10434EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10435
10436bool kvm_arch_has_assigned_device(struct kvm *kvm)
10437{
10438 return atomic_read(&kvm->arch.assigned_device_count);
10439}
10440EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10441
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010442void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10443{
10444 atomic_inc(&kvm->arch.noncoherent_dma_count);
10445}
10446EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10447
10448void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10449{
10450 atomic_dec(&kvm->arch.noncoherent_dma_count);
10451}
10452EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10453
10454bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10455{
10456 return atomic_read(&kvm->arch.noncoherent_dma_count);
10457}
10458EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10459
Alex Williamson14717e22016-05-05 11:58:35 -060010460bool kvm_arch_has_irq_bypass(void)
10461{
Sean Christopherson92735b12019-08-02 15:06:17 -070010462 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010463}
10464
Feng Wu87276882015-09-18 22:29:40 +080010465int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10466 struct irq_bypass_producer *prod)
10467{
10468 struct kvm_kernel_irqfd *irqfd =
10469 container_of(cons, struct kvm_kernel_irqfd, consumer);
10470
Alex Williamson14717e22016-05-05 11:58:35 -060010471 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010472
Alex Williamson14717e22016-05-05 11:58:35 -060010473 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
10474 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010475}
10476
10477void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10478 struct irq_bypass_producer *prod)
10479{
10480 int ret;
10481 struct kvm_kernel_irqfd *irqfd =
10482 container_of(cons, struct kvm_kernel_irqfd, consumer);
10483
Feng Wu87276882015-09-18 22:29:40 +080010484 WARN_ON(irqfd->producer != prod);
10485 irqfd->producer = NULL;
10486
10487 /*
10488 * When producer of consumer is unregistered, we change back to
10489 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010490 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010491 * int this case doesn't want to receive the interrupts.
10492 */
10493 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
10494 if (ret)
10495 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10496 " fails: %d\n", irqfd->consumer.token, ret);
10497}
10498
10499int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10500 uint32_t guest_irq, bool set)
10501{
Feng Wu87276882015-09-18 22:29:40 +080010502 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
10503}
10504
Feng Wu520040142016-01-25 16:53:33 +080010505bool kvm_vector_hashing_enabled(void)
10506{
10507 return vector_hashing;
10508}
Feng Wu520040142016-01-25 16:53:33 +080010509
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010510bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10511{
10512 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10513}
10514EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10515
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010516u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10517{
10518 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10519
10520 /* The STIBP bit doesn't fault even if it's not advertised */
10521 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10522 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10523 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10524 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10525 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10526 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10527
10528 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10529 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10530 bits &= ~SPEC_CTRL_SSBD;
10531 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10532 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10533 bits &= ~SPEC_CTRL_SSBD;
10534
10535 return bits;
10536}
10537EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010538
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010539EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010540EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010541EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10542EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10543EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10544EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010545EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010546EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010547EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010548EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010549EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010550EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010551EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010552EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010553EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010554EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010555EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010556EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010557EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10558EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010559EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);