blob: 259139a145cbba5bc7136235fcd8019b3f0f7897 [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"
彭浩(Richard)88197e62020-05-21 05:57:49 +000021#include "ioapic.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080022#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080023#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020024#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030025#include "kvm_cache_regs.h"
Sean Christopherson2f728d62020-02-18 15:29:49 -080026#include "kvm_emulate.h"
Avi Kivity26eef702008-07-03 14:59:22 +030027#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020028#include "cpuid.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020029#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030030#include "hyperv.h"
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -060031#include "lapic.h"
Joao Martins23200b72018-06-13 09:55:44 -040032#include "xen.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020033
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020034#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030035#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020036#include <linux/kvm.h>
37#include <linux/fs.h>
38#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040039#include <linux/export.h>
40#include <linux/moduleparam.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080041#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050042#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010043#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030044#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010045#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030046#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020047#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080049#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080050#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020051#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020052#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020053#include <linux/timekeeper_internal.h>
54#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080055#include <linux/kvm_irqfd.h>
56#include <linux/irqbypass.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010057#include <linux/sched/stat.h>
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080058#include <linux/sched/isolation.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050059#include <linux/mem_encrypt.h>
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +020060#include <linux/entry-kvm.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010061
Avi Kivityaec51dc2009-07-01 16:01:02 +030062#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080063
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020064#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080065#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020066#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080067#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020068#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020069#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100070#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030071#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080072#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010073#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010074#include <asm/hypervisor.h>
Babu Moger97150922020-09-11 14:29:12 -050075#include <asm/tlbflush.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080076#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090077#include <asm/emulate_prefix.h>
Sean Christophersonfe7e9482021-04-12 16:21:43 +120078#include <asm/sgx.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000079#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020080
Dave Hansend1898b72016-06-01 10:42:20 -070081#define CREATE_TRACE_POINTS
82#include "trace.h"
83
Carsten Otte313a3dc2007-10-11 19:16:52 +020084#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080085#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080086u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
87EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080088
Avi Kivity0f65dd72011-04-20 13:37:53 +030089#define emul_to_vcpu(ctxt) \
Sean Christophersonc9b8b072020-02-18 15:29:48 -080090 ((struct kvm_vcpu *)(ctxt)->vcpu)
Avi Kivity0f65dd72011-04-20 13:37:53 +030091
Joerg Roedel50a37eb2008-01-31 14:57:38 +010092/* EFER defaults:
93 * - enable syscall per default because its emulated by KVM
94 * - enable LME and LMA per default on 64 bit KVM
95 */
96#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080097static
98u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010099#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +0800100static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100101#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +0200102
Sean Christophersonb11306b2019-12-10 14:44:13 -0800103static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
104
Radim Krčmářc5192652016-07-12 22:09:28 +0200105#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
106 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200107
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300108static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300109static void process_nmi(struct kvm_vcpu *vcpu);
Jay Zhou1f7becf2021-01-18 16:47:20 +0800110static void process_smi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200111static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100112static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800113static void store_regs(struct kvm_vcpu *vcpu);
114static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200115
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700116struct kvm_x86_ops kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300117EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800118
Jason Baron9af54712021-01-14 22:27:55 -0500119#define KVM_X86_OP(func) \
120 DEFINE_STATIC_CALL_NULL(kvm_x86_##func, \
121 *(((struct kvm_x86_ops *)0)->func));
122#define KVM_X86_OP_NULL KVM_X86_OP
123#include <asm/kvm-x86-ops.h>
124EXPORT_STATIC_CALL_GPL(kvm_x86_get_cs_db_l_bits);
125EXPORT_STATIC_CALL_GPL(kvm_x86_cache_reg);
126EXPORT_STATIC_CALL_GPL(kvm_x86_tlb_flush_current);
127
Paolo Bonzini893590c2015-11-06 11:46:24 +0100128static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030129module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200130
Like Xud8550662021-01-08 09:36:55 +0800131bool __read_mostly report_ignored_msrs = true;
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200132module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
Like Xud8550662021-01-08 09:36:55 +0800133EXPORT_SYMBOL_GPL(report_ignored_msrs);
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200134
Wanpeng Li4c276252018-05-05 04:02:32 -0700135unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200136module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
137
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300138static bool __read_mostly kvmclock_periodic_sync = true;
139module_param(kvmclock_periodic_sync, bool, S_IRUGO);
140
Paolo Bonzini893590c2015-11-06 11:46:24 +0100141bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100142EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100143u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100144EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800145u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
146EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
147u64 __read_mostly kvm_max_tsc_scaling_ratio;
148EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700149u64 __read_mostly kvm_default_tsc_scaling_ratio;
150EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +0800151bool __read_mostly kvm_has_bus_lock_exit;
152EXPORT_SYMBOL_GPL(kvm_has_bus_lock_exit);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100153
Zachary Amsdencc578282012-02-03 15:43:50 -0200154/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100155static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200156module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
157
Sean Christophersonc3941d92019-04-17 10:15:33 -0700158/*
159 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
Ingo Molnard9f6e122021-03-18 15:28:01 +0100160 * adaptive tuning starting from default advancement of 1000ns. '0' disables
Sean Christophersonc3941d92019-04-17 10:15:33 -0700161 * advancement entirely. Any other value is used as-is and disables adaptive
Ingo Molnard9f6e122021-03-18 15:28:01 +0100162 * tuning, i.e. allows privileged userspace to set an exact advancement time.
Sean Christophersonc3941d92019-04-17 10:15:33 -0700163 */
164static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800165module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500166
Feng Wu520040142016-01-25 16:53:33 +0800167static bool __read_mostly vector_hashing = true;
168module_param(vector_hashing, bool, S_IRUGO);
169
Liran Alonc4ae60e2018-03-12 13:12:47 +0200170bool __read_mostly enable_vmware_backdoor = false;
171module_param(enable_vmware_backdoor, bool, S_IRUGO);
172EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
173
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700174static bool __read_mostly force_emulation_prefix = false;
175module_param(force_emulation_prefix, bool, S_IRUGO);
176
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800177int __read_mostly pi_inject_timer = -1;
178module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
179
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700180/*
181 * Restoring the host value for MSRs that are only consumed when running in
182 * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
183 * returns to userspace, i.e. the kernel can run with the guest's value.
184 */
185#define KVM_MAX_NR_USER_RETURN_MSRS 16
Avi Kivity18863bd2009-09-07 11:12:18 +0300186
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700187struct kvm_user_return_msrs {
Avi Kivity18863bd2009-09-07 11:12:18 +0300188 struct user_return_notifier urn;
189 bool registered;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700190 struct kvm_user_return_msr_values {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800191 u64 host;
192 u64 curr;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700193 } values[KVM_MAX_NR_USER_RETURN_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300194};
195
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700196u32 __read_mostly kvm_nr_uret_msrs;
197EXPORT_SYMBOL_GPL(kvm_nr_uret_msrs);
198static u32 __read_mostly kvm_uret_msrs_list[KVM_MAX_NR_USER_RETURN_MSRS];
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700199static struct kvm_user_return_msrs __percpu *user_return_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300200
Sean Christophersoncfc48182020-03-02 15:56:23 -0800201#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
202 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
203 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
204 | XFEATURE_MASK_PKRU)
205
Sean Christopherson91661982020-03-02 15:57:06 -0800206u64 __read_mostly host_efer;
207EXPORT_SYMBOL_GPL(host_efer);
208
Mohammed Gamalb96e6502020-09-03 16:11:22 +0200209bool __read_mostly allow_smaller_maxphyaddr = 0;
Mohammed Gamal3edd6832020-07-10 17:48:11 +0200210EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr);
211
Tom Lendacky86137772020-12-10 11:10:07 -0600212u64 __read_mostly host_xss;
213EXPORT_SYMBOL_GPL(host_xss);
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100214u64 __read_mostly supported_xss;
215EXPORT_SYMBOL_GPL(supported_xss);
Aaron Lewis139a12c2019-10-21 16:30:25 -0700216
Hollis Blanchard417bc302007-10-31 17:24:23 -0500217struct kvm_stats_debugfs_item debugfs_entries[] = {
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200218 VCPU_STAT("pf_fixed", pf_fixed),
219 VCPU_STAT("pf_guest", pf_guest),
220 VCPU_STAT("tlb_flush", tlb_flush),
221 VCPU_STAT("invlpg", invlpg),
222 VCPU_STAT("exits", exits),
223 VCPU_STAT("io_exits", io_exits),
224 VCPU_STAT("mmio_exits", mmio_exits),
225 VCPU_STAT("signal_exits", signal_exits),
226 VCPU_STAT("irq_window", irq_window_exits),
227 VCPU_STAT("nmi_window", nmi_window_exits),
228 VCPU_STAT("halt_exits", halt_exits),
229 VCPU_STAT("halt_successful_poll", halt_successful_poll),
230 VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
231 VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
232 VCPU_STAT("halt_wakeup", halt_wakeup),
233 VCPU_STAT("hypercalls", hypercalls),
234 VCPU_STAT("request_irq", request_irq_exits),
235 VCPU_STAT("irq_exits", irq_exits),
236 VCPU_STAT("host_state_reload", host_state_reload),
237 VCPU_STAT("fpu_reload", fpu_reload),
238 VCPU_STAT("insn_emulation", insn_emulation),
239 VCPU_STAT("insn_emulation_fail", insn_emulation_fail),
240 VCPU_STAT("irq_injections", irq_injections),
241 VCPU_STAT("nmi_injections", nmi_injections),
242 VCPU_STAT("req_event", req_event),
243 VCPU_STAT("l1d_flush", l1d_flush),
David Matlackcb953122020-05-08 11:22:40 -0700244 VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
245 VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
Dongli Zhang43c11d92021-03-05 14:57:47 -0800246 VCPU_STAT("nested_run", nested_run),
Wanpeng Li4a7132e2021-04-09 12:18:30 +0800247 VCPU_STAT("directed_yield_attempted", directed_yield_attempted),
248 VCPU_STAT("directed_yield_successful", directed_yield_successful),
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200249 VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped),
250 VM_STAT("mmu_pte_write", mmu_pte_write),
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200251 VM_STAT("mmu_pde_zapped", mmu_pde_zapped),
252 VM_STAT("mmu_flooded", mmu_flooded),
253 VM_STAT("mmu_recycled", mmu_recycled),
254 VM_STAT("mmu_cache_miss", mmu_cache_miss),
255 VM_STAT("mmu_unsync", mmu_unsync),
256 VM_STAT("remote_tlb_flush", remote_tlb_flush),
257 VM_STAT("largepages", lpages, .mode = 0444),
258 VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444),
259 VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions),
Hollis Blanchard417bc302007-10-31 17:24:23 -0500260 { NULL }
261};
262
Dexuan Cui2acf9232010-06-10 11:27:12 +0800263u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800264u64 __read_mostly supported_xcr0;
265EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800266
Sean Christopherson80fbd282020-06-08 11:02:18 -0700267static struct kmem_cache *x86_fpu_cache;
Marc Orrb666a4b2018-11-06 14:53:56 -0800268
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800269static struct kmem_cache *x86_emulator_cache;
270
Peter Xu6abe9c12020-06-22 18:04:41 -0400271/*
272 * When called, it means the previous get/set msr reached an invalid msr.
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200273 * Return true if we want to ignore/silent this failed msr access.
Peter Xu6abe9c12020-06-22 18:04:41 -0400274 */
Haiwei Lid6328262021-03-13 13:10:32 +0800275static bool kvm_msr_ignored_check(u32 msr, u64 data, bool write)
Peter Xu6abe9c12020-06-22 18:04:41 -0400276{
277 const char *op = write ? "wrmsr" : "rdmsr";
278
279 if (ignore_msrs) {
280 if (report_ignored_msrs)
Takashi Iwaid383b312020-10-30 16:14:14 +0100281 kvm_pr_unimpl("ignored %s: 0x%x data 0x%llx\n",
282 op, msr, data);
Peter Xu6abe9c12020-06-22 18:04:41 -0400283 /* Mask the error */
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200284 return true;
Peter Xu6abe9c12020-06-22 18:04:41 -0400285 } else {
Takashi Iwaid383b312020-10-30 16:14:14 +0100286 kvm_debug_ratelimited("unhandled %s: 0x%x data 0x%llx\n",
287 op, msr, data);
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200288 return false;
Peter Xu6abe9c12020-06-22 18:04:41 -0400289 }
290}
291
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800292static struct kmem_cache *kvm_alloc_emulator_cache(void)
293{
Sean Christopherson06add252020-02-18 15:29:50 -0800294 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
295 unsigned int size = sizeof(struct x86_emulate_ctxt);
296
297 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800298 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800299 SLAB_ACCOUNT, useroffset,
300 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800301}
302
Jan Kiszkab6785de2012-09-20 07:43:17 +0200303static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300304
Gleb Natapovaf585b92010-10-14 11:22:46 +0200305static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
306{
307 int i;
Peter Xudd03bca2020-04-16 11:58:59 -0400308 for (i = 0; i < ASYNC_PF_PER_VCPU; i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +0200309 vcpu->arch.apf.gfns[i] = ~0;
310}
311
Avi Kivity18863bd2009-09-07 11:12:18 +0300312static void kvm_on_user_return(struct user_return_notifier *urn)
313{
314 unsigned slot;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700315 struct kvm_user_return_msrs *msrs
316 = container_of(urn, struct kvm_user_return_msrs, urn);
317 struct kvm_user_return_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700318 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300319
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700320 /*
321 * Disabling irqs at this point since the following code could be
322 * interrupted and executed through kvm_arch_hardware_disable()
323 */
324 local_irq_save(flags);
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700325 if (msrs->registered) {
326 msrs->registered = false;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700327 user_return_notifier_unregister(urn);
328 }
329 local_irq_restore(flags);
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700330 for (slot = 0; slot < kvm_nr_uret_msrs; ++slot) {
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700331 values = &msrs->values[slot];
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800332 if (values->host != values->curr) {
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700333 wrmsrl(kvm_uret_msrs_list[slot], values->host);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800334 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300335 }
336 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300337}
338
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700339static int kvm_probe_user_return_msr(u32 msr)
Sean Christopherson5104d7f2021-05-04 10:17:24 -0700340{
341 u64 val;
342 int ret;
343
344 preempt_disable();
345 ret = rdmsrl_safe(msr, &val);
346 if (ret)
347 goto out;
348 ret = wrmsrl_safe(msr, val);
349out:
350 preempt_enable();
351 return ret;
352}
Sean Christopherson5104d7f2021-05-04 10:17:24 -0700353
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700354int kvm_add_user_return_msr(u32 msr)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800355{
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700356 BUG_ON(kvm_nr_uret_msrs >= KVM_MAX_NR_USER_RETURN_MSRS);
357
358 if (kvm_probe_user_return_msr(msr))
359 return -1;
360
361 kvm_uret_msrs_list[kvm_nr_uret_msrs] = msr;
362 return kvm_nr_uret_msrs++;
Avi Kivity18863bd2009-09-07 11:12:18 +0300363}
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700364EXPORT_SYMBOL_GPL(kvm_add_user_return_msr);
Avi Kivity18863bd2009-09-07 11:12:18 +0300365
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -0700366int kvm_find_user_return_msr(u32 msr)
367{
368 int i;
369
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700370 for (i = 0; i < kvm_nr_uret_msrs; ++i) {
371 if (kvm_uret_msrs_list[i] == msr)
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -0700372 return i;
373 }
374 return -1;
375}
376EXPORT_SYMBOL_GPL(kvm_find_user_return_msr);
377
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700378static void kvm_user_return_msr_cpu_online(void)
Avi Kivity18863bd2009-09-07 11:12:18 +0300379{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800380 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700381 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Sean Christopherson05c19c22019-11-22 12:04:50 -0800382 u64 value;
383 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300384
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700385 for (i = 0; i < kvm_nr_uret_msrs; ++i) {
386 rdmsrl_safe(kvm_uret_msrs_list[i], &value);
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700387 msrs->values[i].host = value;
388 msrs->values[i].curr = value;
Sean Christopherson05c19c22019-11-22 12:04:50 -0800389 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300390}
391
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700392int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300393{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200394 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700395 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700396 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300397
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700398 value = (value & mask) | (msrs->values[slot].host & ~mask);
399 if (value == msrs->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700400 return 0;
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700401 err = wrmsrl_safe(kvm_uret_msrs_list[slot], value);
Andy Honig8b3c3102014-08-27 11:16:44 -0700402 if (err)
403 return 1;
404
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700405 msrs->values[slot].curr = value;
406 if (!msrs->registered) {
407 msrs->urn.on_user_return = kvm_on_user_return;
408 user_return_notifier_register(&msrs->urn);
409 msrs->registered = true;
Avi Kivity18863bd2009-09-07 11:12:18 +0300410 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700411 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300412}
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700413EXPORT_SYMBOL_GPL(kvm_set_user_return_msr);
Avi Kivity18863bd2009-09-07 11:12:18 +0300414
Radim Krčmář13a34e02014-08-28 15:13:03 +0200415static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200416{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200417 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700418 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200419
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700420 if (msrs->registered)
421 kvm_on_user_return(&msrs->urn);
Avi Kivity3548bab2009-11-28 14:18:47 +0200422}
423
Carsten Otte6866b832007-10-29 16:09:10 +0100424u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
425{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300426 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100427}
428EXPORT_SYMBOL_GPL(kvm_get_apic_base);
429
Jim Mattson58871642018-05-09 16:56:04 -0400430enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
431{
432 return kvm_apic_mode(kvm_get_apic_base(vcpu));
433}
434EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
435
Jan Kiszka58cb6282014-01-24 16:48:44 +0100436int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100437{
Jim Mattson58871642018-05-09 16:56:04 -0400438 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
439 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Sean Christophersona8ac8642021-02-03 16:01:15 -0800440 u64 reserved_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu) | 0x2ff |
Radim Krčmářd6321d42017-08-05 00:12:49 +0200441 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100442
Jim Mattson58871642018-05-09 16:56:04 -0400443 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700444 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400445 if (!msr_info->host_initiated) {
446 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
447 return 1;
448 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
449 return 1;
450 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100451
452 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800453 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100454 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100455}
456EXPORT_SYMBOL_GPL(kvm_set_apic_base);
457
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +0200458asmlinkage __visible noinstr void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000459{
460 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200461 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000462}
463EXPORT_SYMBOL_GPL(kvm_spurious_fault);
464
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200465#define EXCPT_BENIGN 0
466#define EXCPT_CONTRIBUTORY 1
467#define EXCPT_PF 2
468
469static int exception_class(int vector)
470{
471 switch (vector) {
472 case PF_VECTOR:
473 return EXCPT_PF;
474 case DE_VECTOR:
475 case TS_VECTOR:
476 case NP_VECTOR:
477 case SS_VECTOR:
478 case GP_VECTOR:
479 return EXCPT_CONTRIBUTORY;
480 default:
481 break;
482 }
483 return EXCPT_BENIGN;
484}
485
Nadav Amitd6e8c852014-07-24 14:51:24 +0300486#define EXCPT_FAULT 0
487#define EXCPT_TRAP 1
488#define EXCPT_ABORT 2
489#define EXCPT_INTERRUPT 3
490
491static int exception_type(int vector)
492{
493 unsigned int mask;
494
495 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
496 return EXCPT_INTERRUPT;
497
498 mask = 1 << vector;
499
500 /* #DB is trap, as instruction watchpoints are handled elsewhere */
501 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
502 return EXCPT_TRAP;
503
504 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
505 return EXCPT_ABORT;
506
507 /* Reserved exceptions will result in fault */
508 return EXCPT_FAULT;
509}
510
Jim Mattsonda998b42018-10-16 14:29:22 -0700511void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
512{
513 unsigned nr = vcpu->arch.exception.nr;
514 bool has_payload = vcpu->arch.exception.has_payload;
515 unsigned long payload = vcpu->arch.exception.payload;
516
517 if (!has_payload)
518 return;
519
520 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700521 case DB_VECTOR:
522 /*
523 * "Certain debug exceptions may clear bit 0-3. The
524 * remaining contents of the DR6 register are never
525 * cleared by the processor".
526 */
527 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
528 /*
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800529 * In order to reflect the #DB exception payload in guest
530 * dr6, three components need to be considered: active low
531 * bit, FIXED_1 bits and active high bits (e.g. DR6_BD,
532 * DR6_BS and DR6_BT)
533 * DR6_ACTIVE_LOW contains the FIXED_1 and active low bits.
534 * In the target guest dr6:
535 * FIXED_1 bits should always be set.
536 * Active low bits should be cleared if 1-setting in payload.
537 * Active high bits should be set if 1-setting in payload.
538 *
539 * Note, the payload is compatible with the pending debug
540 * exceptions/exit qualification under VMX, that active_low bits
541 * are active high in payload.
542 * So they need to be flipped for DR6.
Jim Mattsonf10c7292018-10-16 14:29:23 -0700543 */
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800544 vcpu->arch.dr6 |= DR6_ACTIVE_LOW;
Jim Mattsonf10c7292018-10-16 14:29:23 -0700545 vcpu->arch.dr6 |= payload;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800546 vcpu->arch.dr6 ^= payload & DR6_ACTIVE_LOW;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800547
548 /*
549 * The #DB payload is defined as compatible with the 'pending
550 * debug exceptions' field under VMX, not DR6. While bit 12 is
551 * defined in the 'pending debug exceptions' field (enabled
552 * breakpoint), it is reserved and must be zero in DR6.
553 */
554 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700555 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700556 case PF_VECTOR:
557 vcpu->arch.cr2 = payload;
558 break;
559 }
560
561 vcpu->arch.exception.has_payload = false;
562 vcpu->arch.exception.payload = 0;
563}
564EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
565
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200566static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200567 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700568 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200569{
570 u32 prev_nr;
571 int class1, class2;
572
Avi Kivity3842d132010-07-27 12:30:24 +0300573 kvm_make_request(KVM_REQ_EVENT, vcpu);
574
Wanpeng Li664f8e22017-08-24 03:35:09 -0700575 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200576 queue:
Wanpeng Li664f8e22017-08-24 03:35:09 -0700577 if (reinject) {
578 /*
579 * On vmentry, vcpu->arch.exception.pending is only
580 * true if an event injection was blocked by
581 * nested_run_pending. In that case, however,
582 * vcpu_enter_guest requests an immediate exit,
583 * and the guest shouldn't proceed far enough to
584 * need reinjection.
585 */
586 WARN_ON_ONCE(vcpu->arch.exception.pending);
587 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700588 if (WARN_ON_ONCE(has_payload)) {
589 /*
590 * A reinjected event has already
591 * delivered its payload.
592 */
593 has_payload = false;
594 payload = 0;
595 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700596 } else {
597 vcpu->arch.exception.pending = true;
598 vcpu->arch.exception.injected = false;
599 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200600 vcpu->arch.exception.has_error_code = has_error;
601 vcpu->arch.exception.nr = nr;
602 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700603 vcpu->arch.exception.has_payload = has_payload;
604 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800605 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700606 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200607 return;
608 }
609
610 /* to check exception */
611 prev_nr = vcpu->arch.exception.nr;
612 if (prev_nr == DF_VECTOR) {
613 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300614 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200615 return;
616 }
617 class1 = exception_class(prev_nr);
618 class2 = exception_class(nr);
619 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
620 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700621 /*
622 * Generate double fault per SDM Table 5-5. Set
623 * exception.pending = true so that the double fault
624 * can trigger a nested vmexit.
625 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200626 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700627 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200628 vcpu->arch.exception.has_error_code = true;
629 vcpu->arch.exception.nr = DF_VECTOR;
630 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700631 vcpu->arch.exception.has_payload = false;
632 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200633 } else
634 /* replace previous exception with a new one in a hope
635 that instruction re-execution will regenerate lost
636 exception */
637 goto queue;
638}
639
Avi Kivity298101d2007-11-25 13:41:11 +0200640void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
641{
Jim Mattson91e86d22018-10-16 14:29:21 -0700642 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200643}
644EXPORT_SYMBOL_GPL(kvm_queue_exception);
645
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200646void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
647{
Jim Mattson91e86d22018-10-16 14:29:21 -0700648 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200649}
650EXPORT_SYMBOL_GPL(kvm_requeue_exception);
651
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400652void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
653 unsigned long payload)
Jim Mattsonf10c7292018-10-16 14:29:23 -0700654{
655 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
656}
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400657EXPORT_SYMBOL_GPL(kvm_queue_exception_p);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700658
Jim Mattsonda998b42018-10-16 14:29:22 -0700659static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
660 u32 error_code, unsigned long payload)
661{
662 kvm_multiple_exception(vcpu, nr, true, error_code,
663 true, payload, false);
664}
665
Kyle Huey6affcbe2016-11-29 12:40:40 -0800666int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200667{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100668 if (err)
669 kvm_inject_gp(vcpu, 0);
670 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800671 return kvm_skip_emulated_instruction(vcpu);
672
673 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100674}
675EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200676
Avi Kivity6389ee92010-11-29 16:12:30 +0200677void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200678{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200679 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700680 vcpu->arch.exception.nested_apf =
681 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700682 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700683 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700684 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
685 } else {
686 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
687 fault->address);
688 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200689}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300690EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200691
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700692bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
693 struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200694{
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400695 struct kvm_mmu *fault_mmu;
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700696 WARN_ON_ONCE(fault->vector != PF_VECTOR);
697
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400698 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu :
699 vcpu->arch.walk_mmu;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200700
Junaid Shahidee1fa202020-03-20 14:28:03 -0700701 /*
702 * Invalidate the TLB entry for the faulting address, if it exists,
703 * else the access will fault indefinitely (and to emulate hardware).
704 */
705 if ((fault->error_code & PFERR_PRESENT_MASK) &&
706 !(fault->error_code & PFERR_RSVD_MASK))
707 kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address,
708 fault_mmu->root_hpa);
709
710 fault_mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200711 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200712}
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700713EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200714
Sheng Yang3419ffc2008-05-15 09:52:48 +0800715void kvm_inject_nmi(struct kvm_vcpu *vcpu)
716{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300717 atomic_inc(&vcpu->arch.nmi_queued);
718 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800719}
720EXPORT_SYMBOL_GPL(kvm_inject_nmi);
721
Avi Kivity298101d2007-11-25 13:41:11 +0200722void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
723{
Jim Mattson91e86d22018-10-16 14:29:21 -0700724 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200725}
726EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
727
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200728void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
729{
Jim Mattson91e86d22018-10-16 14:29:21 -0700730 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200731}
732EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
733
Carsten Ottea03490e2007-10-29 16:09:35 +0100734/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300735 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
736 * a #GP and return false.
737 */
738bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200739{
Jason Baronb36464772021-01-14 22:27:56 -0500740 if (static_call(kvm_x86_get_cpl)(vcpu) <= required_cpl)
Avi Kivity0a79b002009-09-01 12:03:25 +0300741 return true;
742 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
743 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100744}
Avi Kivity0a79b002009-09-01 12:03:25 +0300745EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100746
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300747bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
748{
749 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
750 return true;
751
752 kvm_queue_exception(vcpu, UD_VECTOR);
753 return false;
754}
755EXPORT_SYMBOL_GPL(kvm_require_dr);
756
Carsten Ottea03490e2007-10-29 16:09:35 +0100757/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200758 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200759 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200760 * can read from guest physical or from the guest's guest physical memory.
761 */
762int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
763 gfn_t ngfn, void *data, int offset, int len,
764 u32 access)
765{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200766 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200767 gfn_t real_gfn;
768 gpa_t ngpa;
769
770 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200771 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200772 if (real_gfn == UNMAPPED_GVA)
773 return -EFAULT;
774
775 real_gfn = gpa_to_gfn(real_gfn);
776
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200777 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200778}
779EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
780
Fengguang Wu69b00492015-01-19 22:33:39 +0800781static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200782 void *data, int offset, int len, u32 access)
783{
784 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
785 data, offset, len, access);
786}
787
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700788static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
789{
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800790 return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2);
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700791}
792
Joerg Roedelec92fe42010-09-10 17:30:51 +0200793/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700794 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100795 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200796int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100797{
798 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
799 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
800 int i;
801 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200802 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100803
Joerg Roedelff03a072010-09-10 17:30:57 +0200804 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
805 offset * sizeof(u64), sizeof(pdpte),
806 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100807 if (ret < 0) {
808 ret = 0;
809 goto out;
810 }
811 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400812 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700813 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100814 ret = 0;
815 goto out;
816 }
817 }
818 ret = 1;
819
Joerg Roedelff03a072010-09-10 17:30:57 +0200820 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700821 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
822
Carsten Ottea03490e2007-10-29 16:09:35 +0100823out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100824
825 return ret;
826}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100827EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100828
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100829bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200830{
Joerg Roedelff03a072010-09-10 17:30:57 +0200831 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200832 int offset;
833 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200834 int r;
835
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200836 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200837 return false;
838
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700839 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300840 return true;
841
Paolo Bonzinia5121772017-07-24 18:54:38 +0200842 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
843 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200844 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
845 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200846 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800847 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200848
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800849 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200850}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100851EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200852
Tom Lendackyf27ad382020-12-10 11:09:56 -0600853void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned long cr0)
854{
855 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
856
857 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
858 kvm_clear_async_pf_completion_queue(vcpu);
859 kvm_async_pf_hash_reset(vcpu);
860 }
861
862 if ((cr0 ^ old_cr0) & update_bits)
863 kvm_mmu_reset_context(vcpu);
864
865 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
866 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
867 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
868 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
869}
870EXPORT_SYMBOL_GPL(kvm_post_set_cr0);
871
Avi Kivity49a9b072010-06-10 17:02:14 +0300872int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100873{
Sheng Yangaad82702010-05-12 16:40:42 +0800874 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Jim Mattsond42e3fa2020-07-07 15:36:30 -0700875 unsigned long pdptr_bits = X86_CR0_CD | X86_CR0_NW | X86_CR0_PG;
Sheng Yangaad82702010-05-12 16:40:42 +0800876
Avi Kivityf9a48e62010-01-06 19:10:22 +0200877 cr0 |= X86_CR0_ET;
878
Gleb Natapovab344822010-01-21 15:28:46 +0200879#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300880 if (cr0 & 0xffffffff00000000UL)
881 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200882#endif
883
884 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100885
Gleb Natapov0f122442010-04-28 19:15:31 +0300886 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
887 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100888
Gleb Natapov0f122442010-04-28 19:15:31 +0300889 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
890 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100891
Carsten Ottea03490e2007-10-29 16:09:35 +0100892#ifdef CONFIG_X86_64
Sean Christopherson05487212020-07-13 18:57:32 -0700893 if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) &&
894 (cr0 & X86_CR0_PG)) {
895 int cs_db, cs_l;
Carsten Ottea03490e2007-10-29 16:09:35 +0100896
Sean Christopherson05487212020-07-13 18:57:32 -0700897 if (!is_pae(vcpu))
898 return 1;
Jason Baronb36464772021-01-14 22:27:56 -0500899 static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
Sean Christopherson05487212020-07-13 18:57:32 -0700900 if (cs_l)
Gleb Natapov0f122442010-04-28 19:15:31 +0300901 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100902 }
Sean Christopherson05487212020-07-13 18:57:32 -0700903#endif
904 if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) &&
905 is_pae(vcpu) && ((cr0 ^ old_cr0) & pdptr_bits) &&
906 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)))
907 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100908
Mao, Junjiead756a12012-07-02 01:18:48 +0000909 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
910 return 1;
911
Jason Baronb36464772021-01-14 22:27:56 -0500912 static_call(kvm_x86_set_cr0)(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100913
Tom Lendackyf27ad382020-12-10 11:09:56 -0600914 kvm_post_set_cr0(vcpu, old_cr0, cr0);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800915
Gleb Natapov0f122442010-04-28 19:15:31 +0300916 return 0;
917}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200918EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100919
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200920void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100921{
Avi Kivity49a9b072010-06-10 17:02:14 +0300922 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100923}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200924EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100925
Aaron Lewis139a12c2019-10-21 16:30:25 -0700926void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300927{
Tom Lendacky16809ec2020-12-10 11:10:08 -0600928 if (vcpu->arch.guest_state_protected)
929 return;
930
Aaron Lewis139a12c2019-10-21 16:30:25 -0700931 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300932
Aaron Lewis139a12c2019-10-21 16:30:25 -0700933 if (vcpu->arch.xcr0 != host_xcr0)
934 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
935
936 if (vcpu->arch.xsaves_enabled &&
937 vcpu->arch.ia32_xss != host_xss)
938 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
939 }
Babu Moger37486132020-05-12 18:59:06 -0500940
941 if (static_cpu_has(X86_FEATURE_PKU) &&
942 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
943 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) &&
944 vcpu->arch.pkru != vcpu->arch.host_pkru)
945 __write_pkru(vcpu->arch.pkru);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300946}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700947EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
948
949void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
950{
Tom Lendacky16809ec2020-12-10 11:10:08 -0600951 if (vcpu->arch.guest_state_protected)
952 return;
953
Babu Moger37486132020-05-12 18:59:06 -0500954 if (static_cpu_has(X86_FEATURE_PKU) &&
955 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
956 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) {
957 vcpu->arch.pkru = rdpkru();
958 if (vcpu->arch.pkru != vcpu->arch.host_pkru)
959 __write_pkru(vcpu->arch.host_pkru);
960 }
961
Aaron Lewis139a12c2019-10-21 16:30:25 -0700962 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
963
964 if (vcpu->arch.xcr0 != host_xcr0)
965 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
966
967 if (vcpu->arch.xsaves_enabled &&
968 vcpu->arch.ia32_xss != host_xss)
969 wrmsrl(MSR_IA32_XSS, host_xss);
970 }
971
972}
973EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300974
Fengguang Wu69b00492015-01-19 22:33:39 +0800975static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800976{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000977 u64 xcr0 = xcr;
978 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200979 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800980
981 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
982 if (index != XCR_XFEATURE_ENABLED_MASK)
983 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700984 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800985 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700986 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800987 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200988
989 /*
990 * Do not allow the guest to set bits that we do not support
991 * saving. However, xcr0 bit 0 is always set, even if the
992 * emulated CPU does not support XSAVE (see fx_init).
993 */
Dave Hansend91cab72015-09-02 16:31:26 -0700994 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200995 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800996 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200997
Dave Hansend91cab72015-09-02 16:31:26 -0700998 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
999 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +00001000 return 1;
1001
Dave Hansend91cab72015-09-02 16:31:26 -07001002 if (xcr0 & XFEATURE_MASK_AVX512) {
1003 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +08001004 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -07001005 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +08001006 return 1;
1007 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08001008 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +00001009
Dave Hansend91cab72015-09-02 16:31:26 -07001010 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08001011 kvm_update_cpuid_runtime(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001012 return 0;
1013}
1014
Sean Christopherson92f98952021-02-04 16:57:46 -08001015int kvm_emulate_xsetbv(struct kvm_vcpu *vcpu)
Dexuan Cui2acf9232010-06-10 11:27:12 +08001016{
Sean Christopherson92f98952021-02-04 16:57:46 -08001017 if (static_call(kvm_x86_get_cpl)(vcpu) != 0 ||
1018 __kvm_set_xcr(vcpu, kvm_rcx_read(vcpu), kvm_read_edx_eax(vcpu))) {
1019 kvm_inject_gp(vcpu, 0);
1020 return 1;
1021 }
Paolo Bonzinibbefd4f2020-12-14 07:49:54 -05001022
Sean Christopherson92f98952021-02-04 16:57:46 -08001023 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001024}
Sean Christopherson92f98952021-02-04 16:57:46 -08001025EXPORT_SYMBOL_GPL(kvm_emulate_xsetbv);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001026
Sean Christophersonee69c922020-10-06 18:44:16 -07001027bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Wanpeng Li3ca94192019-09-18 17:50:10 +08001028{
Sean Christophersonb11306b2019-12-10 14:44:13 -08001029 if (cr4 & cr4_reserved_bits)
Sean Christophersonee69c922020-10-06 18:44:16 -07001030 return false;
Wanpeng Li3ca94192019-09-18 17:50:10 +08001031
Krish Sadhukhanb899c132020-07-08 00:39:55 +00001032 if (cr4 & vcpu->arch.cr4_guest_rsvd_bits)
Sean Christophersonee69c922020-10-06 18:44:16 -07001033 return false;
Wanpeng Li3ca94192019-09-18 17:50:10 +08001034
Jason Baronb36464772021-01-14 22:27:56 -05001035 return static_call(kvm_x86_is_valid_cr4)(vcpu, cr4);
Wanpeng Li3ca94192019-09-18 17:50:10 +08001036}
Sean Christophersonee69c922020-10-06 18:44:16 -07001037EXPORT_SYMBOL_GPL(kvm_is_valid_cr4);
Wanpeng Li3ca94192019-09-18 17:50:10 +08001038
Tom Lendacky5b51cb12020-12-10 11:09:57 -06001039void kvm_post_set_cr4(struct kvm_vcpu *vcpu, unsigned long old_cr4, unsigned long cr4)
1040{
1041 unsigned long mmu_role_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
1042 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
1043
1044 if (((cr4 ^ old_cr4) & mmu_role_bits) ||
1045 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
1046 kvm_mmu_reset_context(vcpu);
1047}
1048EXPORT_SYMBOL_GPL(kvm_post_set_cr4);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001049
Avi Kivitya83b29c2010-06-10 17:02:15 +03001050int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +01001051{
Avi Kivityfc78f512009-12-07 12:16:48 +02001052 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +08001053 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Jim Mattsoncb957ad2020-08-17 11:16:54 -07001054 X86_CR4_SMEP;
Xiao Guangrong0be02262015-05-11 22:55:21 +08001055
Sean Christophersonee69c922020-10-06 18:44:16 -07001056 if (!kvm_is_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02001057 return 1;
1058
Carsten Ottea03490e2007-10-29 16:09:35 +01001059 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +03001060 if (!(cr4 & X86_CR4_PAE))
1061 return 1;
Sean Christophersond74fcfc2020-07-02 19:17:14 -07001062 if ((cr4 ^ old_cr4) & X86_CR4_LA57)
1063 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +03001064 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
1065 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +02001066 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
1067 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +03001068 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001069
Mao, Junjiead756a12012-07-02 01:18:48 +00001070 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +02001071 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +00001072 return 1;
1073
1074 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
1075 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
1076 return 1;
1077 }
1078
Jason Baronb36464772021-01-14 22:27:56 -05001079 static_call(kvm_x86_set_cr4)(vcpu, cr4);
Gleb Natapov0f122442010-04-28 19:15:31 +03001080
Tom Lendacky5b51cb12020-12-10 11:09:57 -06001081 kvm_post_set_cr4(vcpu, old_cr4, cr4);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001082
Gleb Natapov0f122442010-04-28 19:15:31 +03001083 return 0;
1084}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001085EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +01001086
Avi Kivity23902182010-06-10 17:02:16 +03001087int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +01001088{
Junaid Shahidade61e22018-06-27 14:59:15 -07001089 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +01001090#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -07001091 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
1092
Junaid Shahidade61e22018-06-27 14:59:15 -07001093 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001094 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1095 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001096 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001097#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001098
Avi Kivity9f8fe502010-12-05 17:30:00 +02001099 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001100 if (!skip_tlb_flush) {
1101 kvm_mmu_sync_roots(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001102 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001103 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001104 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001105 }
1106
Sean Christopherson886bbcc2021-04-21 19:21:21 -07001107 /*
1108 * Do not condition the GPA check on long mode, this helper is used to
1109 * stuff CR3, e.g. for RSM emulation, and there is no guarantee that
1110 * the current vCPU mode is accurate.
1111 */
1112 if (kvm_vcpu_is_illegal_gpa(vcpu, cr3))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001113 return 1;
Sean Christopherson886bbcc2021-04-21 19:21:21 -07001114
1115 if (is_pae_paging(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001116 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001117
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07001118 kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001119 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001120 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001121
Gleb Natapov0f122442010-04-28 19:15:31 +03001122 return 0;
1123}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001124EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001125
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001126int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001127{
Gleb Natapov0f122442010-04-28 19:15:31 +03001128 if (cr8 & CR8_RESERVED_BITS)
1129 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001130 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001131 kvm_lapic_set_tpr(vcpu, cr8);
1132 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001133 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001134 return 0;
1135}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001136EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001137
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001138unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001139{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001140 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001141 return kvm_lapic_get_cr8(vcpu);
1142 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001143 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001144}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001145EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001146
Nadav Amitae561ed2015-04-02 03:10:37 +03001147static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1148{
1149 int i;
1150
1151 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1152 for (i = 0; i < KVM_NR_DB_REGS; i++)
1153 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1154 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1155 }
1156}
1157
Paolo Bonzini7c866632020-05-16 08:42:28 -04001158void kvm_update_dr7(struct kvm_vcpu *vcpu)
Jan Kiszkac8639012012-09-21 05:42:55 +02001159{
1160 unsigned long dr7;
1161
1162 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1163 dr7 = vcpu->arch.guest_debug_dr7;
1164 else
1165 dr7 = vcpu->arch.dr7;
Jason Baronb36464772021-01-14 22:27:56 -05001166 static_call(kvm_x86_set_dr7)(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001167 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1168 if (dr7 & DR7_BP_EN_MASK)
1169 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001170}
Paolo Bonzini7c866632020-05-16 08:42:28 -04001171EXPORT_SYMBOL_GPL(kvm_update_dr7);
Jan Kiszkac8639012012-09-21 05:42:55 +02001172
Nadav Amit6f43ed02014-07-15 17:37:46 +03001173static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1174{
1175 u64 fixed = DR6_FIXED_1;
1176
Radim Krčmářd6321d42017-08-05 00:12:49 +02001177 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001178 fixed |= DR6_RTM;
Chenyi Qiange8ea85f2021-02-02 17:04:32 +08001179
1180 if (!guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))
1181 fixed |= DR6_BUS_LOCK;
Nadav Amit6f43ed02014-07-15 17:37:46 +03001182 return fixed;
1183}
1184
Paolo Bonzini996ff542020-12-14 07:49:54 -05001185int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001186{
Marios Pomonisea740052019-12-11 12:47:52 -08001187 size_t size = ARRAY_SIZE(vcpu->arch.db);
1188
Gleb Natapov020df072010-04-13 10:05:23 +03001189 switch (dr) {
1190 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001191 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001192 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1193 vcpu->arch.eff_db[dr] = val;
1194 break;
1195 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001196 case 6:
Krish Sadhukhanf5f61452020-05-22 18:19:51 -04001197 if (!kvm_dr6_valid(val))
Paolo Bonzini996ff542020-12-14 07:49:54 -05001198 return 1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001199 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001200 break;
1201 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001202 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001203 if (!kvm_dr7_valid(val))
Paolo Bonzini996ff542020-12-14 07:49:54 -05001204 return 1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001205 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001206 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001207 break;
1208 }
1209
1210 return 0;
1211}
1212EXPORT_SYMBOL_GPL(kvm_set_dr);
1213
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05001214void kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001215{
Marios Pomonisea740052019-12-11 12:47:52 -08001216 size_t size = ARRAY_SIZE(vcpu->arch.db);
1217
Gleb Natapov020df072010-04-13 10:05:23 +03001218 switch (dr) {
1219 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001220 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001221 break;
1222 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001223 case 6:
Paolo Bonzini5679b802020-05-04 11:28:25 -04001224 *val = vcpu->arch.dr6;
Gleb Natapov020df072010-04-13 10:05:23 +03001225 break;
1226 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001227 default: /* 7 */
1228 *val = vcpu->arch.dr7;
1229 break;
1230 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001231}
Gleb Natapov020df072010-04-13 10:05:23 +03001232EXPORT_SYMBOL_GPL(kvm_get_dr);
1233
Sean Christophersonc483c452021-02-04 16:57:48 -08001234int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu)
Avi Kivity022cd0e2011-11-10 14:57:23 +02001235{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001236 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001237 u64 data;
Avi Kivity022cd0e2011-11-10 14:57:23 +02001238
Sean Christophersonc483c452021-02-04 16:57:48 -08001239 if (kvm_pmu_rdpmc(vcpu, ecx, &data)) {
1240 kvm_inject_gp(vcpu, 0);
1241 return 1;
1242 }
1243
Sean Christophersonde3cd112019-04-30 10:36:17 -07001244 kvm_rax_write(vcpu, (u32)data);
1245 kvm_rdx_write(vcpu, data >> 32);
Sean Christophersonc483c452021-02-04 16:57:48 -08001246 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001247}
Sean Christophersonc483c452021-02-04 16:57:48 -08001248EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001249
Carsten Otte043405e2007-10-10 17:16:19 +02001250/*
1251 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1252 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1253 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001254 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1255 * extract the supported MSRs from the related const lists.
1256 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001257 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001258 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001259 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001260 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001261
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001262static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001263 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001264 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001265#ifdef CONFIG_X86_64
1266 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1267#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001268 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001269 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001270 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001271 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1272 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1273 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1274 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1275 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1276 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001277 MSR_IA32_UMWAIT_CONTROL,
1278
Jim Mattsone2ada662019-08-21 11:20:04 -07001279 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1280 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1281 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1282 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1283 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1284 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1285 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1286 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1287 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1288 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1289 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1290 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1291 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001292 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1293 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1294 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1295 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1296 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1297 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1298 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1299 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1300 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001301};
1302
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001303static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001304static unsigned num_msrs_to_save;
1305
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001306static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001307 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1308 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1309 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1310 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001311 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001312 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1313 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001314 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001315 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001316 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001317 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001318 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001319 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001320 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1321 HV_X64_MSR_TSC_EMULATION_STATUS,
Jon Doronf97f5a52020-05-29 16:45:40 +03001322 HV_X64_MSR_SYNDBG_OPTIONS,
1323 HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS,
1324 HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER,
1325 HV_X64_MSR_SYNDBG_PENDING_BUFFER,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001326
1327 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02001328 MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001329
Will Auldba904632012-11-29 12:42:50 -08001330 MSR_IA32_TSC_ADJUST,
Dave Hansen09141ec2020-03-05 09:47:06 -08001331 MSR_IA32_TSC_DEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001332 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001333 MSR_IA32_PERF_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001334 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001335 MSR_IA32_MCG_STATUS,
1336 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001337 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001338 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001339 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001340 MSR_PLATFORM_INFO,
1341 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001342 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001343 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001344 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001345
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001346 /*
1347 * The following list leaves out MSRs whose values are determined
1348 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1349 * We always support the "true" VMX control MSRs, even if the host
1350 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001351 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001352 */
1353 MSR_IA32_VMX_BASIC,
1354 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1355 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1356 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1357 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1358 MSR_IA32_VMX_MISC,
1359 MSR_IA32_VMX_CR0_FIXED0,
1360 MSR_IA32_VMX_CR4_FIXED0,
1361 MSR_IA32_VMX_VMCS_ENUM,
1362 MSR_IA32_VMX_PROCBASED_CTLS2,
1363 MSR_IA32_VMX_EPT_VPID_CAP,
1364 MSR_IA32_VMX_VMFUNC,
1365
Borislav Petkov191c8132019-04-18 18:32:50 +02001366 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001367 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001368};
1369
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001370static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001371static unsigned num_emulated_msrs;
1372
Tom Lendacky801e4592018-02-21 13:39:51 -06001373/*
1374 * List of msr numbers which are used to expose MSR-based features that
1375 * can be used by a hypervisor to validate requested CPU features.
1376 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001377static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001378 MSR_IA32_VMX_BASIC,
1379 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1380 MSR_IA32_VMX_PINBASED_CTLS,
1381 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1382 MSR_IA32_VMX_PROCBASED_CTLS,
1383 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1384 MSR_IA32_VMX_EXIT_CTLS,
1385 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1386 MSR_IA32_VMX_ENTRY_CTLS,
1387 MSR_IA32_VMX_MISC,
1388 MSR_IA32_VMX_CR0_FIXED0,
1389 MSR_IA32_VMX_CR0_FIXED1,
1390 MSR_IA32_VMX_CR4_FIXED0,
1391 MSR_IA32_VMX_CR4_FIXED1,
1392 MSR_IA32_VMX_VMCS_ENUM,
1393 MSR_IA32_VMX_PROCBASED_CTLS2,
1394 MSR_IA32_VMX_EPT_VPID_CAP,
1395 MSR_IA32_VMX_VMFUNC,
1396
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001397 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001398 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001399 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001400 MSR_IA32_PERF_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001401};
1402
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001403static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001404static unsigned int num_msr_based_features;
1405
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001406static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001407{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001408 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001409
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001410 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1411 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001412
1413 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001414 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1415 * the nested hypervisor runs with NX huge pages. If it is not,
Ingo Molnard9f6e122021-03-18 15:28:01 +01001416 * L1 is anyway vulnerable to ITLB_MULTIHIT exploits from other
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001417 * L1 guests, so it need not worry about its own (L2) guests.
1418 */
1419 data |= ARCH_CAP_PSCHANGE_MC_NO;
1420
1421 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001422 * If we're doing cache flushes (either "always" or "cond")
1423 * we will do one whenever the guest does a vmlaunch/vmresume.
1424 * If an outer hypervisor is doing the cache flush for us
1425 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1426 * capability to the guest too, and if EPT is disabled we're not
1427 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1428 * require a nested hypervisor to do a flush of its own.
1429 */
1430 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1431 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1432
Paolo Bonzini0c549142019-08-19 17:24:07 +02001433 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1434 data |= ARCH_CAP_RDCL_NO;
1435 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1436 data |= ARCH_CAP_SSB_NO;
1437 if (!boot_cpu_has_bug(X86_BUG_MDS))
1438 data |= ARCH_CAP_MDS_NO;
1439
Paolo Bonzini71316362021-01-28 11:45:00 -05001440 if (!boot_cpu_has(X86_FEATURE_RTM)) {
1441 /*
1442 * If RTM=0 because the kernel has disabled TSX, the host might
1443 * have TAA_NO or TSX_CTRL. Clear TAA_NO (the guest sees RTM=0
1444 * and therefore knows that there cannot be TAA) but keep
1445 * TSX_CTRL: some buggy userspaces leave it set on tsx=on hosts,
1446 * and we want to allow migrating those guests to tsx=off hosts.
1447 */
1448 data &= ~ARCH_CAP_TAA_NO;
1449 } else if (!boot_cpu_has_bug(X86_BUG_TAA)) {
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001450 data |= ARCH_CAP_TAA_NO;
Paolo Bonzini71316362021-01-28 11:45:00 -05001451 } else {
1452 /*
1453 * Nothing to do here; we emulate TSX_CTRL if present on the
1454 * host so the guest can choose between disabling TSX or
1455 * using VERW to clear CPU buffers.
1456 */
1457 }
Pawan Guptae1d38b62019-10-23 12:23:33 +02001458
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001459 return data;
1460}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001461
Wanpeng Li66421c12018-02-28 14:03:30 +08001462static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1463{
1464 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001465 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001466 msr->data = kvm_get_arch_capabilities();
1467 break;
1468 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001469 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001470 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001471 default:
Jason Baronb36464772021-01-14 22:27:56 -05001472 return static_call(kvm_x86_get_msr_feature)(msr);
Wanpeng Li66421c12018-02-28 14:03:30 +08001473 }
1474 return 0;
1475}
1476
Tom Lendacky801e4592018-02-21 13:39:51 -06001477static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1478{
1479 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001480 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001481
1482 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001483 r = kvm_get_msr_feature(&msr);
Peter Xu12bc2132020-06-22 18:04:42 -04001484
1485 if (r == KVM_MSR_RET_INVALID) {
1486 /* Unconditionally clear the output for simplicity */
1487 *data = 0;
Haiwei Lid6328262021-03-13 13:10:32 +08001488 if (kvm_msr_ignored_check(index, 0, false))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001489 r = 0;
Peter Xu12bc2132020-06-22 18:04:42 -04001490 }
1491
Wanpeng Li66421c12018-02-28 14:03:30 +08001492 if (r)
1493 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001494
1495 *data = msr.data;
1496
1497 return 0;
1498}
1499
Sean Christopherson11988492019-04-02 08:19:15 -07001500static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1501{
1502 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1503 return false;
1504
1505 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1506 return false;
1507
Sean Christopherson0a629562019-04-02 08:19:16 -07001508 if (efer & (EFER_LME | EFER_LMA) &&
1509 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1510 return false;
1511
1512 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1513 return false;
1514
Sean Christopherson11988492019-04-02 08:19:15 -07001515 return true;
1516
1517}
Jan Kiszka384bb782013-04-20 10:52:36 +02001518bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001519{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001520 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001521 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001522
Sean Christopherson11988492019-04-02 08:19:15 -07001523 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001524}
1525EXPORT_SYMBOL_GPL(kvm_valid_efer);
1526
Sean Christopherson11988492019-04-02 08:19:15 -07001527static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001528{
1529 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001530 u64 efer = msr_info->data;
Maxim Levitsky72f211e2020-10-01 14:29:53 +03001531 int r;
Jan Kiszka384bb782013-04-20 10:52:36 +02001532
Sean Christopherson11988492019-04-02 08:19:15 -07001533 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001534 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001535
Sean Christopherson11988492019-04-02 08:19:15 -07001536 if (!msr_info->host_initiated) {
1537 if (!__kvm_valid_efer(vcpu, efer))
1538 return 1;
1539
1540 if (is_paging(vcpu) &&
1541 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1542 return 1;
1543 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001544
Carsten Otte15c4a642007-10-30 18:44:17 +01001545 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001546 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001547
Jason Baronb36464772021-01-14 22:27:56 -05001548 r = static_call(kvm_x86_set_efer)(vcpu, efer);
Maxim Levitsky72f211e2020-10-01 14:29:53 +03001549 if (r) {
1550 WARN_ON(r > 0);
1551 return r;
1552 }
Sheng Yanga3d204e2010-05-12 16:40:40 +08001553
Sheng Yangaad82702010-05-12 16:40:42 +08001554 /* Update reserved bits */
1555 if ((efer ^ old_efer) & EFER_NX)
1556 kvm_mmu_reset_context(vcpu);
1557
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001558 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001559}
1560
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001561void kvm_enable_efer_bits(u64 mask)
1562{
1563 efer_reserved_bits &= ~mask;
1564}
1565EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1566
Alexander Graf51de8152020-09-25 16:34:17 +02001567bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type)
1568{
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001569 struct kvm_x86_msr_filter *msr_filter;
1570 struct msr_bitmap_range *ranges;
Alexander Graf1a1552542020-09-25 16:34:21 +02001571 struct kvm *kvm = vcpu->kvm;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001572 bool allowed;
Alexander Graf1a1552542020-09-25 16:34:21 +02001573 int idx;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001574 u32 i;
Alexander Graf1a1552542020-09-25 16:34:21 +02001575
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001576 /* x2APIC MSRs do not support filtering. */
1577 if (index >= 0x800 && index <= 0x8ff)
Alexander Graf1a1552542020-09-25 16:34:21 +02001578 return true;
1579
Alexander Graf1a1552542020-09-25 16:34:21 +02001580 idx = srcu_read_lock(&kvm->srcu);
1581
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001582 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu);
1583 if (!msr_filter) {
1584 allowed = true;
1585 goto out;
1586 }
1587
1588 allowed = msr_filter->default_allow;
1589 ranges = msr_filter->ranges;
1590
1591 for (i = 0; i < msr_filter->count; i++) {
Alexander Graf1a1552542020-09-25 16:34:21 +02001592 u32 start = ranges[i].base;
1593 u32 end = start + ranges[i].nmsrs;
1594 u32 flags = ranges[i].flags;
1595 unsigned long *bitmap = ranges[i].bitmap;
1596
1597 if ((index >= start) && (index < end) && (flags & type)) {
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001598 allowed = !!test_bit(index - start, bitmap);
Alexander Graf1a1552542020-09-25 16:34:21 +02001599 break;
1600 }
1601 }
1602
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001603out:
Alexander Graf1a1552542020-09-25 16:34:21 +02001604 srcu_read_unlock(&kvm->srcu, idx);
1605
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001606 return allowed;
Alexander Graf51de8152020-09-25 16:34:17 +02001607}
1608EXPORT_SYMBOL_GPL(kvm_msr_allowed);
1609
Carsten Otte15c4a642007-10-30 18:44:17 +01001610/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001611 * Write @data into the MSR specified by @index. Select MSR specific fault
1612 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001613 * Returns 0 on success, non-0 otherwise.
1614 * Assumes vcpu_load() was already called.
1615 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001616static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1617 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001618{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001619 struct msr_data msr;
1620
Alexander Graf1a1552542020-09-25 16:34:21 +02001621 if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_WRITE))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001622 return KVM_MSR_RET_FILTERED;
Alexander Graf1a1552542020-09-25 16:34:21 +02001623
Sean Christophersonf20935d2019-09-05 14:22:54 -07001624 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001625 case MSR_FS_BASE:
1626 case MSR_GS_BASE:
1627 case MSR_KERNEL_GS_BASE:
1628 case MSR_CSTAR:
1629 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001630 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001631 return 1;
1632 break;
1633 case MSR_IA32_SYSENTER_EIP:
1634 case MSR_IA32_SYSENTER_ESP:
1635 /*
1636 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1637 * non-canonical address is written on Intel but not on
1638 * AMD (which ignores the top 32-bits, because it does
1639 * not implement 64-bit SYSENTER).
1640 *
1641 * 64-bit code should hence be able to write a non-canonical
1642 * value on AMD. Making the address canonical ensures that
1643 * vmentry does not fail on Intel after writing a non-canonical
1644 * value, and that something deterministic happens if the guest
1645 * invokes 64-bit SYSENTER.
1646 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001647 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Sean Christopherson61a05d42021-05-04 10:17:33 -07001648 break;
1649 case MSR_TSC_AUX:
1650 if (!kvm_is_supported_user_return_msr(MSR_TSC_AUX))
1651 return 1;
1652
1653 if (!host_initiated &&
1654 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) &&
1655 !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))
1656 return 1;
1657
1658 /*
1659 * Per Intel's SDM, bits 63:32 are reserved, but AMD's APM has
1660 * incomplete and conflicting architectural behavior. Current
1661 * AMD CPUs completely ignore bits 63:32, i.e. they aren't
1662 * reserved and always read as zeros. Enforce Intel's reserved
1663 * bits check if and only if the guest CPU is Intel, and clear
1664 * the bits in all other cases. This ensures cross-vendor
1665 * migration will provide consistent behavior for the guest.
1666 */
1667 if (guest_cpuid_is_intel(vcpu) && (data >> 32) != 0)
1668 return 1;
1669
1670 data = (u32)data;
1671 break;
Nadav Amit854e8bb2014-09-16 03:24:05 +03001672 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001673
1674 msr.data = data;
1675 msr.index = index;
1676 msr.host_initiated = host_initiated;
1677
Jason Baronb36464772021-01-14 22:27:56 -05001678 return static_call(kvm_x86_set_msr)(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001679}
1680
Peter Xu6abe9c12020-06-22 18:04:41 -04001681static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu,
1682 u32 index, u64 data, bool host_initiated)
1683{
1684 int ret = __kvm_set_msr(vcpu, index, data, host_initiated);
1685
1686 if (ret == KVM_MSR_RET_INVALID)
Haiwei Lid6328262021-03-13 13:10:32 +08001687 if (kvm_msr_ignored_check(index, data, true))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001688 ret = 0;
Peter Xu6abe9c12020-06-22 18:04:41 -04001689
1690 return ret;
1691}
1692
Sean Christophersonf20935d2019-09-05 14:22:54 -07001693/*
1694 * Read the MSR specified by @index into @data. Select MSR specific fault
1695 * checks are bypassed if @host_initiated is %true.
1696 * Returns 0 on success, non-0 otherwise.
1697 * Assumes vcpu_load() was already called.
1698 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001699int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1700 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001701{
1702 struct msr_data msr;
1703 int ret;
1704
Alexander Graf1a1552542020-09-25 16:34:21 +02001705 if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_READ))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001706 return KVM_MSR_RET_FILTERED;
Alexander Graf1a1552542020-09-25 16:34:21 +02001707
Sean Christopherson61a05d42021-05-04 10:17:33 -07001708 switch (index) {
1709 case MSR_TSC_AUX:
1710 if (!kvm_is_supported_user_return_msr(MSR_TSC_AUX))
1711 return 1;
1712
1713 if (!host_initiated &&
1714 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) &&
1715 !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))
1716 return 1;
1717 break;
1718 }
1719
Sean Christophersonf20935d2019-09-05 14:22:54 -07001720 msr.index = index;
1721 msr.host_initiated = host_initiated;
1722
Jason Baronb36464772021-01-14 22:27:56 -05001723 ret = static_call(kvm_x86_get_msr)(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001724 if (!ret)
1725 *data = msr.data;
1726 return ret;
1727}
1728
Peter Xu6abe9c12020-06-22 18:04:41 -04001729static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu,
1730 u32 index, u64 *data, bool host_initiated)
1731{
1732 int ret = __kvm_get_msr(vcpu, index, data, host_initiated);
1733
1734 if (ret == KVM_MSR_RET_INVALID) {
1735 /* Unconditionally clear *data for simplicity */
1736 *data = 0;
Haiwei Lid6328262021-03-13 13:10:32 +08001737 if (kvm_msr_ignored_check(index, 0, false))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001738 ret = 0;
Peter Xu6abe9c12020-06-22 18:04:41 -04001739 }
1740
1741 return ret;
1742}
1743
Sean Christophersonf20935d2019-09-05 14:22:54 -07001744int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1745{
Peter Xu6abe9c12020-06-22 18:04:41 -04001746 return kvm_get_msr_ignored_check(vcpu, index, data, false);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001747}
1748EXPORT_SYMBOL_GPL(kvm_get_msr);
1749
1750int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1751{
Peter Xu6abe9c12020-06-22 18:04:41 -04001752 return kvm_set_msr_ignored_check(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001753}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001754EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001755
Paolo Bonzini8b474422020-12-14 07:44:46 -05001756static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu)
Alexander Graf1ae09952020-09-25 16:34:16 +02001757{
Paolo Bonzini8b474422020-12-14 07:44:46 -05001758 int err = vcpu->run->msr.error;
1759 if (!err) {
Alexander Graf1ae09952020-09-25 16:34:16 +02001760 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data);
1761 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32);
1762 }
1763
Jason Baronb36464772021-01-14 22:27:56 -05001764 return static_call(kvm_x86_complete_emulated_msr)(vcpu, err);
Alexander Graf1ae09952020-09-25 16:34:16 +02001765}
1766
1767static int complete_emulated_wrmsr(struct kvm_vcpu *vcpu)
1768{
Jason Baronb36464772021-01-14 22:27:56 -05001769 return static_call(kvm_x86_complete_emulated_msr)(vcpu, vcpu->run->msr.error);
Alexander Graf1ae09952020-09-25 16:34:16 +02001770}
1771
1772static u64 kvm_msr_reason(int r)
1773{
1774 switch (r) {
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001775 case KVM_MSR_RET_INVALID:
Alexander Graf1ae09952020-09-25 16:34:16 +02001776 return KVM_MSR_EXIT_REASON_UNKNOWN;
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001777 case KVM_MSR_RET_FILTERED:
Alexander Graf1a1552542020-09-25 16:34:21 +02001778 return KVM_MSR_EXIT_REASON_FILTER;
Alexander Graf1ae09952020-09-25 16:34:16 +02001779 default:
1780 return KVM_MSR_EXIT_REASON_INVAL;
1781 }
1782}
1783
1784static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index,
1785 u32 exit_reason, u64 data,
1786 int (*completion)(struct kvm_vcpu *vcpu),
1787 int r)
1788{
1789 u64 msr_reason = kvm_msr_reason(r);
1790
1791 /* Check if the user wanted to know about this MSR fault */
1792 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason))
1793 return 0;
1794
1795 vcpu->run->exit_reason = exit_reason;
1796 vcpu->run->msr.error = 0;
1797 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad));
1798 vcpu->run->msr.reason = msr_reason;
1799 vcpu->run->msr.index = index;
1800 vcpu->run->msr.data = data;
1801 vcpu->arch.complete_userspace_io = completion;
1802
1803 return 1;
1804}
1805
1806static int kvm_get_msr_user_space(struct kvm_vcpu *vcpu, u32 index, int r)
1807{
1808 return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_RDMSR, 0,
1809 complete_emulated_rdmsr, r);
1810}
1811
1812static int kvm_set_msr_user_space(struct kvm_vcpu *vcpu, u32 index, u64 data, int r)
1813{
1814 return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_WRMSR, data,
1815 complete_emulated_wrmsr, r);
1816}
1817
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001818int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1819{
1820 u32 ecx = kvm_rcx_read(vcpu);
1821 u64 data;
Alexander Graf1ae09952020-09-25 16:34:16 +02001822 int r;
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001823
Alexander Graf1ae09952020-09-25 16:34:16 +02001824 r = kvm_get_msr(vcpu, ecx, &data);
1825
1826 /* MSR read failed? See if we should ask user space */
1827 if (r && kvm_get_msr_user_space(vcpu, ecx, r)) {
1828 /* Bounce to user space */
1829 return 0;
1830 }
1831
Paolo Bonzini8b474422020-12-14 07:44:46 -05001832 if (!r) {
1833 trace_kvm_msr_read(ecx, data);
1834
1835 kvm_rax_write(vcpu, data & -1u);
1836 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1837 } else {
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001838 trace_kvm_msr_read_ex(ecx);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001839 }
1840
Jason Baronb36464772021-01-14 22:27:56 -05001841 return static_call(kvm_x86_complete_emulated_msr)(vcpu, r);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001842}
1843EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1844
1845int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1846{
1847 u32 ecx = kvm_rcx_read(vcpu);
1848 u64 data = kvm_read_edx_eax(vcpu);
Alexander Graf1ae09952020-09-25 16:34:16 +02001849 int r;
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001850
Alexander Graf1ae09952020-09-25 16:34:16 +02001851 r = kvm_set_msr(vcpu, ecx, data);
1852
1853 /* MSR write failed? See if we should ask user space */
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03001854 if (r && kvm_set_msr_user_space(vcpu, ecx, data, r))
Alexander Graf1ae09952020-09-25 16:34:16 +02001855 /* Bounce to user space */
1856 return 0;
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03001857
1858 /* Signal all other negative errors to userspace */
1859 if (r < 0)
1860 return r;
Alexander Graf1ae09952020-09-25 16:34:16 +02001861
Paolo Bonzini8b474422020-12-14 07:44:46 -05001862 if (!r)
1863 trace_kvm_msr_write(ecx, data);
1864 else
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001865 trace_kvm_msr_write_ex(ecx, data);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001866
Jason Baronb36464772021-01-14 22:27:56 -05001867 return static_call(kvm_x86_complete_emulated_msr)(vcpu, r);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001868}
1869EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1870
Sean Christopherson5ff3a352021-02-04 16:57:47 -08001871int kvm_emulate_as_nop(struct kvm_vcpu *vcpu)
1872{
1873 return kvm_skip_emulated_instruction(vcpu);
1874}
1875EXPORT_SYMBOL_GPL(kvm_emulate_as_nop);
1876
1877int kvm_emulate_invd(struct kvm_vcpu *vcpu)
1878{
1879 /* Treat an INVD instruction as a NOP and just skip it. */
1880 return kvm_emulate_as_nop(vcpu);
1881}
1882EXPORT_SYMBOL_GPL(kvm_emulate_invd);
1883
1884int kvm_emulate_mwait(struct kvm_vcpu *vcpu)
1885{
1886 pr_warn_once("kvm: MWAIT instruction emulated as NOP!\n");
1887 return kvm_emulate_as_nop(vcpu);
1888}
1889EXPORT_SYMBOL_GPL(kvm_emulate_mwait);
1890
1891int kvm_handle_invalid_op(struct kvm_vcpu *vcpu)
1892{
1893 kvm_queue_exception(vcpu, UD_VECTOR);
1894 return 1;
1895}
1896EXPORT_SYMBOL_GPL(kvm_handle_invalid_op);
1897
1898int kvm_emulate_monitor(struct kvm_vcpu *vcpu)
1899{
1900 pr_warn_once("kvm: MONITOR instruction emulated as NOP!\n");
1901 return kvm_emulate_as_nop(vcpu);
1902}
1903EXPORT_SYMBOL_GPL(kvm_emulate_monitor);
1904
Paolo Bonzinid89d04a2021-02-02 10:44:23 -05001905static inline bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001906{
Frederic Weisbecker4ae7dc92021-02-01 00:05:48 +01001907 xfer_to_guest_mode_prepare();
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001908 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02001909 xfer_to_guest_mode_work_pending();
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001910}
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001911
Carsten Otte313a3dc2007-10-11 19:16:52 +02001912/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001913 * The fast path for frequent and performance sensitive wrmsr emulation,
1914 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1915 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1916 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1917 * other cases which must be called after interrupts are enabled on the host.
1918 */
1919static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1920{
Wanpeng Lie1be9ac2020-03-26 10:20:01 +08001921 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1922 return 1;
1923
1924 if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001925 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
Wanpeng Li4064a4c2020-04-02 16:20:26 +08001926 ((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
1927 ((u32)(data >> 32) != X2APIC_BROADCAST)) {
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001928
Wanpeng Lid5361672020-03-26 10:20:02 +08001929 data &= ~(1 << 12);
1930 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001931 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001932 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1933 trace_kvm_apic_write(APIC_ICR, (u32)data);
1934 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001935 }
1936
1937 return 1;
1938}
1939
Wanpeng Liae95f562020-04-28 14:23:28 +08001940static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data)
1941{
1942 if (!kvm_can_use_hv_timer(vcpu))
1943 return 1;
1944
1945 kvm_set_lapic_tscdeadline_msr(vcpu, data);
1946 return 0;
1947}
1948
Wanpeng Li404d5d72020-04-28 14:23:25 +08001949fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001950{
1951 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001952 u64 data;
Wanpeng Li404d5d72020-04-28 14:23:25 +08001953 fastpath_t ret = EXIT_FASTPATH_NONE;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001954
1955 switch (msr) {
1956 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001957 data = kvm_read_edx_eax(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08001958 if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) {
1959 kvm_skip_emulated_instruction(vcpu);
1960 ret = EXIT_FASTPATH_EXIT_HANDLED;
Haiwei Li80bc97f2020-05-18 09:31:38 +08001961 }
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001962 break;
Dave Hansen09141ec2020-03-05 09:47:06 -08001963 case MSR_IA32_TSC_DEADLINE:
Wanpeng Liae95f562020-04-28 14:23:28 +08001964 data = kvm_read_edx_eax(vcpu);
1965 if (!handle_fastpath_set_tscdeadline(vcpu, data)) {
1966 kvm_skip_emulated_instruction(vcpu);
1967 ret = EXIT_FASTPATH_REENTER_GUEST;
1968 }
1969 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001970 default:
Wanpeng Li404d5d72020-04-28 14:23:25 +08001971 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001972 }
1973
Wanpeng Li404d5d72020-04-28 14:23:25 +08001974 if (ret != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001975 trace_kvm_msr_write(msr, data);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001976
Wanpeng Li404d5d72020-04-28 14:23:25 +08001977 return ret;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001978}
1979EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1980
1981/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001982 * Adapt set_msr() to msr_io()'s calling convention
1983 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001984static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1985{
Peter Xu6abe9c12020-06-22 18:04:41 -04001986 return kvm_get_msr_ignored_check(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001987}
1988
Carsten Otte313a3dc2007-10-11 19:16:52 +02001989static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1990{
Peter Xu6abe9c12020-06-22 18:04:41 -04001991 return kvm_set_msr_ignored_check(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001992}
1993
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001994#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001995struct pvclock_clock {
1996 int vclock_mode;
1997 u64 cycle_last;
1998 u64 mask;
1999 u32 mult;
2000 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002001 u64 base_cycles;
2002 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002003};
2004
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002005struct pvclock_gtod_data {
2006 seqcount_t seq;
2007
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002008 struct pvclock_clock clock; /* extract of a clocksource struct */
2009 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002010
Paolo Bonzini917f9472020-01-22 14:32:20 +01002011 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002012 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002013};
2014
2015static struct pvclock_gtod_data pvclock_gtod_data;
2016
2017static void update_pvclock_gtod(struct timekeeper *tk)
2018{
2019 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
2020
2021 write_seqcount_begin(&vdata->seq);
2022
2023 /* copy pvclock gtod data */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002024 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Peter Zijlstra876e7882015-03-19 10:09:06 +01002025 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
2026 vdata->clock.mask = tk->tkr_mono.mask;
2027 vdata->clock.mult = tk->tkr_mono.mult;
2028 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002029 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
2030 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002031
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002032 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002033 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
2034 vdata->raw_clock.mask = tk->tkr_raw.mask;
2035 vdata->raw_clock.mult = tk->tkr_raw.mult;
2036 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002037 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
2038 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002039
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002040 vdata->wall_time_sec = tk->xtime_sec;
2041
Paolo Bonzini917f9472020-01-22 14:32:20 +01002042 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002043
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002044 write_seqcount_end(&vdata->seq);
2045}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002046
2047static s64 get_kvmclock_base_ns(void)
2048{
2049 /* Count up from boot time, but with the frequency of the raw clock. */
2050 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
2051}
2052#else
2053static s64 get_kvmclock_base_ns(void)
2054{
2055 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
2056 return ktime_get_boottime_ns();
2057}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002058#endif
2059
Joao Martins629b5342018-06-28 15:06:43 -04002060void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock, int sec_hi_ofs)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002061{
Avi Kivity9ed3c442010-05-04 15:00:37 +03002062 int version;
2063 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002064 struct pvclock_wall_clock wc;
Joao Martins629b5342018-06-28 15:06:43 -04002065 u32 wc_sec_hi;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002066 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002067
2068 if (!wall_clock)
2069 return;
2070
Avi Kivity9ed3c442010-05-04 15:00:37 +03002071 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
2072 if (r)
2073 return;
2074
2075 if (version & 1)
2076 ++version; /* first time write, random junk */
2077
2078 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002079
Nicholas Krause1dab1342015-12-30 13:08:46 -05002080 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
2081 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002082
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002083 /*
2084 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10002085 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002086 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002087 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002088 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002089
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002090 wc.nsec = do_div(wall_nsec, 1000000000);
2091 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002092 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002093
2094 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
2095
Joao Martins629b5342018-06-28 15:06:43 -04002096 if (sec_hi_ofs) {
2097 wc_sec_hi = wall_nsec >> 32;
2098 kvm_write_guest(kvm, wall_clock + sec_hi_ofs,
2099 &wc_sec_hi, sizeof(wc_sec_hi));
2100 }
2101
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002102 version++;
2103 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002104}
2105
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00002106static void kvm_write_system_time(struct kvm_vcpu *vcpu, gpa_t system_time,
2107 bool old_msr, bool host_initiated)
2108{
2109 struct kvm_arch *ka = &vcpu->kvm->arch;
2110
2111 if (vcpu->vcpu_id == 0 && !host_initiated) {
Oliver Upton1e293d12020-10-27 16:10:43 -07002112 if (ka->boot_vcpu_runs_old_kvmclock != old_msr)
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00002113 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
2114
2115 ka->boot_vcpu_runs_old_kvmclock = old_msr;
2116 }
2117
2118 vcpu->arch.time = system_time;
2119 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
2120
2121 /* we verify if the enable bit is set... */
2122 vcpu->arch.pv_time_enabled = false;
2123 if (!(system_time & 1))
2124 return;
2125
2126 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
2127 &vcpu->arch.pv_time, system_time & ~1ULL,
2128 sizeof(struct pvclock_vcpu_time_info)))
2129 vcpu->arch.pv_time_enabled = true;
2130
2131 return;
2132}
2133
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002134static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
2135{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01002136 do_shl32_div32(dividend, divisor);
2137 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002138}
2139
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002140static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002141 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002142{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002143 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002144 int32_t shift = 0;
2145 uint64_t tps64;
2146 uint32_t tps32;
2147
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002148 tps64 = base_hz;
2149 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02002150 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002151 tps64 >>= 1;
2152 shift--;
2153 }
2154
2155 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02002156 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
2157 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002158 scaled64 >>= 1;
2159 else
2160 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002161 shift++;
2162 }
2163
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002164 *pshift = shift;
2165 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002166}
2167
Marcelo Tosattid8281992012-11-27 23:29:01 -02002168#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002169static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002170#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002171
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002172static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08002173static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002174
Zachary Amsdencc578282012-02-03 15:43:50 -02002175static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10002176{
Zachary Amsdencc578282012-02-03 15:43:50 -02002177 u64 v = (u64)khz * (1000000 + ppm);
2178 do_div(v, 1000000);
2179 return v;
2180}
2181
Haozhong Zhang381d5852015-10-20 15:39:04 +08002182static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
2183{
2184 u64 ratio;
2185
2186 /* Guest TSC same frequency as host TSC? */
2187 if (!scale) {
2188 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
2189 return 0;
2190 }
2191
2192 /* TSC scaling supported? */
2193 if (!kvm_has_tsc_control) {
2194 if (user_tsc_khz > tsc_khz) {
2195 vcpu->arch.tsc_catchup = 1;
2196 vcpu->arch.tsc_always_catchup = 1;
2197 return 0;
2198 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02002199 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08002200 return -1;
2201 }
2202 }
2203
2204 /* TSC scaling required - calculate ratio */
2205 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
2206 user_tsc_khz, tsc_khz);
2207
2208 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02002209 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
2210 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002211 return -1;
2212 }
2213
2214 vcpu->arch.tsc_scaling_ratio = ratio;
2215 return 0;
2216}
2217
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002218static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02002219{
2220 u32 thresh_lo, thresh_hi;
2221 int use_scaling = 0;
2222
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002223 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002224 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08002225 /* set tsc_scaling_ratio to a safe value */
2226 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08002227 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08002228 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002229
Zachary Amsdenc2855452010-09-18 14:38:15 -10002230 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002231 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02002232 &vcpu->arch.virtual_tsc_shift,
2233 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002234 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02002235
2236 /*
2237 * Compute the variation in TSC rate which is acceptable
2238 * within the range of tolerance and decide if the
2239 * rate being applied is within that bounds of the hardware
2240 * rate. If so, no scaling or compensation need be done.
2241 */
2242 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
2243 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002244 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
2245 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 -02002246 use_scaling = 1;
2247 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002248 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002249}
2250
2251static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
2252{
Zachary Amsdene26101b2012-02-03 15:43:57 -02002253 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02002254 vcpu->arch.virtual_tsc_mult,
2255 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002256 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002257 return tsc;
2258}
2259
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002260static inline int gtod_is_based_on_tsc(int mode)
2261{
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002262 return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002263}
2264
Fengguang Wu69b00492015-01-19 22:33:39 +08002265static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002266{
2267#ifdef CONFIG_X86_64
2268 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002269 struct kvm_arch *ka = &vcpu->kvm->arch;
2270 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2271
2272 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2273 atomic_read(&vcpu->kvm->online_vcpus));
2274
Marcelo Tosatti7f187922014-11-04 21:30:44 -02002275 /*
2276 * Once the masterclock is enabled, always perform request in
2277 * order to update it.
2278 *
2279 * In order to enable masterclock, the host clocksource must be TSC
2280 * and the vcpus need to have matched TSCs. When that happens,
2281 * perform request to enable masterclock.
2282 */
2283 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002284 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002285 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
2286
2287 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
2288 atomic_read(&vcpu->kvm->online_vcpus),
2289 ka->use_master_clock, gtod->clock.vclock_mode);
2290#endif
2291}
2292
Haozhong Zhang35181e82015-10-20 15:39:03 +08002293/*
2294 * Multiply tsc by a fixed point number represented by ratio.
2295 *
2296 * The most significant 64-N bits (mult) of ratio represent the
2297 * integral part of the fixed point number; the remaining N bits
2298 * (frac) represent the fractional part, ie. ratio represents a fixed
2299 * point number (mult + frac * 2^(-N)).
2300 *
2301 * N equals to kvm_tsc_scaling_ratio_frac_bits.
2302 */
2303static inline u64 __scale_tsc(u64 ratio, u64 tsc)
2304{
2305 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
2306}
2307
2308u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
2309{
2310 u64 _tsc = tsc;
2311 u64 ratio = vcpu->arch.tsc_scaling_ratio;
2312
2313 if (ratio != kvm_default_tsc_scaling_ratio)
2314 _tsc = __scale_tsc(ratio, tsc);
2315
2316 return _tsc;
2317}
2318EXPORT_SYMBOL_GPL(kvm_scale_tsc);
2319
Haozhong Zhang07c14192015-10-20 15:39:05 +08002320static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2321{
2322 u64 tsc;
2323
2324 tsc = kvm_scale_tsc(vcpu, rdtsc());
2325
2326 return target_tsc - tsc;
2327}
2328
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002329u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
2330{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002331 return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002332}
2333EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
2334
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002335static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
2336{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002337 vcpu->arch.l1_tsc_offset = offset;
Jason Baronb36464772021-01-14 22:27:56 -05002338 vcpu->arch.tsc_offset = static_call(kvm_x86_write_l1_tsc_offset)(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002339}
2340
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002341static inline bool kvm_check_tsc_unstable(void)
2342{
2343#ifdef CONFIG_X86_64
2344 /*
2345 * TSC is marked unstable when we're running on Hyper-V,
2346 * 'TSC page' clocksource is good.
2347 */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002348 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002349 return false;
2350#endif
2351 return check_tsc_unstable();
2352}
2353
Paolo Bonzini0c899c22020-09-24 14:45:27 +02002354static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data)
Zachary Amsden99e3e302010-08-19 22:07:17 -10002355{
2356 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002357 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002358 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002359 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002360 bool already_matched;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002361 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002362
Jan Kiszka038f8c12011-02-04 10:49:11 +01002363 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08002364 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002365 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002366 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002367
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002368 if (vcpu->arch.virtual_tsc_khz) {
Paolo Bonzini0c899c22020-09-24 14:45:27 +02002369 if (data == 0) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03002370 /*
2371 * detection of vcpu initialization -- need to sync
2372 * with other vCPUs. This particularly helps to keep
2373 * kvm_clock stable after CPU hotplug
2374 */
2375 synchronizing = true;
2376 } else {
2377 u64 tsc_exp = kvm->arch.last_tsc_write +
2378 nsec_to_cycles(vcpu, elapsed);
2379 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
2380 /*
2381 * Special case: TSC write with a small delta (1 second)
2382 * of virtual cycle time against real time is
2383 * interpreted as an attempt to synchronize the CPU.
2384 */
2385 synchronizing = data < tsc_exp + tsc_hz &&
2386 data + tsc_hz > tsc_exp;
2387 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002388 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002389
2390 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002391 * For a reliable TSC, we can match TSC offsets, and for an unstable
2392 * TSC, we add elapsed time in this computation. We could let the
2393 * compensation code attempt to catch up if we fall behind, but
2394 * it's better to try to match offsets from the beginning.
2395 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002396 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002397 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002398 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002399 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002400 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002401 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002402 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002403 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002404 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002405 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002406 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002407 } else {
2408 /*
2409 * We split periods of matched TSC writes into generations.
2410 * For each generation, we track the original measured
2411 * nanosecond time, offset, and write, so if TSCs are in
2412 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002413 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002414 *
2415 * These values are tracked in kvm->arch.cur_xxx variables.
2416 */
2417 kvm->arch.cur_tsc_generation++;
2418 kvm->arch.cur_tsc_nsec = ns;
2419 kvm->arch.cur_tsc_write = data;
2420 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002421 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002422 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002423
2424 /*
2425 * We also track th most recent recorded KHZ, write and time to
2426 * allow the matching interval to be extended at each write.
2427 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002428 kvm->arch.last_tsc_nsec = ns;
2429 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002430 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002431
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002432 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002433
2434 /* Keep track of which generation this VCPU has synchronized to */
2435 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2436 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2437 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2438
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002439 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002440 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002441
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002442 spin_lock_irqsave(&kvm->arch.pvclock_gtod_sync_lock, flags);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002443 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002444 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002445 } else if (!already_matched) {
2446 kvm->arch.nr_vcpus_matched_tsc++;
2447 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002448
2449 kvm_track_tsc_matching(vcpu);
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002450 spin_unlock_irqrestore(&kvm->arch.pvclock_gtod_sync_lock, flags);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002451}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002452
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002453static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2454 s64 adjustment)
2455{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002456 u64 tsc_offset = vcpu->arch.l1_tsc_offset;
Leonid Shatz326e7422018-11-06 12:14:25 +02002457 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002458}
2459
2460static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2461{
2462 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2463 WARN_ON(adjustment < 0);
2464 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002465 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002466}
2467
Marcelo Tosattid8281992012-11-27 23:29:01 -02002468#ifdef CONFIG_X86_64
2469
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002470static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002471{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002472 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002473 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002474
2475 if (likely(ret >= last))
2476 return ret;
2477
2478 /*
2479 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002480 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002481 * very likely) and there's a data dependence, so force GCC
2482 * to generate a branch instead. I don't barrier() because
2483 * we don't actually need a barrier, and if this function
2484 * ever gets inlined it will generate worse code.
2485 */
2486 asm volatile ("");
2487 return last;
2488}
2489
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002490static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2491 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002492{
2493 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002494 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002495
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002496 switch (clock->vclock_mode) {
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002497 case VDSO_CLOCKMODE_HVCLOCK:
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002498 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2499 tsc_timestamp);
2500 if (tsc_pg_val != U64_MAX) {
2501 /* TSC page valid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002502 *mode = VDSO_CLOCKMODE_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002503 v = (tsc_pg_val - clock->cycle_last) &
2504 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002505 } else {
2506 /* TSC page invalid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002507 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002508 }
2509 break;
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002510 case VDSO_CLOCKMODE_TSC:
2511 *mode = VDSO_CLOCKMODE_TSC;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002512 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002513 v = (*tsc_timestamp - clock->cycle_last) &
2514 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002515 break;
2516 default:
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002517 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002518 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002519
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002520 if (*mode == VDSO_CLOCKMODE_NONE)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002521 *tsc_timestamp = v = 0;
2522
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002523 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002524}
2525
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002526static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002527{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002528 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002529 unsigned long seq;
2530 int mode;
2531 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002532
Marcelo Tosattid8281992012-11-27 23:29:01 -02002533 do {
2534 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002535 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002536 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002537 ns >>= gtod->raw_clock.shift;
2538 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002539 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002540 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002541
2542 return mode;
2543}
2544
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002545static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002546{
2547 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2548 unsigned long seq;
2549 int mode;
2550 u64 ns;
2551
2552 do {
2553 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002554 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002555 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002556 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002557 ns >>= gtod->clock.shift;
2558 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2559
2560 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2561 ts->tv_nsec = ns;
2562
2563 return mode;
2564}
2565
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002566/* returns true if host is using TSC based clocksource */
2567static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002568{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002569 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002570 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002571 return false;
2572
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002573 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002574 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002575}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002576
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002577/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002578static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002579 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002580{
2581 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002582 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002583 return false;
2584
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002585 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002586}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002587#endif
2588
2589/*
2590 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002591 * Assuming a stable TSC across physical CPUS, and a stable TSC
2592 * across virtual CPUs, the following condition is possible.
2593 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002594 * CPUs at the next numbered event.
2595 *
2596 * "timespecX" represents host monotonic time. "tscX" represents
2597 * RDTSC value.
2598 *
2599 * VCPU0 on CPU0 | VCPU1 on CPU1
2600 *
2601 * 1. read timespec0,tsc0
2602 * 2. | timespec1 = timespec0 + N
2603 * | tsc1 = tsc0 + M
2604 * 3. transition to guest | transition to guest
2605 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2606 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2607 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2608 *
2609 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2610 *
2611 * - ret0 < ret1
2612 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2613 * ...
2614 * - 0 < N - M => M < N
2615 *
2616 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2617 * always the case (the difference between two distinct xtime instances
2618 * might be smaller then the difference between corresponding TSC reads,
2619 * when updating guest vcpus pvclock areas).
2620 *
2621 * To avoid that problem, do not allow visibility of distinct
2622 * system_timestamp/tsc_timestamp values simultaneously: use a master
2623 * copy of host monotonic time values. Update that master copy
2624 * in lockstep.
2625 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002626 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002627 *
2628 */
2629
2630static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2631{
2632#ifdef CONFIG_X86_64
2633 struct kvm_arch *ka = &kvm->arch;
2634 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002635 bool host_tsc_clocksource, vcpus_matched;
2636
2637 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2638 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002639
2640 /*
2641 * If the host uses TSC clock, then passthrough TSC as stable
2642 * to the guest.
2643 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002644 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002645 &ka->master_kernel_ns,
2646 &ka->master_cycle_now);
2647
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002648 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002649 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002650 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002651
Marcelo Tosattid8281992012-11-27 23:29:01 -02002652 if (ka->use_master_clock)
2653 atomic_set(&kvm_guest_has_master_clock, 1);
2654
2655 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002656 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2657 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002658#endif
2659}
2660
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002661void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2662{
2663 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2664}
2665
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002666static void kvm_gen_update_masterclock(struct kvm *kvm)
2667{
2668#ifdef CONFIG_X86_64
2669 int i;
2670 struct kvm_vcpu *vcpu;
2671 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002672 unsigned long flags;
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002673
Vitaly Kuznetsove880c6e2021-03-16 15:37:34 +01002674 kvm_hv_invalidate_tsc_page(kvm);
2675
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002676 kvm_make_mclock_inprogress_request(kvm);
Paolo Bonzinic2c647f2021-03-25 14:05:11 -04002677
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002678 /* no guest entries from this point */
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002679 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002680 pvclock_update_vm_gtod_copy(kvm);
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002681 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002682
2683 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002684 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002685
2686 /* guest entries allowed */
2687 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002688 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002689#endif
2690}
2691
Marcelo Tosattie891a322017-04-17 12:51:37 -03002692u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002693{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002694 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002695 struct pvclock_vcpu_time_info hv_clock;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002696 unsigned long flags;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002697 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002698
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002699 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Paolo Bonzini8b953442016-11-16 18:31:30 +01002700 if (!ka->use_master_clock) {
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002701 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002702 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002703 }
2704
Paolo Bonzini8b953442016-11-16 18:31:30 +01002705 hv_clock.tsc_timestamp = ka->master_cycle_now;
2706 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002707 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Paolo Bonzini8b953442016-11-16 18:31:30 +01002708
Wanpeng Lie2c22062017-05-11 18:12:05 -07002709 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2710 get_cpu();
2711
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002712 if (__this_cpu_read(cpu_tsc_khz)) {
2713 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2714 &hv_clock.tsc_shift,
2715 &hv_clock.tsc_to_system_mul);
2716 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2717 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002718 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002719
2720 put_cpu();
2721
2722 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002723}
2724
Joao Martinsaa096aa2019-02-01 13:01:45 -05002725static void kvm_setup_pvclock_page(struct kvm_vcpu *v,
2726 struct gfn_to_hva_cache *cache,
2727 unsigned int offset)
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002728{
2729 struct kvm_vcpu_arch *vcpu = &v->arch;
2730 struct pvclock_vcpu_time_info guest_hv_clock;
2731
Joao Martinsaa096aa2019-02-01 13:01:45 -05002732 if (unlikely(kvm_read_guest_offset_cached(v->kvm, cache,
2733 &guest_hv_clock, offset, sizeof(guest_hv_clock))))
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002734 return;
2735
2736 /* This VCPU is paused, but it's legal for a guest to read another
2737 * VCPU's kvmclock, so we really have to follow the specification where
2738 * it says that version is odd if data is being modified, and even after
2739 * it is consistent.
2740 *
2741 * Version field updates must be kept separate. This is because
2742 * kvm_write_guest_cached might use a "rep movs" instruction, and
2743 * writes within a string instruction are weakly ordered. So there
2744 * are three writes overall.
2745 *
2746 * As a small optimization, only write the version field in the first
2747 * and third write. The vcpu->pv_time cache is still valid, because the
2748 * version field is the first in the struct.
2749 */
2750 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2751
Liran Alon51c4b8b2017-11-05 16:11:30 +02002752 if (guest_hv_clock.version & 1)
2753 ++guest_hv_clock.version; /* first time write, random junk */
2754
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002755 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Joao Martinsaa096aa2019-02-01 13:01:45 -05002756 kvm_write_guest_offset_cached(v->kvm, cache,
2757 &vcpu->hv_clock, offset,
2758 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002759
2760 smp_wmb();
2761
2762 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2763 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2764
2765 if (vcpu->pvclock_set_guest_stopped_request) {
2766 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2767 vcpu->pvclock_set_guest_stopped_request = false;
2768 }
2769
2770 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2771
Joao Martinsaa096aa2019-02-01 13:01:45 -05002772 kvm_write_guest_offset_cached(v->kvm, cache,
2773 &vcpu->hv_clock, offset,
2774 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002775
2776 smp_wmb();
2777
2778 vcpu->hv_clock.version++;
Joao Martinsaa096aa2019-02-01 13:01:45 -05002779 kvm_write_guest_offset_cached(v->kvm, cache,
2780 &vcpu->hv_clock, offset,
2781 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002782}
2783
Zachary Amsden34c238a2010-09-18 14:38:14 -10002784static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002785{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002786 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002787 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002788 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002789 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002790 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002791 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002792 bool use_master_clock;
2793
2794 kernel_ns = 0;
2795 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002796
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002797 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002798 * If the host uses TSC clock, then passthrough TSC as stable
2799 * to the guest.
2800 */
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002801 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002802 use_master_clock = ka->use_master_clock;
2803 if (use_master_clock) {
2804 host_tsc = ka->master_cycle_now;
2805 kernel_ns = ka->master_kernel_ns;
2806 }
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002807 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002808
2809 /* Keep irq disabled to prevent changes to the clock */
2810 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002811 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2812 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002813 local_irq_restore(flags);
2814 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2815 return 1;
2816 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002817 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002818 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002819 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002820 }
2821
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002822 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002823
2824 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002825 * We may have to catch up the TSC to match elapsed wall clock
2826 * time for two reasons, even if kvmclock is used.
2827 * 1) CPU could have been running below the maximum TSC rate
2828 * 2) Broken TSC compensation resets the base at each VCPU
2829 * entry to avoid unknown leaps of TSC even when running
2830 * again on the same CPU. This may cause apparent elapsed
2831 * time to disappear, and the guest to stand still or run
2832 * very slowly.
2833 */
2834 if (vcpu->tsc_catchup) {
2835 u64 tsc = compute_guest_tsc(v, kernel_ns);
2836 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002837 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002838 tsc_timestamp = tsc;
2839 }
2840 }
2841
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002842 local_irq_restore(flags);
2843
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002844 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002845
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002846 if (kvm_has_tsc_control)
2847 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2848
2849 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002850 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002851 &vcpu->hv_clock.tsc_shift,
2852 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002853 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002854 }
2855
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002856 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002857 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002858 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002859
Marcelo Tosattid8281992012-11-27 23:29:01 -02002860 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002861 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002862 if (use_master_clock)
2863 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2864
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002865 vcpu->hv_clock.flags = pvclock_flags;
2866
Paolo Bonzini095cf552016-02-08 12:54:12 +01002867 if (vcpu->pv_time_enabled)
Joao Martinsaa096aa2019-02-01 13:01:45 -05002868 kvm_setup_pvclock_page(v, &vcpu->pv_time, 0);
2869 if (vcpu->xen.vcpu_info_set)
2870 kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_info_cache,
2871 offsetof(struct compat_vcpu_info, time));
Joao Martinsf2340cd2018-07-23 11:20:57 -04002872 if (vcpu->xen.vcpu_time_info_set)
2873 kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0);
Paolo Bonzini095cf552016-02-08 12:54:12 +01002874 if (v == kvm_get_vcpu(v->kvm, 0))
2875 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002876 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002877}
2878
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002879/*
2880 * kvmclock updates which are isolated to a given vcpu, such as
2881 * vcpu->cpu migration, should not allow system_timestamp from
2882 * the rest of the vcpus to remain static. Otherwise ntp frequency
2883 * correction applies to one vcpu's system_timestamp but not
2884 * the others.
2885 *
2886 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002887 * We need to rate-limit these requests though, as they can
2888 * considerably slow guests that have a large number of vcpus.
2889 * The time for a remote vcpu to update its kvmclock is bound
2890 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002891 */
2892
Andrew Jones7e44e442014-02-28 12:52:54 +01002893#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2894
2895static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002896{
2897 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002898 struct delayed_work *dwork = to_delayed_work(work);
2899 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2900 kvmclock_update_work);
2901 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002902 struct kvm_vcpu *vcpu;
2903
2904 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002905 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002906 kvm_vcpu_kick(vcpu);
2907 }
2908}
2909
Andrew Jones7e44e442014-02-28 12:52:54 +01002910static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2911{
2912 struct kvm *kvm = v->kvm;
2913
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002914 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002915 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2916 KVMCLOCK_UPDATE_DELAY);
2917}
2918
Andrew Jones332967a2014-02-28 12:52:55 +01002919#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2920
2921static void kvmclock_sync_fn(struct work_struct *work)
2922{
2923 struct delayed_work *dwork = to_delayed_work(work);
2924 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2925 kvmclock_sync_work);
2926 struct kvm *kvm = container_of(ka, struct kvm, arch);
2927
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002928 if (!kvmclock_periodic_sync)
2929 return;
2930
Andrew Jones332967a2014-02-28 12:52:55 +01002931 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2932 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2933 KVMCLOCK_SYNC_PERIOD);
2934}
2935
Borislav Petkov191c8132019-04-18 18:32:50 +02002936/*
2937 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2938 */
2939static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2940{
2941 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08002942 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02002943 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2944
2945 return false;
2946}
2947
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002948static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002949{
2950 u64 mcg_cap = vcpu->arch.mcg_cap;
2951 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002952 u32 msr = msr_info->index;
2953 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002954
2955 switch (msr) {
2956 case MSR_IA32_MCG_STATUS:
2957 vcpu->arch.mcg_status = data;
2958 break;
2959 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002960 if (!(mcg_cap & MCG_CTL_P) &&
2961 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002962 return 1;
2963 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002964 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002965 vcpu->arch.mcg_ctl = data;
2966 break;
2967 default:
2968 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002969 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002970 u32 offset = array_index_nospec(
2971 msr - MSR_IA32_MC0_CTL,
2972 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2973
Andre Przywara114be422010-03-24 17:46:42 +01002974 /* only 0 or all 1s can be written to IA32_MCi_CTL
2975 * some Linux kernels though clear bit 10 in bank 4 to
2976 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2977 * this to avoid an uncatched #GP in the guest
2978 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002979 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002980 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002981 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002982
2983 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002984 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002985 (offset & 0x3) == 1 && data != 0) {
2986 if (!can_set_mci_status(vcpu))
2987 return -1;
2988 }
2989
Huang Ying890ca9a2009-05-11 16:48:15 +08002990 vcpu->arch.mce_banks[offset] = data;
2991 break;
2992 }
2993 return 1;
2994 }
2995 return 0;
2996}
2997
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002998static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu)
2999{
3000 u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
3001
3002 return (vcpu->arch.apf.msr_en_val & mask) == mask;
3003}
3004
Gleb Natapov344d9582010-10-14 11:22:50 +02003005static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
3006{
3007 gpa_t gpa = data & ~0x3f;
3008
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003009 /* Bits 4:5 are reserved, Should be zero */
3010 if (data & 0x30)
Gleb Natapov344d9582010-10-14 11:22:50 +02003011 return 1;
3012
Oliver Upton66570e92020-08-18 15:24:28 +00003013 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_VMEXIT) &&
3014 (data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT))
3015 return 1;
3016
3017 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT) &&
3018 (data & KVM_ASYNC_PF_DELIVERY_AS_INT))
3019 return 1;
3020
Wanpeng Li9d3c4472020-06-29 18:26:31 +08003021 if (!lapic_in_kernel(vcpu))
Vitaly Kuznetsovd831de12020-09-11 11:31:47 +02003022 return data ? 1 : 0;
Wanpeng Li9d3c4472020-06-29 18:26:31 +08003023
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003024 vcpu->arch.apf.msr_en_val = data;
Gleb Natapov344d9582010-10-14 11:22:50 +02003025
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003026 if (!kvm_pv_async_pf_enabled(vcpu)) {
Gleb Natapov344d9582010-10-14 11:22:50 +02003027 kvm_clear_async_pf_completion_queue(vcpu);
3028 kvm_async_pf_hash_reset(vcpu);
3029 return 0;
3030 }
3031
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003032 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003033 sizeof(u64)))
Gleb Natapov344d9582010-10-14 11:22:50 +02003034 return 1;
3035
Gleb Natapov6adba522010-10-14 11:22:55 +02003036 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07003037 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003038
Gleb Natapov344d9582010-10-14 11:22:50 +02003039 kvm_async_pf_wakeup_all(vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003040
3041 return 0;
3042}
3043
3044static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data)
3045{
3046 /* Bits 8-63 are reserved */
3047 if (data >> 8)
3048 return 1;
3049
3050 if (!lapic_in_kernel(vcpu))
3051 return 1;
3052
3053 vcpu->arch.apf.msr_int_val = data;
3054
3055 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK;
3056
Gleb Natapov344d9582010-10-14 11:22:50 +02003057 return 0;
3058}
3059
Glauber Costa12f9a482011-02-01 14:16:40 -05003060static void kvmclock_reset(struct kvm_vcpu *vcpu)
3061{
Andy Honig0b794592013-02-20 14:48:10 -08003062 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02003063 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05003064}
3065
Sean Christopherson77809382020-03-20 14:28:18 -07003066static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08003067{
3068 ++vcpu->stat.tlb_flush;
Jason Baronb36464772021-01-14 22:27:56 -05003069 static_call(kvm_x86_tlb_flush_all)(vcpu);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08003070}
3071
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04003072static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
3073{
3074 ++vcpu->stat.tlb_flush;
Jason Baronb36464772021-01-14 22:27:56 -05003075 static_call(kvm_x86_tlb_flush_guest)(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04003076}
3077
Glauber Costac9aaa892011-07-11 15:28:14 -04003078static void record_steal_time(struct kvm_vcpu *vcpu)
3079{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003080 struct kvm_host_map map;
3081 struct kvm_steal_time *st;
3082
David Woodhouse30b5c852021-03-01 12:53:09 +00003083 if (kvm_xen_msr_enabled(vcpu->kvm)) {
3084 kvm_xen_runstate_set_running(vcpu);
3085 return;
3086 }
3087
Glauber Costac9aaa892011-07-11 15:28:14 -04003088 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3089 return;
3090
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003091 /* -EAGAIN is returned in atomic context so we can just return. */
3092 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
3093 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04003094 return;
3095
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003096 st = map.hva +
3097 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3098
Wanpeng Lif38a7b72017-12-12 17:33:04 -08003099 /*
3100 * Doing a TLB flush here, on the guest's behalf, can avoid
3101 * expensive IPIs.
3102 */
Oliver Upton66570e92020-08-18 15:24:28 +00003103 if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) {
3104 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
3105 st->preempted & KVM_VCPU_FLUSH_TLB);
3106 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
3107 kvm_vcpu_flush_tlb_guest(vcpu);
3108 }
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003109
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003110 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003111
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003112 if (st->version & 1)
3113 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003114
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003115 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003116
3117 smp_wmb();
3118
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003119 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08003120 vcpu->arch.st.last_steal;
3121 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003122
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003123 smp_wmb();
3124
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003125 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04003126
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003127 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04003128}
3129
Will Auld8fe8ab42012-11-29 12:42:12 -08003130int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003131{
Gleb Natapov57537852012-01-15 14:17:22 +02003132 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08003133 u32 msr = msr_info->index;
3134 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02003135
Joao Martins1232f8e2018-06-13 06:10:37 -04003136 if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr)
Joao Martins23200b72018-06-13 09:55:44 -04003137 return kvm_xen_write_hypercall_page(vcpu, data);
Joao Martins1232f8e2018-06-13 06:10:37 -04003138
Carsten Otte15c4a642007-10-30 18:44:17 +01003139 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01003140 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003141 case MSR_IA32_UCODE_WRITE:
3142 case MSR_VM_HSAVE_PA:
3143 case MSR_AMD64_PATCH_LOADER:
3144 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02003145 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003146 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003147 break;
3148
Wanpeng Li518e7b92018-02-28 14:03:31 +08003149 case MSR_IA32_UCODE_REV:
3150 if (msr_info->host_initiated)
3151 vcpu->arch.microcode_version = data;
3152 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003153 case MSR_IA32_ARCH_CAPABILITIES:
3154 if (!msr_info->host_initiated)
3155 return 1;
3156 vcpu->arch.arch_capabilities = data;
3157 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02003158 case MSR_IA32_PERF_CAPABILITIES: {
3159 struct kvm_msr_entry msr_ent = {.index = msr, .data = 0};
3160
3161 if (!msr_info->host_initiated)
3162 return 1;
3163 if (guest_cpuid_has(vcpu, X86_FEATURE_PDCM) && kvm_get_msr_feature(&msr_ent))
3164 return 1;
3165 if (data & ~msr_ent.data)
3166 return 1;
3167
3168 vcpu->arch.perf_capabilities = data;
3169
3170 return 0;
3171 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003172 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07003173 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02003174 case MSR_K7_HWCR:
3175 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01003176 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08003177 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02003178
3179 /* Handle McStatusWrEn */
3180 if (data == BIT_ULL(18)) {
3181 vcpu->arch.msr_hwcr = data;
3182 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003183 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
3184 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02003185 return 1;
3186 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003187 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003188 case MSR_FAM10H_MMIO_CONF_BASE:
3189 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003190 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
3191 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003192 return 1;
3193 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003194 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003195 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003196 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003197 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01003198 return kvm_set_apic_base(vcpu, msr_info);
Xiaoyao Libf10bd02020-06-16 15:33:07 +08003199 case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003200 return kvm_x2apic_msr_write(vcpu, msr, data);
Dave Hansen09141ec2020-03-05 09:47:06 -08003201 case MSR_IA32_TSC_DEADLINE:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003202 kvm_set_lapic_tscdeadline_msr(vcpu, data);
3203 break;
Will Auldba904632012-11-29 12:42:50 -08003204 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003205 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08003206 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06003207 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08003208 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08003209 }
3210 vcpu->arch.ia32_tsc_adjust_msr = data;
3211 }
3212 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003213 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08003214 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
3215 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
3216 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
3217 return 1;
3218 vcpu->arch.ia32_misc_enable_msr = data;
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08003219 kvm_update_cpuid_runtime(vcpu);
Wanpeng Li511a85562019-05-21 14:06:54 +08003220 } else {
3221 vcpu->arch.ia32_misc_enable_msr = data;
3222 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003223 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003224 case MSR_IA32_SMBASE:
3225 if (!msr_info->host_initiated)
3226 return 1;
3227 vcpu->arch.smbase = data;
3228 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003229 case MSR_IA32_POWER_CTL:
3230 vcpu->arch.msr_ia32_power_ctl = data;
3231 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003232 case MSR_IA32_TSC:
Paolo Bonzini0c899c22020-09-24 14:45:27 +02003233 if (msr_info->host_initiated) {
3234 kvm_synchronize_tsc(vcpu, data);
3235 } else {
3236 u64 adj = kvm_compute_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset;
3237 adjust_tsc_offset_guest(vcpu, adj);
3238 vcpu->arch.ia32_tsc_adjust_msr += adj;
3239 }
Paolo Bonzinidd259932018-04-13 11:38:35 +02003240 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003241 case MSR_IA32_XSS:
3242 if (!msr_info->host_initiated &&
3243 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3244 return 1;
3245 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08003246 * KVM supports exposing PT to the guest, but does not support
3247 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
3248 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003249 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01003250 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003251 return 1;
3252 vcpu->arch.ia32_xss = data;
3253 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003254 case MSR_SMI_COUNT:
3255 if (!msr_info->host_initiated)
3256 return 1;
3257 vcpu->arch.smi_count = data;
3258 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003259 case MSR_KVM_WALL_CLOCK_NEW:
Oliver Upton66570e92020-08-18 15:24:28 +00003260 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3261 return 1;
3262
Joao Martins629b5342018-06-28 15:06:43 -04003263 vcpu->kvm->arch.wall_clock = data;
3264 kvm_write_wall_clock(vcpu->kvm, data, 0);
Oliver Upton66570e92020-08-18 15:24:28 +00003265 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003266 case MSR_KVM_WALL_CLOCK:
Oliver Upton66570e92020-08-18 15:24:28 +00003267 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3268 return 1;
3269
Joao Martins629b5342018-06-28 15:06:43 -04003270 vcpu->kvm->arch.wall_clock = data;
3271 kvm_write_wall_clock(vcpu->kvm, data, 0);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003272 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003273 case MSR_KVM_SYSTEM_TIME_NEW:
Oliver Upton66570e92020-08-18 15:24:28 +00003274 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3275 return 1;
3276
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003277 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003278 break;
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003279 case MSR_KVM_SYSTEM_TIME:
Oliver Upton66570e92020-08-18 15:24:28 +00003280 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3281 return 1;
3282
3283 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated);
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003284 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003285 case MSR_KVM_ASYNC_PF_EN:
Oliver Upton66570e92020-08-18 15:24:28 +00003286 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3287 return 1;
3288
Gleb Natapov344d9582010-10-14 11:22:50 +02003289 if (kvm_pv_enable_async_pf(vcpu, data))
3290 return 1;
3291 break;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003292 case MSR_KVM_ASYNC_PF_INT:
Oliver Upton66570e92020-08-18 15:24:28 +00003293 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
3294 return 1;
3295
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003296 if (kvm_pv_enable_async_pf_int(vcpu, data))
3297 return 1;
3298 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003299 case MSR_KVM_ASYNC_PF_ACK:
Oliver Upton66570e92020-08-18 15:24:28 +00003300 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3301 return 1;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003302 if (data & 0x1) {
3303 vcpu->arch.apf.pageready_pending = false;
3304 kvm_check_async_pf_completion(vcpu);
3305 }
3306 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003307 case MSR_KVM_STEAL_TIME:
Oliver Upton66570e92020-08-18 15:24:28 +00003308 if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME))
3309 return 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04003310
3311 if (unlikely(!sched_info_on()))
3312 return 1;
3313
3314 if (data & KVM_STEAL_RESERVED_MASK)
3315 return 1;
3316
Glauber Costac9aaa892011-07-11 15:28:14 -04003317 vcpu->arch.st.msr_val = data;
3318
3319 if (!(data & KVM_MSR_ENABLED))
3320 break;
3321
Glauber Costac9aaa892011-07-11 15:28:14 -04003322 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
3323
3324 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003325 case MSR_KVM_PV_EOI_EN:
Oliver Upton66570e92020-08-18 15:24:28 +00003326 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI))
3327 return 1;
3328
Ladi Prosek72bbf932018-10-16 18:49:59 +02003329 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003330 return 1;
3331 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003332
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003333 case MSR_KVM_POLL_CONTROL:
Oliver Upton66570e92020-08-18 15:24:28 +00003334 if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL))
3335 return 1;
3336
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003337 /* only enable bit supported */
3338 if (data & (-1ULL << 1))
3339 return 1;
3340
3341 vcpu->arch.msr_kvm_poll_control = data;
3342 break;
3343
Huang Ying890ca9a2009-05-11 16:48:15 +08003344 case MSR_IA32_MCG_CTL:
3345 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003346 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003347 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02003348
Wei Huang6912ac32015-06-12 01:34:56 -04003349 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3350 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003351 pr = true;
3352 fallthrough;
Wei Huang6912ac32015-06-12 01:34:56 -04003353 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3354 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003355 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003356 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02003357
3358 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03003359 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
3360 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02003361 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003362 case MSR_K7_CLK_CTL:
3363 /*
3364 * Ignore all writes to this no longer documented MSR.
3365 * Writes are only relevant for old K7 processors,
3366 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08003367 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003368 * affected processor models on the command line, hence
3369 * the need to ignore the workaround.
3370 */
3371 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003372 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003373 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3374 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003375 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3376 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003377 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003378 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3379 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3380 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003381 return kvm_hv_set_msr_common(vcpu, msr, data,
3382 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003383 case MSR_IA32_BBL_CR_CTL3:
3384 /* Drop writes to this legacy MSR -- see rdmsr
3385 * counterpart for further detail.
3386 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003387 if (report_ignored_msrs)
3388 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
3389 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05003390 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003391 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003392 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003393 return 1;
3394 vcpu->arch.osvw.length = data;
3395 break;
3396 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003397 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003398 return 1;
3399 vcpu->arch.osvw.status = data;
3400 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003401 case MSR_PLATFORM_INFO:
3402 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07003403 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
3404 cpuid_fault_enabled(vcpu)))
3405 return 1;
3406 vcpu->arch.msr_platform_info = data;
3407 break;
3408 case MSR_MISC_FEATURES_ENABLES:
3409 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
3410 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3411 !supports_cpuid_fault(vcpu)))
3412 return 1;
3413 vcpu->arch.msr_misc_features_enables = data;
3414 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003415 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003416 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003417 return kvm_pmu_set_msr(vcpu, msr_info);
Peter Xu6abe9c12020-06-22 18:04:41 -04003418 return KVM_MSR_RET_INVALID;
Carsten Otte15c4a642007-10-30 18:44:17 +01003419 }
3420 return 0;
3421}
3422EXPORT_SYMBOL_GPL(kvm_set_msr_common);
3423
Paolo Bonzini44883f02018-07-26 13:01:52 +02003424static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003425{
3426 u64 data;
3427 u64 mcg_cap = vcpu->arch.mcg_cap;
3428 unsigned bank_num = mcg_cap & 0xff;
3429
3430 switch (msr) {
3431 case MSR_IA32_P5_MC_ADDR:
3432 case MSR_IA32_P5_MC_TYPE:
3433 data = 0;
3434 break;
3435 case MSR_IA32_MCG_CAP:
3436 data = vcpu->arch.mcg_cap;
3437 break;
3438 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003439 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003440 return 1;
3441 data = vcpu->arch.mcg_ctl;
3442 break;
3443 case MSR_IA32_MCG_STATUS:
3444 data = vcpu->arch.mcg_status;
3445 break;
3446 default:
3447 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003448 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003449 u32 offset = array_index_nospec(
3450 msr - MSR_IA32_MC0_CTL,
3451 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3452
Huang Ying890ca9a2009-05-11 16:48:15 +08003453 data = vcpu->arch.mce_banks[offset];
3454 break;
3455 }
3456 return 1;
3457 }
3458 *pdata = data;
3459 return 0;
3460}
3461
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003462int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003463{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003464 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003465 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003466 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003467 case MSR_IA32_LASTBRANCHFROMIP:
3468 case MSR_IA32_LASTBRANCHTOIP:
3469 case MSR_IA32_LASTINTFROMIP:
3470 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303471 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003472 case MSR_K8_TSEG_ADDR:
3473 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003474 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003475 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003476 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003477 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003478 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003479 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003480 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003481 case MSR_F15H_EX_CFG:
Venkatesh Srinivas2ca1a062020-04-16 11:42:54 -07003482 /*
3483 * Intel Sandy Bridge CPUs must support the RAPL (running average power
3484 * limit) MSRs. Just return 0, as we do not want to expose the host
3485 * data here. Do not conditionalize this on CPUID, as KVM does not do
3486 * so for existing CPU-specific MSRs.
3487 */
3488 case MSR_RAPL_POWER_UNIT:
3489 case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */
3490 case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */
3491 case MSR_PKG_ENERGY_STATUS: /* Total package */
3492 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003493 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003494 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003495 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Vitaly Kuznetsovc28fa562021-03-29 14:48:04 +02003496 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
3497 return kvm_pmu_get_msr(vcpu, msr_info);
3498 if (!msr_info->host_initiated)
3499 return 1;
3500 msr_info->data = 0;
3501 break;
Wei Huang6912ac32015-06-12 01:34:56 -04003502 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3503 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3504 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3505 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003506 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003507 return kvm_pmu_get_msr(vcpu, msr_info);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003508 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003509 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003510 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003511 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003512 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003513 case MSR_IA32_ARCH_CAPABILITIES:
3514 if (!msr_info->host_initiated &&
3515 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3516 return 1;
3517 msr_info->data = vcpu->arch.arch_capabilities;
3518 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02003519 case MSR_IA32_PERF_CAPABILITIES:
3520 if (!msr_info->host_initiated &&
3521 !guest_cpuid_has(vcpu, X86_FEATURE_PDCM))
3522 return 1;
3523 msr_info->data = vcpu->arch.perf_capabilities;
3524 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003525 case MSR_IA32_POWER_CTL:
3526 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3527 break;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003528 case MSR_IA32_TSC: {
3529 /*
3530 * Intel SDM states that MSR_IA32_TSC read adds the TSC offset
3531 * even when not intercepted. AMD manual doesn't explicitly
3532 * state this but appears to behave the same.
3533 *
Maxim Levitskyee6fa052020-09-21 13:38:05 +03003534 * On userspace reads and writes, however, we unconditionally
Paolo Bonzinic0623f52020-10-21 18:05:58 -04003535 * return L1's TSC value to ensure backwards-compatible
Maxim Levitskyee6fa052020-09-21 13:38:05 +03003536 * behavior for migration.
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003537 */
3538 u64 tsc_offset = msr_info->host_initiated ? vcpu->arch.l1_tsc_offset :
3539 vcpu->arch.tsc_offset;
3540
3541 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + tsc_offset;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003542 break;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003543 }
Avi Kivity9ba075a2008-05-26 20:06:35 +03003544 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003545 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003546 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003547 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003548 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003549 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003550 /*
3551 * MSR_EBC_FREQUENCY_ID
3552 * Conservative value valid for even the basic CPU models.
3553 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3554 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3555 * and 266MHz for model 3, or 4. Set Core Clock
3556 * Frequency to System Bus Frequency Ratio to 1 (bits
3557 * 31:24) even though these are only valid for CPU
3558 * models > 2, however guests may end up dividing or
3559 * multiplying by zero otherwise.
3560 */
3561 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003562 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003563 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003564 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003565 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003566 break;
Xiaoyao Libf10bd02020-06-16 15:33:07 +08003567 case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003568 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Dave Hansen09141ec2020-03-05 09:47:06 -08003569 case MSR_IA32_TSC_DEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003570 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003571 break;
Will Auldba904632012-11-29 12:42:50 -08003572 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003573 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003574 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003575 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003576 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003577 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003578 case MSR_IA32_SMBASE:
3579 if (!msr_info->host_initiated)
3580 return 1;
3581 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003582 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003583 case MSR_SMI_COUNT:
3584 msr_info->data = vcpu->arch.smi_count;
3585 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003586 case MSR_IA32_PERF_STATUS:
3587 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003588 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003589 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003590 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003591 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003592 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003593 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003594 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003595 case MSR_KVM_WALL_CLOCK:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003596 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3597 return 1;
3598
3599 msr_info->data = vcpu->kvm->arch.wall_clock;
3600 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003601 case MSR_KVM_WALL_CLOCK_NEW:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003602 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3603 return 1;
3604
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003605 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003606 break;
3607 case MSR_KVM_SYSTEM_TIME:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003608 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3609 return 1;
3610
3611 msr_info->data = vcpu->arch.time;
3612 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003613 case MSR_KVM_SYSTEM_TIME_NEW:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003614 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3615 return 1;
3616
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003617 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003618 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003619 case MSR_KVM_ASYNC_PF_EN:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003620 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3621 return 1;
3622
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003623 msr_info->data = vcpu->arch.apf.msr_en_val;
3624 break;
3625 case MSR_KVM_ASYNC_PF_INT:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003626 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
3627 return 1;
3628
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003629 msr_info->data = vcpu->arch.apf.msr_int_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003630 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003631 case MSR_KVM_ASYNC_PF_ACK:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003632 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3633 return 1;
3634
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003635 msr_info->data = 0;
3636 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003637 case MSR_KVM_STEAL_TIME:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003638 if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME))
3639 return 1;
3640
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003641 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003642 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003643 case MSR_KVM_PV_EOI_EN:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003644 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI))
3645 return 1;
3646
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003647 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003648 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003649 case MSR_KVM_POLL_CONTROL:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003650 if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL))
3651 return 1;
3652
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003653 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3654 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003655 case MSR_IA32_P5_MC_ADDR:
3656 case MSR_IA32_P5_MC_TYPE:
3657 case MSR_IA32_MCG_CAP:
3658 case MSR_IA32_MCG_CTL:
3659 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003660 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003661 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3662 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003663 case MSR_IA32_XSS:
3664 if (!msr_info->host_initiated &&
3665 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3666 return 1;
3667 msr_info->data = vcpu->arch.ia32_xss;
3668 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003669 case MSR_K7_CLK_CTL:
3670 /*
3671 * Provide expected ramp-up count for K7. All other
3672 * are set to zero, indicating minimum divisors for
3673 * every field.
3674 *
3675 * This prevents guest kernels on AMD host with CPU
3676 * type 6, model 8 and higher from exploding due to
3677 * the rdmsr failing.
3678 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003679 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003680 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003681 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003682 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3683 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003684 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3685 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003686 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003687 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3688 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3689 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003690 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003691 msr_info->index, &msr_info->data,
3692 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003693 case MSR_IA32_BBL_CR_CTL3:
3694 /* This legacy MSR exists but isn't fully documented in current
3695 * silicon. It is however accessed by winxp in very narrow
3696 * scenarios where it sets bit #19, itself documented as
3697 * a "reserved" bit. Best effort attempt to source coherent
3698 * read data here should the balance of the register be
3699 * interpreted by the guest:
3700 *
3701 * L2 cache control register 3: 64GB range, 256KB size,
3702 * enabled, latency 0x1, configured
3703 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003704 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003705 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003706 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003707 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003708 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003709 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003710 break;
3711 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003712 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003713 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003714 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003715 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003716 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003717 if (!msr_info->host_initiated &&
3718 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3719 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003720 msr_info->data = vcpu->arch.msr_platform_info;
3721 break;
3722 case MSR_MISC_FEATURES_ENABLES:
3723 msr_info->data = vcpu->arch.msr_misc_features_enables;
3724 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003725 case MSR_K7_HWCR:
3726 msr_info->data = vcpu->arch.msr_hwcr;
3727 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003728 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003729 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003730 return kvm_pmu_get_msr(vcpu, msr_info);
Peter Xu6abe9c12020-06-22 18:04:41 -04003731 return KVM_MSR_RET_INVALID;
Carsten Otte15c4a642007-10-30 18:44:17 +01003732 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003733 return 0;
3734}
3735EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3736
Carsten Otte313a3dc2007-10-11 19:16:52 +02003737/*
3738 * Read or write a bunch of msrs. All parameters are kernel addresses.
3739 *
3740 * @return number of msrs set successfully.
3741 */
3742static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3743 struct kvm_msr_entry *entries,
3744 int (*do_msr)(struct kvm_vcpu *vcpu,
3745 unsigned index, u64 *data))
3746{
Tom Lendacky801e4592018-02-21 13:39:51 -06003747 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003748
Carsten Otte313a3dc2007-10-11 19:16:52 +02003749 for (i = 0; i < msrs->nmsrs; ++i)
3750 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3751 break;
3752
Carsten Otte313a3dc2007-10-11 19:16:52 +02003753 return i;
3754}
3755
3756/*
3757 * Read or write a bunch of msrs. Parameters are user addresses.
3758 *
3759 * @return number of msrs set successfully.
3760 */
3761static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3762 int (*do_msr)(struct kvm_vcpu *vcpu,
3763 unsigned index, u64 *data),
3764 int writeback)
3765{
3766 struct kvm_msrs msrs;
3767 struct kvm_msr_entry *entries;
3768 int r, n;
3769 unsigned size;
3770
3771 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003772 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003773 goto out;
3774
3775 r = -E2BIG;
3776 if (msrs.nmsrs >= MAX_IO_MSRS)
3777 goto out;
3778
Carsten Otte313a3dc2007-10-11 19:16:52 +02003779 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003780 entries = memdup_user(user_msrs->entries, size);
3781 if (IS_ERR(entries)) {
3782 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003783 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003784 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003785
3786 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3787 if (r < 0)
3788 goto out_free;
3789
3790 r = -EFAULT;
3791 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3792 goto out_free;
3793
3794 r = n;
3795
3796out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003797 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003798out:
3799 return r;
3800}
3801
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003802static inline bool kvm_can_mwait_in_guest(void)
3803{
3804 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003805 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3806 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003807}
3808
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02003809static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu,
3810 struct kvm_cpuid2 __user *cpuid_arg)
3811{
3812 struct kvm_cpuid2 cpuid;
3813 int r;
3814
3815 r = -EFAULT;
3816 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
3817 return r;
3818
3819 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries);
3820 if (r)
3821 return r;
3822
3823 r = -EFAULT;
3824 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
3825 return r;
3826
3827 return 0;
3828}
3829
Alexander Graf784aa3d2014-07-14 18:27:35 +02003830int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003831{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003832 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003833
3834 switch (ext) {
3835 case KVM_CAP_IRQCHIP:
3836 case KVM_CAP_HLT:
3837 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003838 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003839 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003840 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003841 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003842 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003843 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003844 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003845 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003846 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003847 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003848 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003849 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003850 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003851 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003852 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003853 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003854 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003855 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003856 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003857 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003858 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003859 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003860 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003861 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003862 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003863 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003864 case KVM_CAP_HYPERV_CPUID:
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02003865 case KVM_CAP_SYS_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003866 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003867 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003868 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003869 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003870 case KVM_CAP_ASYNC_PF:
Vitaly Kuznetsov72de5fa4c2020-05-25 16:41:22 +02003871 case KVM_CAP_ASYNC_PF_INT:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003872 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003873 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003874 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003875 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003876 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003877 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003878 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003879 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003880 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003881 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003882 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003883 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003884 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003885 case KVM_CAP_EXCEPTION_PAYLOAD:
Peter Xub9b27822020-05-05 11:47:50 -04003886 case KVM_CAP_SET_GUEST_DEBUG:
Jim Mattson1aa561b2020-06-03 16:56:21 -07003887 case KVM_CAP_LAST_CPU:
Alexander Graf1ae09952020-09-25 16:34:16 +02003888 case KVM_CAP_X86_USER_SPACE_MSR:
Alexander Graf1a1552542020-09-25 16:34:21 +02003889 case KVM_CAP_X86_MSR_FILTER:
Oliver Upton66570e92020-08-18 15:24:28 +00003890 case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
Sean Christophersonfe7e9482021-04-12 16:21:43 +12003891#ifdef CONFIG_X86_SGX_KVM
3892 case KVM_CAP_SGX_ATTRIBUTE:
3893#endif
Nathan Tempelman54526d12021-04-08 22:32:14 +00003894 case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003895 r = 1;
3896 break;
Maxim Levitsky7e582cc2021-04-01 16:54:45 +03003897 case KVM_CAP_SET_GUEST_DEBUG2:
3898 return KVM_GUESTDBG_VALID_MASK;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05003899#ifdef CONFIG_KVM_XEN
Joao Martins23200b72018-06-13 09:55:44 -04003900 case KVM_CAP_XEN_HVM:
3901 r = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR |
David Woodhouse8d4e7e82020-12-04 01:02:04 +00003902 KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL |
3903 KVM_XEN_HVM_CONFIG_SHARED_INFO;
David Woodhouse30b5c852021-03-01 12:53:09 +00003904 if (sched_info_on())
3905 r |= KVM_XEN_HVM_CONFIG_RUNSTATE;
Joao Martins23200b72018-06-13 09:55:44 -04003906 break;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05003907#endif
Ken Hofsass01643c52018-01-31 16:03:36 -08003908 case KVM_CAP_SYNC_REGS:
3909 r = KVM_SYNC_X86_VALID_FIELDS;
3910 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003911 case KVM_CAP_ADJUST_CLOCK:
3912 r = KVM_CLOCK_TSC_STABLE;
3913 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003914 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003915 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3916 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003917 if(kvm_can_mwait_in_guest())
3918 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003919 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003920 case KVM_CAP_X86_SMM:
3921 /* SMBASE is usually relocated above 1M on modern chipsets,
3922 * and SMM handlers might indeed rely on 4G segment limits,
3923 * so do not report SMM to be available if real mode is
3924 * emulated via vm86 mode. Still, do not go to great lengths
3925 * to avoid userspace's usage of the feature, because it is a
3926 * fringe case that is not enabled except via specific settings
3927 * of the module parameters.
3928 */
Jason Baronb36464772021-01-14 22:27:56 -05003929 r = static_call(kvm_x86_has_emulated_msr)(kvm, MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003930 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003931 case KVM_CAP_VAPIC:
Jason Baronb36464772021-01-14 22:27:56 -05003932 r = !static_call(kvm_x86_cpu_has_accelerated_tpr)();
Avi Kivity774ead32007-12-26 13:57:04 +02003933 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003934 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003935 r = KVM_SOFT_MAX_VCPUS;
3936 break;
3937 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003938 r = KVM_MAX_VCPUS;
3939 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003940 case KVM_CAP_MAX_VCPU_ID:
3941 r = KVM_MAX_VCPU_ID;
3942 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003943 case KVM_CAP_PV_MMU: /* obsolete */
3944 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003945 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003946 case KVM_CAP_MCE:
3947 r = KVM_MAX_MCE_BANKS;
3948 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003949 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003950 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003951 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003952 case KVM_CAP_TSC_CONTROL:
3953 r = kvm_has_tsc_control;
3954 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003955 case KVM_CAP_X2APIC_API:
3956 r = KVM_X2APIC_API_VALID_FLAGS;
3957 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003958 case KVM_CAP_NESTED_STATE:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003959 r = kvm_x86_ops.nested_ops->get_state ?
3960 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003961 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003962 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003963 r = kvm_x86_ops.enable_direct_tlbflush != NULL;
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003964 break;
3965 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003966 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003967 break;
Mohammed Gamal3edd6832020-07-10 17:48:11 +02003968 case KVM_CAP_SMALLER_MAXPHYADDR:
3969 r = (int) allow_smaller_maxphyaddr;
3970 break;
Andrew Jones004a0122020-08-04 19:06:04 +02003971 case KVM_CAP_STEAL_TIME:
3972 r = sched_info_on();
3973 break;
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08003974 case KVM_CAP_X86_BUS_LOCK_EXIT:
3975 if (kvm_has_bus_lock_exit)
3976 r = KVM_BUS_LOCK_DETECTION_OFF |
3977 KVM_BUS_LOCK_DETECTION_EXIT;
3978 else
3979 r = 0;
3980 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003981 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003982 break;
3983 }
3984 return r;
3985
3986}
3987
Carsten Otte043405e2007-10-10 17:16:19 +02003988long kvm_arch_dev_ioctl(struct file *filp,
3989 unsigned int ioctl, unsigned long arg)
3990{
3991 void __user *argp = (void __user *)arg;
3992 long r;
3993
3994 switch (ioctl) {
3995 case KVM_GET_MSR_INDEX_LIST: {
3996 struct kvm_msr_list __user *user_msr_list = argp;
3997 struct kvm_msr_list msr_list;
3998 unsigned n;
3999
4000 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004001 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02004002 goto out;
4003 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004004 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004005 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02004006 goto out;
4007 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02004008 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02004009 goto out;
4010 r = -EFAULT;
4011 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
4012 num_msrs_to_save * sizeof(u32)))
4013 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02004014 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02004015 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004016 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02004017 goto out;
4018 r = 0;
4019 break;
4020 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02004021 case KVM_GET_SUPPORTED_CPUID:
4022 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02004023 struct kvm_cpuid2 __user *cpuid_arg = argp;
4024 struct kvm_cpuid2 cpuid;
4025
4026 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004027 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02004028 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02004029
4030 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
4031 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02004032 if (r)
4033 goto out;
4034
4035 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004036 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02004037 goto out;
4038 r = 0;
4039 break;
4040 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08004041 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08004042 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004043 if (copy_to_user(argp, &kvm_mce_cap_supported,
4044 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004045 goto out;
4046 r = 0;
4047 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004048 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
4049 struct kvm_msr_list __user *user_msr_list = argp;
4050 struct kvm_msr_list msr_list;
4051 unsigned int n;
4052
4053 r = -EFAULT;
4054 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
4055 goto out;
4056 n = msr_list.nmsrs;
4057 msr_list.nmsrs = num_msr_based_features;
4058 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
4059 goto out;
4060 r = -E2BIG;
4061 if (n < msr_list.nmsrs)
4062 goto out;
4063 r = -EFAULT;
4064 if (copy_to_user(user_msr_list->indices, &msr_based_features,
4065 num_msr_based_features * sizeof(u32)))
4066 goto out;
4067 r = 0;
4068 break;
4069 }
4070 case KVM_GET_MSRS:
4071 r = msr_io(NULL, argp, do_get_msr_feature, 1);
4072 break;
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02004073 case KVM_GET_SUPPORTED_HV_CPUID:
4074 r = kvm_ioctl_get_supported_hv_cpuid(NULL, argp);
4075 break;
Carsten Otte043405e2007-10-10 17:16:19 +02004076 default:
4077 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08004078 break;
Carsten Otte043405e2007-10-10 17:16:19 +02004079 }
4080out:
4081 return r;
4082}
4083
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004084static void wbinvd_ipi(void *garbage)
4085{
4086 wbinvd();
4087}
4088
4089static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
4090{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06004091 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004092}
4093
Carsten Otte313a3dc2007-10-11 19:16:52 +02004094void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
4095{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004096 /* Address WBINVD may be executed by guest */
4097 if (need_emulate_wbinvd(vcpu)) {
Jason Baronb36464772021-01-14 22:27:56 -05004098 if (static_call(kvm_x86_has_wbinvd_exit)())
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004099 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
4100 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
4101 smp_call_function_single(vcpu->cpu,
4102 wbinvd_ipi, NULL, 1);
4103 }
4104
Jason Baronb36464772021-01-14 22:27:56 -05004105 static_call(kvm_x86_vcpu_load)(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02004106
Babu Moger37486132020-05-12 18:59:06 -05004107 /* Save host pkru register if supported */
4108 vcpu->arch.host_pkru = read_pkru();
4109
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02004110 /* Apply any externally detected TSC adjustments (due to suspend) */
4111 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
4112 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
4113 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08004114 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02004115 }
4116
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01004117 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02004118 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02004119 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10004120 if (tsc_delta < 0)
4121 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07004122
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01004123 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08004124 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02004125 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04004126 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10004127 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10004128 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02004129
4130 if (kvm_lapic_hv_timer_in_use(vcpu))
4131 kvm_lapic_restart_hv_timer(vcpu);
4132
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02004133 /*
4134 * On a host with synchronized TSC, there is no need to update
4135 * kvmclock on vcpu->cpu migration
4136 */
4137 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03004138 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10004139 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02004140 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10004141 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10004142 }
Glauber Costac9aaa892011-07-11 15:28:14 -04004143
Glauber Costac9aaa892011-07-11 15:28:14 -04004144 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004145}
4146
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004147static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
4148{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004149 struct kvm_host_map map;
4150 struct kvm_steal_time *st;
4151
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004152 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
4153 return;
4154
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00004155 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00004156 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004157
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004158 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
4159 &vcpu->arch.st.cache, true))
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004160 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004161
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004162 st = map.hva +
4163 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
4164
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00004165 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004166
4167 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004168}
4169
Carsten Otte313a3dc2007-10-11 19:16:52 +02004170void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
4171{
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004172 int idx;
4173
Tom Lendackyf1c63662020-12-14 10:29:50 -05004174 if (vcpu->preempted && !vcpu->arch.guest_state_protected)
Jason Baronb36464772021-01-14 22:27:56 -05004175 vcpu->arch.preempted_in_kernel = !static_call(kvm_x86_get_cpl)(vcpu);
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004176
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004177 /*
4178 * Take the srcu lock as memslots will be accessed to check the gfn
4179 * cache generation against the memslots generation.
4180 */
4181 idx = srcu_read_lock(&vcpu->kvm->srcu);
David Woodhouse30b5c852021-03-01 12:53:09 +00004182 if (kvm_xen_msr_enabled(vcpu->kvm))
4183 kvm_xen_runstate_set_preempted(vcpu);
4184 else
4185 kvm_steal_time_set_preempted(vcpu);
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004186 srcu_read_unlock(&vcpu->kvm->srcu, idx);
David Woodhouse30b5c852021-03-01 12:53:09 +00004187
Jason Baronb36464772021-01-14 22:27:56 -05004188 static_call(kvm_x86_vcpu_put)(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02004189 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01004190 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02004191 * If userspace has set any breakpoints or watchpoints, dr6 is restored
4192 * on every vmexit, but if not, we might have a stale dr6 from the
4193 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01004194 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02004195 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004196}
4197
Carsten Otte313a3dc2007-10-11 19:16:52 +02004198static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
4199 struct kvm_lapic_state *s)
4200{
Liran Alonfa59cc02017-12-24 18:12:53 +02004201 if (vcpu->arch.apicv_active)
Jason Baronb36464772021-01-14 22:27:56 -05004202 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004203
Radim Krčmářa92e2542016-07-12 22:09:22 +02004204 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004205}
4206
4207static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
4208 struct kvm_lapic_state *s)
4209{
Radim Krčmářa92e2542016-07-12 22:09:22 +02004210 int r;
4211
4212 r = kvm_apic_set_state(vcpu, s);
4213 if (r)
4214 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03004215 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004216
4217 return 0;
4218}
4219
Matt Gingell127a4572015-11-17 17:32:05 +01004220static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
4221{
Paolo Bonzini71cc8492020-11-27 09:18:20 +01004222 /*
4223 * We can accept userspace's request for interrupt injection
4224 * as long as we have a place to store the interrupt number.
4225 * The actual injection will happen when the CPU is able to
4226 * deliver the interrupt.
4227 */
4228 if (kvm_cpu_has_extint(vcpu))
4229 return false;
4230
4231 /* Acknowledging ExtINT does not happen if LINT0 is masked. */
Matt Gingell127a4572015-11-17 17:32:05 +01004232 return (!lapic_in_kernel(vcpu) ||
4233 kvm_apic_accept_pic_intr(vcpu));
4234}
4235
Matt Gingell782d4222015-11-16 15:26:00 -08004236static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
4237{
4238 return kvm_arch_interrupt_allowed(vcpu) &&
Matt Gingell782d4222015-11-16 15:26:00 -08004239 kvm_cpu_accept_dm_intr(vcpu);
4240}
4241
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004242static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
4243 struct kvm_interrupt *irq)
4244{
Chen Gang02cdb502013-02-27 11:33:25 +08004245 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004246 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004247
4248 if (!irqchip_in_kernel(vcpu->kvm)) {
4249 kvm_queue_interrupt(vcpu, irq->irq, false);
4250 kvm_make_request(KVM_REQ_EVENT, vcpu);
4251 return 0;
4252 }
4253
4254 /*
4255 * With in-kernel LAPIC, we only use this to inject EXTINT, so
4256 * fail for in-kernel 8259.
4257 */
4258 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004259 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004260
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004261 if (vcpu->arch.pending_external_vector != -1)
4262 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004263
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004264 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08004265 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004266 return 0;
4267}
4268
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004269static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
4270{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004271 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004272
4273 return 0;
4274}
4275
Paolo Bonzinif0778252015-04-01 15:06:40 +02004276static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
4277{
Paolo Bonzini64d60672015-05-07 11:36:11 +02004278 kvm_make_request(KVM_REQ_SMI, vcpu);
4279
Paolo Bonzinif0778252015-04-01 15:06:40 +02004280 return 0;
4281}
4282
Avi Kivityb209749f2007-10-22 16:50:39 +02004283static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
4284 struct kvm_tpr_access_ctl *tac)
4285{
4286 if (tac->flags)
4287 return -EINVAL;
4288 vcpu->arch.tpr_access_reporting = !!tac->enabled;
4289 return 0;
4290}
4291
Huang Ying890ca9a2009-05-11 16:48:15 +08004292static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
4293 u64 mcg_cap)
4294{
4295 int r;
4296 unsigned bank_num = mcg_cap & 0xff, bank;
4297
4298 r = -EINVAL;
Jim Mattsonc4e0e4ab2020-05-11 15:56:16 -07004299 if (!bank_num || bank_num > KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08004300 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004301 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08004302 goto out;
4303 r = 0;
4304 vcpu->arch.mcg_cap = mcg_cap;
4305 /* Init IA32_MCG_CTL to all 1s */
4306 if (mcg_cap & MCG_CTL_P)
4307 vcpu->arch.mcg_ctl = ~(u64)0;
4308 /* Init IA32_MCi_CTL to all 1s */
4309 for (bank = 0; bank < bank_num; bank++)
4310 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004311
Jason Baronb36464772021-01-14 22:27:56 -05004312 static_call(kvm_x86_setup_mce)(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08004313out:
4314 return r;
4315}
4316
4317static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
4318 struct kvm_x86_mce *mce)
4319{
4320 u64 mcg_cap = vcpu->arch.mcg_cap;
4321 unsigned bank_num = mcg_cap & 0xff;
4322 u64 *banks = vcpu->arch.mce_banks;
4323
4324 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
4325 return -EINVAL;
4326 /*
4327 * if IA32_MCG_CTL is not all 1s, the uncorrected error
4328 * reporting is disabled
4329 */
4330 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
4331 vcpu->arch.mcg_ctl != ~(u64)0)
4332 return 0;
4333 banks += 4 * mce->bank;
4334 /*
4335 * if IA32_MCi_CTL is not all 1s, the uncorrected error
4336 * reporting is disabled for the bank
4337 */
4338 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
4339 return 0;
4340 if (mce->status & MCI_STATUS_UC) {
4341 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02004342 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03004343 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08004344 return 0;
4345 }
4346 if (banks[1] & MCI_STATUS_VAL)
4347 mce->status |= MCI_STATUS_OVER;
4348 banks[2] = mce->addr;
4349 banks[3] = mce->misc;
4350 vcpu->arch.mcg_status = mce->mcg_status;
4351 banks[1] = mce->status;
4352 kvm_queue_exception(vcpu, MC_VECTOR);
4353 } else if (!(banks[1] & MCI_STATUS_VAL)
4354 || !(banks[1] & MCI_STATUS_UC)) {
4355 if (banks[1] & MCI_STATUS_VAL)
4356 mce->status |= MCI_STATUS_OVER;
4357 banks[2] = mce->addr;
4358 banks[3] = mce->misc;
4359 banks[1] = mce->status;
4360 } else
4361 banks[1] |= MCI_STATUS_OVER;
4362 return 0;
4363}
4364
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004365static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
4366 struct kvm_vcpu_events *events)
4367{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004368 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07004369
Jay Zhou1f7becf2021-01-18 16:47:20 +08004370 if (kvm_check_request(KVM_REQ_SMI, vcpu))
4371 process_smi(vcpu);
4372
Wanpeng Li664f8e22017-08-24 03:35:09 -07004373 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08004374 * In guest mode, payload delivery should be deferred,
4375 * so that the L1 hypervisor can intercept #PF before
4376 * CR2 is modified (or intercept #DB before DR6 is
4377 * modified under nVMX). Unless the per-VM capability,
4378 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
4379 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
4380 * opportunistically defer the exception payload, deliver it if the
4381 * capability hasn't been requested before processing a
4382 * KVM_GET_VCPU_EVENTS.
4383 */
4384 if (!vcpu->kvm->arch.exception_payload_enabled &&
4385 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
4386 kvm_deliver_exception_payload(vcpu);
4387
4388 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07004389 * The API doesn't provide the instruction length for software
4390 * exceptions, so don't report them. As long as the guest RIP
4391 * isn't advanced, we should expect to encounter the exception
4392 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07004393 */
Jim Mattson59073aa2018-10-16 14:29:20 -07004394 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
4395 events->exception.injected = 0;
4396 events->exception.pending = 0;
4397 } else {
4398 events->exception.injected = vcpu->arch.exception.injected;
4399 events->exception.pending = vcpu->arch.exception.pending;
4400 /*
4401 * For ABI compatibility, deliberately conflate
4402 * pending and injected exceptions when
4403 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
4404 */
4405 if (!vcpu->kvm->arch.exception_payload_enabled)
4406 events->exception.injected |=
4407 vcpu->arch.exception.pending;
4408 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004409 events->exception.nr = vcpu->arch.exception.nr;
4410 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
4411 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07004412 events->exception_has_payload = vcpu->arch.exception.has_payload;
4413 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004414
Jan Kiszka03b82a32010-02-15 10:45:41 +01004415 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03004416 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004417 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01004418 events->interrupt.soft = 0;
Jason Baronb36464772021-01-14 22:27:56 -05004419 events->interrupt.shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004420
4421 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004422 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jason Baronb36464772021-01-14 22:27:56 -05004423 events->nmi.masked = static_call(kvm_x86_get_nmi_mask)(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004424 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004425
Jan Kiszka66450a22013-03-13 12:42:34 +01004426 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004427
Paolo Bonzinif0778252015-04-01 15:06:40 +02004428 events->smi.smm = is_smm(vcpu);
4429 events->smi.pending = vcpu->arch.smi_pending;
4430 events->smi.smm_inside_nmi =
4431 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
4432 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
4433
Jan Kiszkadab4b912009-12-06 18:24:15 +01004434 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02004435 | KVM_VCPUEVENT_VALID_SHADOW
4436 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07004437 if (vcpu->kvm->arch.exception_payload_enabled)
4438 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
4439
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004440 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004441}
4442
Sean Christophersonc5833c72019-04-02 08:03:10 -07004443static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004444
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004445static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
4446 struct kvm_vcpu_events *events)
4447{
Jan Kiszkadab4b912009-12-06 18:24:15 +01004448 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01004449 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02004450 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07004451 | KVM_VCPUEVENT_VALID_SMM
4452 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004453 return -EINVAL;
4454
Jim Mattson59073aa2018-10-16 14:29:20 -07004455 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
4456 if (!vcpu->kvm->arch.exception_payload_enabled)
4457 return -EINVAL;
4458 if (events->exception.pending)
4459 events->exception.injected = 0;
4460 else
4461 events->exception_has_payload = 0;
4462 } else {
4463 events->exception.pending = 0;
4464 events->exception_has_payload = 0;
4465 }
4466
4467 if ((events->exception.injected || events->exception.pending) &&
4468 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02004469 return -EINVAL;
4470
David Hildenbrand28bf2882017-03-23 11:46:03 +01004471 /* INITs are latched while in SMM */
4472 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
4473 (events->smi.smm || events->smi.pending) &&
4474 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4475 return -EINVAL;
4476
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004477 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07004478 vcpu->arch.exception.injected = events->exception.injected;
4479 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004480 vcpu->arch.exception.nr = events->exception.nr;
4481 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
4482 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07004483 vcpu->arch.exception.has_payload = events->exception_has_payload;
4484 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004485
Liran Alon04140b42018-03-23 03:01:31 +03004486 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004487 vcpu->arch.interrupt.nr = events->interrupt.nr;
4488 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01004489 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
Jason Baronb36464772021-01-14 22:27:56 -05004490 static_call(kvm_x86_set_interrupt_shadow)(vcpu,
4491 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004492
4493 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01004494 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
4495 vcpu->arch.nmi_pending = events->nmi.pending;
Jason Baronb36464772021-01-14 22:27:56 -05004496 static_call(kvm_x86_set_nmi_mask)(vcpu, events->nmi.masked);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004497
Jan Kiszka66450a22013-03-13 12:42:34 +01004498 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004499 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01004500 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004501
Paolo Bonzinif0778252015-04-01 15:06:40 +02004502 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07004503 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
4504 if (events->smi.smm)
4505 vcpu->arch.hflags |= HF_SMM_MASK;
4506 else
4507 vcpu->arch.hflags &= ~HF_SMM_MASK;
4508 kvm_smm_changed(vcpu);
4509 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004510
Paolo Bonzinif0778252015-04-01 15:06:40 +02004511 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004512
4513 if (events->smi.smm) {
4514 if (events->smi.smm_inside_nmi)
4515 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02004516 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004517 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02004518 }
4519
4520 if (lapic_in_kernel(vcpu)) {
4521 if (events->smi.latched_init)
4522 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
4523 else
4524 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02004525 }
4526 }
4527
Avi Kivity3842d132010-07-27 12:30:24 +03004528 kvm_make_request(KVM_REQ_EVENT, vcpu);
4529
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004530 return 0;
4531}
4532
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004533static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
4534 struct kvm_debugregs *dbgregs)
4535{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004536 unsigned long val;
4537
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004538 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004539 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004540 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004541 dbgregs->dr7 = vcpu->arch.dr7;
4542 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004543 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004544}
4545
4546static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
4547 struct kvm_debugregs *dbgregs)
4548{
4549 if (dbgregs->flags)
4550 return -EINVAL;
4551
Paolo Bonzinifd238002020-11-13 08:31:09 -05004552 if (!kvm_dr6_valid(dbgregs->dr6))
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004553 return -EINVAL;
Paolo Bonzinifd238002020-11-13 08:31:09 -05004554 if (!kvm_dr7_valid(dbgregs->dr7))
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004555 return -EINVAL;
4556
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004557 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004558 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004559 vcpu->arch.dr6 = dbgregs->dr6;
4560 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004561 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004562
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004563 return 0;
4564}
4565
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004566#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4567
4568static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4569{
Marc Orrb666a4b2018-11-06 14:53:56 -08004570 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004571 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004572 u64 valid;
4573
4574 /*
4575 * Copy legacy XSAVE area, to avoid complications with CPUID
4576 * leaves 0 and 1 in the loop below.
4577 */
4578 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4579
4580 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004581 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004582 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4583
4584 /*
4585 * Copy each region from the possibly compacted offset to the
4586 * non-compacted offset.
4587 */
Dave Hansend91cab72015-09-02 16:31:26 -07004588 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004589 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004590 u64 xfeature_mask = valid & -valid;
4591 int xfeature_nr = fls64(xfeature_mask) - 1;
4592 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004593
4594 if (src) {
4595 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004596 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004597 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004598 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004599 memcpy(dest + offset, &vcpu->arch.pkru,
4600 sizeof(vcpu->arch.pkru));
4601 else
4602 memcpy(dest + offset, src, size);
4603
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004604 }
4605
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004606 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004607 }
4608}
4609
4610static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4611{
Marc Orrb666a4b2018-11-06 14:53:56 -08004612 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004613 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4614 u64 valid;
4615
4616 /*
4617 * Copy legacy XSAVE area, to avoid complications with CPUID
4618 * leaves 0 and 1 in the loop below.
4619 */
4620 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4621
4622 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004623 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004624 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004625 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004626
4627 /*
4628 * Copy each region from the non-compacted offset to the
4629 * possibly compacted offset.
4630 */
Dave Hansend91cab72015-09-02 16:31:26 -07004631 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004632 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004633 u64 xfeature_mask = valid & -valid;
4634 int xfeature_nr = fls64(xfeature_mask) - 1;
4635 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004636
4637 if (dest) {
4638 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004639 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004640 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004641 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004642 memcpy(&vcpu->arch.pkru, src + offset,
4643 sizeof(vcpu->arch.pkru));
4644 else
4645 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004646 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004647
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004648 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004649 }
4650}
4651
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004652static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4653 struct kvm_xsave *guest_xsave)
4654{
Tom Lendackyed02b212020-12-10 11:10:01 -06004655 if (!vcpu->arch.guest_fpu)
4656 return;
4657
Borislav Petkovd366bf72016-04-04 22:25:02 +02004658 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004659 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4660 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004661 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004662 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004663 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004664 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004665 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004666 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004667 }
4668}
4669
Wanpeng Lia5758132017-05-11 02:58:55 -07004670#define XSAVE_MXCSR_OFFSET 24
4671
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004672static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4673 struct kvm_xsave *guest_xsave)
4674{
Tom Lendackyed02b212020-12-10 11:10:01 -06004675 u64 xstate_bv;
4676 u32 mxcsr;
4677
4678 if (!vcpu->arch.guest_fpu)
4679 return 0;
4680
4681 xstate_bv = *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
4682 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004683
Borislav Petkovd366bf72016-04-04 22:25:02 +02004684 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004685 /*
4686 * Here we allow setting states that are not present in
4687 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4688 * with old userspace.
4689 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004690 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004691 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004692 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004693 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004694 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4695 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004696 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004697 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004698 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004699 }
4700 return 0;
4701}
4702
4703static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4704 struct kvm_xcrs *guest_xcrs)
4705{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004706 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004707 guest_xcrs->nr_xcrs = 0;
4708 return;
4709 }
4710
4711 guest_xcrs->nr_xcrs = 1;
4712 guest_xcrs->flags = 0;
4713 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4714 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4715}
4716
4717static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4718 struct kvm_xcrs *guest_xcrs)
4719{
4720 int i, r = 0;
4721
Borislav Petkovd366bf72016-04-04 22:25:02 +02004722 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004723 return -EINVAL;
4724
4725 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4726 return -EINVAL;
4727
4728 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4729 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004730 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004731 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004732 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004733 break;
4734 }
4735 if (r)
4736 r = -EINVAL;
4737 return r;
4738}
4739
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004740/*
4741 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4742 * stopped by the hypervisor. This function will be called from the host only.
4743 * EINVAL is returned when the host attempts to set the flag for a guest that
4744 * does not support pv clocks.
4745 */
4746static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4747{
Andy Honig0b794592013-02-20 14:48:10 -08004748 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004749 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004750 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004751 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4752 return 0;
4753}
4754
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004755static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4756 struct kvm_enable_cap *cap)
4757{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004758 int r;
4759 uint16_t vmcs_version;
4760 void __user *user_ptr;
4761
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004762 if (cap->flags)
4763 return -EINVAL;
4764
4765 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004766 case KVM_CAP_HYPERV_SYNIC2:
4767 if (cap->args[0])
4768 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004769 fallthrough;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004770
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004771 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004772 if (!irqchip_in_kernel(vcpu->kvm))
4773 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004774 return kvm_hv_activate_synic(vcpu, cap->cap ==
4775 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004776 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004777 if (!kvm_x86_ops.nested_ops->enable_evmcs)
Sean Christopherson51589172018-12-03 13:53:10 -08004778 return -ENOTTY;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004779 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004780 if (!r) {
4781 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4782 if (copy_to_user(user_ptr, &vmcs_version,
4783 sizeof(vmcs_version)))
4784 r = -EFAULT;
4785 }
4786 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004787 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004788 if (!kvm_x86_ops.enable_direct_tlbflush)
Tianyu Lan344c6c82019-08-22 22:30:20 +08004789 return -ENOTTY;
4790
Jason Baronb36464772021-01-14 22:27:56 -05004791 return static_call(kvm_x86_enable_direct_tlbflush)(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004792
Oliver Upton66570e92020-08-18 15:24:28 +00004793 case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
4794 vcpu->arch.pv_cpuid.enforce = cap->args[0];
Oliver Upton01b4f512020-10-27 16:10:42 -07004795 if (vcpu->arch.pv_cpuid.enforce)
4796 kvm_update_pv_runtime(vcpu);
Oliver Upton66570e92020-08-18 15:24:28 +00004797
4798 return 0;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004799 default:
4800 return -EINVAL;
4801 }
4802}
4803
Carsten Otte313a3dc2007-10-11 19:16:52 +02004804long kvm_arch_vcpu_ioctl(struct file *filp,
4805 unsigned int ioctl, unsigned long arg)
4806{
4807 struct kvm_vcpu *vcpu = filp->private_data;
4808 void __user *argp = (void __user *)arg;
4809 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004810 union {
4811 struct kvm_lapic_state *lapic;
4812 struct kvm_xsave *xsave;
4813 struct kvm_xcrs *xcrs;
4814 void *buffer;
4815 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004816
Christoffer Dall9b0624712017-12-04 21:35:36 +01004817 vcpu_load(vcpu);
4818
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004819 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004820 switch (ioctl) {
4821 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004822 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004823 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004824 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004825 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4826 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004827
Dave Hansenb772ff32008-08-11 10:01:47 -07004828 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004829 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004830 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004831 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004832 if (r)
4833 goto out;
4834 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004835 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004836 goto out;
4837 r = 0;
4838 break;
4839 }
4840 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004841 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004842 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004843 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004844 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004845 if (IS_ERR(u.lapic)) {
4846 r = PTR_ERR(u.lapic);
4847 goto out_nofree;
4848 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004849
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004850 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004851 break;
4852 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004853 case KVM_INTERRUPT: {
4854 struct kvm_interrupt irq;
4855
4856 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004857 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004858 goto out;
4859 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004860 break;
4861 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004862 case KVM_NMI: {
4863 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004864 break;
4865 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004866 case KVM_SMI: {
4867 r = kvm_vcpu_ioctl_smi(vcpu);
4868 break;
4869 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004870 case KVM_SET_CPUID: {
4871 struct kvm_cpuid __user *cpuid_arg = argp;
4872 struct kvm_cpuid cpuid;
4873
4874 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004875 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004876 goto out;
4877 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004878 break;
4879 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004880 case KVM_SET_CPUID2: {
4881 struct kvm_cpuid2 __user *cpuid_arg = argp;
4882 struct kvm_cpuid2 cpuid;
4883
4884 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004885 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004886 goto out;
4887 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004888 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004889 break;
4890 }
4891 case KVM_GET_CPUID2: {
4892 struct kvm_cpuid2 __user *cpuid_arg = argp;
4893 struct kvm_cpuid2 cpuid;
4894
4895 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004896 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004897 goto out;
4898 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004899 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004900 if (r)
4901 goto out;
4902 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004903 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004904 goto out;
4905 r = 0;
4906 break;
4907 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004908 case KVM_GET_MSRS: {
4909 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004910 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004911 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004912 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004913 }
4914 case KVM_SET_MSRS: {
4915 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004916 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004917 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004918 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004919 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004920 case KVM_TPR_ACCESS_REPORTING: {
4921 struct kvm_tpr_access_ctl tac;
4922
4923 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004924 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004925 goto out;
4926 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4927 if (r)
4928 goto out;
4929 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004930 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004931 goto out;
4932 r = 0;
4933 break;
4934 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004935 case KVM_SET_VAPIC_ADDR: {
4936 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004937 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004938
4939 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004940 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004941 goto out;
4942 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004943 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004944 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004945 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004946 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004947 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004948 break;
4949 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004950 case KVM_X86_SETUP_MCE: {
4951 u64 mcg_cap;
4952
4953 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004954 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004955 goto out;
4956 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4957 break;
4958 }
4959 case KVM_X86_SET_MCE: {
4960 struct kvm_x86_mce mce;
4961
4962 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004963 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004964 goto out;
4965 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4966 break;
4967 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004968 case KVM_GET_VCPU_EVENTS: {
4969 struct kvm_vcpu_events events;
4970
4971 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4972
4973 r = -EFAULT;
4974 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4975 break;
4976 r = 0;
4977 break;
4978 }
4979 case KVM_SET_VCPU_EVENTS: {
4980 struct kvm_vcpu_events events;
4981
4982 r = -EFAULT;
4983 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4984 break;
4985
4986 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4987 break;
4988 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004989 case KVM_GET_DEBUGREGS: {
4990 struct kvm_debugregs dbgregs;
4991
4992 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4993
4994 r = -EFAULT;
4995 if (copy_to_user(argp, &dbgregs,
4996 sizeof(struct kvm_debugregs)))
4997 break;
4998 r = 0;
4999 break;
5000 }
5001 case KVM_SET_DEBUGREGS: {
5002 struct kvm_debugregs dbgregs;
5003
5004 r = -EFAULT;
5005 if (copy_from_user(&dbgregs, argp,
5006 sizeof(struct kvm_debugregs)))
5007 break;
5008
5009 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
5010 break;
5011 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005012 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08005013 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005014 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005015 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005016 break;
5017
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005018 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005019
5020 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005021 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005022 break;
5023 r = 0;
5024 break;
5025 }
5026 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02005027 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01005028 if (IS_ERR(u.xsave)) {
5029 r = PTR_ERR(u.xsave);
5030 goto out_nofree;
5031 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005032
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005033 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005034 break;
5035 }
5036 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08005037 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005038 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005039 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005040 break;
5041
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005042 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005043
5044 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005045 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005046 sizeof(struct kvm_xcrs)))
5047 break;
5048 r = 0;
5049 break;
5050 }
5051 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02005052 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01005053 if (IS_ERR(u.xcrs)) {
5054 r = PTR_ERR(u.xcrs);
5055 goto out_nofree;
5056 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005057
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005058 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005059 break;
5060 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01005061 case KVM_SET_TSC_KHZ: {
5062 u32 user_tsc_khz;
5063
5064 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01005065 user_tsc_khz = (u32)arg;
5066
Marcelo Tosatti26769f92020-06-16 08:47:41 -03005067 if (kvm_has_tsc_control &&
5068 user_tsc_khz >= kvm_max_guest_tsc_khz)
Joerg Roedel92a1f122011-03-25 09:44:51 +01005069 goto out;
5070
Zachary Amsdencc578282012-02-03 15:43:50 -02005071 if (user_tsc_khz == 0)
5072 user_tsc_khz = tsc_khz;
5073
Haozhong Zhang381d5852015-10-20 15:39:04 +08005074 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
5075 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01005076
Joerg Roedel92a1f122011-03-25 09:44:51 +01005077 goto out;
5078 }
5079 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02005080 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01005081 goto out;
5082 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05005083 case KVM_KVMCLOCK_CTRL: {
5084 r = kvm_set_guest_paused(vcpu);
5085 goto out;
5086 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03005087 case KVM_ENABLE_CAP: {
5088 struct kvm_enable_cap cap;
5089
5090 r = -EFAULT;
5091 if (copy_from_user(&cap, argp, sizeof(cap)))
5092 goto out;
5093 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
5094 break;
5095 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005096 case KVM_GET_NESTED_STATE: {
5097 struct kvm_nested_state __user *user_kvm_nested_state = argp;
5098 u32 user_data_size;
5099
5100 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04005101 if (!kvm_x86_ops.nested_ops->get_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005102 break;
5103
5104 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03005105 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005106 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03005107 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005108
Paolo Bonzini33b22172020-04-17 10:24:18 -04005109 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state,
5110 user_data_size);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005111 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03005112 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005113
5114 if (r > user_data_size) {
5115 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03005116 r = -EFAULT;
5117 else
5118 r = -E2BIG;
5119 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005120 }
Liran Alon26b471c2018-09-16 14:28:20 +03005121
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005122 r = 0;
5123 break;
5124 }
5125 case KVM_SET_NESTED_STATE: {
5126 struct kvm_nested_state __user *user_kvm_nested_state = argp;
5127 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08005128 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005129
5130 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04005131 if (!kvm_x86_ops.nested_ops->set_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005132 break;
5133
Liran Alon26b471c2018-09-16 14:28:20 +03005134 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005135 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03005136 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005137
Liran Alon26b471c2018-09-16 14:28:20 +03005138 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005139 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03005140 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005141
5142 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02005143 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04005144 | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING
5145 | KVM_STATE_NESTED_GIF_SET))
Liran Alon26b471c2018-09-16 14:28:20 +03005146 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005147
5148 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02005149 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
5150 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03005151 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005152
Sean Christophersonad5996d2019-11-22 08:58:18 -08005153 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini33b22172020-04-17 10:24:18 -04005154 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08005155 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005156 break;
5157 }
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02005158 case KVM_GET_SUPPORTED_HV_CPUID:
5159 r = kvm_ioctl_get_supported_hv_cpuid(vcpu, argp);
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01005160 break;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005161#ifdef CONFIG_KVM_XEN
David Woodhouse3e324612021-02-02 16:53:25 +00005162 case KVM_XEN_VCPU_GET_ATTR: {
5163 struct kvm_xen_vcpu_attr xva;
5164
5165 r = -EFAULT;
5166 if (copy_from_user(&xva, argp, sizeof(xva)))
5167 goto out;
5168 r = kvm_xen_vcpu_get_attr(vcpu, &xva);
5169 if (!r && copy_to_user(argp, &xva, sizeof(xva)))
5170 r = -EFAULT;
5171 break;
5172 }
5173 case KVM_XEN_VCPU_SET_ATTR: {
5174 struct kvm_xen_vcpu_attr xva;
5175
5176 r = -EFAULT;
5177 if (copy_from_user(&xva, argp, sizeof(xva)))
5178 goto out;
5179 r = kvm_xen_vcpu_set_attr(vcpu, &xva);
5180 break;
5181 }
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005182#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +02005183 default:
5184 r = -EINVAL;
5185 }
5186out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005187 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01005188out_nofree:
5189 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005190 return r;
5191}
5192
Souptick Joarder1499fa82018-04-19 00:49:58 +05305193vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01005194{
5195 return VM_FAULT_SIGBUS;
5196}
5197
Carsten Otte1fe779f2007-10-29 16:08:35 +01005198static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
5199{
5200 int ret;
5201
5202 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08005203 return -EINVAL;
Jason Baronb36464772021-01-14 22:27:56 -05005204 ret = static_call(kvm_x86_set_tss_addr)(kvm, addr);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005205 return ret;
5206}
5207
Sheng Yangb927a3c2009-07-21 10:42:48 +08005208static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
5209 u64 ident_addr)
5210{
Jason Baronb36464772021-01-14 22:27:56 -05005211 return static_call(kvm_x86_set_identity_map_addr)(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08005212}
5213
Carsten Otte1fe779f2007-10-29 16:08:35 +01005214static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005215 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01005216{
5217 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
5218 return -EINVAL;
5219
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005220 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005221
5222 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005223 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005224
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005225 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005226 return 0;
5227}
5228
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005229static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01005230{
Dave Hansen39de71e2010-08-19 18:11:14 -07005231 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005232}
5233
Carsten Otte1fe779f2007-10-29 16:08:35 +01005234static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
5235{
David Hildenbrand90bca052017-04-07 10:50:23 +02005236 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005237 int r;
5238
5239 r = 0;
5240 switch (chip->chip_id) {
5241 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02005242 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01005243 sizeof(struct kvm_pic_state));
5244 break;
5245 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02005246 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01005247 sizeof(struct kvm_pic_state));
5248 break;
5249 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02005250 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005251 break;
5252 default:
5253 r = -EINVAL;
5254 break;
5255 }
5256 return r;
5257}
5258
5259static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
5260{
David Hildenbrand90bca052017-04-07 10:50:23 +02005261 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005262 int r;
5263
5264 r = 0;
5265 switch (chip->chip_id) {
5266 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02005267 spin_lock(&pic->lock);
5268 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01005269 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02005270 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005271 break;
5272 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02005273 spin_lock(&pic->lock);
5274 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01005275 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02005276 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005277 break;
5278 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02005279 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005280 break;
5281 default:
5282 r = -EINVAL;
5283 break;
5284 }
David Hildenbrand90bca052017-04-07 10:50:23 +02005285 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005286 return r;
5287}
5288
Sheng Yange0f63cb2008-03-04 00:50:59 +08005289static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
5290{
Radim Krčmář34f39412016-03-02 22:56:50 +01005291 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
5292
5293 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
5294
5295 mutex_lock(&kps->lock);
5296 memcpy(ps, &kps->channels, sizeof(*ps));
5297 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305298 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08005299}
5300
5301static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
5302{
Andrew Honig01856042015-11-18 14:50:23 -08005303 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01005304 struct kvm_pit *pit = kvm->arch.vpit;
5305
5306 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01005307 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08005308 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01005309 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
5310 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305311 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04005312}
5313
5314static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
5315{
Beth Kone9f42752009-07-07 11:50:38 -04005316 mutex_lock(&kvm->arch.vpit->pit_state.lock);
5317 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
5318 sizeof(ps->channels));
5319 ps->flags = kvm->arch.vpit->pit_state.flags;
5320 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005321 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305322 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04005323}
5324
5325static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
5326{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305327 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08005328 int i;
Beth Kone9f42752009-07-07 11:50:38 -04005329 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01005330 struct kvm_pit *pit = kvm->arch.vpit;
5331
5332 mutex_lock(&pit->pit_state.lock);
5333 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04005334 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
5335 if (!prev_legacy && cur_legacy)
5336 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01005337 memcpy(&pit->pit_state.channels, &ps->channels,
5338 sizeof(pit->pit_state.channels));
5339 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08005340 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01005341 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01005342 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01005343 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305344 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08005345}
5346
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005347static int kvm_vm_ioctl_reinject(struct kvm *kvm,
5348 struct kvm_reinject_control *control)
5349{
Radim Krčmář71474e22016-03-02 22:56:45 +01005350 struct kvm_pit *pit = kvm->arch.vpit;
5351
Radim Krčmář71474e22016-03-02 22:56:45 +01005352 /* pit->pit_state.lock was overloaded to prevent userspace from getting
5353 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
5354 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
5355 */
5356 mutex_lock(&pit->pit_state.lock);
5357 kvm_pit_set_reinject(pit, control->pit_reinject);
5358 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01005359
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005360 return 0;
5361}
5362
Sean Christopherson0dff0842020-02-18 13:07:29 -08005363void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08005364{
Sean Christophersona018eba2021-02-12 16:50:10 -08005365
Kai Huang88178fd2015-01-28 10:54:27 +08005366 /*
Sean Christophersona018eba2021-02-12 16:50:10 -08005367 * Flush all CPUs' dirty log buffers to the dirty_bitmap. Called
5368 * before reporting dirty_bitmap to userspace. KVM flushes the buffers
5369 * on all VM-Exits, thus we only need to kick running vCPUs to force a
5370 * VM-Exit.
Kai Huang88178fd2015-01-28 10:54:27 +08005371 */
Sean Christophersona018eba2021-02-12 16:50:10 -08005372 struct kvm_vcpu *vcpu;
5373 int i;
5374
5375 kvm_for_each_vcpu(i, vcpu, kvm)
5376 kvm_vcpu_kick(vcpu);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08005377}
5378
Yang Zhangaa2fbe62013-04-11 19:21:40 +08005379int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
5380 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04005381{
5382 if (!irqchip_in_kernel(kvm))
5383 return -ENXIO;
5384
5385 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08005386 irq_event->irq, irq_event->level,
5387 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04005388 return 0;
5389}
5390
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01005391int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
5392 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03005393{
5394 int r;
5395
5396 if (cap->flags)
5397 return -EINVAL;
5398
5399 switch (cap->cap) {
5400 case KVM_CAP_DISABLE_QUIRKS:
5401 kvm->arch.disabled_quirks = cap->args[0];
5402 r = 0;
5403 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07005404 case KVM_CAP_SPLIT_IRQCHIP: {
5405 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07005406 r = -EINVAL;
5407 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
5408 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07005409 r = -EEXIST;
5410 if (irqchip_in_kernel(kvm))
5411 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02005412 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07005413 goto split_irqchip_unlock;
5414 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02005415 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07005416 goto split_irqchip_unlock;
5417 /* Pairs with irqchip_in_kernel. */
5418 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005419 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07005420 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07005421 r = 0;
5422split_irqchip_unlock:
5423 mutex_unlock(&kvm->lock);
5424 break;
5425 }
Radim Krčmář371313132016-07-12 22:09:27 +02005426 case KVM_CAP_X2APIC_API:
5427 r = -EINVAL;
5428 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
5429 break;
5430
5431 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
5432 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02005433 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
5434 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02005435
5436 r = 0;
5437 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005438 case KVM_CAP_X86_DISABLE_EXITS:
5439 r = -EINVAL;
5440 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
5441 break;
5442
5443 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
5444 kvm_can_mwait_in_guest())
5445 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03005446 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07005447 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005448 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
5449 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08005450 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
5451 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005452 r = 0;
5453 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07005454 case KVM_CAP_MSR_PLATFORM_INFO:
5455 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
5456 r = 0;
5457 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07005458 case KVM_CAP_EXCEPTION_PAYLOAD:
5459 kvm->arch.exception_payload_enabled = cap->args[0];
5460 r = 0;
5461 break;
Alexander Graf1ae09952020-09-25 16:34:16 +02005462 case KVM_CAP_X86_USER_SPACE_MSR:
5463 kvm->arch.user_space_msr_mask = cap->args[0];
5464 r = 0;
5465 break;
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005466 case KVM_CAP_X86_BUS_LOCK_EXIT:
5467 r = -EINVAL;
5468 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE)
5469 break;
5470
5471 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) &&
5472 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT))
5473 break;
5474
5475 if (kvm_has_bus_lock_exit &&
5476 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)
5477 kvm->arch.bus_lock_detection_enabled = true;
5478 r = 0;
5479 break;
Sean Christophersonfe7e9482021-04-12 16:21:43 +12005480#ifdef CONFIG_X86_SGX_KVM
5481 case KVM_CAP_SGX_ATTRIBUTE: {
5482 unsigned long allowed_attributes = 0;
5483
5484 r = sgx_set_attribute(&allowed_attributes, cap->args[0]);
5485 if (r)
5486 break;
5487
5488 /* KVM only supports the PROVISIONKEY privileged attribute. */
5489 if ((allowed_attributes & SGX_ATTR_PROVISIONKEY) &&
5490 !(allowed_attributes & ~SGX_ATTR_PROVISIONKEY))
5491 kvm->arch.sgx_provisioning_allowed = true;
5492 else
5493 r = -EINVAL;
5494 break;
5495 }
5496#endif
Nathan Tempelman54526d12021-04-08 22:32:14 +00005497 case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
5498 r = -EINVAL;
5499 if (kvm_x86_ops.vm_copy_enc_context_from)
5500 r = kvm_x86_ops.vm_copy_enc_context_from(kvm, cap->args[0]);
5501 return r;
Nadav Amit90de4a12015-04-13 01:53:41 +03005502 default:
5503 r = -EINVAL;
5504 break;
5505 }
5506 return r;
5507}
5508
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005509static struct kvm_x86_msr_filter *kvm_alloc_msr_filter(bool default_allow)
Alexander Graf1a1552542020-09-25 16:34:21 +02005510{
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005511 struct kvm_x86_msr_filter *msr_filter;
Alexander Graf1a1552542020-09-25 16:34:21 +02005512
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005513 msr_filter = kzalloc(sizeof(*msr_filter), GFP_KERNEL_ACCOUNT);
5514 if (!msr_filter)
5515 return NULL;
Alexander Graf1a1552542020-09-25 16:34:21 +02005516
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005517 msr_filter->default_allow = default_allow;
5518 return msr_filter;
Alexander Graf1a1552542020-09-25 16:34:21 +02005519}
5520
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005521static void kvm_free_msr_filter(struct kvm_x86_msr_filter *msr_filter)
Alexander Graf1a1552542020-09-25 16:34:21 +02005522{
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005523 u32 i;
5524
5525 if (!msr_filter)
5526 return;
5527
5528 for (i = 0; i < msr_filter->count; i++)
5529 kfree(msr_filter->ranges[i].bitmap);
5530
5531 kfree(msr_filter);
5532}
5533
5534static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,
5535 struct kvm_msr_filter_range *user_range)
5536{
Alexander Graf1a1552542020-09-25 16:34:21 +02005537 unsigned long *bitmap = NULL;
5538 size_t bitmap_size;
Alexander Graf1a1552542020-09-25 16:34:21 +02005539
5540 if (!user_range->nmsrs)
5541 return 0;
5542
Siddharth Chandrasekaranaca35282021-05-03 14:21:11 +02005543 if (user_range->flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE))
5544 return -EINVAL;
5545
5546 if (!user_range->flags)
5547 return -EINVAL;
5548
Alexander Graf1a1552542020-09-25 16:34:21 +02005549 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long);
5550 if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE)
5551 return -EINVAL;
5552
5553 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size);
5554 if (IS_ERR(bitmap))
5555 return PTR_ERR(bitmap);
5556
Siddharth Chandrasekaranaca35282021-05-03 14:21:11 +02005557 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) {
Alexander Graf1a1552542020-09-25 16:34:21 +02005558 .flags = user_range->flags,
5559 .base = user_range->base,
5560 .nmsrs = user_range->nmsrs,
5561 .bitmap = bitmap,
5562 };
5563
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005564 msr_filter->count++;
Alexander Graf1a1552542020-09-25 16:34:21 +02005565 return 0;
Alexander Graf1a1552542020-09-25 16:34:21 +02005566}
5567
5568static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp)
5569{
5570 struct kvm_msr_filter __user *user_msr_filter = argp;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005571 struct kvm_x86_msr_filter *new_filter, *old_filter;
Alexander Graf1a1552542020-09-25 16:34:21 +02005572 struct kvm_msr_filter filter;
5573 bool default_allow;
Paolo Bonzini043248b2020-10-20 10:57:01 -04005574 bool empty = true;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005575 int r = 0;
Alexander Graf1a1552542020-09-25 16:34:21 +02005576 u32 i;
5577
5578 if (copy_from_user(&filter, user_msr_filter, sizeof(filter)))
5579 return -EFAULT;
5580
Paolo Bonzini043248b2020-10-20 10:57:01 -04005581 for (i = 0; i < ARRAY_SIZE(filter.ranges); i++)
5582 empty &= !filter.ranges[i].nmsrs;
Alexander Graf1a1552542020-09-25 16:34:21 +02005583
5584 default_allow = !(filter.flags & KVM_MSR_FILTER_DEFAULT_DENY);
Paolo Bonzini043248b2020-10-20 10:57:01 -04005585 if (empty && !default_allow)
5586 return -EINVAL;
5587
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005588 new_filter = kvm_alloc_msr_filter(default_allow);
5589 if (!new_filter)
5590 return -ENOMEM;
Paolo Bonzini043248b2020-10-20 10:57:01 -04005591
Alexander Graf1a1552542020-09-25 16:34:21 +02005592 for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) {
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005593 r = kvm_add_msr_filter(new_filter, &filter.ranges[i]);
5594 if (r) {
5595 kvm_free_msr_filter(new_filter);
5596 return r;
5597 }
Alexander Graf1a1552542020-09-25 16:34:21 +02005598 }
5599
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005600 mutex_lock(&kvm->lock);
5601
5602 /* The per-VM filter is protected by kvm->lock... */
5603 old_filter = srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1);
5604
5605 rcu_assign_pointer(kvm->arch.msr_filter, new_filter);
5606 synchronize_srcu(&kvm->srcu);
5607
5608 kvm_free_msr_filter(old_filter);
5609
Alexander Graf1a1552542020-09-25 16:34:21 +02005610 kvm_make_all_cpus_request(kvm, KVM_REQ_MSR_FILTER_CHANGED);
5611 mutex_unlock(&kvm->lock);
5612
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005613 return 0;
Alexander Graf1a1552542020-09-25 16:34:21 +02005614}
5615
Carsten Otte1fe779f2007-10-29 16:08:35 +01005616long kvm_arch_vm_ioctl(struct file *filp,
5617 unsigned int ioctl, unsigned long arg)
5618{
5619 struct kvm *kvm = filp->private_data;
5620 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03005621 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07005622 /*
5623 * This union makes it completely explicit to gcc-3.x
5624 * that these two variables' stack usage should be
5625 * combined, not added together.
5626 */
5627 union {
5628 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04005629 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005630 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07005631 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005632
5633 switch (ioctl) {
5634 case KVM_SET_TSS_ADDR:
5635 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005636 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005637 case KVM_SET_IDENTITY_MAP_ADDR: {
5638 u64 ident_addr;
5639
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005640 mutex_lock(&kvm->lock);
5641 r = -EINVAL;
5642 if (kvm->created_vcpus)
5643 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005644 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005645 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005646 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005647 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005648set_identity_unlock:
5649 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08005650 break;
5651 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01005652 case KVM_SET_NR_MMU_PAGES:
5653 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005654 break;
5655 case KVM_GET_NR_MMU_PAGES:
5656 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
5657 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005658 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005659 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01005660
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005661 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01005662 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005663 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005664
Avi Kivity3e515702012-03-05 14:23:29 +02005665 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02005666 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02005667 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005668
5669 r = kvm_pic_init(kvm);
5670 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005671 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005672
5673 r = kvm_ioapic_init(kvm);
5674 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01005675 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005676 goto create_irqchip_unlock;
5677 }
5678
Avi Kivity399ec802008-11-19 13:58:46 +02005679 r = kvm_setup_default_irq_routing(kvm);
5680 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08005681 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005682 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005683 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02005684 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01005685 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005686 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005687 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005688 create_irqchip_unlock:
5689 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005690 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005691 }
Sheng Yang78376992008-01-28 05:10:22 +08005692 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005693 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
5694 goto create_pit;
5695 case KVM_CREATE_PIT2:
5696 r = -EFAULT;
5697 if (copy_from_user(&u.pit_config, argp,
5698 sizeof(struct kvm_pit_config)))
5699 goto out;
5700 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005701 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02005702 r = -EEXIST;
5703 if (kvm->arch.vpit)
5704 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08005705 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005706 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08005707 if (kvm->arch.vpit)
5708 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02005709 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005710 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08005711 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005712 case KVM_GET_IRQCHIP: {
5713 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005714 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005715
Sasha Levinff5c2c02011-12-04 19:36:29 +02005716 chip = memdup_user(argp, sizeof(*chip));
5717 if (IS_ERR(chip)) {
5718 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005719 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005720 }
5721
Carsten Otte1fe779f2007-10-29 16:08:35 +01005722 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005723 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005724 goto get_irqchip_out;
5725 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5726 if (r)
5727 goto get_irqchip_out;
5728 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005729 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005730 goto get_irqchip_out;
5731 r = 0;
5732 get_irqchip_out:
5733 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005734 break;
5735 }
5736 case KVM_SET_IRQCHIP: {
5737 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005738 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005739
Sasha Levinff5c2c02011-12-04 19:36:29 +02005740 chip = memdup_user(argp, sizeof(*chip));
5741 if (IS_ERR(chip)) {
5742 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005743 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005744 }
5745
Carsten Otte1fe779f2007-10-29 16:08:35 +01005746 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005747 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005748 goto set_irqchip_out;
5749 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005750 set_irqchip_out:
5751 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005752 break;
5753 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005754 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005755 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005756 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005757 goto out;
5758 r = -ENXIO;
5759 if (!kvm->arch.vpit)
5760 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005761 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005762 if (r)
5763 goto out;
5764 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005765 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005766 goto out;
5767 r = 0;
5768 break;
5769 }
5770 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005771 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005772 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005773 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005774 mutex_lock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005775 r = -ENXIO;
5776 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005777 goto set_pit_out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005778 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005779set_pit_out:
5780 mutex_unlock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005781 break;
5782 }
Beth Kone9f42752009-07-07 11:50:38 -04005783 case KVM_GET_PIT2: {
5784 r = -ENXIO;
5785 if (!kvm->arch.vpit)
5786 goto out;
5787 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5788 if (r)
5789 goto out;
5790 r = -EFAULT;
5791 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5792 goto out;
5793 r = 0;
5794 break;
5795 }
5796 case KVM_SET_PIT2: {
5797 r = -EFAULT;
5798 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5799 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005800 mutex_lock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005801 r = -ENXIO;
5802 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005803 goto set_pit2_out;
Beth Kone9f42752009-07-07 11:50:38 -04005804 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005805set_pit2_out:
5806 mutex_unlock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005807 break;
5808 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005809 case KVM_REINJECT_CONTROL: {
5810 struct kvm_reinject_control control;
5811 r = -EFAULT;
5812 if (copy_from_user(&control, argp, sizeof(control)))
5813 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005814 r = -ENXIO;
5815 if (!kvm->arch.vpit)
5816 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005817 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005818 break;
5819 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005820 case KVM_SET_BOOT_CPU_ID:
5821 r = 0;
5822 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005823 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005824 r = -EBUSY;
5825 else
5826 kvm->arch.bsp_vcpu_id = arg;
5827 mutex_unlock(&kvm->lock);
5828 break;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005829#ifdef CONFIG_KVM_XEN
Ed Swierkffde22a2009-10-15 15:21:43 -07005830 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005831 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005832 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005833 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005834 goto out;
David Woodhouse78e98782021-02-02 13:19:35 +00005835 r = kvm_xen_hvm_config(kvm, &xhc);
Ed Swierkffde22a2009-10-15 15:21:43 -07005836 break;
5837 }
Joao Martinsa76b9642020-12-03 15:52:25 +00005838 case KVM_XEN_HVM_GET_ATTR: {
5839 struct kvm_xen_hvm_attr xha;
5840
5841 r = -EFAULT;
5842 if (copy_from_user(&xha, argp, sizeof(xha)))
Hannes Edercded19f2009-02-21 02:19:13 +01005843 goto out;
Joao Martinsa76b9642020-12-03 15:52:25 +00005844 r = kvm_xen_hvm_get_attr(kvm, &xha);
5845 if (!r && copy_to_user(argp, &xha, sizeof(xha)))
5846 r = -EFAULT;
5847 break;
5848 }
5849 case KVM_XEN_HVM_SET_ATTR: {
5850 struct kvm_xen_hvm_attr xha;
5851
5852 r = -EFAULT;
5853 if (copy_from_user(&xha, argp, sizeof(xha)))
5854 goto out;
5855 r = kvm_xen_hvm_set_attr(kvm, &xha);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005856 break;
5857 }
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005858#endif
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005859 case KVM_SET_CLOCK: {
Vitaly Kuznetsov77fcbe82021-03-31 14:41:29 +02005860 struct kvm_arch *ka = &kvm->arch;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005861 struct kvm_clock_data user_ns;
5862 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005863
5864 r = -EFAULT;
5865 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5866 goto out;
5867
5868 r = -EINVAL;
5869 if (user_ns.flags)
5870 goto out;
5871
5872 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005873 /*
5874 * TODO: userspace has to take care of races with VCPU_RUN, so
5875 * kvm_gen_update_masterclock() can be cut down to locked
5876 * pvclock_update_vm_gtod_copy().
5877 */
5878 kvm_gen_update_masterclock(kvm);
Vitaly Kuznetsov77fcbe82021-03-31 14:41:29 +02005879
5880 /*
5881 * This pairs with kvm_guest_time_update(): when masterclock is
5882 * in use, we use master_kernel_ns + kvmclock_offset to set
5883 * unsigned 'system_time' so if we use get_kvmclock_ns() (which
5884 * is slightly ahead) here we risk going negative on unsigned
5885 * 'system_time' when 'user_ns.clock' is very small.
5886 */
5887 spin_lock_irq(&ka->pvclock_gtod_sync_lock);
5888 if (kvm->arch.use_master_clock)
5889 now_ns = ka->master_kernel_ns;
5890 else
5891 now_ns = get_kvmclock_base_ns();
5892 ka->kvmclock_offset = user_ns.clock - now_ns;
5893 spin_unlock_irq(&ka->pvclock_gtod_sync_lock);
5894
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005895 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005896 break;
5897 }
5898 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005899 struct kvm_clock_data user_ns;
5900 u64 now_ns;
5901
Marcelo Tosattie891a322017-04-17 12:51:37 -03005902 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005903 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005904 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005905 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005906
5907 r = -EFAULT;
5908 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5909 goto out;
5910 r = 0;
5911 break;
5912 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005913 case KVM_MEMORY_ENCRYPT_OP: {
5914 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005915 if (kvm_x86_ops.mem_enc_op)
Jason Baronb36464772021-01-14 22:27:56 -05005916 r = static_call(kvm_x86_mem_enc_op)(kvm, argp);
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005917 break;
5918 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005919 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5920 struct kvm_enc_region region;
5921
5922 r = -EFAULT;
5923 if (copy_from_user(&region, argp, sizeof(region)))
5924 goto out;
5925
5926 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005927 if (kvm_x86_ops.mem_enc_reg_region)
Jason Baronb36464772021-01-14 22:27:56 -05005928 r = static_call(kvm_x86_mem_enc_reg_region)(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005929 break;
5930 }
5931 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5932 struct kvm_enc_region region;
5933
5934 r = -EFAULT;
5935 if (copy_from_user(&region, argp, sizeof(region)))
5936 goto out;
5937
5938 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005939 if (kvm_x86_ops.mem_enc_unreg_region)
Jason Baronb36464772021-01-14 22:27:56 -05005940 r = static_call(kvm_x86_mem_enc_unreg_region)(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005941 break;
5942 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005943 case KVM_HYPERV_EVENTFD: {
5944 struct kvm_hyperv_eventfd hvevfd;
5945
5946 r = -EFAULT;
5947 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5948 goto out;
5949 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5950 break;
5951 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005952 case KVM_SET_PMU_EVENT_FILTER:
5953 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5954 break;
Alexander Graf1a1552542020-09-25 16:34:21 +02005955 case KVM_X86_SET_MSR_FILTER:
5956 r = kvm_vm_ioctl_set_msr_filter(kvm, argp);
5957 break;
Carsten Otte043405e2007-10-10 17:16:19 +02005958 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005959 r = -ENOTTY;
Carsten Otte043405e2007-10-10 17:16:19 +02005960 }
5961out:
5962 return r;
5963}
5964
5965static void kvm_init_msr_list(void)
5966{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005967 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005968 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005969 unsigned i;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005970
Jim Mattsone2ada662019-08-21 11:20:04 -07005971 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005972 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005973
5974 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005975
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005976 num_msrs_to_save = 0;
5977 num_emulated_msrs = 0;
5978 num_msr_based_features = 0;
5979
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005980 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5981 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005982 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005983
5984 /*
5985 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005986 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005987 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005988 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005989 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005990 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005991 continue;
5992 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005993 case MSR_TSC_AUX:
Sean Christopherson36fa06f2021-05-04 10:17:26 -07005994 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP) &&
5995 !kvm_cpu_cap_has(X86_FEATURE_RDPID))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005996 continue;
5997 break;
Maxim Levitskyf4cfcd22020-05-23 19:14:55 +03005998 case MSR_IA32_UMWAIT_CONTROL:
5999 if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG))
6000 continue;
6001 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08006002 case MSR_IA32_RTIT_CTL:
6003 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006004 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08006005 continue;
6006 break;
6007 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006008 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08006009 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
6010 continue;
6011 break;
6012 case MSR_IA32_RTIT_OUTPUT_BASE:
6013 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006014 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08006015 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
6016 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
6017 continue;
6018 break;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08006019 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006020 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006021 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08006022 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
6023 continue;
6024 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02006025 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006026 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006027 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
6028 continue;
6029 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02006030 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006031 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006032 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
6033 continue;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08006034 break;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006035 default:
6036 break;
6037 }
6038
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006039 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Ottebbd9b642007-10-30 18:44:21 +01006040 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02006041
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006042 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
Jason Baronb36464772021-01-14 22:27:56 -05006043 if (!static_call(kvm_x86_has_emulated_msr)(NULL, emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02006044 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02006045
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006046 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02006047 }
Tom Lendacky801e4592018-02-21 13:39:51 -06006048
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006049 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06006050 struct kvm_msr_entry msr;
6051
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006052 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08006053 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06006054 continue;
6055
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006056 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06006057 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01006058}
6059
6060static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
6061 const void *v)
6062{
Avi Kivity70252a12010-01-19 12:51:22 +02006063 int handled = 0;
6064 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006065
Avi Kivity70252a12010-01-19 12:51:22 +02006066 do {
6067 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01006068 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006069 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
6070 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02006071 break;
6072 handled += n;
6073 addr += n;
6074 len -= n;
6075 v += n;
6076 } while (len);
6077
6078 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006079}
6080
6081static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
6082{
Avi Kivity70252a12010-01-19 12:51:22 +02006083 int handled = 0;
6084 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006085
Avi Kivity70252a12010-01-19 12:51:22 +02006086 do {
6087 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01006088 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006089 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
6090 addr, n, v))
6091 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02006092 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006093 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02006094 handled += n;
6095 addr += n;
6096 len -= n;
6097 v += n;
6098 } while (len);
6099
6100 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006101}
6102
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006103static void kvm_set_segment(struct kvm_vcpu *vcpu,
6104 struct kvm_segment *var, int seg)
6105{
Jason Baronb36464772021-01-14 22:27:56 -05006106 static_call(kvm_x86_set_segment)(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006107}
6108
6109void kvm_get_segment(struct kvm_vcpu *vcpu,
6110 struct kvm_segment *var, int seg)
6111{
Jason Baronb36464772021-01-14 22:27:56 -05006112 static_call(kvm_x86_get_segment)(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006113}
6114
Paolo Bonzini54987b72014-09-02 13:23:06 +02006115gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
6116 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02006117{
6118 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02006119
6120 BUG_ON(!mmu_is_nested(vcpu));
6121
6122 /* NPT walks are always user-walks */
6123 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006124 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02006125
6126 return t_gpa;
6127}
6128
Avi Kivityab9ae312010-11-22 17:53:26 +02006129gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
6130 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006131{
Jason Baronb36464772021-01-14 22:27:56 -05006132 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02006133 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006134}
Sean Christopherson54f958c2021-04-12 16:21:33 +12006135EXPORT_SYMBOL_GPL(kvm_mmu_gva_to_gpa_read);
Gleb Natapov1871c602010-02-10 14:21:32 +02006136
Avi Kivityab9ae312010-11-22 17:53:26 +02006137 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
6138 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006139{
Jason Baronb36464772021-01-14 22:27:56 -05006140 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02006141 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02006142 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006143}
6144
Avi Kivityab9ae312010-11-22 17:53:26 +02006145gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
6146 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006147{
Jason Baronb36464772021-01-14 22:27:56 -05006148 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02006149 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02006150 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006151}
Sean Christopherson54f958c2021-04-12 16:21:33 +12006152EXPORT_SYMBOL_GPL(kvm_mmu_gva_to_gpa_write);
Gleb Natapov1871c602010-02-10 14:21:32 +02006153
6154/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02006155gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
6156 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006157{
Avi Kivityab9ae312010-11-22 17:53:26 +02006158 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006159}
6160
6161static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
6162 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006163 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006164{
6165 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006166 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006167
6168 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02006169 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02006170 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006171 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02006172 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006173 int ret;
6174
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006175 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02006176 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006177 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
6178 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006179 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006180 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006181 goto out;
6182 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01006183
Izik Eidus77c20022008-12-29 01:42:19 +02006184 bytes -= toread;
6185 data += toread;
6186 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006187 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006188out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006189 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006190}
Izik Eidus77c20022008-12-29 01:42:19 +02006191
Gleb Natapov1871c602010-02-10 14:21:32 +02006192/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03006193static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
6194 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006195 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006196{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006197 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Jason Baronb36464772021-01-14 22:27:56 -05006198 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02006199 unsigned offset;
6200 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006201
Paolo Bonzini44583cb2014-05-13 14:02:13 +02006202 /* Inline kvm_read_guest_virt_helper for speed. */
6203 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
6204 exception);
6205 if (unlikely(gpa == UNMAPPED_GVA))
6206 return X86EMUL_PROPAGATE_FAULT;
6207
6208 offset = addr & (PAGE_SIZE-1);
6209 if (WARN_ON(offset + bytes > PAGE_SIZE))
6210 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006211 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
6212 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02006213 if (unlikely(ret < 0))
6214 return X86EMUL_IO_NEEDED;
6215
6216 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02006217}
6218
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006219int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03006220 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006221 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006222{
Jason Baronb36464772021-01-14 22:27:56 -05006223 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006224
Paolo Bonzini353c0952019-01-29 18:41:16 +01006225 /*
6226 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
6227 * is returned, but our callers are not ready for that and they blindly
6228 * call kvm_inject_page_fault. Ensure that they at least do not leak
6229 * uninitialized kernel stack memory into cr2 and error code.
6230 */
6231 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02006232 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006233 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006234}
Nadav Har'El064aea72011-05-25 23:04:56 +03006235EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02006236
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006237static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
6238 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006239 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02006240{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006241 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006242 u32 access = 0;
6243
Jason Baronb36464772021-01-14 22:27:56 -05006244 if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006245 access |= PFERR_USER_MASK;
6246
6247 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006248}
6249
Radim Krčmář7a036a62015-10-30 16:36:24 +01006250static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
6251 unsigned long addr, void *val, unsigned int bytes)
6252{
6253 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6254 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
6255
6256 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
6257}
6258
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006259static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
6260 struct kvm_vcpu *vcpu, u32 access,
6261 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02006262{
6263 void *data = val;
6264 int r = X86EMUL_CONTINUE;
6265
6266 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02006267 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006268 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02006269 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02006270 unsigned offset = addr & (PAGE_SIZE-1);
6271 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
6272 int ret;
6273
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006274 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02006275 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006276 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02006277 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006278 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02006279 goto out;
6280 }
6281
6282 bytes -= towrite;
6283 data += towrite;
6284 addr += towrite;
6285 }
6286out:
6287 return r;
6288}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006289
6290static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006291 unsigned int bytes, struct x86_exception *exception,
6292 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006293{
6294 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006295 u32 access = PFERR_WRITE_MASK;
6296
Jason Baronb36464772021-01-14 22:27:56 -05006297 if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006298 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006299
6300 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006301 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006302}
6303
6304int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
6305 unsigned int bytes, struct x86_exception *exception)
6306{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006307 /* kvm_write_guest_virt_system can pull in tons of pages. */
6308 vcpu->arch.l1tf_flush_l1d = true;
6309
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006310 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
6311 PFERR_WRITE_MASK, exception);
6312}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006313EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02006314
Wanpeng Li082d06e2018-04-03 16:28:48 -07006315int handle_ud(struct kvm_vcpu *vcpu)
6316{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09006317 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006318 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006319 char sig[5]; /* ud2; .ascii "kvm" */
6320 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07006321
Jason Baronb36464772021-01-14 22:27:56 -05006322 if (unlikely(!static_call(kvm_x86_can_emulate_instruction)(vcpu, NULL, 0)))
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07006323 return 1;
6324
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006325 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006326 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
6327 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09006328 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006329 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07006330 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006331 }
6332
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006333 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07006334}
6335EXPORT_SYMBOL_GPL(handle_ud);
6336
Tom Lendacky0f89b202016-12-14 14:59:23 -05006337static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
6338 gpa_t gpa, bool write)
6339{
6340 /* For APIC access vmexit */
6341 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
6342 return 1;
6343
6344 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
6345 trace_vcpu_match_mmio(gva, gpa, write, true);
6346 return 1;
6347 }
6348
6349 return 0;
6350}
6351
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006352static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
6353 gpa_t *gpa, struct x86_exception *exception,
6354 bool write)
6355{
Jason Baronb36464772021-01-14 22:27:56 -05006356 u32 access = ((static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0)
Avi Kivity97d64b72012-09-12 14:52:00 +03006357 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006358
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08006359 /*
6360 * currently PKRU is only applied to ept enabled guest so
6361 * there is no pkey in EPT page table for L1 guest or EPT
6362 * shadow page table for L2 guest.
6363 */
Avi Kivity97d64b72012-09-12 14:52:00 +03006364 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08006365 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07006366 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08006367 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
6368 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08006369 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08006370 return 1;
6371 }
6372
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006373 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
6374
6375 if (*gpa == UNMAPPED_GVA)
6376 return -1;
6377
Tom Lendacky0f89b202016-12-14 14:59:23 -05006378 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006379}
6380
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006381int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006382 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02006383{
6384 int ret;
6385
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006386 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02006387 if (ret < 0)
6388 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08006389 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02006390 return 1;
6391}
6392
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006393struct read_write_emulator_ops {
6394 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
6395 int bytes);
6396 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
6397 void *val, int bytes);
6398 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
6399 int bytes, void *val);
6400 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
6401 void *val, int bytes);
6402 bool write;
6403};
6404
6405static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
6406{
6407 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006408 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006409 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006410 vcpu->mmio_read_completed = 0;
6411 return 1;
6412 }
6413
6414 return 0;
6415}
6416
6417static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
6418 void *val, int bytes)
6419{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006420 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006421}
6422
6423static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
6424 void *val, int bytes)
6425{
6426 return emulator_write_phys(vcpu, gpa, val, bytes);
6427}
6428
6429static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
6430{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006431 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006432 return vcpu_mmio_write(vcpu, gpa, bytes, val);
6433}
6434
6435static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
6436 void *val, int bytes)
6437{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006438 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006439 return X86EMUL_IO_NEEDED;
6440}
6441
6442static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
6443 void *val, int bytes)
6444{
Avi Kivityf78146b2012-04-18 19:22:47 +03006445 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
6446
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006447 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006448 return X86EMUL_CONTINUE;
6449}
6450
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006451static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006452 .read_write_prepare = read_prepare,
6453 .read_write_emulate = read_emulate,
6454 .read_write_mmio = vcpu_mmio_read,
6455 .read_write_exit_mmio = read_exit_mmio,
6456};
6457
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006458static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006459 .read_write_emulate = write_emulate,
6460 .read_write_mmio = write_mmio,
6461 .read_write_exit_mmio = write_exit_mmio,
6462 .write = true,
6463};
6464
Xiao Guangrong22388a32011-07-13 14:32:31 +08006465static int emulator_read_write_onepage(unsigned long addr, void *val,
6466 unsigned int bytes,
6467 struct x86_exception *exception,
6468 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006469 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006470{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006471 gpa_t gpa;
6472 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08006473 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03006474 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006475 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05006476
6477 /*
6478 * If the exit was due to a NPF we may already have a GPA.
6479 * If the GPA is present, use it to avoid the GVA to GPA table walk.
6480 * Note, this cannot be used on string operations since string
6481 * operation using rep will only have the initial GPA from the NPF
6482 * occurred.
6483 */
Sean Christopherson744e6992020-02-18 15:03:09 -08006484 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
6485 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
6486 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02006487 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
6488 } else {
6489 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
6490 if (ret < 0)
6491 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05006492 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08006493
Brijesh Singh618232e2017-08-17 18:36:57 +02006494 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01006495 return X86EMUL_CONTINUE;
6496
Carsten Ottebbd9b642007-10-30 18:44:21 +01006497 /*
6498 * Is this MMIO handled locally?
6499 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08006500 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02006501 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006502 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006503
Avi Kivity70252a12010-01-19 12:51:22 +02006504 gpa += handled;
6505 bytes -= handled;
6506 val += handled;
6507
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006508 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
6509 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
6510 frag->gpa = gpa;
6511 frag->data = val;
6512 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03006513 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006514}
6515
Xiubo Li52eb5a62015-03-13 17:39:45 +08006516static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
6517 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08006518 void *val, unsigned int bytes,
6519 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006520 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006521{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006522 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03006523 gpa_t gpa;
6524 int rc;
6525
6526 if (ops->read_write_prepare &&
6527 ops->read_write_prepare(vcpu, val, bytes))
6528 return X86EMUL_CONTINUE;
6529
6530 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006531
Carsten Ottebbd9b642007-10-30 18:44:21 +01006532 /* Crossing a page boundary? */
6533 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03006534 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006535
6536 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08006537 rc = emulator_read_write_onepage(addr, val, now, exception,
6538 vcpu, ops);
6539
Carsten Ottebbd9b642007-10-30 18:44:21 +01006540 if (rc != X86EMUL_CONTINUE)
6541 return rc;
6542 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02006543 if (ctxt->mode != X86EMUL_MODE_PROT64)
6544 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006545 val += now;
6546 bytes -= now;
6547 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08006548
Avi Kivityf78146b2012-04-18 19:22:47 +03006549 rc = emulator_read_write_onepage(addr, val, bytes, exception,
6550 vcpu, ops);
6551 if (rc != X86EMUL_CONTINUE)
6552 return rc;
6553
6554 if (!vcpu->mmio_nr_fragments)
6555 return rc;
6556
6557 gpa = vcpu->mmio_fragments[0].gpa;
6558
6559 vcpu->mmio_needed = 1;
6560 vcpu->mmio_cur_fragment = 0;
6561
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006562 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03006563 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
6564 vcpu->run->exit_reason = KVM_EXIT_MMIO;
6565 vcpu->run->mmio.phys_addr = gpa;
6566
6567 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08006568}
6569
6570static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
6571 unsigned long addr,
6572 void *val,
6573 unsigned int bytes,
6574 struct x86_exception *exception)
6575{
6576 return emulator_read_write(ctxt, addr, val, bytes,
6577 exception, &read_emultor);
6578}
6579
Xiubo Li52eb5a62015-03-13 17:39:45 +08006580static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08006581 unsigned long addr,
6582 const void *val,
6583 unsigned int bytes,
6584 struct x86_exception *exception)
6585{
6586 return emulator_read_write(ctxt, addr, (void *)val, bytes,
6587 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006588}
Carsten Ottebbd9b642007-10-30 18:44:21 +01006589
Avi Kivitydaea3e72010-03-15 13:59:54 +02006590#define CMPXCHG_TYPE(t, ptr, old, new) \
6591 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
6592
6593#ifdef CONFIG_X86_64
6594# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
6595#else
6596# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01006597 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02006598#endif
6599
Avi Kivity0f65dd72011-04-20 13:37:53 +03006600static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
6601 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006602 const void *old,
6603 const void *new,
6604 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03006605 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006606{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006607 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006608 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02006609 u64 page_line_mask;
Avi Kivitydaea3e72010-03-15 13:59:54 +02006610 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02006611 char *kaddr;
6612 bool exchanged;
6613
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006614 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02006615 if (bytes > 8 || (bytes & (bytes - 1)))
6616 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006617
Avi Kivitydaea3e72010-03-15 13:59:54 +02006618 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006619
Avi Kivitydaea3e72010-03-15 13:59:54 +02006620 if (gpa == UNMAPPED_GVA ||
6621 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
6622 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006623
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02006624 /*
6625 * Emulate the atomic as a straight write to avoid #AC if SLD is
6626 * enabled in the host and the access splits a cache line.
6627 */
6628 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
6629 page_line_mask = ~(cache_line_size() - 1);
6630 else
6631 page_line_mask = PAGE_MASK;
6632
6633 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask))
Avi Kivitydaea3e72010-03-15 13:59:54 +02006634 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006635
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006636 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08006637 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02006638
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006639 kaddr = map.hva + offset_in_page(gpa);
6640
Avi Kivitydaea3e72010-03-15 13:59:54 +02006641 switch (bytes) {
6642 case 1:
6643 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
6644 break;
6645 case 2:
6646 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
6647 break;
6648 case 4:
6649 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
6650 break;
6651 case 8:
6652 exchanged = CMPXCHG64(kaddr, old, new);
6653 break;
6654 default:
6655 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006656 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006657
6658 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02006659
6660 if (!exchanged)
6661 return X86EMUL_CMPXCHG_FAILED;
6662
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08006663 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03006664
6665 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02006666
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006667emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02006668 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006669
Avi Kivity0f65dd72011-04-20 13:37:53 +03006670 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006671}
6672
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006673static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
6674{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006675 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006676
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006677 for (i = 0; i < vcpu->arch.pio.count; i++) {
6678 if (vcpu->arch.pio.in)
6679 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
6680 vcpu->arch.pio.size, pd);
6681 else
6682 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
6683 vcpu->arch.pio.port, vcpu->arch.pio.size,
6684 pd);
6685 if (r)
6686 break;
6687 pd += vcpu->arch.pio.size;
6688 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006689 return r;
6690}
6691
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006692static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
6693 unsigned short port, void *val,
6694 unsigned int count, bool in)
6695{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006696 vcpu->arch.pio.port = port;
6697 vcpu->arch.pio.in = in;
6698 vcpu->arch.pio.count = count;
6699 vcpu->arch.pio.size = size;
6700
6701 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
6702 vcpu->arch.pio.count = 0;
6703 return 1;
6704 }
6705
6706 vcpu->run->exit_reason = KVM_EXIT_IO;
6707 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
6708 vcpu->run->io.size = size;
6709 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
6710 vcpu->run->io.count = count;
6711 vcpu->run->io.port = port;
6712
6713 return 0;
6714}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006715
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006716static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6717 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006718{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006719 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03006720
Gleb Natapov79729952010-03-18 15:20:24 +02006721 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006722 goto data_avail;
6723
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006724 memset(vcpu->arch.pio_data, 0, size * count);
6725
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006726 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
6727 if (ret) {
6728data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006729 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006730 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02006731 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006732 return 1;
6733 }
6734
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006735 return 0;
6736}
6737
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006738static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
6739 int size, unsigned short port, void *val,
6740 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006741{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006742 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006743
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006744}
6745
6746static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
6747 unsigned short port, const void *val,
6748 unsigned int count)
6749{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006750 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006751 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006752 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006753}
6754
Carsten Ottebbd9b642007-10-30 18:44:21 +01006755static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
6756 int size, unsigned short port,
6757 const void *val, unsigned int count)
6758{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006759 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006760}
6761
6762static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
6763{
Jason Baronb36464772021-01-14 22:27:56 -05006764 return static_call(kvm_x86_get_segment_base)(vcpu, seg);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006765}
6766
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006767static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006768{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006769 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006770}
6771
Jiang Biaoae6a2372016-11-07 08:54:51 +08006772static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006773{
6774 if (!need_emulate_wbinvd(vcpu))
6775 return X86EMUL_CONTINUE;
6776
Jason Baronb36464772021-01-14 22:27:56 -05006777 if (static_call(kvm_x86_has_wbinvd_exit)()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01006778 int cpu = get_cpu();
6779
6780 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Wanpeng Lic2162e12021-03-12 10:45:51 +08006781 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006782 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006783 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006784 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006785 } else
6786 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006787 return X86EMUL_CONTINUE;
6788}
Joel Schopp5cb56052015-03-02 13:43:31 -06006789
6790int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6791{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006792 kvm_emulate_wbinvd_noskip(vcpu);
6793 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006794}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006795EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6796
Joel Schopp5cb56052015-03-02 13:43:31 -06006797
6798
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006799static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6800{
Joel Schopp5cb56052015-03-02 13:43:31 -06006801 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006802}
6803
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05006804static void emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6805 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006806{
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05006807 kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006808}
6809
Xiubo Li52eb5a62015-03-13 17:39:45 +08006810static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6811 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006812{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006813
Paolo Bonzini996ff542020-12-14 07:49:54 -05006814 return kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006815}
6816
Gleb Natapov52a46612010-03-18 15:20:03 +02006817static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6818{
6819 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6820}
6821
Avi Kivity717746e2011-04-20 13:37:53 +03006822static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006823{
Avi Kivity717746e2011-04-20 13:37:53 +03006824 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006825 unsigned long value;
6826
6827 switch (cr) {
6828 case 0:
6829 value = kvm_read_cr0(vcpu);
6830 break;
6831 case 2:
6832 value = vcpu->arch.cr2;
6833 break;
6834 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006835 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006836 break;
6837 case 4:
6838 value = kvm_read_cr4(vcpu);
6839 break;
6840 case 8:
6841 value = kvm_get_cr8(vcpu);
6842 break;
6843 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006844 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006845 return 0;
6846 }
6847
6848 return value;
6849}
6850
Avi Kivity717746e2011-04-20 13:37:53 +03006851static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006852{
Avi Kivity717746e2011-04-20 13:37:53 +03006853 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006854 int res = 0;
6855
Gleb Natapov52a46612010-03-18 15:20:03 +02006856 switch (cr) {
6857 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006858 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006859 break;
6860 case 2:
6861 vcpu->arch.cr2 = val;
6862 break;
6863 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006864 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006865 break;
6866 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006867 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006868 break;
6869 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006870 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006871 break;
6872 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006873 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006874 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006875 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006876
6877 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006878}
6879
Avi Kivity717746e2011-04-20 13:37:53 +03006880static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006881{
Jason Baronb36464772021-01-14 22:27:56 -05006882 return static_call(kvm_x86_get_cpl)(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006883}
6884
Avi Kivity4bff1e862011-04-20 13:37:53 +03006885static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006886{
Jason Baronb36464772021-01-14 22:27:56 -05006887 static_call(kvm_x86_get_gdt)(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006888}
6889
Avi Kivity4bff1e862011-04-20 13:37:53 +03006890static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006891{
Jason Baronb36464772021-01-14 22:27:56 -05006892 static_call(kvm_x86_get_idt)(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006893}
6894
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006895static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6896{
Jason Baronb36464772021-01-14 22:27:56 -05006897 static_call(kvm_x86_set_gdt)(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006898}
6899
6900static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6901{
Jason Baronb36464772021-01-14 22:27:56 -05006902 static_call(kvm_x86_set_idt)(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006903}
6904
Avi Kivity4bff1e862011-04-20 13:37:53 +03006905static unsigned long emulator_get_cached_segment_base(
6906 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006907{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006908 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006909}
6910
Avi Kivity1aa36612011-04-27 13:20:30 +03006911static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6912 struct desc_struct *desc, u32 *base3,
6913 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006914{
6915 struct kvm_segment var;
6916
Avi Kivity4bff1e862011-04-20 13:37:53 +03006917 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006918 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006919
Gleb Natapov378a8b02013-01-21 15:36:48 +02006920 if (var.unusable) {
6921 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006922 if (base3)
6923 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006924 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006925 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006926
6927 if (var.g)
6928 var.limit >>= 12;
6929 set_desc_limit(desc, var.limit);
6930 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006931#ifdef CONFIG_X86_64
6932 if (base3)
6933 *base3 = var.base >> 32;
6934#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006935 desc->type = var.type;
6936 desc->s = var.s;
6937 desc->dpl = var.dpl;
6938 desc->p = var.present;
6939 desc->avl = var.avl;
6940 desc->l = var.l;
6941 desc->d = var.db;
6942 desc->g = var.g;
6943
6944 return true;
6945}
6946
Avi Kivity1aa36612011-04-27 13:20:30 +03006947static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6948 struct desc_struct *desc, u32 base3,
6949 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006950{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006951 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006952 struct kvm_segment var;
6953
Avi Kivity1aa36612011-04-27 13:20:30 +03006954 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006955 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006956#ifdef CONFIG_X86_64
6957 var.base |= ((u64)base3) << 32;
6958#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006959 var.limit = get_desc_limit(desc);
6960 if (desc->g)
6961 var.limit = (var.limit << 12) | 0xfff;
6962 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006963 var.dpl = desc->dpl;
6964 var.db = desc->d;
6965 var.s = desc->s;
6966 var.l = desc->l;
6967 var.g = desc->g;
6968 var.avl = desc->avl;
6969 var.present = desc->p;
6970 var.unusable = !var.present;
6971 var.padding = 0;
6972
6973 kvm_set_segment(vcpu, &var, seg);
6974 return;
6975}
6976
Avi Kivity717746e2011-04-20 13:37:53 +03006977static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6978 u32 msr_index, u64 *pdata)
6979{
Alexander Graf1ae09952020-09-25 16:34:16 +02006980 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6981 int r;
6982
6983 r = kvm_get_msr(vcpu, msr_index, pdata);
6984
6985 if (r && kvm_get_msr_user_space(vcpu, msr_index, r)) {
6986 /* Bounce to user space */
6987 return X86EMUL_IO_NEEDED;
6988 }
6989
6990 return r;
Avi Kivity717746e2011-04-20 13:37:53 +03006991}
6992
6993static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6994 u32 msr_index, u64 data)
6995{
Alexander Graf1ae09952020-09-25 16:34:16 +02006996 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6997 int r;
6998
6999 r = kvm_set_msr(vcpu, msr_index, data);
7000
7001 if (r && kvm_set_msr_user_space(vcpu, msr_index, data, r)) {
7002 /* Bounce to user space */
7003 return X86EMUL_IO_NEEDED;
7004 }
7005
7006 return r;
Avi Kivity717746e2011-04-20 13:37:53 +03007007}
7008
Paolo Bonzini64d60672015-05-07 11:36:11 +02007009static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
7010{
7011 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7012
7013 return vcpu->arch.smbase;
7014}
7015
7016static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
7017{
7018 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7019
7020 vcpu->arch.smbase = smbase;
7021}
7022
Nadav Amit67f4d422014-06-02 18:34:09 +03007023static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
7024 u32 pmc)
7025{
Like Xu98ff80f2019-10-27 18:52:40 +08007026 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03007027}
7028
Avi Kivity222d21a2011-11-10 14:57:30 +02007029static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
7030 u32 pmc, u64 *pdata)
7031{
Wei Huangc6702c92015-06-19 13:44:45 +02007032 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02007033}
7034
Avi Kivity6c3287f2011-04-20 15:43:05 +03007035static void emulator_halt(struct x86_emulate_ctxt *ctxt)
7036{
7037 emul_to_vcpu(ctxt)->arch.halt_request = 1;
7038}
7039
Avi Kivity29535382011-04-20 13:37:53 +03007040static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007041 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02007042 enum x86_intercept_stage stage)
7043{
Jason Baronb36464772021-01-14 22:27:56 -05007044 return static_call(kvm_x86_check_intercept)(emul_to_vcpu(ctxt), info, stage,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007045 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02007046}
7047
Yu Zhange911eb32017-08-24 20:27:52 +08007048static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08007049 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
7050 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01007051{
Sean Christophersonf91af512020-03-04 17:34:37 -08007052 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01007053}
7054
Sean Christopherson5ae78e92019-12-17 13:32:38 -08007055static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
7056{
7057 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
7058}
7059
7060static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
7061{
7062 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
7063}
7064
7065static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
7066{
7067 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
7068}
7069
Avi Kivitydd856ef2012-08-27 23:46:17 +03007070static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
7071{
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07007072 return kvm_register_read_raw(emul_to_vcpu(ctxt), reg);
Avi Kivitydd856ef2012-08-27 23:46:17 +03007073}
7074
7075static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
7076{
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07007077 kvm_register_write_raw(emul_to_vcpu(ctxt), reg, val);
Avi Kivitydd856ef2012-08-27 23:46:17 +03007078}
7079
Nadav Amit801806d2015-01-26 09:32:23 +02007080static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
7081{
Jason Baronb36464772021-01-14 22:27:56 -05007082 static_call(kvm_x86_set_nmi_mask)(emul_to_vcpu(ctxt), masked);
Nadav Amit801806d2015-01-26 09:32:23 +02007083}
7084
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007085static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
7086{
7087 return emul_to_vcpu(ctxt)->arch.hflags;
7088}
7089
7090static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
7091{
Sean Christophersonc5833c72019-04-02 08:03:10 -07007092 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007093}
7094
Sean Christophersoned193212019-04-02 08:03:09 -07007095static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
7096 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007097{
Jason Baronb36464772021-01-14 22:27:56 -05007098 return static_call(kvm_x86_pre_leave_smm)(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007099}
7100
Sean Christophersonc5833c72019-04-02 08:03:10 -07007101static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
7102{
7103 kvm_smm_changed(emul_to_vcpu(ctxt));
7104}
7105
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02007106static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
7107{
7108 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
7109}
7110
Mathias Krause0225fb52012-08-30 01:30:16 +02007111static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03007112 .read_gpr = emulator_read_gpr,
7113 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007114 .read_std = emulator_read_std,
7115 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01007116 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02007117 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01007118 .read_emulated = emulator_read_emulated,
7119 .write_emulated = emulator_write_emulated,
7120 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03007121 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007122 .pio_in_emulated = emulator_pio_in_emulated,
7123 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03007124 .get_segment = emulator_get_segment,
7125 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03007126 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007127 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03007128 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03007129 .set_gdt = emulator_set_gdt,
7130 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02007131 .get_cr = emulator_get_cr,
7132 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02007133 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03007134 .get_dr = emulator_get_dr,
7135 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02007136 .get_smbase = emulator_get_smbase,
7137 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03007138 .set_msr = emulator_set_msr,
7139 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03007140 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02007141 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03007142 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03007143 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03007144 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02007145 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01007146 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08007147 .guest_has_long_mode = emulator_guest_has_long_mode,
7148 .guest_has_movbe = emulator_guest_has_movbe,
7149 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02007150 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007151 .get_hflags = emulator_get_hflags,
7152 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007153 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07007154 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02007155 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01007156};
7157
Gleb Natapov95cb2292010-04-28 19:15:43 +03007158static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
7159{
Jason Baronb36464772021-01-14 22:27:56 -05007160 u32 int_shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03007161 /*
7162 * an sti; sti; sequence only disable interrupts for the first
7163 * instruction. So, if the last instruction, be it emulated or
7164 * not, left the system with the INT_STI flag enabled, it
7165 * means that the last instruction is an sti. We should not
7166 * leave the flag on in this case. The same goes for mov ss
7167 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02007168 if (int_shadow & mask)
7169 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007170 if (unlikely(int_shadow || mask)) {
Jason Baronb36464772021-01-14 22:27:56 -05007171 static_call(kvm_x86_set_interrupt_shadow)(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007172 if (!mask)
7173 kvm_make_request(KVM_REQ_EVENT, vcpu);
7174 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03007175}
7176
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007177static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03007178{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007179 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02007180 if (ctxt->exception.vector == PF_VECTOR)
Sean Christopherson53b3d8e2020-03-20 14:28:01 -07007181 return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007182
7183 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02007184 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
7185 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03007186 else
Avi Kivityda9cb572010-11-22 17:53:21 +02007187 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007188 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03007189}
7190
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007191static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
7192{
7193 struct x86_emulate_ctxt *ctxt;
7194
7195 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
7196 if (!ctxt) {
7197 pr_err("kvm: failed to allocate vcpu's emulator\n");
7198 return NULL;
7199 }
7200
7201 ctxt->vcpu = vcpu;
7202 ctxt->ops = &emulate_ops;
7203 vcpu->arch.emulate_ctxt = ctxt;
7204
7205 return ctxt;
7206}
7207
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007208static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
7209{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007210 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007211 int cs_db, cs_l;
7212
Jason Baronb36464772021-01-14 22:27:56 -05007213 static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007214
Sean Christopherson744e6992020-02-18 15:03:09 -08007215 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007216 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007217 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
7218
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007219 ctxt->eip = kvm_rip_read(vcpu);
7220 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
7221 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03007222 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007223 cs_db ? X86EMUL_MODE_PROT32 :
7224 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02007225 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007226 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
7227 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007228
Avi Kivitydd856ef2012-08-27 23:46:17 +03007229 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007230 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007231}
7232
Sean Christopherson9497e1f2019-08-27 14:40:36 -07007233void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02007234{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007235 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02007236 int ret;
7237
7238 init_emulate_ctxt(vcpu);
7239
Avi Kivity9dac77f2011-06-01 15:34:25 +03007240 ctxt->op_bytes = 2;
7241 ctxt->ad_bytes = 2;
7242 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007243 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02007244
Sean Christopherson9497e1f2019-08-27 14:40:36 -07007245 if (ret != X86EMUL_CONTINUE) {
7246 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7247 } else {
7248 ctxt->eip = ctxt->_eip;
7249 kvm_rip_write(vcpu, ctxt->eip);
7250 kvm_set_rflags(vcpu, ctxt->eflags);
7251 }
Mohammed Gamal63995652010-09-19 14:34:06 +02007252}
7253EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
7254
Liran Alone2366172018-03-12 13:12:49 +02007255static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007256{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007257 ++vcpu->stat.insn_emulation_fail;
7258 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02007259
Sean Christopherson42cbf062019-08-27 14:40:31 -07007260 if (emulation_type & EMULTYPE_VMWARE_GP) {
7261 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007262 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07007263 }
Liran Alone2366172018-03-12 13:12:49 +02007264
Sean Christopherson738fece2019-08-27 14:40:34 -07007265 if (emulation_type & EMULTYPE_SKIP) {
7266 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7267 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7268 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007269 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07007270 }
7271
Sean Christopherson22da61c2019-08-27 14:40:28 -07007272 kvm_queue_exception(vcpu, UD_VECTOR);
7273
Jason Baronb36464772021-01-14 22:27:56 -05007274 if (!is_guest_mode(vcpu) && static_call(kvm_x86_get_cpl)(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01007275 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7276 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7277 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007278 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01007279 }
Liran Alone2366172018-03-12 13:12:49 +02007280
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007281 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007282}
7283
Sean Christopherson736c2912019-12-06 15:57:14 -08007284static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03007285 bool write_fault_to_shadow_pgtable,
7286 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03007287{
Sean Christopherson736c2912019-12-06 15:57:14 -08007288 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08007289 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03007290
Sean Christopherson92daa482020-02-18 15:03:08 -08007291 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03007292 return false;
7293
Sean Christopherson92daa482020-02-18 15:03:08 -08007294 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
7295 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07007296 return false;
7297
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007298 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007299 /*
7300 * Write permission should be allowed since only
7301 * write access need to be emulated.
7302 */
Sean Christopherson736c2912019-12-06 15:57:14 -08007303 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03007304
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007305 /*
7306 * If the mapping is invalid in guest, let cpu retry
7307 * it to generate fault.
7308 */
7309 if (gpa == UNMAPPED_GVA)
7310 return true;
7311 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03007312
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08007313 /*
7314 * Do not retry the unhandleable instruction if it faults on the
7315 * readonly host memory, otherwise it will goto a infinite loop:
7316 * retry instruction -> write #PF -> emulation fail -> retry
7317 * instruction -> ...
7318 */
7319 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007320
7321 /*
7322 * If the instruction failed on the error pfn, it can not be fixed,
7323 * report the error to userspace.
7324 */
7325 if (is_error_noslot_pfn(pfn))
7326 return false;
7327
7328 kvm_release_pfn_clean(pfn);
7329
7330 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007331 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007332 unsigned int indirect_shadow_pages;
7333
Ben Gardon531810c2021-02-02 10:57:24 -08007334 write_lock(&vcpu->kvm->mmu_lock);
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007335 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
Ben Gardon531810c2021-02-02 10:57:24 -08007336 write_unlock(&vcpu->kvm->mmu_lock);
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007337
7338 if (indirect_shadow_pages)
7339 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
7340
Gleb Natapova6f177e2010-07-08 12:41:12 +03007341 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08007342 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03007343
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007344 /*
7345 * if emulation was due to access to shadowed page table
7346 * and it failed try to unshadow page and re-enter the
7347 * guest to let CPU execute the instruction.
7348 */
7349 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007350
7351 /*
7352 * If the access faults on its page table, it can not
7353 * be fixed by unprotecting shadow page and it should
7354 * be reported to userspace.
7355 */
7356 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03007357}
7358
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007359static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08007360 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007361{
7362 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08007363 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007364
7365 last_retry_eip = vcpu->arch.last_retry_eip;
7366 last_retry_addr = vcpu->arch.last_retry_addr;
7367
7368 /*
7369 * If the emulation is caused by #PF and it is non-page_table
7370 * writing instruction, it means the VM-EXIT is caused by shadow
7371 * page protected, we can zap the shadow page and retry this
7372 * instruction directly.
7373 *
7374 * Note: if the guest uses a non-page-table modifying instruction
7375 * on the PDE that points to the instruction, then we will unmap
7376 * the instruction and go to an infinite loop. So, we cache the
7377 * last retried eip and the last fault address, if we meet the eip
7378 * and the address again, we can break out of the potential infinite
7379 * loop.
7380 */
7381 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
7382
Sean Christopherson92daa482020-02-18 15:03:08 -08007383 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007384 return false;
7385
Sean Christopherson92daa482020-02-18 15:03:08 -08007386 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
7387 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07007388 return false;
7389
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007390 if (x86_page_table_writing_insn(ctxt))
7391 return false;
7392
Sean Christopherson736c2912019-12-06 15:57:14 -08007393 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007394 return false;
7395
7396 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08007397 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007398
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007399 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08007400 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007401
Xiao Guangrong22368022013-01-13 23:44:12 +08007402 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007403
7404 return true;
7405}
7406
Gleb Natapov716d51a2012-09-03 15:24:26 +03007407static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
7408static int complete_emulated_pio(struct kvm_vcpu *vcpu);
7409
Paolo Bonzini64d60672015-05-07 11:36:11 +02007410static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02007411{
Paolo Bonzini64d60672015-05-07 11:36:11 +02007412 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007413 /* This is a good place to trace that we are exiting SMM. */
7414 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
7415
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007416 /* Process a latched INIT or SMI, if any. */
7417 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007418 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02007419
7420 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007421}
7422
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007423static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
7424 unsigned long *db)
7425{
7426 u32 dr6 = 0;
7427 int i;
7428 u32 enable, rwlen;
7429
7430 enable = dr7;
7431 rwlen = dr7 >> 16;
7432 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
7433 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
7434 dr6 |= (1 << i);
7435 return dr6;
7436}
7437
Sean Christopherson120c2c42019-08-27 14:40:29 -07007438static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007439{
7440 struct kvm_run *kvm_run = vcpu->run;
7441
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007442 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08007443 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW;
Peter Xud5d260c2020-05-05 16:49:59 -04007444 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007445 kvm_run->debug.arch.exception = DB_VECTOR;
7446 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007447 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007448 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07007449 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007450 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007451}
7452
Kyle Huey6affcbe2016-11-29 12:40:40 -08007453int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
7454{
Jason Baronb36464772021-01-14 22:27:56 -05007455 unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02007456 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007457
Jason Baronb36464772021-01-14 22:27:56 -05007458 r = static_call(kvm_x86_skip_emulated_instruction)(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007459 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02007460 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007461
7462 /*
7463 * rflags is the old, "raw" value of the flags. The new value has
7464 * not been saved yet.
7465 *
7466 * This is correct even for TF set by the guest, because "the
7467 * processor will not generate this exception after the instruction
7468 * that sets the TF flag".
7469 */
7470 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07007471 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007472 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007473}
7474EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
7475
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007476static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
7477{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007478 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
7479 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02007480 struct kvm_run *kvm_run = vcpu->run;
7481 unsigned long eip = kvm_get_linear_rip(vcpu);
7482 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007483 vcpu->arch.guest_debug_dr7,
7484 vcpu->arch.eff_db);
7485
7486 if (dr6 != 0) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08007487 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW;
Nadav Amit82b32772014-11-02 11:54:45 +02007488 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007489 kvm_run->debug.arch.exception = DB_VECTOR;
7490 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007491 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007492 return true;
7493 }
7494 }
7495
Nadav Amit4161a562014-07-17 01:19:31 +03007496 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
7497 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02007498 unsigned long eip = kvm_get_linear_rip(vcpu);
7499 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007500 vcpu->arch.dr7,
7501 vcpu->arch.db);
7502
7503 if (dr6 != 0) {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04007504 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007505 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007506 return true;
7507 }
7508 }
7509
7510 return false;
7511}
7512
Liran Alon04789b62018-03-12 13:12:50 +02007513static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
7514{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02007515 switch (ctxt->opcode_len) {
7516 case 1:
7517 switch (ctxt->b) {
7518 case 0xe4: /* IN */
7519 case 0xe5:
7520 case 0xec:
7521 case 0xed:
7522 case 0xe6: /* OUT */
7523 case 0xe7:
7524 case 0xee:
7525 case 0xef:
7526 case 0x6c: /* INS */
7527 case 0x6d:
7528 case 0x6e: /* OUTS */
7529 case 0x6f:
7530 return true;
7531 }
7532 break;
7533 case 2:
7534 switch (ctxt->b) {
7535 case 0x33: /* RDPMC */
7536 return true;
7537 }
7538 break;
Liran Alon04789b62018-03-12 13:12:50 +02007539 }
7540
7541 return false;
7542}
7543
Wei Huang4aa26912021-01-26 03:18:28 -05007544/*
7545 * Decode to be emulated instruction. Return EMULATION_OK if success.
7546 */
7547int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
7548 void *insn, int insn_len)
7549{
7550 int r = EMULATION_OK;
7551 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
7552
7553 init_emulate_ctxt(vcpu);
7554
7555 /*
7556 * We will reenter on the same instruction since we do not set
7557 * complete_userspace_io. This does not handle watchpoints yet,
7558 * those would be handled in the emulate_ops.
7559 */
7560 if (!(emulation_type & EMULTYPE_SKIP) &&
7561 kvm_vcpu_check_breakpoint(vcpu, &r))
7562 return r;
7563
7564 ctxt->interruptibility = 0;
7565 ctxt->have_exception = false;
7566 ctxt->exception.vector = -1;
7567 ctxt->perm_ok = false;
7568
7569 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
7570
7571 r = x86_decode_insn(ctxt, insn, insn_len);
7572
7573 trace_kvm_emulate_insn_start(vcpu);
7574 ++vcpu->stat.insn_emulation;
7575
7576 return r;
7577}
7578EXPORT_SYMBOL_GPL(x86_decode_emulated_instruction);
7579
Sean Christopherson736c2912019-12-06 15:57:14 -08007580int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
7581 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01007582{
Gleb Natapov95cb2292010-04-28 19:15:43 +03007583 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007584 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007585 bool writeback = true;
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007586 bool write_fault_to_spt;
7587
Jason Baronb36464772021-01-14 22:27:56 -05007588 if (unlikely(!static_call(kvm_x86_can_emulate_instruction)(vcpu, insn, insn_len)))
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007589 return 1;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007590
Paolo Bonzinic595cee2018-07-02 13:07:14 +02007591 vcpu->arch.l1tf_flush_l1d = true;
7592
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007593 /*
7594 * Clear write_fault_to_shadow_pgtable here to ensure it is
7595 * never reused.
7596 */
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007597 write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007598 vcpu->arch.write_fault_to_shadow_pgtable = false;
Gleb Natapov8d7d81022011-04-12 12:36:21 +03007599
Sheng Yang571008d2008-01-02 14:49:22 +08007600 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Wei Huang4aa26912021-01-26 03:18:28 -05007601 kvm_clear_exception_queue(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007602
Wei Huang4aa26912021-01-26 03:18:28 -05007603 r = x86_decode_emulated_instruction(vcpu, emulation_type,
7604 insn, insn_len);
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09007605 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07007606 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07007607 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
7608 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007609 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07007610 }
Sean Christopherson736c2912019-12-06 15:57:14 -08007611 if (reexecute_instruction(vcpu, cr2_or_gpa,
7612 write_fault_to_spt,
7613 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007614 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00007615 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00007616 /*
7617 * #UD should result in just EMULATION_FAILED, and trap-like
7618 * exception should not be encountered during decode.
7619 */
7620 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
7621 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00007622 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007623 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00007624 }
Liran Alone2366172018-03-12 13:12:49 +02007625 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007626 }
7627 }
7628
Sean Christopherson42cbf062019-08-27 14:40:31 -07007629 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
7630 !is_vmware_backdoor_opcode(ctxt)) {
7631 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007632 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07007633 }
Liran Alon04789b62018-03-12 13:12:50 +02007634
Sean Christopherson1957aa62019-08-27 14:40:39 -07007635 /*
7636 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
7637 * for kvm_skip_emulated_instruction(). The caller is responsible for
7638 * updating interruptibility state and injecting single-step #DBs.
7639 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03007640 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03007641 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03007642 if (ctxt->eflags & X86_EFLAGS_RF)
7643 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007644 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03007645 }
7646
Sean Christopherson736c2912019-12-06 15:57:14 -08007647 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007648 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007649
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007650 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03007651 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007652 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
7653 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03007654 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007655 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03007656
Gleb Natapov5cd21912010-03-18 15:20:26 +02007657restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08007658 if (emulation_type & EMULTYPE_PF) {
7659 /* Save the faulting GPA (cr2) in the address field */
7660 ctxt->exception.address = cr2_or_gpa;
7661
7662 /* With shadow page tables, cr2 contains a GVA or nGPA. */
7663 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08007664 ctxt->gpa_available = true;
7665 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08007666 }
7667 } else {
7668 /* Sanitize the address out of an abundance of paranoia. */
7669 ctxt->exception.address = 0;
7670 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05007671
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007672 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03007673
Joerg Roedel775fde82011-04-04 12:39:24 +02007674 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007675 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02007676
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007677 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08007678 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03007679 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007680 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03007681
Liran Alone2366172018-03-12 13:12:49 +02007682 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007683 }
7684
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007685 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007686 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007687 if (inject_emulated_exception(vcpu))
7688 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007689 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02007690 if (!vcpu->arch.pio.in) {
7691 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03007692 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02007693 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007694 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007695 vcpu->arch.complete_userspace_io = complete_emulated_pio;
7696 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007697 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007698 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07007699 ++vcpu->stat.mmio_exits;
7700
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007701 if (!vcpu->mmio_is_write)
7702 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007703 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007704 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007705 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03007706 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007707 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007708 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03007709
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007710 if (writeback) {
Jason Baronb36464772021-01-14 22:27:56 -05007711 unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007712 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007713 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02007714 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007715 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
7716 kvm_rip_write(vcpu, ctxt->eip);
Felipe Franciosi384dea12020-05-19 08:11:22 +00007717 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
Sean Christopherson120c2c42019-08-27 14:40:29 -07007718 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007719 if (kvm_x86_ops.update_emulated_instruction)
Jason Baronb36464772021-01-14 22:27:56 -05007720 static_call(kvm_x86_update_emulated_instruction)(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02007721 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007722 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007723
7724 /*
7725 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
7726 * do nothing, and it will be requested again as soon as
7727 * the shadow expires. But we still need to check here,
7728 * because POPF has no interrupt shadow.
7729 */
7730 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
7731 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007732 } else
7733 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03007734
Gleb Natapove85d28f2010-07-29 15:11:52 +03007735 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007736}
Sean Christophersonc60658d2018-08-23 13:56:53 -07007737
7738int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
7739{
7740 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
7741}
7742EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
7743
7744int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
7745 void *insn, int insn_len)
7746{
7747 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
7748}
7749EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007750
Sean Christopherson8764ed52019-04-29 07:04:15 -07007751static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
7752{
7753 vcpu->arch.pio.count = 0;
7754 return 1;
7755}
7756
Sean Christopherson45def772019-03-11 20:01:05 -07007757static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
7758{
7759 vcpu->arch.pio.count = 0;
7760
7761 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
7762 return 1;
7763
7764 return kvm_skip_emulated_instruction(vcpu);
7765}
7766
Sean Christophersondca7f122018-03-08 08:57:27 -08007767static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
7768 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01007769{
Sean Christophersonde3cd112019-04-30 10:36:17 -07007770 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007771 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
7772
Sean Christopherson8764ed52019-04-29 07:04:15 -07007773 if (ret)
7774 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07007775
Sean Christopherson8764ed52019-04-29 07:04:15 -07007776 /*
7777 * Workaround userspace that relies on old KVM behavior of %rip being
7778 * incremented prior to exiting to userspace to handle "OUT 0x7e".
7779 */
7780 if (port == 0x7e &&
7781 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
7782 vcpu->arch.complete_userspace_io =
7783 complete_fast_pio_out_port_0x7e;
7784 kvm_skip_emulated_instruction(vcpu);
7785 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07007786 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
7787 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
7788 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07007789 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01007790}
Carsten Ottede7d7892007-10-30 18:44:25 +01007791
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007792static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
7793{
7794 unsigned long val;
7795
7796 /* We should only ever be called with arch.pio.count equal to 1 */
7797 BUG_ON(vcpu->arch.pio.count != 1);
7798
Sean Christopherson45def772019-03-11 20:01:05 -07007799 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
7800 vcpu->arch.pio.count = 0;
7801 return 1;
7802 }
7803
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007804 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007805 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007806
7807 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007808 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007809 * the copy and tracing
7810 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007811 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07007812 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007813
Sean Christopherson45def772019-03-11 20:01:05 -07007814 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007815}
7816
Sean Christophersondca7f122018-03-08 08:57:27 -08007817static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
7818 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007819{
7820 unsigned long val;
7821 int ret;
7822
7823 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007824 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007825
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007826 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007827 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007828 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007829 return ret;
7830 }
7831
Sean Christopherson45def772019-03-11 20:01:05 -07007832 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007833 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7834
7835 return 0;
7836}
Sean Christophersondca7f122018-03-08 08:57:27 -08007837
7838int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7839{
Sean Christopherson45def772019-03-11 20:01:05 -07007840 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007841
Sean Christophersondca7f122018-03-08 08:57:27 -08007842 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007843 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007844 else
Sean Christopherson45def772019-03-11 20:01:05 -07007845 ret = kvm_fast_pio_out(vcpu, size, port);
7846 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007847}
7848EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007849
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007850static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007851{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007852 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007853 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007854}
7855
7856static void tsc_khz_changed(void *data)
7857{
7858 struct cpufreq_freqs *freq = data;
7859 unsigned long khz = 0;
7860
7861 if (data)
7862 khz = freq->new;
7863 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7864 khz = cpufreq_quick_get(raw_smp_processor_id());
7865 if (!khz)
7866 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007867 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007868}
7869
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007870#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007871static void kvm_hyperv_tsc_notifier(void)
7872{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007873 struct kvm *kvm;
7874 struct kvm_vcpu *vcpu;
7875 int cpu;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04007876 unsigned long flags;
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007877
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007878 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007879 list_for_each_entry(kvm, &vm_list, vm_list)
7880 kvm_make_mclock_inprogress_request(kvm);
7881
7882 hyperv_stop_tsc_emulation();
7883
7884 /* TSC frequency always matches when on Hyper-V */
7885 for_each_present_cpu(cpu)
7886 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7887 kvm_max_guest_tsc_khz = tsc_khz;
7888
7889 list_for_each_entry(kvm, &vm_list, vm_list) {
7890 struct kvm_arch *ka = &kvm->arch;
7891
Paolo Bonzinia83829f2021-03-25 14:11:14 -04007892 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007893 pvclock_update_vm_gtod_copy(kvm);
Paolo Bonzinia83829f2021-03-25 14:11:14 -04007894 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007895
7896 kvm_for_each_vcpu(cpu, vcpu, kvm)
7897 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7898
7899 kvm_for_each_vcpu(cpu, vcpu, kvm)
7900 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007901 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007902 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007903}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007904#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007905
Viresh Kumardf240142019-04-29 15:03:58 +05307906static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007907{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007908 struct kvm *kvm;
7909 struct kvm_vcpu *vcpu;
7910 int i, send_ipi = 0;
7911
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007912 /*
7913 * We allow guests to temporarily run on slowing clocks,
7914 * provided we notify them after, or to run on accelerating
7915 * clocks, provided we notify them before. Thus time never
7916 * goes backwards.
7917 *
7918 * However, we have a problem. We can't atomically update
7919 * the frequency of a given CPU from this function; it is
7920 * merely a notifier, which can be called from any CPU.
7921 * Changing the TSC frequency at arbitrary points in time
7922 * requires a recomputation of local variables related to
7923 * the TSC for each VCPU. We must flag these local variables
7924 * to be updated and be sure the update takes place with the
7925 * new frequency before any guests proceed.
7926 *
7927 * Unfortunately, the combination of hotplug CPU and frequency
7928 * change creates an intractable locking scenario; the order
7929 * of when these callouts happen is undefined with respect to
7930 * CPU hotplug, and they can race with each other. As such,
7931 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7932 * undefined; you can actually have a CPU frequency change take
7933 * place in between the computation of X and the setting of the
7934 * variable. To protect against this problem, all updates of
7935 * the per_cpu tsc_khz variable are done in an interrupt
7936 * protected IPI, and all callers wishing to update the value
7937 * must wait for a synchronous IPI to complete (which is trivial
7938 * if the caller is on the CPU already). This establishes the
7939 * necessary total order on variable updates.
7940 *
7941 * Note that because a guest time update may take place
7942 * anytime after the setting of the VCPU's request bit, the
7943 * correct TSC value must be set before the request. However,
7944 * to ensure the update actually makes it to any guest which
7945 * starts running in hardware virtualization between the set
7946 * and the acquisition of the spinlock, we must also ping the
7947 * CPU after setting the request bit.
7948 *
7949 */
7950
Viresh Kumardf240142019-04-29 15:03:58 +05307951 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007952
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007953 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007954 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007955 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307956 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007957 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007958 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007959 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007960 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007961 }
7962 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007963 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007964
7965 if (freq->old < freq->new && send_ipi) {
7966 /*
7967 * We upscale the frequency. Must make the guest
7968 * doesn't see old kvmclock values while running with
7969 * the new frequency, otherwise we risk the guest sees
7970 * time go backwards.
7971 *
7972 * In case we update the frequency for another cpu
7973 * (which might be in guest context) send an interrupt
7974 * to kick the cpu out of guest context. Next time
7975 * guest context is entered kvmclock will be updated,
7976 * so the guest will not see stale values.
7977 */
Viresh Kumardf240142019-04-29 15:03:58 +05307978 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007979 }
Viresh Kumardf240142019-04-29 15:03:58 +05307980}
7981
7982static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7983 void *data)
7984{
7985 struct cpufreq_freqs *freq = data;
7986 int cpu;
7987
7988 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7989 return 0;
7990 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7991 return 0;
7992
7993 for_each_cpu(cpu, freq->policy->cpus)
7994 __kvmclock_cpufreq_notifier(freq, cpu);
7995
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007996 return 0;
7997}
7998
7999static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008000 .notifier_call = kvmclock_cpufreq_notifier
8001};
8002
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008003static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008004{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008005 tsc_khz_changed(NULL);
8006 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008007}
8008
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008009static void kvm_timer_init(void)
8010{
Zachary Amsdenc2855452010-09-18 14:38:15 -10008011 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05308012
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008013 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10008014#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01008015 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02008016 int cpu;
8017
Avi Kivity3e26f232010-12-16 12:16:34 +02008018 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01008019 policy = cpufreq_cpu_get(cpu);
Wanpeng Li9a119972020-03-02 15:15:36 +08008020 if (policy) {
8021 if (policy->cpuinfo.max_freq)
8022 max_tsc_khz = policy->cpuinfo.max_freq;
8023 cpufreq_cpu_put(policy);
8024 }
Avi Kivity3e26f232010-12-16 12:16:34 +02008025 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10008026#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008027 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
8028 CPUFREQ_TRANSITION_NOTIFIER);
8029 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05308030
Thomas Gleixner73c1b412016-12-21 20:19:54 +01008031 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008032 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008033}
8034
Andi Kleendd60d212017-07-25 17:20:32 -07008035DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
8036EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008037
Gleb Natapovf5132b02011-11-10 14:57:22 +02008038int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008039{
Alex,Shi086c9852011-10-20 15:34:01 +08008040 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008041}
8042
8043static int kvm_is_user_mode(void)
8044{
8045 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008046
Alex,Shi086c9852011-10-20 15:34:01 +08008047 if (__this_cpu_read(current_vcpu))
Jason Baronb36464772021-01-14 22:27:56 -05008048 user_mode = static_call(kvm_x86_get_cpl)(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008049
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008050 return user_mode != 0;
8051}
8052
8053static unsigned long kvm_get_guest_ip(void)
8054{
8055 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008056
Alex,Shi086c9852011-10-20 15:34:01 +08008057 if (__this_cpu_read(current_vcpu))
8058 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008059
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008060 return ip;
8061}
8062
Luwei Kang8479e042019-02-18 19:26:07 -05008063static void kvm_handle_intel_pt_intr(void)
8064{
8065 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
8066
8067 kvm_make_request(KVM_REQ_PMI, vcpu);
8068 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
8069 (unsigned long *)&vcpu->arch.pmu.global_status);
8070}
8071
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008072static struct perf_guest_info_callbacks kvm_guest_cbs = {
8073 .is_in_guest = kvm_is_in_guest,
8074 .is_user_mode = kvm_is_user_mode,
8075 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05008076 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008077};
8078
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008079#ifdef CONFIG_X86_64
8080static void pvclock_gtod_update_fn(struct work_struct *work)
8081{
Marcelo Tosattid8281992012-11-27 23:29:01 -02008082 struct kvm *kvm;
8083
8084 struct kvm_vcpu *vcpu;
8085 int i;
8086
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008087 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008088 list_for_each_entry(kvm, &vm_list, vm_list)
8089 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08008090 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008091 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008092 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008093}
8094
8095static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
8096
8097/*
8098 * Notification about pvclock gtod data update.
8099 */
8100static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
8101 void *priv)
8102{
8103 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
8104 struct timekeeper *tk = priv;
8105
8106 update_pvclock_gtod(tk);
8107
8108 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01008109 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008110 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01008111 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008112 atomic_read(&kvm_guest_has_master_clock) != 0)
8113 queue_work(system_long_wq, &pvclock_gtod_work);
8114
8115 return 0;
8116}
8117
8118static struct notifier_block pvclock_gtod_notifier = {
8119 .notifier_call = pvclock_gtod_notify,
8120};
8121#endif
8122
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008123int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02008124{
Sean Christophersond008dfd2020-03-21 13:25:56 -07008125 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008126 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008127
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008128 if (kvm_x86_ops.hardware_enable) {
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008129 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008130 r = -EEXIST;
8131 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008132 }
8133
8134 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01008135 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008136 r = -EOPNOTSUPP;
8137 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008138 }
8139 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01008140 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008141 r = -EOPNOTSUPP;
8142 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008143 }
8144
Marc Orrb666a4b2018-11-06 14:53:56 -08008145 /*
8146 * KVM explicitly assumes that the guest has an FPU and
8147 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
8148 * vCPU's FPU state as a fxregs_state struct.
8149 */
8150 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
8151 printk(KERN_ERR "kvm: inadequate fpu\n");
8152 r = -EOPNOTSUPP;
8153 goto out;
8154 }
8155
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008156 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01008157 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08008158 __alignof__(struct fpu), SLAB_ACCOUNT,
8159 NULL);
8160 if (!x86_fpu_cache) {
8161 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
8162 goto out;
8163 }
8164
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008165 x86_emulator_cache = kvm_alloc_emulator_cache();
8166 if (!x86_emulator_cache) {
8167 pr_err("kvm: failed to allocate cache for x86 emulator\n");
8168 goto out_free_x86_fpu_cache;
8169 }
8170
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07008171 user_return_msrs = alloc_percpu(struct kvm_user_return_msrs);
8172 if (!user_return_msrs) {
8173 printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008174 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008175 }
Sean Christophersone5fda4b2021-05-04 10:17:32 -07008176 kvm_nr_uret_msrs = 0;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008177
Avi Kivity97db56c2008-01-13 13:23:56 +02008178 r = kvm_mmu_module_init();
8179 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008180 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02008181
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008182 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008183
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008184 perf_register_guest_info_callbacks(&kvm_guest_cbs);
8185
Sean Christophersoncfc48182020-03-02 15:56:23 -08008186 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08008187 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08008188 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
8189 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08008190
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08008191 if (pi_inject_timer == -1)
8192 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008193#ifdef CONFIG_X86_64
8194 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008195
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008196 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008197 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008198#endif
8199
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008200 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008201
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008202out_free_percpu:
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07008203 free_percpu(user_return_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008204out_free_x86_emulator_cache:
8205 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08008206out_free_x86_fpu_cache:
8207 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008208out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008209 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02008210}
Hollis Blanchard8776e512007-10-31 17:24:24 -05008211
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008212void kvm_arch_exit(void)
8213{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008214#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008215 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008216 clear_hv_tscchange_cb();
8217#endif
David Matlackcef84c32016-12-16 14:30:36 -08008218 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008219 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
8220
Jan Kiszka888d2562009-04-17 19:24:58 +02008221 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
8222 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
8223 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008224 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008225#ifdef CONFIG_X86_64
8226 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
Thomas Gleixner594b27e2021-05-05 23:48:17 +02008227 cancel_work_sync(&pvclock_gtod_work);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008228#endif
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008229 kvm_x86_ops.hardware_enable = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008230 kvm_mmu_module_exit();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07008231 free_percpu(user_return_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08008232 kmem_cache_destroy(x86_fpu_cache);
Paolo Bonzinib59b1532021-02-26 04:54:45 -05008233#ifdef CONFIG_KVM_XEN
Paolo Bonzinic462f852021-02-26 04:49:06 -05008234 static_key_deferred_flush(&kvm_xen_enabled);
David Woodhouse7d6bbeb2021-02-02 15:48:05 +00008235 WARN_ON(static_branch_unlikely(&kvm_xen_enabled.key));
Paolo Bonzinib59b1532021-02-26 04:54:45 -05008236#endif
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008237}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008238
Paolo Bonzini872f36e2021-01-08 05:54:44 -05008239static int __kvm_vcpu_halt(struct kvm_vcpu *vcpu, int state, int reason)
Hollis Blanchard8776e512007-10-31 17:24:24 -05008240{
8241 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02008242 if (lapic_in_kernel(vcpu)) {
Tom Lendacky647daca2021-01-04 14:20:01 -06008243 vcpu->arch.mp_state = state;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008244 return 1;
8245 } else {
Tom Lendacky647daca2021-01-04 14:20:01 -06008246 vcpu->run->exit_reason = reason;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008247 return 0;
8248 }
8249}
Tom Lendacky647daca2021-01-04 14:20:01 -06008250
8251int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
8252{
8253 return __kvm_vcpu_halt(vcpu, KVM_MP_STATE_HALTED, KVM_EXIT_HLT);
8254}
Joel Schopp5cb56052015-03-02 13:43:31 -06008255EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
8256
8257int kvm_emulate_halt(struct kvm_vcpu *vcpu)
8258{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008259 int ret = kvm_skip_emulated_instruction(vcpu);
8260 /*
8261 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
8262 * KVM_EXIT_DEBUG here.
8263 */
8264 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06008265}
Hollis Blanchard8776e512007-10-31 17:24:24 -05008266EXPORT_SYMBOL_GPL(kvm_emulate_halt);
8267
Tom Lendacky647daca2021-01-04 14:20:01 -06008268int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu)
8269{
8270 int ret = kvm_skip_emulated_instruction(vcpu);
8271
8272 return __kvm_vcpu_halt(vcpu, KVM_MP_STATE_AP_RESET_HOLD, KVM_EXIT_AP_RESET_HOLD) && ret;
8273}
8274EXPORT_SYMBOL_GPL(kvm_emulate_ap_reset_hold);
8275
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008276#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008277static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
8278 unsigned long clock_type)
8279{
8280 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02008281 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01008282 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008283 int ret;
8284
8285 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
8286 return -KVM_EOPNOTSUPP;
8287
YANG LI7ca7f3b2021-01-11 17:32:58 +08008288 if (!kvm_get_walltime_and_clockread(&ts, &cycle))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008289 return -KVM_EOPNOTSUPP;
8290
8291 clock_pairing.sec = ts.tv_sec;
8292 clock_pairing.nsec = ts.tv_nsec;
8293 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
8294 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02008295 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008296
8297 ret = 0;
8298 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
8299 sizeof(struct kvm_clock_pairing)))
8300 ret = -KVM_EFAULT;
8301
8302 return ret;
8303}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008304#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008305
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308306/*
8307 * kvm_pv_kick_cpu_op: Kick a vcpu.
8308 *
8309 * @apicid - apicid of vcpu to be kicked.
8310 */
8311static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
8312{
Raghavendra K T24d21662013-08-26 14:18:35 +05308313 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308314
Peter Xu150a84f2019-12-04 20:07:21 +01008315 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01008316 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08008317 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05308318 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06008319 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308320
Raghavendra K T24d21662013-08-26 14:18:35 +05308321 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08008322 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308323}
8324
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008325bool kvm_apicv_activated(struct kvm *kvm)
8326{
8327 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
8328}
8329EXPORT_SYMBOL_GPL(kvm_apicv_activated);
8330
8331void kvm_apicv_init(struct kvm *kvm, bool enable)
8332{
8333 if (enable)
8334 clear_bit(APICV_INHIBIT_REASON_DISABLE,
8335 &kvm->arch.apicv_inhibit_reasons);
8336 else
8337 set_bit(APICV_INHIBIT_REASON_DISABLE,
8338 &kvm->arch.apicv_inhibit_reasons);
8339}
8340EXPORT_SYMBOL_GPL(kvm_apicv_init);
8341
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008342static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id)
Wanpeng Li71506292019-06-11 20:23:49 +08008343{
8344 struct kvm_vcpu *target = NULL;
8345 struct kvm_apic_map *map;
8346
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008347 vcpu->stat.directed_yield_attempted++;
8348
Wanpeng Li71506292019-06-11 20:23:49 +08008349 rcu_read_lock();
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008350 map = rcu_dereference(vcpu->kvm->arch.apic_map);
Wanpeng Li71506292019-06-11 20:23:49 +08008351
8352 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
8353 target = map->phys_map[dest_id]->vcpu;
8354
8355 rcu_read_unlock();
8356
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008357 if (!target || !READ_ONCE(target->ready))
8358 goto no_yield;
8359
Wanpeng Lia1fa4cb2021-04-09 12:18:31 +08008360 /* Ignore requests to yield to self */
8361 if (vcpu == target)
8362 goto no_yield;
8363
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008364 if (kvm_vcpu_yield_to(target) <= 0)
8365 goto no_yield;
8366
8367 vcpu->stat.directed_yield_successful++;
8368
8369no_yield:
8370 return;
Wanpeng Li71506292019-06-11 20:23:49 +08008371}
8372
Hollis Blanchard8776e512007-10-31 17:24:24 -05008373int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
8374{
8375 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03008376 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008377
Joao Martins23200b72018-06-13 09:55:44 -04008378 if (kvm_xen_hypercall_enabled(vcpu->kvm))
8379 return kvm_xen_hypercall(vcpu);
8380
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01008381 if (kvm_hv_hypercall_enabled(vcpu))
Radim Krčmář696ca772018-05-24 17:50:56 +02008382 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02008383
Sean Christophersonde3cd112019-04-30 10:36:17 -07008384 nr = kvm_rax_read(vcpu);
8385 a0 = kvm_rbx_read(vcpu);
8386 a1 = kvm_rcx_read(vcpu);
8387 a2 = kvm_rdx_read(vcpu);
8388 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008389
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008390 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04008391
Nadav Amita449c7a2014-06-18 17:19:24 +03008392 op_64_bit = is_64_bit_mode(vcpu);
8393 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05008394 nr &= 0xFFFFFFFF;
8395 a0 &= 0xFFFFFFFF;
8396 a1 &= 0xFFFFFFFF;
8397 a2 &= 0xFFFFFFFF;
8398 a3 &= 0xFFFFFFFF;
8399 }
8400
Jason Baronb36464772021-01-14 22:27:56 -05008401 if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
Jan Kiszka07708c42009-08-03 18:43:28 +02008402 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02008403 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02008404 }
8405
Oliver Upton66570e92020-08-18 15:24:28 +00008406 ret = -KVM_ENOSYS;
8407
Hollis Blanchard8776e512007-10-31 17:24:24 -05008408 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02008409 case KVM_HC_VAPIC_POLL_IRQ:
8410 ret = 0;
8411 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308412 case KVM_HC_KICK_CPU:
Oliver Upton66570e92020-08-18 15:24:28 +00008413 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_UNHALT))
8414 break;
8415
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308416 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008417 kvm_sched_yield(vcpu, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308418 ret = 0;
8419 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008420#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008421 case KVM_HC_CLOCK_PAIRING:
8422 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
8423 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08008424#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08008425 case KVM_HC_SEND_IPI:
Oliver Upton66570e92020-08-18 15:24:28 +00008426 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SEND_IPI))
8427 break;
8428
Wanpeng Li4180bf12018-07-23 14:39:54 +08008429 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
8430 break;
Wanpeng Li71506292019-06-11 20:23:49 +08008431 case KVM_HC_SCHED_YIELD:
Oliver Upton66570e92020-08-18 15:24:28 +00008432 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SCHED_YIELD))
8433 break;
8434
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008435 kvm_sched_yield(vcpu, a0);
Wanpeng Li71506292019-06-11 20:23:49 +08008436 ret = 0;
8437 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008438 default:
8439 ret = -KVM_ENOSYS;
8440 break;
8441 }
Radim Krčmář696ca772018-05-24 17:50:56 +02008442out:
Nadav Amita449c7a2014-06-18 17:19:24 +03008443 if (!op_64_bit)
8444 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07008445 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03008446
Amit Shahf11c3a82008-02-21 01:00:30 +05308447 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03008448 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008449}
8450EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
8451
Jan Kiszkab6785de2012-09-20 07:43:17 +02008452static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05008453{
Avi Kivityd6aa1002011-04-20 15:47:13 +03008454 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008455 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008456 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008457
Jason Baronb36464772021-01-14 22:27:56 -05008458 static_call(kvm_x86_patch_hypercall)(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008459
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01008460 return emulator_write_emulated(ctxt, rip, instruction, 3,
8461 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008462}
8463
Avi Kivity851ba692009-08-24 11:10:17 +03008464static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008465{
Matt Gingell782d4222015-11-16 15:26:00 -08008466 return vcpu->run->request_interrupt_window &&
8467 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008468}
8469
Avi Kivity851ba692009-08-24 11:10:17 +03008470static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008471{
Avi Kivity851ba692009-08-24 11:10:17 +03008472 struct kvm_run *kvm_run = vcpu->run;
8473
Tom Lendackyf1c63662020-12-14 10:29:50 -05008474 /*
8475 * if_flag is obsolete and useless, so do not bother
8476 * setting it for SEV-ES guests. Userspace can just
8477 * use kvm_run->ready_for_interrupt_injection.
8478 */
8479 kvm_run->if_flag = !vcpu->arch.guest_state_protected
8480 && (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
8481
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008482 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008483 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01008484 kvm_run->ready_for_interrupt_injection =
8485 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08008486 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Chenyi Qiang15aad3b2020-11-06 17:03:13 +08008487
8488 if (is_smm(vcpu))
8489 kvm_run->flags |= KVM_RUN_X86_SMM;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008490}
8491
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008492static void update_cr8_intercept(struct kvm_vcpu *vcpu)
8493{
8494 int max_irr, tpr;
8495
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008496 if (!kvm_x86_ops.update_cr8_intercept)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008497 return;
8498
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008499 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03008500 return;
8501
Andrey Smetanind62caab2015-11-10 15:36:33 +03008502 if (vcpu->arch.apicv_active)
8503 return;
8504
Gleb Natapov8db3baa2009-05-11 13:35:54 +03008505 if (!vcpu->arch.apic->vapic_addr)
8506 max_irr = kvm_lapic_find_highest_irr(vcpu);
8507 else
8508 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008509
8510 if (max_irr != -1)
8511 max_irr >>= 4;
8512
8513 tpr = kvm_lapic_get_cr8(vcpu);
8514
Jason Baronb36464772021-01-14 22:27:56 -05008515 static_call(kvm_x86_update_cr8_intercept)(vcpu, tpr, max_irr);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008516}
8517
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008518
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08008519int kvm_check_nested_events(struct kvm_vcpu *vcpu)
8520{
8521 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
8522 return -EIO;
8523
8524 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
8525 kvm_x86_ops.nested_ops->triple_fault(vcpu);
8526 return 1;
8527 }
8528
8529 return kvm_x86_ops.nested_ops->check_events(vcpu);
8530}
8531
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008532static void kvm_inject_exception(struct kvm_vcpu *vcpu)
8533{
8534 if (vcpu->arch.exception.error_code && !is_protmode(vcpu))
8535 vcpu->arch.exception.error_code = false;
8536 static_call(kvm_x86_queue_exception)(vcpu);
8537}
8538
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008539static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008540{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008541 int r;
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008542 bool can_inject = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008543
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008544 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07008545
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008546 if (vcpu->arch.exception.injected) {
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008547 kvm_inject_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008548 can_inject = false;
8549 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07008550 /*
Liran Alona042c262018-03-23 03:01:32 +03008551 * Do not inject an NMI or interrupt if there is a pending
8552 * exception. Exceptions and interrupts are recognized at
8553 * instruction boundaries, i.e. the start of an instruction.
8554 * Trap-like exceptions, e.g. #DB, have higher priority than
8555 * NMIs and interrupts, i.e. traps are recognized before an
8556 * NMI/interrupt that's pending on the same instruction.
8557 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
8558 * priority, but are only generated (pended) during instruction
8559 * execution, i.e. a pending fault-like exception means the
8560 * fault occurred on the *previous* instruction and must be
8561 * serviced prior to recognizing any new events in order to
8562 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07008563 */
Liran Alon1a680e32018-03-23 03:01:33 +03008564 else if (!vcpu->arch.exception.pending) {
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008565 if (vcpu->arch.nmi_injected) {
Jason Baronb36464772021-01-14 22:27:56 -05008566 static_call(kvm_x86_set_nmi)(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008567 can_inject = false;
8568 } else if (vcpu->arch.interrupt.injected) {
Jason Baronb36464772021-01-14 22:27:56 -05008569 static_call(kvm_x86_set_irq)(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008570 can_inject = false;
8571 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07008572 }
8573
Sean Christopherson3b82b8d2020-04-22 19:25:47 -07008574 WARN_ON_ONCE(vcpu->arch.exception.injected &&
8575 vcpu->arch.exception.pending);
8576
Liran Alon1a680e32018-03-23 03:01:33 +03008577 /*
8578 * Call check_nested_events() even if we reinjected a previous event
8579 * in order for caller to determine if it should require immediate-exit
8580 * from L2 to L1 due to pending L1 events which require exit
8581 * from L2 to L1.
8582 */
Paolo Bonzini56083bd2020-04-17 10:32:53 -04008583 if (is_guest_mode(vcpu)) {
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08008584 r = kvm_check_nested_events(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008585 if (r < 0)
8586 goto busy;
Wanpeng Li664f8e22017-08-24 03:35:09 -07008587 }
8588
8589 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03008590 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02008591 trace_kvm_inj_exception(vcpu->arch.exception.nr,
8592 vcpu->arch.exception.has_error_code,
8593 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03008594
Wanpeng Li664f8e22017-08-24 03:35:09 -07008595 vcpu->arch.exception.pending = false;
8596 vcpu->arch.exception.injected = true;
8597
Nadav Amitd6e8c852014-07-24 14:51:24 +03008598 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
8599 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
8600 X86_EFLAGS_RF);
8601
Jim Mattsonf10c7292018-10-16 14:29:23 -07008602 if (vcpu->arch.exception.nr == DB_VECTOR) {
Jim Mattsonf10c7292018-10-16 14:29:23 -07008603 kvm_deliver_exception_payload(vcpu);
8604 if (vcpu->arch.dr7 & DR7_GD) {
8605 vcpu->arch.dr7 &= ~DR7_GD;
8606 kvm_update_dr7(vcpu);
8607 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03008608 }
8609
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008610 kvm_inject_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008611 can_inject = false;
Liran Alon1a680e32018-03-23 03:01:33 +03008612 }
8613
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008614 /*
8615 * Finally, inject interrupt events. If an event cannot be injected
8616 * due to architectural conditions (e.g. IF=0) a window-open exit
8617 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending
8618 * and can architecturally be injected, but we cannot do it right now:
8619 * an interrupt could have arrived just now and we have to inject it
8620 * as a vmexit, or there could already an event in the queue, which is
8621 * indicated by can_inject. In that case we request an immediate exit
8622 * in order to make progress and get back here for another iteration.
8623 * The kvm_x86_ops hooks communicate this by returning -EBUSY.
8624 */
8625 if (vcpu->arch.smi_pending) {
Jason Baronb36464772021-01-14 22:27:56 -05008626 r = can_inject ? static_call(kvm_x86_smi_allowed)(vcpu, true) : -EBUSY;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008627 if (r < 0)
8628 goto busy;
8629 if (r) {
8630 vcpu->arch.smi_pending = false;
8631 ++vcpu->arch.smi_count;
8632 enter_smm(vcpu);
8633 can_inject = false;
8634 } else
Jason Baronb36464772021-01-14 22:27:56 -05008635 static_call(kvm_x86_enable_smi_window)(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008636 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008637
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008638 if (vcpu->arch.nmi_pending) {
Jason Baronb36464772021-01-14 22:27:56 -05008639 r = can_inject ? static_call(kvm_x86_nmi_allowed)(vcpu, true) : -EBUSY;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008640 if (r < 0)
8641 goto busy;
8642 if (r) {
8643 --vcpu->arch.nmi_pending;
8644 vcpu->arch.nmi_injected = true;
Jason Baronb36464772021-01-14 22:27:56 -05008645 static_call(kvm_x86_set_nmi)(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008646 can_inject = false;
Jason Baronb36464772021-01-14 22:27:56 -05008647 WARN_ON(static_call(kvm_x86_nmi_allowed)(vcpu, true) < 0);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008648 }
8649 if (vcpu->arch.nmi_pending)
Jason Baronb36464772021-01-14 22:27:56 -05008650 static_call(kvm_x86_enable_nmi_window)(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008651 }
8652
8653 if (kvm_cpu_has_injectable_intr(vcpu)) {
Jason Baronb36464772021-01-14 22:27:56 -05008654 r = can_inject ? static_call(kvm_x86_interrupt_allowed)(vcpu, true) : -EBUSY;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008655 if (r < 0)
8656 goto busy;
8657 if (r) {
8658 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false);
Jason Baronb36464772021-01-14 22:27:56 -05008659 static_call(kvm_x86_set_irq)(vcpu);
8660 WARN_ON(static_call(kvm_x86_interrupt_allowed)(vcpu, true) < 0);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008661 }
8662 if (kvm_cpu_has_injectable_intr(vcpu))
Jason Baronb36464772021-01-14 22:27:56 -05008663 static_call(kvm_x86_enable_irq_window)(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008664 }
8665
8666 if (is_guest_mode(vcpu) &&
8667 kvm_x86_ops.nested_ops->hv_timer_pending &&
8668 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
8669 *req_immediate_exit = true;
8670
8671 WARN_ON(vcpu->arch.exception.pending);
8672 return;
8673
8674busy:
8675 *req_immediate_exit = true;
8676 return;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008677}
8678
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008679static void process_nmi(struct kvm_vcpu *vcpu)
8680{
8681 unsigned limit = 2;
8682
8683 /*
8684 * x86 is limited to one NMI running, and one NMI pending after it.
8685 * If an NMI is already in progress, limit further NMIs to just one.
8686 * Otherwise, allow two (and we'll inject the first one immediately).
8687 */
Jason Baronb36464772021-01-14 22:27:56 -05008688 if (static_call(kvm_x86_get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008689 limit = 1;
8690
8691 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
8692 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
8693 kvm_make_request(KVM_REQ_EVENT, vcpu);
8694}
8695
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008696static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008697{
8698 u32 flags = 0;
8699 flags |= seg->g << 23;
8700 flags |= seg->db << 22;
8701 flags |= seg->l << 21;
8702 flags |= seg->avl << 20;
8703 flags |= seg->present << 15;
8704 flags |= seg->dpl << 13;
8705 flags |= seg->s << 12;
8706 flags |= seg->type << 8;
8707 return flags;
8708}
8709
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008710static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008711{
8712 struct kvm_segment seg;
8713 int offset;
8714
8715 kvm_get_segment(vcpu, &seg, n);
8716 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
8717
8718 if (n < 3)
8719 offset = 0x7f84 + n * 12;
8720 else
8721 offset = 0x7f2c + (n - 3) * 12;
8722
8723 put_smstate(u32, buf, offset + 8, seg.base);
8724 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008725 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008726}
8727
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06008728#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008729static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008730{
8731 struct kvm_segment seg;
8732 int offset;
8733 u16 flags;
8734
8735 kvm_get_segment(vcpu, &seg, n);
8736 offset = 0x7e00 + n * 16;
8737
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008738 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008739 put_smstate(u16, buf, offset, seg.selector);
8740 put_smstate(u16, buf, offset + 2, flags);
8741 put_smstate(u32, buf, offset + 4, seg.limit);
8742 put_smstate(u64, buf, offset + 8, seg.base);
8743}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06008744#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008745
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008746static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008747{
8748 struct desc_ptr dt;
8749 struct kvm_segment seg;
8750 unsigned long val;
8751 int i;
8752
8753 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
8754 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
8755 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
8756 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
8757
8758 for (i = 0; i < 8; i++)
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07008759 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read_raw(vcpu, i));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008760
8761 kvm_get_dr(vcpu, 6, &val);
8762 put_smstate(u32, buf, 0x7fcc, (u32)val);
8763 kvm_get_dr(vcpu, 7, &val);
8764 put_smstate(u32, buf, 0x7fc8, (u32)val);
8765
8766 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
8767 put_smstate(u32, buf, 0x7fc4, seg.selector);
8768 put_smstate(u32, buf, 0x7f64, seg.base);
8769 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008770 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008771
8772 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
8773 put_smstate(u32, buf, 0x7fc0, seg.selector);
8774 put_smstate(u32, buf, 0x7f80, seg.base);
8775 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008776 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008777
Jason Baronb36464772021-01-14 22:27:56 -05008778 static_call(kvm_x86_get_gdt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008779 put_smstate(u32, buf, 0x7f74, dt.address);
8780 put_smstate(u32, buf, 0x7f70, dt.size);
8781
Jason Baronb36464772021-01-14 22:27:56 -05008782 static_call(kvm_x86_get_idt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008783 put_smstate(u32, buf, 0x7f58, dt.address);
8784 put_smstate(u32, buf, 0x7f54, dt.size);
8785
8786 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008787 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008788
8789 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
8790
8791 /* revision id */
8792 put_smstate(u32, buf, 0x7efc, 0x00020000);
8793 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
8794}
8795
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008796#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008797static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008798{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008799 struct desc_ptr dt;
8800 struct kvm_segment seg;
8801 unsigned long val;
8802 int i;
8803
8804 for (i = 0; i < 16; i++)
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07008805 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read_raw(vcpu, i));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008806
8807 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
8808 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
8809
8810 kvm_get_dr(vcpu, 6, &val);
8811 put_smstate(u64, buf, 0x7f68, val);
8812 kvm_get_dr(vcpu, 7, &val);
8813 put_smstate(u64, buf, 0x7f60, val);
8814
8815 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
8816 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
8817 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
8818
8819 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
8820
8821 /* revision id */
8822 put_smstate(u32, buf, 0x7efc, 0x00020064);
8823
8824 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
8825
8826 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
8827 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008828 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008829 put_smstate(u32, buf, 0x7e94, seg.limit);
8830 put_smstate(u64, buf, 0x7e98, seg.base);
8831
Jason Baronb36464772021-01-14 22:27:56 -05008832 static_call(kvm_x86_get_idt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008833 put_smstate(u32, buf, 0x7e84, dt.size);
8834 put_smstate(u64, buf, 0x7e88, dt.address);
8835
8836 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
8837 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008838 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008839 put_smstate(u32, buf, 0x7e74, seg.limit);
8840 put_smstate(u64, buf, 0x7e78, seg.base);
8841
Jason Baronb36464772021-01-14 22:27:56 -05008842 static_call(kvm_x86_get_gdt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008843 put_smstate(u32, buf, 0x7e64, dt.size);
8844 put_smstate(u64, buf, 0x7e68, dt.address);
8845
8846 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008847 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008848}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008849#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008850
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008851static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02008852{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008853 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02008854 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008855 char buf[512];
8856 u32 cr0;
8857
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008858 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008859 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008860#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008861 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008862 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008863 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008864#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008865 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008866
Ladi Prosek0234bf82017-10-11 16:54:40 +02008867 /*
8868 * Give pre_enter_smm() a chance to make ISA-specific changes to the
8869 * vCPU state (e.g. leave guest mode) after we've saved the state into
8870 * the SMM state-save area.
8871 */
Jason Baronb36464772021-01-14 22:27:56 -05008872 static_call(kvm_x86_pre_enter_smm)(vcpu, buf);
Ladi Prosek0234bf82017-10-11 16:54:40 +02008873
8874 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008875 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008876
Jason Baronb36464772021-01-14 22:27:56 -05008877 if (static_call(kvm_x86_get_nmi_mask)(vcpu))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008878 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
8879 else
Jason Baronb36464772021-01-14 22:27:56 -05008880 static_call(kvm_x86_set_nmi_mask)(vcpu, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008881
8882 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
8883 kvm_rip_write(vcpu, 0x8000);
8884
8885 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
Jason Baronb36464772021-01-14 22:27:56 -05008886 static_call(kvm_x86_set_cr0)(vcpu, cr0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008887 vcpu->arch.cr0 = cr0;
8888
Jason Baronb36464772021-01-14 22:27:56 -05008889 static_call(kvm_x86_set_cr4)(vcpu, 0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008890
Paolo Bonzini18c36262015-08-07 12:27:54 +02008891 /* Undocumented: IDT limit is set to zero on entry to SMM. */
8892 dt.address = dt.size = 0;
Jason Baronb36464772021-01-14 22:27:56 -05008893 static_call(kvm_x86_set_idt)(vcpu, &dt);
Paolo Bonzini18c36262015-08-07 12:27:54 +02008894
Paolo Bonzini996ff542020-12-14 07:49:54 -05008895 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008896
8897 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
8898 cs.base = vcpu->arch.smbase;
8899
8900 ds.selector = 0;
8901 ds.base = 0;
8902
8903 cs.limit = ds.limit = 0xffffffff;
8904 cs.type = ds.type = 0x3;
8905 cs.dpl = ds.dpl = 0;
8906 cs.db = ds.db = 0;
8907 cs.s = ds.s = 1;
8908 cs.l = ds.l = 0;
8909 cs.g = ds.g = 1;
8910 cs.avl = ds.avl = 0;
8911 cs.present = ds.present = 1;
8912 cs.unusable = ds.unusable = 0;
8913 cs.padding = ds.padding = 0;
8914
8915 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
8916 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
8917 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
8918 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
8919 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
8920 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
8921
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008922#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008923 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Jason Baronb36464772021-01-14 22:27:56 -05008924 static_call(kvm_x86_set_efer)(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008925#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008926
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08008927 kvm_update_cpuid_runtime(vcpu);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008928 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008929}
8930
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008931static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008932{
8933 vcpu->arch.smi_pending = true;
8934 kvm_make_request(KVM_REQ_EVENT, vcpu);
8935}
8936
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008937void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8938 unsigned long *vcpu_bitmap)
8939{
8940 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008941
8942 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8943
Mao Wenandb5a95e2019-11-19 11:06:40 +08008944 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -05008945 NULL, vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008946
8947 free_cpumask_var(cpus);
8948}
8949
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008950void kvm_make_scan_ioapic_request(struct kvm *kvm)
8951{
8952 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8953}
8954
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008955void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8956{
8957 if (!lapic_in_kernel(vcpu))
8958 return;
8959
8960 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8961 kvm_apic_update_apicv(vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05008962 static_call(kvm_x86_refresh_apicv_exec_ctrl)(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008963}
8964EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8965
8966/*
8967 * NOTE: Do not hold any lock prior to calling this.
8968 *
8969 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8970 * locked, because it calls __x86_set_memory_region() which does
8971 * synchronize_srcu(&kvm->srcu).
8972 */
8973void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8974{
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008975 struct kvm_vcpu *except;
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008976 unsigned long old, new, expected;
8977
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008978 if (!kvm_x86_ops.check_apicv_inhibit_reasons ||
Jason Baronb36464772021-01-14 22:27:56 -05008979 !static_call(kvm_x86_check_apicv_inhibit_reasons)(bit))
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008980 return;
8981
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008982 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
8983 do {
8984 expected = new = old;
8985 if (activate)
8986 __clear_bit(bit, &new);
8987 else
8988 __set_bit(bit, &new);
8989 if (new == old)
8990 break;
8991 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
8992 } while (old != expected);
8993
8994 if (!!old == !!new)
8995 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008996
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008997 trace_kvm_apicv_update_request(activate, bit);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008998 if (kvm_x86_ops.pre_update_apicv_exec_ctrl)
Jason Baronb36464772021-01-14 22:27:56 -05008999 static_call(kvm_x86_pre_update_apicv_exec_ctrl)(kvm, activate);
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05009000
9001 /*
9002 * Sending request to update APICV for all other vcpus,
9003 * while update the calling vcpu immediately instead of
9004 * waiting for another #VMEXIT to handle the request.
9005 */
9006 except = kvm_get_running_vcpu();
9007 kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE,
9008 except);
9009 if (except)
9010 kvm_vcpu_update_apicv(except);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009011}
9012EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
9013
Yang Zhang3d81bc72013-04-11 19:25:13 +08009014static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009015{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08009016 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009017 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009018
Andrey Smetanin63086302015-11-10 15:36:32 +03009019 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009020
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07009021 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03009022 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02009023 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02009024 if (vcpu->arch.apicv_active)
Jason Baronb36464772021-01-14 22:27:56 -05009025 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08009026 if (ioapic_in_kernel(vcpu->kvm))
9027 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02009028 }
Liran Alone40ff1d2018-03-21 02:50:31 +02009029
9030 if (is_guest_mode(vcpu))
9031 vcpu->arch.load_eoi_exitmap_pending = true;
9032 else
9033 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
9034}
9035
9036static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
9037{
9038 u64 eoi_exit_bitmap[4];
9039
9040 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
9041 return;
9042
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +01009043 if (to_hv_vcpu(vcpu))
9044 bitmap_or((ulong *)eoi_exit_bitmap,
9045 vcpu->arch.ioapic_handled_vectors,
9046 to_hv_synic(vcpu)->vec_bitmap, 256);
9047
Jason Baronb36464772021-01-14 22:27:56 -05009048 static_call(kvm_x86_load_eoi_exitmap)(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009049}
9050
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +09009051void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
9052 unsigned long start, unsigned long end)
Radim Krčmářb1394e72017-11-30 19:05:45 +01009053{
9054 unsigned long apic_address;
9055
9056 /*
9057 * The physical address of apic access page is stored in the VMCS.
9058 * Update it when it becomes invalid.
9059 */
9060 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
9061 if (start <= apic_address && apic_address < end)
9062 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
9063}
9064
Tang Chen4256f432014-09-24 15:57:54 +08009065void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
9066{
Paolo Bonzini35754c92015-07-29 12:05:37 +02009067 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02009068 return;
9069
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009070 if (!kvm_x86_ops.set_apic_access_page_addr)
Tang Chen4256f432014-09-24 15:57:54 +08009071 return;
9072
Jason Baronb36464772021-01-14 22:27:56 -05009073 static_call(kvm_x86_set_apic_access_page_addr)(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08009074}
Tang Chen4256f432014-09-24 15:57:54 +08009075
Sean Christophersond264ee02018-08-27 15:21:12 -07009076void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
9077{
9078 smp_send_reschedule(vcpu->cpu);
9079}
9080EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
9081
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09009082/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01009083 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09009084 * exiting to the userspace. Otherwise, the value will be returned to the
9085 * userspace.
9086 */
Avi Kivity851ba692009-08-24 11:10:17 +03009087static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009088{
9089 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08009090 bool req_int_win =
9091 dm_request_for_irq_injection(vcpu) &&
9092 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08009093 fastpath_t exit_fastpath;
Matt Gingell62a193e2015-11-16 15:26:07 -08009094
Jan Kiszka730dca42013-04-28 10:50:52 +02009095 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009096
Peter Xufb04a1e2020-09-30 21:22:22 -04009097 /* Forbid vmenter if vcpu dirty ring is soft-full */
9098 if (unlikely(vcpu->kvm->dirty_ring_size &&
9099 kvm_dirty_ring_soft_full(&vcpu->dirty_ring))) {
9100 vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL;
9101 trace_kvm_dirty_ring_exit(vcpu);
9102 r = 0;
9103 goto out;
9104 }
9105
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02009106 if (kvm_request_pending(vcpu)) {
Paolo Bonzini729c15c2020-09-22 06:53:57 -04009107 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
Paolo Bonzini9a78e152021-01-08 11:43:08 -05009108 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) {
Jim Mattson671ddc72019-10-15 10:44:05 -07009109 r = 0;
9110 goto out;
9111 }
9112 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03009113 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05009114 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03009115 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03009116 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009117 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
9118 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03009119 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
9120 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10009121 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
9122 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10009123 if (unlikely(r))
9124 goto out;
9125 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03009126 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03009127 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05009128 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
9129 kvm_mmu_load_pgd(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07009130 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
Sean Christopherson77809382020-03-20 14:28:18 -07009131 kvm_vcpu_flush_tlb_all(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07009132
9133 /* Flushing all ASIDs flushes the current ASID... */
9134 kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
9135 }
9136 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
9137 kvm_vcpu_flush_tlb_current(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04009138 if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
9139 kvm_vcpu_flush_tlb_guest(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07009140
Avi Kivitya8eeb042010-05-10 12:34:53 +03009141 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009142 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02009143 r = 0;
9144 goto out;
9145 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03009146 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08009147 if (is_guest_mode(vcpu)) {
9148 kvm_x86_ops.nested_ops->triple_fault(vcpu);
9149 } else {
9150 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
9151 vcpu->mmio_needed = 0;
9152 r = 0;
9153 goto out;
9154 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01009155 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02009156 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
9157 /* Page is swapped out. Do synthetic halt */
9158 vcpu->arch.apf.halted = true;
9159 r = 1;
9160 goto out;
9161 }
Glauber Costac9aaa892011-07-11 15:28:14 -04009162 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
9163 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009164 if (kvm_check_request(KVM_REQ_SMI, vcpu))
9165 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009166 if (kvm_check_request(KVM_REQ_NMI, vcpu))
9167 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009168 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02009169 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009170 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02009171 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07009172 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
9173 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
9174 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03009175 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07009176 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
9177 vcpu->run->eoi.vector =
9178 vcpu->arch.pending_ioapic_eoi;
9179 r = 0;
9180 goto out;
9181 }
9182 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08009183 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
9184 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02009185 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
9186 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08009187 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
9188 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03009189 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
9190 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
9191 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
9192 r = 0;
9193 goto out;
9194 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03009195 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
9196 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
9197 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
9198 r = 0;
9199 goto out;
9200 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03009201 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01009202 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
9203
Andrey Smetanindb3975712015-11-10 15:36:35 +03009204 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01009205 vcpu->run->hyperv = hv_vcpu->exit;
Andrey Smetanindb3975712015-11-10 15:36:35 +03009206 r = 0;
9207 goto out;
9208 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03009209
9210 /*
9211 * KVM_REQ_HV_STIMER has to be processed after
9212 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
9213 * depend on the guest clock being up-to-date
9214 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009215 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
9216 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009217 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
9218 kvm_vcpu_update_apicv(vcpu);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02009219 if (kvm_check_request(KVM_REQ_APF_READY, vcpu))
9220 kvm_check_async_pf_completion(vcpu);
Alexander Graf1a1552542020-09-25 16:34:21 +02009221 if (kvm_check_request(KVM_REQ_MSR_FILTER_CHANGED, vcpu))
Jason Baronb36464772021-01-14 22:27:56 -05009222 static_call(kvm_x86_msr_filter_changed)(vcpu);
Makarand Sonarea85863c2021-02-12 16:50:12 -08009223
9224 if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu))
9225 static_call(kvm_x86_update_cpu_dirty_logging)(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02009226 }
Avi Kivityb93463a2007-10-25 16:52:32 +02009227
David Woodhouse40da8cc2020-12-09 20:08:30 +00009228 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
9229 kvm_xen_has_interrupt(vcpu)) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01009230 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01009231 kvm_apic_accept_events(vcpu);
9232 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
9233 r = 1;
9234 goto out;
9235 }
9236
Paolo Bonzinic9d40912020-05-22 11:21:49 -04009237 inject_pending_event(vcpu, &req_immediate_exit);
9238 if (req_int_win)
Jason Baronb36464772021-01-14 22:27:56 -05009239 static_call(kvm_x86_enable_irq_window)(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03009240
Avi Kivity3842d132010-07-27 12:30:24 +03009241 if (kvm_lapic_enabled(vcpu)) {
9242 update_cr8_intercept(vcpu);
9243 kvm_lapic_sync_to_vapic(vcpu);
9244 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009245 }
Avi Kivityb93463a2007-10-25 16:52:32 +02009246
Avi Kivityd8368af2012-05-14 18:07:56 +03009247 r = kvm_mmu_reload(vcpu);
9248 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009249 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03009250 }
9251
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009252 preempt_disable();
9253
Jason Baronb36464772021-01-14 22:27:56 -05009254 static_call(kvm_x86_prepare_guest_switch)(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009255
9256 /*
9257 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
9258 * IPI are then delayed after guest entry, which ensures that they
9259 * result in virtual interrupt delivery.
9260 */
9261 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009262 vcpu->mode = IN_GUEST_MODE;
9263
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02009264 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
9265
Lan Tianyu0f127d12016-03-13 11:10:29 +08009266 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009267 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02009268 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009269 *
Luwei Kang81b01662019-01-31 16:52:02 +08009270 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009271 * pairs with the memory barrier implicit in pi_test_and_set_on
9272 * (see vmx_deliver_posted_interrupt).
9273 *
9274 * 3) This also orders the write to mode from any reads to the page
9275 * tables done while the VCPU is running. Please see the comment
9276 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009277 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02009278 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009279
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009280 /*
9281 * This handles the case where a posted interrupt was
9282 * notified with kvm_vcpu_kick.
9283 */
Liran Alonfa59cc02017-12-24 18:12:53 +02009284 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
Jason Baronb36464772021-01-14 22:27:56 -05009285 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009286
Wanpeng Li5a9f5442020-04-28 14:23:26 +08009287 if (kvm_vcpu_exit_request(vcpu)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009288 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009289 smp_wmb();
9290 local_irq_enable();
9291 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02009292 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009293 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009294 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009295 }
9296
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009297 if (req_immediate_exit) {
9298 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05009299 static_call(kvm_x86_request_immediate_exit)(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009300 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03009301
Sean Christopherson2620fe22020-01-17 11:30:51 -08009302 fpregs_assert_state_consistent();
9303 if (test_thread_flag(TIF_NEED_FPU_LOAD))
9304 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02009305
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009306 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009307 set_debugreg(0, 7);
9308 set_debugreg(vcpu->arch.eff_db[0], 0);
9309 set_debugreg(vcpu->arch.eff_db[1], 1);
9310 set_debugreg(vcpu->arch.eff_db[2], 2);
9311 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009312 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03009313 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009314 }
9315
Paolo Bonzinid89d04a2021-02-02 10:44:23 -05009316 for (;;) {
9317 exit_fastpath = static_call(kvm_x86_run)(vcpu);
9318 if (likely(exit_fastpath != EXIT_FASTPATH_REENTER_GUEST))
9319 break;
9320
9321 if (unlikely(kvm_vcpu_exit_request(vcpu))) {
9322 exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
9323 break;
9324 }
9325
9326 if (vcpu->arch.apicv_active)
9327 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
9328 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009329
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009330 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009331 * Do this here before restoring debug registers on the host. And
9332 * since we do this before handling the vmexit, a DR access vmexit
9333 * can (a) read the correct value of the debug registers, (b) set
9334 * KVM_DEBUGREG_WONT_EXIT again.
9335 */
9336 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009337 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
Jason Baronb36464772021-01-14 22:27:56 -05009338 static_call(kvm_x86_sync_dirty_debug_regs)(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01009339 kvm_update_dr0123(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01009340 kvm_update_dr7(vcpu);
9341 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009342 }
9343
9344 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009345 * If the guest has used debug registers, at least dr7
9346 * will be disabled while returning to the host.
9347 * If we don't have active breakpoints in the host, we don't
9348 * care about the messed up debug address registers. But if
9349 * we have some of them active, restore the old state.
9350 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01009351 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009352 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009353
Jim Mattsonc9671182020-06-03 16:56:23 -07009354 vcpu->arch.last_vmentry_cpu = vcpu->cpu;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08009355 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10009356
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009357 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03009358 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08009359
Jason Baronb36464772021-01-14 22:27:56 -05009360 static_call(kvm_x86_handle_exit_irqoff)(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009361
Sean Christophersond7a08882019-07-10 09:07:34 -07009362 /*
9363 * Consume any pending interrupts, including the possible source of
9364 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
9365 * An instruction is required after local_irq_enable() to fully unblock
9366 * interrupts on processors that implement an interrupt shadow, the
9367 * stat.exits increment will do nicely.
9368 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009369 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07009370 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009371 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07009372 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009373 kvm_after_interrupt(vcpu);
9374
Wanpeng Liec0671d2019-05-20 16:18:08 +08009375 if (lapic_in_kernel(vcpu)) {
9376 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
9377 if (delta != S64_MIN) {
9378 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
9379 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
9380 }
9381 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009382
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009383 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009384 preempt_enable();
9385
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009386 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03009387
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009388 /*
9389 * Profile KVM exit RIPs:
9390 */
9391 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009392 unsigned long rip = kvm_rip_read(vcpu);
9393 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009394 }
9395
Zachary Amsdencc578282012-02-03 15:43:50 -02009396 if (unlikely(vcpu->arch.tsc_always_catchup))
9397 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02009398
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03009399 if (vcpu->arch.apic_attention)
9400 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02009401
Jason Baronb36464772021-01-14 22:27:56 -05009402 r = static_call(kvm_x86_handle_exit)(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009403 return r;
9404
9405cancel_injection:
Sean Christopherson8081ad02020-04-22 19:25:40 -07009406 if (req_immediate_exit)
9407 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05009408 static_call(kvm_x86_cancel_injection)(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03009409 if (unlikely(vcpu->arch.apic_attention))
9410 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009411out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03009412 return r;
9413}
9414
Paolo Bonzini362c6982015-02-06 12:48:04 +01009415static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
9416{
Feng Wubf9f6ac2015-09-18 22:29:55 +08009417 if (!kvm_arch_vcpu_runnable(vcpu) &&
Jason Baronb36464772021-01-14 22:27:56 -05009418 (!kvm_x86_ops.pre_block || static_call(kvm_x86_pre_block)(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01009419 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
9420 kvm_vcpu_block(vcpu);
9421 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009422
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009423 if (kvm_x86_ops.post_block)
Jason Baronb36464772021-01-14 22:27:56 -05009424 static_call(kvm_x86_post_block)(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009425
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01009426 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
9427 return 1;
9428 }
Gleb Natapov09cec752009-03-23 15:11:44 +02009429
Paolo Bonzini362c6982015-02-06 12:48:04 +01009430 kvm_apic_accept_events(vcpu);
9431 switch(vcpu->arch.mp_state) {
9432 case KVM_MP_STATE_HALTED:
Tom Lendacky647daca2021-01-04 14:20:01 -06009433 case KVM_MP_STATE_AP_RESET_HOLD:
Paolo Bonzini362c6982015-02-06 12:48:04 +01009434 vcpu->arch.pv.pv_unhalted = false;
9435 vcpu->arch.mp_state =
9436 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009437 fallthrough;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009438 case KVM_MP_STATE_RUNNABLE:
9439 vcpu->arch.apf.halted = false;
9440 break;
9441 case KVM_MP_STATE_INIT_RECEIVED:
9442 break;
9443 default:
9444 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009445 }
9446 return 1;
9447}
9448
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009449static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
9450{
Paolo Bonzini56083bd2020-04-17 10:32:53 -04009451 if (is_guest_mode(vcpu))
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08009452 kvm_check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01009453
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009454 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
9455 !vcpu->arch.apf.halted);
9456}
9457
Paolo Bonzini362c6982015-02-06 12:48:04 +01009458static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03009459{
9460 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009461 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03009462
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009463 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009464 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03009465
Paolo Bonzini362c6982015-02-06 12:48:04 +01009466 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02009467 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009468 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009469 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01009470 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009471 }
9472
Gleb Natapov09cec752009-03-23 15:11:44 +02009473 if (r <= 0)
9474 break;
9475
Radim Krčmář72875d82017-04-26 22:32:19 +02009476 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02009477 if (kvm_cpu_has_pending_timer(vcpu))
9478 kvm_inject_pending_timer_irqs(vcpu);
9479
Matt Gingell782d4222015-11-16 15:26:00 -08009480 if (dm_request_for_irq_injection(vcpu) &&
9481 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02009482 r = 0;
9483 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02009484 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009485 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02009486 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02009487
Thomas Gleixnerf3020b82020-07-30 09:19:01 +02009488 if (__xfer_to_guest_mode_work_pending()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009489 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02009490 r = xfer_to_guest_mode_handle_work(vcpu);
9491 if (r)
9492 return r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009493 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03009494 }
9495 }
9496
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009497 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009498
9499 return r;
9500}
9501
Gleb Natapov716d51a2012-09-03 15:24:26 +03009502static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
9503{
9504 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009505
Gleb Natapov716d51a2012-09-03 15:24:26 +03009506 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07009507 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009508 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009509 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03009510}
9511
9512static int complete_emulated_pio(struct kvm_vcpu *vcpu)
9513{
9514 BUG_ON(!vcpu->arch.pio.count);
9515
9516 return complete_emulated_io(vcpu);
9517}
9518
Avi Kivityf78146b2012-04-18 19:22:47 +03009519/*
9520 * Implements the following, as a state machine:
9521 *
9522 * read:
9523 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009524 * for each mmio piece in the fragment
9525 * write gpa, len
9526 * exit
9527 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03009528 * execute insn
9529 *
9530 * write:
9531 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009532 * for each mmio piece in the fragment
9533 * write gpa, len
9534 * copy data
9535 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03009536 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03009537static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02009538{
9539 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03009540 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009541 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02009542
Gleb Natapov716d51a2012-09-03 15:24:26 +03009543 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02009544
Gleb Natapov716d51a2012-09-03 15:24:26 +03009545 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009546 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
9547 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009548 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009549 memcpy(frag->data, run->mmio.data, len);
9550
9551 if (frag->len <= 8) {
9552 /* Switch to the next fragment. */
9553 frag++;
9554 vcpu->mmio_cur_fragment++;
9555 } else {
9556 /* Go forward to the next mmio piece. */
9557 frag->data += len;
9558 frag->gpa += len;
9559 frag->len -= len;
9560 }
9561
Andrew Honiga08d3b32014-02-27 19:35:14 +01009562 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03009563 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02009564
9565 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02009566 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03009567 return 1;
9568 vcpu->mmio_read_completed = 1;
9569 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02009570 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009571
Gleb Natapov716d51a2012-09-03 15:24:26 +03009572 run->exit_reason = KVM_EXIT_MMIO;
9573 run->mmio.phys_addr = frag->gpa;
9574 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009575 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
9576 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009577 run->mmio.is_write = vcpu->mmio_is_write;
9578 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
9579 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02009580}
9581
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009582static void kvm_save_current_fpu(struct fpu *fpu)
9583{
9584 /*
9585 * If the target FPU state is not resident in the CPU registers, just
9586 * memcpy() from current, else save CPU state directly to the target.
9587 */
9588 if (test_thread_flag(TIF_NEED_FPU_LOAD))
9589 memcpy(&fpu->state, &current->thread.fpu.state,
9590 fpu_kernel_xstate_size);
9591 else
9592 copy_fpregs_to_fpstate(fpu);
9593}
9594
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009595/* Swap (qemu) user FPU context for the guest FPU context. */
9596static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
9597{
Rik van Riel5f409e22019-04-03 18:41:52 +02009598 fpregs_lock();
9599
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009600 kvm_save_current_fpu(vcpu->arch.user_fpu);
9601
Tom Lendackyed02b212020-12-10 11:10:01 -06009602 /*
9603 * Guests with protected state can't have it set by the hypervisor,
9604 * so skip trying to set it.
9605 */
9606 if (vcpu->arch.guest_fpu)
9607 /* PKRU is separately restored in kvm_x86_ops.run. */
9608 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
9609 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02009610
9611 fpregs_mark_activate();
9612 fpregs_unlock();
9613
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009614 trace_kvm_fpu(1);
9615}
9616
9617/* When vcpu_run ends, restore user space FPU context. */
9618static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
9619{
Rik van Riel5f409e22019-04-03 18:41:52 +02009620 fpregs_lock();
9621
Tom Lendackyed02b212020-12-10 11:10:01 -06009622 /*
9623 * Guests with protected state can't have it read by the hypervisor,
9624 * so skip trying to save it.
9625 */
9626 if (vcpu->arch.guest_fpu)
9627 kvm_save_current_fpu(vcpu->arch.guest_fpu);
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009628
Wanpeng Lid9a710e2019-07-22 12:26:21 +08009629 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02009630
9631 fpregs_mark_activate();
9632 fpregs_unlock();
9633
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009634 ++vcpu->stat.fpu_reload;
9635 trace_kvm_fpu(0);
9636}
9637
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009638int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009639{
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009640 struct kvm_run *kvm_run = vcpu->run;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009641 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009642
Christoffer Dallaccb7572017-12-04 21:35:25 +01009643 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01009644 kvm_sigset_activate(vcpu);
Chenyi Qiang15aad3b2020-11-06 17:03:13 +08009645 kvm_run->flags = 0;
Peter Xu5663d8f2017-12-12 17:15:02 +01009646 kvm_load_guest_fpu(vcpu);
9647
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009648 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02009649 if (kvm_run->immediate_exit) {
9650 r = -EINTR;
9651 goto out;
9652 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009653 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01009654 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02009655 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009656 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02009657 if (signal_pending(current)) {
9658 r = -EINTR;
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009659 kvm_run->exit_reason = KVM_EXIT_INTR;
Jan H. Schönherra0595002017-09-06 00:27:19 +02009660 ++vcpu->stat.signal_exits;
9661 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009662 goto out;
9663 }
9664
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009665 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
Ken Hofsass01643c52018-01-31 16:03:36 -08009666 r = -EINVAL;
9667 goto out;
9668 }
9669
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009670 if (kvm_run->kvm_dirty_regs) {
Ken Hofsass01643c52018-01-31 16:03:36 -08009671 r = sync_regs(vcpu);
9672 if (r != 0)
9673 goto out;
9674 }
9675
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009676 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02009677 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01009678 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
9679 r = -EINVAL;
9680 goto out;
9681 }
9682 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009683
Gleb Natapov716d51a2012-09-03 15:24:26 +03009684 if (unlikely(vcpu->arch.complete_userspace_io)) {
9685 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
9686 vcpu->arch.complete_userspace_io = NULL;
9687 r = cui(vcpu);
9688 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01009689 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03009690 } else
9691 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02009692
Paolo Bonzini460df4c2017-02-08 11:50:15 +01009693 if (kvm_run->immediate_exit)
9694 r = -EINTR;
9695 else
9696 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009697
9698out:
Peter Xu5663d8f2017-12-12 17:15:02 +01009699 kvm_put_guest_fpu(vcpu);
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009700 if (kvm_run->kvm_valid_regs)
Ken Hofsass01643c52018-01-31 16:03:36 -08009701 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03009702 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01009703 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009704
Christoffer Dallaccb7572017-12-04 21:35:25 +01009705 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009706 return r;
9707}
9708
Ken Hofsass01643c52018-01-31 16:03:36 -08009709static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009710{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009711 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
9712 /*
9713 * We are here if userspace calls get_regs() in the middle of
9714 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08009715 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009716 * that usually, but some bad designed PV devices (vmware
9717 * backdoor interface) need this to work
9718 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009719 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009720 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
9721 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07009722 regs->rax = kvm_rax_read(vcpu);
9723 regs->rbx = kvm_rbx_read(vcpu);
9724 regs->rcx = kvm_rcx_read(vcpu);
9725 regs->rdx = kvm_rdx_read(vcpu);
9726 regs->rsi = kvm_rsi_read(vcpu);
9727 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02009728 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07009729 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009730#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07009731 regs->r8 = kvm_r8_read(vcpu);
9732 regs->r9 = kvm_r9_read(vcpu);
9733 regs->r10 = kvm_r10_read(vcpu);
9734 regs->r11 = kvm_r11_read(vcpu);
9735 regs->r12 = kvm_r12_read(vcpu);
9736 regs->r13 = kvm_r13_read(vcpu);
9737 regs->r14 = kvm_r14_read(vcpu);
9738 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009739#endif
9740
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009741 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02009742 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08009743}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009744
Ken Hofsass01643c52018-01-31 16:03:36 -08009745int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
9746{
9747 vcpu_load(vcpu);
9748 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01009749 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009750 return 0;
9751}
9752
Ken Hofsass01643c52018-01-31 16:03:36 -08009753static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009754{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009755 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
9756 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
9757
Sean Christophersonde3cd112019-04-30 10:36:17 -07009758 kvm_rax_write(vcpu, regs->rax);
9759 kvm_rbx_write(vcpu, regs->rbx);
9760 kvm_rcx_write(vcpu, regs->rcx);
9761 kvm_rdx_write(vcpu, regs->rdx);
9762 kvm_rsi_write(vcpu, regs->rsi);
9763 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02009764 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07009765 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009766#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07009767 kvm_r8_write(vcpu, regs->r8);
9768 kvm_r9_write(vcpu, regs->r9);
9769 kvm_r10_write(vcpu, regs->r10);
9770 kvm_r11_write(vcpu, regs->r11);
9771 kvm_r12_write(vcpu, regs->r12);
9772 kvm_r13_write(vcpu, regs->r13);
9773 kvm_r14_write(vcpu, regs->r14);
9774 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009775#endif
9776
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009777 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08009778 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009779
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02009780 vcpu->arch.exception.pending = false;
9781
Avi Kivity3842d132010-07-27 12:30:24 +03009782 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08009783}
Avi Kivity3842d132010-07-27 12:30:24 +03009784
Ken Hofsass01643c52018-01-31 16:03:36 -08009785int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
9786{
9787 vcpu_load(vcpu);
9788 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01009789 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009790 return 0;
9791}
9792
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009793void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
9794{
9795 struct kvm_segment cs;
9796
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009797 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009798 *db = cs.db;
9799 *l = cs.l;
9800}
9801EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
9802
Ken Hofsass01643c52018-01-31 16:03:36 -08009803static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009804{
Gleb Natapov89a27f42010-02-16 10:51:48 +02009805 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009806
Tom Lendacky52657132020-12-10 11:09:59 -06009807 if (vcpu->arch.guest_state_protected)
9808 goto skip_protected_regs;
9809
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009810 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9811 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9812 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9813 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9814 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9815 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009816
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009817 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9818 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009819
Jason Baronb36464772021-01-14 22:27:56 -05009820 static_call(kvm_x86_get_idt)(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009821 sregs->idt.limit = dt.size;
9822 sregs->idt.base = dt.address;
Jason Baronb36464772021-01-14 22:27:56 -05009823 static_call(kvm_x86_get_gdt)(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009824 sregs->gdt.limit = dt.size;
9825 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009826
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009827 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009828 sregs->cr3 = kvm_read_cr3(vcpu);
Tom Lendacky52657132020-12-10 11:09:59 -06009829
9830skip_protected_regs:
9831 sregs->cr0 = kvm_read_cr0(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02009832 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009833 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02009834 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009835 sregs->apic_base = kvm_get_apic_base(vcpu);
9836
Jordan Borgner0e96f312018-10-28 12:58:28 +00009837 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009838
Liran Alon04140b42018-03-23 03:01:31 +03009839 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03009840 set_bit(vcpu->arch.interrupt.nr,
9841 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08009842}
Gleb Natapov16d7a192009-04-21 17:45:10 +03009843
Ken Hofsass01643c52018-01-31 16:03:36 -08009844int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
9845 struct kvm_sregs *sregs)
9846{
9847 vcpu_load(vcpu);
9848 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01009849 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009850 return 0;
9851}
9852
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009853int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
9854 struct kvm_mp_state *mp_state)
9855{
Christoffer Dallfd232562017-12-04 21:35:30 +01009856 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08009857 if (kvm_mpx_supported())
9858 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01009859
Jan Kiszka66450a22013-03-13 12:42:34 +01009860 kvm_apic_accept_events(vcpu);
Tom Lendacky647daca2021-01-04 14:20:01 -06009861 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED ||
9862 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) &&
9863 vcpu->arch.pv.pv_unhalted)
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05309864 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
9865 else
9866 mp_state->mp_state = vcpu->arch.mp_state;
9867
Sean Christophersonf958bd22019-12-09 12:19:31 -08009868 if (kvm_mpx_supported())
9869 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01009870 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009871 return 0;
9872}
9873
9874int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
9875 struct kvm_mp_state *mp_state)
9876{
Christoffer Dalle83dff52017-12-04 21:35:31 +01009877 int ret = -EINVAL;
9878
9879 vcpu_load(vcpu);
9880
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009881 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01009882 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01009883 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01009884
Liran Alon27cbe7d2019-11-11 11:16:40 +02009885 /*
9886 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
9887 * INIT state; latched init should be reported using
9888 * KVM_SET_VCPU_EVENTS, so reject it here.
9889 */
9890 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01009891 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
9892 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01009893 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01009894
Jan Kiszka66450a22013-03-13 12:42:34 +01009895 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
9896 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
9897 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
9898 } else
9899 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03009900 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01009901
9902 ret = 0;
9903out:
9904 vcpu_put(vcpu);
9905 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009906}
9907
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009908int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
9909 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009910{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009911 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009912 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009913
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009914 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02009915
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009916 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009917 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07009918 if (ret) {
9919 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9920 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
9921 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009922 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07009923 }
Gleb Natapovc6975182010-02-18 12:15:01 +02009924
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009925 kvm_rip_write(vcpu, ctxt->eip);
9926 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009927 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02009928}
9929EXPORT_SYMBOL_GPL(kvm_task_switch);
9930
Sean Christophersonee69c922020-10-06 18:44:16 -07009931static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05009932{
Tianyu Lan37b95952018-01-16 17:34:07 +08009933 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05009934 /*
9935 * When EFER.LME and CR0.PG are set, the processor is in
9936 * 64-bit mode (though maybe in a 32-bit code segment).
9937 * CR4.PAE and EFER.LMA must be set.
9938 */
Sean Christophersonee69c922020-10-06 18:44:16 -07009939 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA))
9940 return false;
Sean Christophersonca29e142021-02-03 16:01:12 -08009941 if (kvm_vcpu_is_illegal_gpa(vcpu, sregs->cr3))
Paolo Bonzinic1c35cf2020-11-13 08:30:38 -05009942 return false;
Lan Tianyuf2981032017-12-14 03:01:52 -05009943 } else {
9944 /*
9945 * Not in 64-bit mode: EFER.LMA is clear and the code
9946 * segment cannot be 64-bit.
9947 */
9948 if (sregs->efer & EFER_LMA || sregs->cs.l)
Sean Christophersonee69c922020-10-06 18:44:16 -07009949 return false;
Lan Tianyuf2981032017-12-14 03:01:52 -05009950 }
9951
Sean Christophersonee69c922020-10-06 18:44:16 -07009952 return kvm_is_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05009953}
9954
Ken Hofsass01643c52018-01-31 16:03:36 -08009955static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009956{
Jan Kiszka58cb6282014-01-24 16:48:44 +01009957 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009958 int mmu_reset_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009959 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02009960 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009961 int ret = -EINVAL;
9962
Sean Christophersonee69c922020-10-06 18:44:16 -07009963 if (!kvm_is_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009964 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009965
Jim Mattsond3802282017-08-10 10:14:13 -07009966 apic_base_msr.data = sregs->apic_base;
9967 apic_base_msr.host_initiated = true;
9968 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009969 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009970
Tom Lendacky52657132020-12-10 11:09:59 -06009971 if (vcpu->arch.guest_state_protected)
9972 goto skip_protected_regs;
9973
Gleb Natapov89a27f42010-02-16 10:51:48 +02009974 dt.size = sregs->idt.limit;
9975 dt.address = sregs->idt.base;
Jason Baronb36464772021-01-14 22:27:56 -05009976 static_call(kvm_x86_set_idt)(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009977 dt.size = sregs->gdt.limit;
9978 dt.address = sregs->gdt.base;
Jason Baronb36464772021-01-14 22:27:56 -05009979 static_call(kvm_x86_set_gdt)(vcpu, &dt);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009980
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009981 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009982 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009983 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009984 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009985
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009986 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009987
Avi Kivityf6801df2010-01-21 15:31:50 +02009988 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Jason Baronb36464772021-01-14 22:27:56 -05009989 static_call(kvm_x86_set_efer)(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009990
Avi Kivity4d4ec082009-12-29 18:07:30 +02009991 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Jason Baronb36464772021-01-14 22:27:56 -05009992 static_call(kvm_x86_set_cr0)(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009993 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009994
Avi Kivityfc78f512009-12-07 12:16:48 +02009995 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Jason Baronb36464772021-01-14 22:27:56 -05009996 static_call(kvm_x86_set_cr4)(vcpu, sregs->cr4);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009997
9998 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009999 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +020010000 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -020010001 mmu_reset_needed = 1;
10002 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +080010003 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010004
10005 if (mmu_reset_needed)
10006 kvm_mmu_reset_context(vcpu);
10007
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +020010008 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
10009 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
10010 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
10011 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
10012 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
10013 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010014
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +020010015 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
10016 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010017
Mikhail Ershov5f0269f2009-08-03 14:58:25 +030010018 update_cr8_intercept(vcpu);
10019
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -030010020 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +030010021 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -030010022 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +020010023 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -030010024 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
10025
Tom Lendacky52657132020-12-10 11:09:59 -060010026skip_protected_regs:
10027 max_bits = KVM_NR_INTERRUPTS;
10028 pending_vec = find_first_bit(
10029 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
10030 if (pending_vec < max_bits) {
10031 kvm_queue_interrupt(vcpu, pending_vec, false);
10032 pr_debug("Set back pending irq %d\n", pending_vec);
10033 }
10034
Avi Kivity3842d132010-07-27 12:30:24 +030010035 kvm_make_request(KVM_REQ_EVENT, vcpu);
10036
Christoffer Dallb4ef9d42017-12-04 21:35:29 +010010037 ret = 0;
10038out:
Ken Hofsass01643c52018-01-31 16:03:36 -080010039 return ret;
10040}
10041
10042int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
10043 struct kvm_sregs *sregs)
10044{
10045 int ret;
10046
10047 vcpu_load(vcpu);
10048 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +010010049 vcpu_put(vcpu);
10050 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010051}
10052
Jan Kiszkad0bfb942008-12-15 13:52:10 +010010053int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
10054 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010055{
Jan Kiszka355be0b2009-10-03 00:31:21 +020010056 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010057 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010058
Tom Lendacky8d4846b2020-12-10 11:09:43 -060010059 if (vcpu->arch.guest_state_protected)
10060 return -EINVAL;
10061
Christoffer Dall66b56562017-12-04 21:35:33 +010010062 vcpu_load(vcpu);
10063
Jan Kiszka4f926bf22009-10-30 12:46:59 +010010064 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
10065 r = -EBUSY;
10066 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +030010067 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +010010068 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
10069 kvm_queue_exception(vcpu, DB_VECTOR);
10070 else
10071 kvm_queue_exception(vcpu, BP_VECTOR);
10072 }
10073
Jan Kiszka91586a32009-10-05 13:07:21 +020010074 /*
10075 * Read rflags as long as potentially injected trace flags are still
10076 * filtered out.
10077 */
10078 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +020010079
10080 vcpu->guest_debug = dbg->control;
10081 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
10082 vcpu->guest_debug = 0;
10083
10084 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010085 for (i = 0; i < KVM_NR_DB_REGS; ++i)
10086 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +020010087 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010088 } else {
10089 for (i = 0; i < KVM_NR_DB_REGS; i++)
10090 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010091 }
Jan Kiszkac8639012012-09-21 05:42:55 +020010092 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010093
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010094 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10095 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
10096 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010097
Jan Kiszka91586a32009-10-05 13:07:21 +020010098 /*
10099 * Trigger an rflags update that will inject or remove the trace
10100 * flags.
10101 */
10102 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +010010103
Jason Baronb36464772021-01-14 22:27:56 -050010104 static_call(kvm_x86_update_exception_bitmap)(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +020010105
Jan Kiszka4f926bf22009-10-30 12:46:59 +010010106 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010107
Avi Kivity2122ff52010-05-13 11:25:04 +030010108out:
Christoffer Dall66b56562017-12-04 21:35:33 +010010109 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010110 return r;
10111}
10112
10113/*
Zhang Xiantao8b006792007-11-16 13:05:55 +080010114 * Translate a guest virtual address to a guest physical address.
10115 */
10116int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
10117 struct kvm_translation *tr)
10118{
10119 unsigned long vaddr = tr->linear_address;
10120 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -020010121 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +080010122
Christoffer Dall1da5b612017-12-04 21:35:32 +010010123 vcpu_load(vcpu);
10124
Marcelo Tosattif656ce02009-12-23 14:35:25 -020010125 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +020010126 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -020010127 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +080010128 tr->physical_address = gpa;
10129 tr->valid = gpa != UNMAPPED_GVA;
10130 tr->writeable = 1;
10131 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +080010132
Christoffer Dall1da5b612017-12-04 21:35:32 +010010133 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +080010134 return 0;
10135}
10136
Hollis Blanchardd0752062007-10-31 17:24:25 -050010137int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
10138{
Christoffer Dall13931232017-12-04 21:35:34 +010010139 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010140
Tom Lendackyed02b212020-12-10 11:10:01 -060010141 if (!vcpu->arch.guest_fpu)
10142 return 0;
10143
Christoffer Dall13931232017-12-04 21:35:34 +010010144 vcpu_load(vcpu);
10145
Marc Orrb666a4b2018-11-06 14:53:56 -080010146 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010147 memcpy(fpu->fpr, fxsave->st_space, 128);
10148 fpu->fcw = fxsave->cwd;
10149 fpu->fsw = fxsave->swd;
10150 fpu->ftwx = fxsave->twd;
10151 fpu->last_opcode = fxsave->fop;
10152 fpu->last_ip = fxsave->rip;
10153 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +000010154 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -050010155
Christoffer Dall13931232017-12-04 21:35:34 +010010156 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -050010157 return 0;
10158}
10159
10160int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
10161{
Christoffer Dall6a96bc72017-12-04 21:35:35 +010010162 struct fxregs_state *fxsave;
10163
Tom Lendackyed02b212020-12-10 11:10:01 -060010164 if (!vcpu->arch.guest_fpu)
10165 return 0;
10166
Christoffer Dall6a96bc72017-12-04 21:35:35 +010010167 vcpu_load(vcpu);
10168
Marc Orrb666a4b2018-11-06 14:53:56 -080010169 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010170
Hollis Blanchardd0752062007-10-31 17:24:25 -050010171 memcpy(fxsave->st_space, fpu->fpr, 128);
10172 fxsave->cwd = fpu->fcw;
10173 fxsave->swd = fpu->fsw;
10174 fxsave->twd = fpu->ftwx;
10175 fxsave->fop = fpu->last_opcode;
10176 fxsave->rip = fpu->last_ip;
10177 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +000010178 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -050010179
Christoffer Dall6a96bc72017-12-04 21:35:35 +010010180 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -050010181 return 0;
10182}
10183
Ken Hofsass01643c52018-01-31 16:03:36 -080010184static void store_regs(struct kvm_vcpu *vcpu)
10185{
10186 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
10187
10188 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
10189 __get_regs(vcpu, &vcpu->run->s.regs.regs);
10190
10191 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
10192 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
10193
10194 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
10195 kvm_vcpu_ioctl_x86_get_vcpu_events(
10196 vcpu, &vcpu->run->s.regs.events);
10197}
10198
10199static int sync_regs(struct kvm_vcpu *vcpu)
10200{
10201 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
10202 return -EINVAL;
10203
10204 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
10205 __set_regs(vcpu, &vcpu->run->s.regs.regs);
10206 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
10207 }
10208 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
10209 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
10210 return -EINVAL;
10211 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
10212 }
10213 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
10214 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
10215 vcpu, &vcpu->run->s.regs.events))
10216 return -EINVAL;
10217 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
10218 }
10219
10220 return 0;
10221}
10222
Ingo Molnar0ee6a512015-04-27 06:58:22 +020010223static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -050010224{
Tom Lendackyed02b212020-12-10 11:10:01 -060010225 if (!vcpu->arch.guest_fpu)
10226 return;
10227
Marc Orrb666a4b2018-11-06 14:53:56 -080010228 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +020010229 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -080010230 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +010010231 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010232
Dexuan Cui2acf9232010-06-10 11:27:12 +080010233 /*
10234 * Ensure guest xcr0 is valid for loading
10235 */
Dave Hansend91cab72015-09-02 16:31:26 -070010236 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +080010237
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010238 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010239}
Hollis Blanchardd0752062007-10-31 17:24:25 -050010240
Tom Lendackyed02b212020-12-10 11:10:01 -060010241void kvm_free_guest_fpu(struct kvm_vcpu *vcpu)
10242{
10243 if (vcpu->arch.guest_fpu) {
10244 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
10245 vcpu->arch.guest_fpu = NULL;
10246 }
10247}
10248EXPORT_SYMBOL_GPL(kvm_free_guest_fpu);
10249
Sean Christopherson897cc382019-12-18 13:55:09 -080010250int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010251{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010010252 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -080010253 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
10254 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +080010255
Sean Christopherson897cc382019-12-18 13:55:09 -080010256 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +020010257}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010258
Sean Christophersone529ef62019-12-18 13:55:15 -080010259int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +020010260{
Sean Christopherson95a0d012019-12-18 13:55:23 -080010261 struct page *page;
10262 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010263
Sean Christopherson95a0d012019-12-18 13:55:23 -080010264 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
10265 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
10266 else
10267 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
10268
10269 kvm_set_tsc_khz(vcpu, max_tsc_khz);
10270
10271 r = kvm_mmu_create(vcpu);
10272 if (r < 0)
10273 return r;
10274
10275 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -080010276 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
10277 if (r < 0)
10278 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -060010279 if (kvm_apicv_activated(vcpu->kvm))
10280 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010281 } else
Cun Li6e4e3b42021-01-11 23:24:35 +080010282 static_branch_inc(&kvm_has_noapic_vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010283
10284 r = -ENOMEM;
10285
Shakeel Butt93bb59c2020-12-18 14:01:38 -080010286 page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010287 if (!page)
10288 goto fail_free_lapic;
10289 vcpu->arch.pio_data = page_address(page);
10290
10291 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
10292 GFP_KERNEL_ACCOUNT);
10293 if (!vcpu->arch.mce_banks)
10294 goto fail_free_pio_data;
10295 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
10296
10297 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
10298 GFP_KERNEL_ACCOUNT))
10299 goto fail_free_mce_banks;
10300
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010301 if (!alloc_emulate_ctxt(vcpu))
10302 goto free_wbinvd_dirty_mask;
10303
Sean Christopherson95a0d012019-12-18 13:55:23 -080010304 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
10305 GFP_KERNEL_ACCOUNT);
10306 if (!vcpu->arch.user_fpu) {
10307 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010308 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010309 }
10310
10311 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
10312 GFP_KERNEL_ACCOUNT);
10313 if (!vcpu->arch.guest_fpu) {
10314 pr_err("kvm: failed to allocate vcpu's fpu\n");
10315 goto free_user_fpu;
10316 }
10317 fx_init(vcpu);
10318
Sean Christopherson95a0d012019-12-18 13:55:23 -080010319 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Sean Christophersona8ac8642021-02-03 16:01:15 -080010320 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010321
10322 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
10323
10324 kvm_async_pf_hash_reset(vcpu);
10325 kvm_pmu_init(vcpu);
10326
10327 vcpu->arch.pending_external_vector = -1;
10328 vcpu->arch.preempted_in_kernel = false;
10329
Jason Baronb36464772021-01-14 22:27:56 -050010330 r = static_call(kvm_x86_vcpu_create)(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010331 if (r)
10332 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -080010333
Sean Christopherson0cf91352019-03-07 15:43:02 -080010334 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -070010335 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +080010336 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +010010337 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010338 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +020010339 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010340 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +010010341 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010342
10343free_guest_fpu:
Tom Lendackyed02b212020-12-10 11:10:01 -060010344 kvm_free_guest_fpu(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010345free_user_fpu:
10346 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010347free_emulate_ctxt:
10348 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010349free_wbinvd_dirty_mask:
10350 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
10351fail_free_mce_banks:
10352 kfree(vcpu->arch.mce_banks);
10353fail_free_pio_data:
10354 free_page((unsigned long)vcpu->arch.pio_data);
10355fail_free_lapic:
10356 kvm_free_lapic(vcpu);
10357fail_mmu_destroy:
10358 kvm_mmu_destroy(vcpu);
10359 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010360}
10361
Dominik Dingel31928aa2014-12-04 15:47:07 +010010362void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010363{
Andrew Jones332967a2014-02-28 12:52:55 +010010364 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010365
Christoffer Dallec7660c2017-12-04 21:35:23 +010010366 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +010010367 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +010010368 vcpu_load(vcpu);
Paolo Bonzini0c899c22020-09-24 14:45:27 +020010369 kvm_synchronize_tsc(vcpu, 0);
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010370 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010371
10372 /* poll control enabled by default */
10373 vcpu->arch.msr_kvm_poll_control = 1;
10374
Christoffer Dallec7660c2017-12-04 21:35:23 +010010375 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010376
Wanpeng Lib34de572020-02-28 11:18:41 +080010377 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
10378 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
10379 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010380}
10381
Hollis Blanchardd40ccc62007-11-19 14:04:43 -060010382void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010383{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +010010384 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010385 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +020010386
Paolo Bonzini4cbc4182020-01-30 18:47:38 +010010387 kvm_release_pfn(cache->pfn, cache->dirty, cache);
10388
Sean Christopherson50b143e2019-12-18 13:55:07 -080010389 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010390
Jason Baronb36464772021-01-14 22:27:56 -050010391 static_call(kvm_x86_vcpu_free)(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -080010392
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010393 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -080010394 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
10395 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Tom Lendackyed02b212020-12-10 11:10:01 -060010396 kvm_free_guest_fpu(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010397
10398 kvm_hv_vcpu_uninit(vcpu);
10399 kvm_pmu_destroy(vcpu);
10400 kfree(vcpu->arch.mce_banks);
10401 kvm_free_lapic(vcpu);
10402 idx = srcu_read_lock(&vcpu->kvm->srcu);
10403 kvm_mmu_destroy(vcpu);
10404 srcu_read_unlock(&vcpu->kvm->srcu, idx);
10405 free_page((unsigned long)vcpu->arch.pio_data);
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +020010406 kvfree(vcpu->arch.cpuid_entries);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010407 if (!lapic_in_kernel(vcpu))
Cun Li6e4e3b42021-01-11 23:24:35 +080010408 static_branch_dec(&kvm_has_noapic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010409}
10410
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010411void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010412{
Radim Krčmářb7e31be2018-03-01 15:24:25 +010010413 kvm_lapic_reset(vcpu, init_event);
10414
Paolo Bonzinie69fab52015-06-04 10:44:44 +020010415 vcpu->arch.hflags = 0;
10416
Paolo Bonzinic43203c2016-06-01 22:26:00 +020010417 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +020010418 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +030010419 atomic_set(&vcpu->arch.nmi_queued, 0);
10420 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +020010421 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +030010422 kvm_clear_interrupt_queue(vcpu);
10423 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +020010424
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010425 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +030010426 kvm_update_dr0123(vcpu);
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +080010427 vcpu->arch.dr6 = DR6_ACTIVE_LOW;
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010428 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +020010429 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010430
Nadav Amit11190222015-04-02 03:10:38 +030010431 vcpu->arch.cr2 = 0;
10432
Avi Kivity3842d132010-07-27 12:30:24 +030010433 kvm_make_request(KVM_REQ_EVENT, vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010434 vcpu->arch.apf.msr_en_val = 0;
10435 vcpu->arch.apf.msr_int_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -040010436 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +030010437
Glauber Costa12f9a482011-02-01 14:16:40 -050010438 kvmclock_reset(vcpu);
10439
Gleb Natapovaf585b92010-10-14 11:22:46 +020010440 kvm_clear_async_pf_completion_queue(vcpu);
10441 kvm_async_pf_hash_reset(vcpu);
10442 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010443
Tom Lendackyed02b212020-12-10 11:10:01 -060010444 if (vcpu->arch.guest_fpu && kvm_mpx_supported()) {
Wanpeng Lia554d202017-10-11 05:10:19 -070010445 void *mpx_state_buffer;
10446
10447 /*
10448 * To avoid have the INIT path from kvm_apic_has_events() that be
10449 * called with loaded FPU and does not let userspace fix the state.
10450 */
Rik van Rielf775b132017-11-14 16:54:23 -050010451 if (init_event)
10452 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -080010453 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +020010454 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -070010455 if (mpx_state_buffer)
10456 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -080010457 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +020010458 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -070010459 if (mpx_state_buffer)
10460 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -050010461 if (init_event)
10462 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -070010463 }
10464
Paolo Bonzini64d60672015-05-07 11:36:11 +020010465 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010466 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +020010467 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -070010468
Kyle Hueydb2336a2017-03-20 01:16:28 -070010469 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -070010470
10471 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +020010472 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020010473
Julian Stecklina66f7b722012-12-05 15:26:19 +010010474 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
10475 vcpu->arch.regs_avail = ~0;
10476 vcpu->arch.regs_dirty = ~0;
10477
Wanpeng Lia554d202017-10-11 05:10:19 -070010478 vcpu->arch.ia32_xss = 0;
10479
Jason Baronb36464772021-01-14 22:27:56 -050010480 static_call(kvm_x86_vcpu_reset)(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010481}
10482
Paolo Bonzini2b4a2732014-11-24 14:35:24 +010010483void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +010010484{
10485 struct kvm_segment cs;
10486
10487 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
10488 cs.selector = vector << 8;
10489 cs.base = vector << 12;
10490 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
10491 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010492}
Tom Lendacky647daca2021-01-04 14:20:01 -060010493EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010494
Radim Krčmář13a34e02014-08-28 15:13:03 +020010495int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010496{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -100010497 struct kvm *kvm;
10498 struct kvm_vcpu *vcpu;
10499 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010500 int ret;
10501 u64 local_tsc;
10502 u64 max_tsc = 0;
10503 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +030010504
Sean Christopherson7e34fbd2020-09-23 11:03:55 -070010505 kvm_user_return_msr_cpu_online();
Jason Baronb36464772021-01-14 22:27:56 -050010506 ret = static_call(kvm_x86_hardware_enable)();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010507 if (ret != 0)
10508 return ret;
10509
Andy Lutomirski4ea16362015-06-25 18:44:07 +020010510 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010010511 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010512 list_for_each_entry(kvm, &vm_list, vm_list) {
10513 kvm_for_each_vcpu(i, vcpu, kvm) {
10514 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +080010515 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010516 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
10517 backwards_tsc = true;
10518 if (vcpu->arch.last_host_tsc > max_tsc)
10519 max_tsc = vcpu->arch.last_host_tsc;
10520 }
10521 }
10522 }
10523
10524 /*
10525 * Sometimes, even reliable TSCs go backwards. This happens on
10526 * platforms that reset TSC during suspend or hibernate actions, but
10527 * maintain synchronization. We must compensate. Fortunately, we can
10528 * detect that condition here, which happens early in CPU bringup,
10529 * before any KVM threads can be running. Unfortunately, we can't
10530 * bring the TSCs fully up to date with real time, as we aren't yet far
10531 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020010532 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010533 * variables that haven't been updated yet.
10534 *
10535 * So we simply find the maximum observed TSC above, then record the
10536 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
10537 * the adjustment will be applied. Note that we accumulate
10538 * adjustments, in case multiple suspend cycles happen before some VCPU
10539 * gets a chance to run again. In the event that no KVM threads get a
10540 * chance to run, we will miss the entire elapsed period, as we'll have
10541 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
10542 * loose cycle time. This isn't too big a deal, since the loss will be
10543 * uniform across all VCPUs (not to mention the scenario is extremely
10544 * unlikely). It is possible that a second hibernate recovery happens
10545 * much faster than a first, causing the observed TSC here to be
10546 * smaller; this would require additional padding adjustment, which is
10547 * why we set last_host_tsc to the local tsc observed here.
10548 *
10549 * N.B. - this code below runs only on platforms with reliable TSC,
10550 * as that is the only way backwards_tsc is set above. Also note
10551 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
10552 * have the same delta_cyc adjustment applied if backwards_tsc
10553 * is detected. Note further, this adjustment is only done once,
10554 * as we reset last_host_tsc on all VCPUs to stop this from being
10555 * called multiple times (one for each physical CPU bringup).
10556 *
Guo Chao4a969982012-06-28 15:17:27 +080010557 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010558 * will be compensated by the logic in vcpu_load, which sets the TSC to
10559 * catchup mode. This will catchup all VCPUs to real time, but cannot
10560 * guarantee that they stay in perfect synchronization.
10561 */
10562 if (backwards_tsc) {
10563 u64 delta_cyc = max_tsc - local_tsc;
10564 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +020010565 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010566 kvm_for_each_vcpu(i, vcpu, kvm) {
10567 vcpu->arch.tsc_offset_adjustment += delta_cyc;
10568 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +080010569 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010570 }
10571
10572 /*
10573 * We have to disable TSC offset matching.. if you were
10574 * booting a VM while issuing an S4 host suspend....
10575 * you may have some problem. Solving this issue is
10576 * left as an exercise to the reader.
10577 */
10578 kvm->arch.last_tsc_nsec = 0;
10579 kvm->arch.last_tsc_write = 0;
10580 }
10581
10582 }
10583 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010584}
10585
Radim Krčmář13a34e02014-08-28 15:13:03 +020010586void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010587{
Jason Baronb36464772021-01-14 22:27:56 -050010588 static_call(kvm_x86_hardware_disable)();
Radim Krčmář13a34e02014-08-28 15:13:03 +020010589 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010590}
10591
Sean Christophersonb9904082020-03-21 13:25:55 -070010592int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010593{
Sean Christophersond008dfd2020-03-21 13:25:56 -070010594 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030010595 int r;
10596
Sean Christopherson91661982020-03-02 15:57:06 -080010597 rdmsrl_safe(MSR_EFER, &host_efer);
10598
Paolo Bonzini408e9a32020-03-05 16:11:56 +010010599 if (boot_cpu_has(X86_FEATURE_XSAVES))
10600 rdmsrl(MSR_IA32_XSS, host_xss);
10601
Sean Christophersond008dfd2020-03-21 13:25:56 -070010602 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030010603 if (r != 0)
10604 return r;
10605
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010606 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
Jason Baronb36464772021-01-14 22:27:56 -050010607 kvm_ops_static_call_update();
Sean Christopherson69c6f692020-03-21 13:25:59 -070010608
Paolo Bonzini408e9a32020-03-05 16:11:56 +010010609 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
10610 supported_xss = 0;
10611
Paolo Bonzini139f7422020-05-05 09:40:46 -040010612#define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f)
10613 cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_);
10614#undef __kvm_cpu_cap_has
Sean Christophersonb11306b2019-12-10 14:44:13 -080010615
Haozhong Zhang35181e82015-10-20 15:39:03 +080010616 if (kvm_has_tsc_control) {
10617 /*
10618 * Make sure the user can only configure tsc_khz values that
10619 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -030010620 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +080010621 * be 1 on all machines.
10622 */
10623 u64 max = min(0x7fffffffULL,
10624 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
10625 kvm_max_guest_tsc_khz = max;
10626
Haozhong Zhangad7218832015-10-20 15:39:02 +080010627 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +080010628 }
Haozhong Zhangad7218832015-10-20 15:39:02 +080010629
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030010630 kvm_init_msr_list();
10631 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010632}
10633
10634void kvm_arch_hardware_unsetup(void)
10635{
Jason Baronb36464772021-01-14 22:27:56 -050010636 static_call(kvm_x86_hardware_unsetup)();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010637}
10638
Sean Christophersonb9904082020-03-21 13:25:55 -070010639int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010640{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080010641 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -070010642 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080010643
10644 WARN_ON(!irqs_disabled());
10645
Paolo Bonzini139f7422020-05-05 09:40:46 -040010646 if (__cr4_reserved_bits(cpu_has, c) !=
10647 __cr4_reserved_bits(cpu_has, &boot_cpu_data))
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080010648 return -EIO;
10649
Sean Christophersond008dfd2020-03-21 13:25:56 -070010650 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010651}
10652
Paolo Bonzinid71ba782015-07-29 11:56:48 +020010653bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
10654{
10655 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
10656}
10657EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
10658
10659bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
10660{
10661 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
10662}
10663
Cun Li6e4e3b42021-01-11 23:24:35 +080010664__read_mostly DEFINE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu);
10665EXPORT_SYMBOL_GPL(kvm_has_noapic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +030010666
Radim Krčmáře790d9e2014-08-21 18:08:05 +020010667void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
10668{
Like Xub35e5542019-10-27 18:52:43 +080010669 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
10670
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010671 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +080010672 if (pmu->version && unlikely(pmu->event_count)) {
10673 pmu->need_cleanup = true;
10674 kvm_make_request(KVM_REQ_PMU, vcpu);
10675 }
Jason Baronb36464772021-01-14 22:27:56 -050010676 static_call(kvm_x86_sched_in)(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +020010677}
10678
Sean Christopherson562b6b02020-01-26 16:41:13 -080010679void kvm_arch_free_vm(struct kvm *kvm)
10680{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +010010681 kfree(to_kvm_hv(kvm)->hv_pa_pg);
Sean Christopherson562b6b02020-01-26 16:41:13 -080010682 vfree(kvm);
10683}
10684
10685
Carsten Ottee08b9632012-01-04 10:25:20 +010010686int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010687{
Carsten Ottee08b9632012-01-04 10:25:20 +010010688 if (type)
10689 return -EINVAL;
10690
Paolo Bonzini6ef768f2014-11-20 13:45:31 +010010691 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +080010692 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -070010693 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +010010694 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030010695 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010696 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010697
Sheng Yang5550af42008-10-15 20:15:06 +080010698 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
10699 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -060010700 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
10701 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
10702 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +080010703
Jan Kiszka038f8c12011-02-04 10:49:11 +010010704 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +030010705 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -020010706 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
10707
Paolo Bonzini8171cd62020-01-22 14:36:09 +010010708 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -020010709 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +010010710
Drew Schmitt6fbbde92018-08-20 10:32:15 -070010711 kvm->arch.guest_can_read_msr_platform_info = true;
10712
Andrew Jones7e44e442014-02-28 12:52:54 +010010713 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +010010714 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +010010715
Roman Kagancbc02362018-02-01 16:48:31 +030010716 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +080010717 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +080010718 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +080010719
Jason Baronb36464772021-01-14 22:27:56 -050010720 return static_call(kvm_x86_vm_init)(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010721}
10722
Junaid Shahid1aa9b952019-11-04 20:26:00 +010010723int kvm_arch_post_init_vm(struct kvm *kvm)
10724{
10725 return kvm_mmu_post_init_vm(kvm);
10726}
10727
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010728static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
10729{
Christoffer Dallec7660c2017-12-04 21:35:23 +010010730 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010731 kvm_mmu_unload(vcpu);
10732 vcpu_put(vcpu);
10733}
10734
10735static void kvm_free_vcpus(struct kvm *kvm)
10736{
10737 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010738 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010739
10740 /*
10741 * Unpin any mmu pages first.
10742 */
Gleb Natapovaf585b92010-10-14 11:22:46 +020010743 kvm_for_each_vcpu(i, vcpu, kvm) {
10744 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010745 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010746 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010747 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -080010748 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010749
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010750 mutex_lock(&kvm->lock);
10751 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
10752 kvm->vcpus[i] = NULL;
10753
10754 atomic_set(&kvm->online_vcpus, 0);
10755 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010756}
10757
Sheng Yangad8ba2c2009-01-06 10:03:02 +080010758void kvm_arch_sync_events(struct kvm *kvm)
10759{
Andrew Jones332967a2014-02-28 12:52:55 +010010760 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +010010761 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +080010762 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +080010763}
10764
Peter Xuff5a9832020-09-30 21:20:33 -040010765#define ERR_PTR_USR(e) ((void __user *)ERR_PTR(e))
10766
10767/**
10768 * __x86_set_memory_region: Setup KVM internal memory slot
10769 *
10770 * @kvm: the kvm pointer to the VM.
10771 * @id: the slot ID to setup.
10772 * @gpa: the GPA to install the slot (unused when @size == 0).
10773 * @size: the size of the slot. Set to zero to uninstall a slot.
10774 *
10775 * This function helps to setup a KVM internal memory slot. Specify
10776 * @size > 0 to install a new slot, while @size == 0 to uninstall a
10777 * slot. The return code can be one of the following:
10778 *
10779 * HVA: on success (uninstall will return a bogus HVA)
10780 * -errno: on error
10781 *
10782 * The caller should always use IS_ERR() to check the return value
10783 * before use. Note, the KVM internal memory slots are guaranteed to
10784 * remain valid and unchanged until the VM is destroyed, i.e., the
10785 * GPA->HVA translation will not change. However, the HVA is a user
10786 * address, i.e. its accessibility is not guaranteed, and must be
10787 * accessed via __copy_{to,from}_user().
10788 */
10789void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
10790 u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010791{
10792 int i, r;
Kees Cook3f649ab2020-06-03 13:09:38 -070010793 unsigned long hva, old_npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010794 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010795 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010796
10797 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010798 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
Peter Xuff5a9832020-09-30 21:20:33 -040010799 return ERR_PTR_USR(-EINVAL);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010800
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010801 slot = id_to_memslot(slots, id);
10802 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010803 if (slot && slot->npages)
Peter Xuff5a9832020-09-30 21:20:33 -040010804 return ERR_PTR_USR(-EEXIST);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010805
10806 /*
10807 * MAP_SHARED to prevent internal slot pages from being moved
10808 * by fork()/COW.
10809 */
10810 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
10811 MAP_SHARED | MAP_ANONYMOUS, 0);
10812 if (IS_ERR((void *)hva))
Peter Xuff5a9832020-09-30 21:20:33 -040010813 return (void __user *)hva;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010814 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010815 if (!slot || !slot->npages)
Muhammad Usama Anjum46914532021-03-05 23:08:16 +050010816 return NULL;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010817
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010818 old_npages = slot->npages;
Zheng Zhan Liangb66f9ba2021-02-01 13:53:10 +080010819 hva = slot->userspace_addr;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010820 }
10821
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010822 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010823 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010824
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010825 m.slot = id | (i << 16);
10826 m.flags = 0;
10827 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010828 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010829 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010830 r = __kvm_set_memory_region(kvm, &m);
10831 if (r < 0)
Peter Xuff5a9832020-09-30 21:20:33 -040010832 return ERR_PTR_USR(r);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010833 }
10834
Eric Biggers103c7632018-01-31 17:30:21 -080010835 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010836 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010837
Peter Xuff5a9832020-09-30 21:20:33 -040010838 return (void __user *)hva;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010839}
10840EXPORT_SYMBOL_GPL(__x86_set_memory_region);
10841
Junaid Shahid1aa9b952019-11-04 20:26:00 +010010842void kvm_arch_pre_destroy_vm(struct kvm *kvm)
10843{
10844 kvm_mmu_pre_destroy_vm(kvm);
10845}
10846
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010847void kvm_arch_destroy_vm(struct kvm *kvm)
10848{
Andrew Honig27469d22013-04-18 09:38:14 -070010849 if (current->mm == kvm->mm) {
10850 /*
10851 * Free memory regions allocated on behalf of userspace,
10852 * unless the the memory map has changed due to process exit
10853 * or fd copying.
10854 */
Peter Xu6a3c6232020-01-09 09:57:16 -050010855 mutex_lock(&kvm->slots_lock);
10856 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
10857 0, 0);
10858 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
10859 0, 0);
10860 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
10861 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -070010862 }
Jason Baronb36464772021-01-14 22:27:56 -050010863 static_call_cond(kvm_x86_vm_destroy)(kvm);
Sean Christophersonb318e8d2021-03-16 11:44:33 -070010864 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1));
Peter Xuc7611592017-03-15 16:01:19 +080010865 kvm_pic_destroy(kvm);
10866 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010867 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +020010868 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -070010869 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +080010870 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +020010871 kvm_page_track_cleanup(kvm);
David Woodhouse7d6bbeb2021-02-02 15:48:05 +000010872 kvm_xen_destroy_vm(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +030010873 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010874}
Zhang Xiantao0de10342007-11-20 16:25:04 +080010875
Sean Christophersone96c81e2020-02-18 13:07:27 -080010876void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010877{
10878 int i;
10879
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010880 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -080010881 kvfree(slot->arch.rmap[i]);
10882 slot->arch.rmap[i] = NULL;
10883
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010884 if (i == 0)
10885 continue;
10886
Sean Christophersone96c81e2020-02-18 13:07:27 -080010887 kvfree(slot->arch.lpage_info[i - 1]);
10888 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010889 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080010890
Sean Christophersone96c81e2020-02-18 13:07:27 -080010891 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010892}
10893
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010894static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
10895 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010896{
10897 int i;
10898
Sean Christophersonedd4fa32020-02-18 13:07:15 -080010899 /*
10900 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
10901 * old arrays will be freed by __kvm_set_memory_region() if installing
10902 * the new memslot is successful.
10903 */
10904 memset(&slot->arch, 0, sizeof(slot->arch));
10905
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010906 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010907 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010908 unsigned long ugfn;
10909 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010910 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010911
10912 lpages = gfn_to_index(slot->base_gfn + npages - 1,
10913 slot->base_gfn, level) + 1;
10914
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010915 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -070010916 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -080010917 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010918 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010919 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010920 if (i == 0)
10921 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010922
Ben Gardon254272c2019-02-11 11:02:50 -080010923 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010924 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010925 goto out_free;
10926
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010927 slot->arch.lpage_info[i - 1] = linfo;
10928
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010929 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010930 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010931 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010932 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010933 ugfn = slot->userspace_addr >> PAGE_SHIFT;
10934 /*
10935 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -080010936 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010937 */
Sean Christopherson600087b2020-03-02 15:57:05 -080010938 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010939 unsigned long j;
10940
10941 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010942 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010943 }
10944 }
10945
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080010946 if (kvm_page_track_create_memslot(slot, npages))
10947 goto out_free;
10948
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010949 return 0;
10950
10951out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010952 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +010010953 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010954 slot->arch.rmap[i] = NULL;
10955 if (i == 0)
10956 continue;
10957
Thomas Huth548ef282015-02-24 21:29:25 +010010958 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010959 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010960 }
10961 return -ENOMEM;
10962}
10963
Sean Christopherson15248252019-02-05 12:54:17 -080010964void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010965{
Boris Ostrovsky91724812019-12-05 01:30:51 +000010966 struct kvm_vcpu *vcpu;
10967 int i;
10968
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +090010969 /*
10970 * memslots->generation has been incremented.
10971 * mmio generation may have reached its maximum value.
10972 */
Sean Christopherson15248252019-02-05 12:54:17 -080010973 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +000010974
10975 /* Force re-initialization of steal_time cache */
10976 kvm_for_each_vcpu(i, vcpu, kvm)
10977 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010978}
10979
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010980int kvm_arch_prepare_memory_region(struct kvm *kvm,
10981 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010982 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +090010983 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +080010984{
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010985 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
10986 return kvm_alloc_memslot_metadata(memslot,
10987 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010988 return 0;
10989}
10990
Makarand Sonarea85863c2021-02-12 16:50:12 -080010991
10992static void kvm_mmu_update_cpu_dirty_logging(struct kvm *kvm, bool enable)
10993{
10994 struct kvm_arch *ka = &kvm->arch;
10995
10996 if (!kvm_x86_ops.cpu_dirty_log_size)
10997 return;
10998
10999 if ((enable && ++ka->cpu_dirty_logging_count == 1) ||
11000 (!enable && --ka->cpu_dirty_logging_count == 0))
11001 kvm_make_all_cpus_request(kvm, KVM_REQ_UPDATE_CPU_DIRTY_LOGGING);
11002
11003 WARN_ON_ONCE(ka->cpu_dirty_logging_count < 0);
11004}
11005
Kai Huang88178fd2015-01-28 10:54:27 +080011006static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
Anthony Yznaga37416792020-06-02 13:07:30 -070011007 struct kvm_memory_slot *old,
11008 struct kvm_memory_slot *new,
11009 enum kvm_mr_change change)
Kai Huang88178fd2015-01-28 10:54:27 +080011010{
Makarand Sonarea85863c2021-02-12 16:50:12 -080011011 bool log_dirty_pages = new->flags & KVM_MEM_LOG_DIRTY_PAGES;
11012
Anthony Yznaga37416792020-06-02 13:07:30 -070011013 /*
Makarand Sonarea85863c2021-02-12 16:50:12 -080011014 * Update CPU dirty logging if dirty logging is being toggled. This
11015 * applies to all operations.
11016 */
11017 if ((old->flags ^ new->flags) & KVM_MEM_LOG_DIRTY_PAGES)
11018 kvm_mmu_update_cpu_dirty_logging(kvm, log_dirty_pages);
11019
11020 /*
11021 * Nothing more to do for RO slots (which can't be dirtied and can't be
Sean Christophersonb6e16ae2021-02-12 16:50:13 -080011022 * made writable) or CREATE/MOVE/DELETE of a slot.
11023 *
11024 * For a memslot with dirty logging disabled:
11025 * CREATE: No dirty mappings will already exist.
11026 * MOVE/DELETE: The old mappings will already have been cleaned up by
11027 * kvm_arch_flush_shadow_memslot()
11028 *
11029 * For a memslot with dirty logging enabled:
11030 * CREATE: No shadow pages exist, thus nothing to write-protect
11031 * and no dirty bits to clear.
11032 * MOVE/DELETE: The old mappings will already have been cleaned up by
11033 * kvm_arch_flush_shadow_memslot().
Anthony Yznaga37416792020-06-02 13:07:30 -070011034 */
11035 if ((change != KVM_MR_FLAGS_ONLY) || (new->flags & KVM_MEM_READONLY))
Kai Huang88178fd2015-01-28 10:54:27 +080011036 return;
Kai Huang88178fd2015-01-28 10:54:27 +080011037
11038 /*
Sean Christopherson52f460792021-02-12 16:50:11 -080011039 * READONLY and non-flags changes were filtered out above, and the only
11040 * other flag is LOG_DIRTY_PAGES, i.e. something is wrong if dirty
11041 * logging isn't being toggled on or off.
Anthony Yznaga37416792020-06-02 13:07:30 -070011042 */
Sean Christopherson52f460792021-02-12 16:50:11 -080011043 if (WARN_ON_ONCE(!((old->flags ^ new->flags) & KVM_MEM_LOG_DIRTY_PAGES)))
11044 return;
11045
Sean Christophersonb6e16ae2021-02-12 16:50:13 -080011046 if (!log_dirty_pages) {
11047 /*
11048 * Dirty logging tracks sptes in 4k granularity, meaning that
11049 * large sptes have to be split. If live migration succeeds,
11050 * the guest in the source machine will be destroyed and large
11051 * sptes will be created in the destination. However, if the
11052 * guest continues to run in the source machine (for example if
11053 * live migration fails), small sptes will remain around and
11054 * cause bad performance.
11055 *
11056 * Scan sptes if dirty logging has been stopped, dropping those
11057 * which can be collapsed into a single large-page spte. Later
11058 * page faults will create the large-page sptes.
11059 */
Anthony Yznaga37416792020-06-02 13:07:30 -070011060 kvm_mmu_zap_collapsible_sptes(kvm, new);
Sean Christophersonb6e16ae2021-02-12 16:50:13 -080011061 } else {
Sean Christophersona1419f82021-02-12 16:50:14 -080011062 /* By default, write-protect everything to log writes. */
11063 int level = PG_LEVEL_4K;
Anthony Yznaga37416792020-06-02 13:07:30 -070011064
Sean Christophersona018eba2021-02-12 16:50:10 -080011065 if (kvm_x86_ops.cpu_dirty_log_size) {
Sean Christophersona1419f82021-02-12 16:50:14 -080011066 /*
11067 * Clear all dirty bits, unless pages are treated as
11068 * dirty from the get-go.
11069 */
Sean Christophersona018eba2021-02-12 16:50:10 -080011070 if (!kvm_dirty_log_manual_protect_and_init_set(kvm))
11071 kvm_mmu_slot_leaf_clear_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080011072
11073 /*
Sean Christophersona1419f82021-02-12 16:50:14 -080011074 * Write-protect large pages on write so that dirty
11075 * logging happens at 4k granularity. No need to
11076 * write-protect small SPTEs since write accesses are
11077 * logged by the CPU via dirty bits.
11078 */
11079 level = PG_LEVEL_2M;
11080 } else if (kvm_dirty_log_manual_protect_and_init_set(kvm)) {
11081 /*
Jay Zhou3c9bd402020-02-27 09:32:27 +080011082 * If we're with initial-all-set, we don't need
11083 * to write protect any small page because
11084 * they're reported as dirty already. However
11085 * we still need to write-protect huge pages
11086 * so that the page split can happen lazily on
11087 * the first write to the huge page.
11088 */
Sean Christophersona1419f82021-02-12 16:50:14 -080011089 level = PG_LEVEL_2M;
Jay Zhou3c9bd402020-02-27 09:32:27 +080011090 }
Sean Christophersona1419f82021-02-12 16:50:14 -080011091 kvm_mmu_slot_remove_write_access(kvm, new, level);
Kai Huang88178fd2015-01-28 10:54:27 +080011092 }
11093}
11094
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011095void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020011096 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080011097 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020011098 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090011099 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011100{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080011101 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080011102 kvm_mmu_change_mmu_pages(kvm,
11103 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080011104
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090011105 /*
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020011106 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090011107 */
Anthony Yznaga37416792020-06-02 13:07:30 -070011108 kvm_mmu_slot_apply_flags(kvm, old, (struct kvm_memory_slot *) new, change);
Sean Christopherson21198842020-02-18 13:07:25 -080011109
11110 /* Free the arrays associated with the old memslot. */
11111 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080011112 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080011113}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080011114
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030011115void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030011116{
Sean Christopherson7390de12019-02-05 13:01:31 -080011117 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030011118}
11119
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030011120void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
11121 struct kvm_memory_slot *slot)
11122{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080011123 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030011124}
11125
Liran Alone6c67d82018-09-04 10:56:52 +030011126static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
11127{
11128 return (is_guest_mode(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070011129 kvm_x86_ops.guest_apic_has_interrupt &&
Jason Baronb36464772021-01-14 22:27:56 -050011130 static_call(kvm_x86_guest_apic_has_interrupt)(vcpu));
Liran Alone6c67d82018-09-04 10:56:52 +030011131}
11132
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011133static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
11134{
11135 if (!list_empty_careful(&vcpu->async_pf.done))
11136 return true;
11137
11138 if (kvm_apic_has_events(vcpu))
11139 return true;
11140
11141 if (vcpu->arch.pv.pv_unhalted)
11142 return true;
11143
Wanpeng Lia5f01f82017-09-13 04:04:01 -070011144 if (vcpu->arch.exception.pending)
11145 return true;
11146
ZhuangYanying47a66ee2017-05-26 13:16:48 +080011147 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
11148 (vcpu->arch.nmi_pending &&
Jason Baronb36464772021-01-14 22:27:56 -050011149 static_call(kvm_x86_nmi_allowed)(vcpu, false)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011150 return true;
11151
ZhuangYanying47a66ee2017-05-26 13:16:48 +080011152 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -040011153 (vcpu->arch.smi_pending &&
Jason Baronb36464772021-01-14 22:27:56 -050011154 static_call(kvm_x86_smi_allowed)(vcpu, false)))
Paolo Bonzini73917732015-10-13 10:19:35 +020011155 return true;
11156
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011157 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030011158 (kvm_cpu_has_interrupt(vcpu) ||
11159 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011160 return true;
11161
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030011162 if (kvm_hv_has_stimer_pending(vcpu))
11163 return true;
11164
Sean Christophersond2060bd2020-04-22 19:25:39 -070011165 if (is_guest_mode(vcpu) &&
11166 kvm_x86_ops.nested_ops->hv_timer_pending &&
11167 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
11168 return true;
11169
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011170 return false;
11171}
11172
Zhang Xiantao1d737c82007-12-14 09:35:10 +080011173int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
11174{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011175 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080011176}
Zhang Xiantao57361992007-12-17 14:21:40 +080011177
Haiwei Li10dbdf92021-04-21 11:25:13 +080011178bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu)
11179{
11180 if (vcpu->arch.apicv_active && static_call(kvm_x86_dy_apicv_has_pending_interrupt)(vcpu))
11181 return true;
11182
11183 return false;
11184}
11185
Wanpeng Li17e433b2019-08-05 10:03:19 +080011186bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
11187{
11188 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
11189 return true;
11190
11191 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
11192 kvm_test_request(KVM_REQ_SMI, vcpu) ||
11193 kvm_test_request(KVM_REQ_EVENT, vcpu))
11194 return true;
11195
Haiwei Li10dbdf92021-04-21 11:25:13 +080011196 return kvm_arch_dy_has_pending_interrupt(vcpu);
Wanpeng Li17e433b2019-08-05 10:03:19 +080011197}
11198
Longpeng(Mike)199b5762017-08-08 12:05:32 +080011199bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
11200{
Wanpeng Lib86bb112021-04-22 16:34:19 +080011201 if (vcpu->arch.guest_state_protected)
11202 return true;
11203
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080011204 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080011205}
11206
Christoffer Dallb6d33832012-03-08 16:44:24 -050011207int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080011208{
Christoffer Dallb6d33832012-03-08 16:44:24 -050011209 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080011210}
Gleb Natapov78646122009-03-23 12:12:11 +020011211
11212int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
11213{
Jason Baronb36464772021-01-14 22:27:56 -050011214 return static_call(kvm_x86_interrupt_allowed)(vcpu, false);
Gleb Natapov78646122009-03-23 12:12:11 +020011215}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011216
Nadav Amit82b32772014-11-02 11:54:45 +020011217unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
11218{
Tom Lendacky7ed9abf2020-12-10 11:09:54 -060011219 /* Can't read the RIP when guest state is protected, just return 0 */
11220 if (vcpu->arch.guest_state_protected)
11221 return 0;
11222
Nadav Amit82b32772014-11-02 11:54:45 +020011223 if (is_64_bit_mode(vcpu))
11224 return kvm_rip_read(vcpu);
11225 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
11226 kvm_rip_read(vcpu));
11227}
11228EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
11229
Jan Kiszkaf92653e2010-02-23 17:47:55 +010011230bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
11231{
Nadav Amit82b32772014-11-02 11:54:45 +020011232 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010011233}
11234EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
11235
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011236unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
11237{
11238 unsigned long rflags;
11239
Jason Baronb36464772021-01-14 22:27:56 -050011240 rflags = static_call(kvm_x86_get_rflags)(vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011241 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010011242 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011243 return rflags;
11244}
11245EXPORT_SYMBOL_GPL(kvm_get_rflags);
11246
Paolo Bonzini6addfc42014-03-27 11:29:28 +010011247static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011248{
11249 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010011250 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010011251 rflags |= X86_EFLAGS_TF;
Jason Baronb36464772021-01-14 22:27:56 -050011252 static_call(kvm_x86_set_rflags)(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010011253}
11254
11255void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
11256{
11257 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030011258 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011259}
11260EXPORT_SYMBOL_GPL(kvm_set_rflags);
11261
Gleb Natapov56028d02010-10-17 18:13:42 +020011262void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
11263{
11264 int r;
11265
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011266 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080011267 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020011268 return;
11269
11270 r = kvm_mmu_reload(vcpu);
11271 if (unlikely(r))
11272 return;
11273
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011274 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080011275 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080011276 return;
11277
Sean Christopherson7a026742020-02-06 14:14:34 -080011278 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020011279}
11280
Gleb Natapovaf585b92010-10-14 11:22:46 +020011281static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
11282{
Peter Xudd03bca2020-04-16 11:58:59 -040011283 BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
11284
Gleb Natapovaf585b92010-10-14 11:22:46 +020011285 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
11286}
11287
11288static inline u32 kvm_async_pf_next_probe(u32 key)
11289{
Peter Xudd03bca2020-04-16 11:58:59 -040011290 return (key + 1) & (ASYNC_PF_PER_VCPU - 1);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011291}
11292
11293static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
11294{
11295 u32 key = kvm_async_pf_hash_fn(gfn);
11296
11297 while (vcpu->arch.apf.gfns[key] != ~0)
11298 key = kvm_async_pf_next_probe(key);
11299
11300 vcpu->arch.apf.gfns[key] = gfn;
11301}
11302
11303static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
11304{
11305 int i;
11306 u32 key = kvm_async_pf_hash_fn(gfn);
11307
Peter Xudd03bca2020-04-16 11:58:59 -040011308 for (i = 0; i < ASYNC_PF_PER_VCPU &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080011309 (vcpu->arch.apf.gfns[key] != gfn &&
11310 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020011311 key = kvm_async_pf_next_probe(key);
11312
11313 return key;
11314}
11315
11316bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
11317{
11318 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
11319}
11320
11321static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
11322{
11323 u32 i, j, k;
11324
11325 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
Peter Xu0fd46042020-04-16 11:59:10 -040011326
11327 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn))
11328 return;
11329
Gleb Natapovaf585b92010-10-14 11:22:46 +020011330 while (true) {
11331 vcpu->arch.apf.gfns[i] = ~0;
11332 do {
11333 j = kvm_async_pf_next_probe(j);
11334 if (vcpu->arch.apf.gfns[j] == ~0)
11335 return;
11336 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
11337 /*
11338 * k lies cyclically in ]i,j]
11339 * | i.k.j |
11340 * |....j i.k.| or |.k..j i...|
11341 */
11342 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
11343 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
11344 i = j;
11345 }
11346}
11347
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020011348static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020011349{
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020011350 u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT;
11351
11352 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason,
11353 sizeof(reason));
11354}
11355
11356static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token)
11357{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011358 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
Paolo Bonzini4e335d92017-05-02 16:20:18 +020011359
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011360 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
11361 &token, offset, sizeof(token));
11362}
11363
11364static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu)
11365{
11366 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
11367 u32 val;
11368
11369 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
11370 &val, offset, sizeof(val)))
11371 return false;
11372
11373 return !val;
Gleb Natapov7c907052010-10-14 11:22:53 +020011374}
11375
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011376static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
11377{
11378 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
11379 return false;
11380
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011381 if (!kvm_pv_async_pf_enabled(vcpu) ||
Jason Baronb36464772021-01-14 22:27:56 -050011382 (vcpu->arch.apf.send_user_only && static_call(kvm_x86_get_cpl)(vcpu) == 0))
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011383 return false;
11384
11385 return true;
11386}
11387
11388bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
11389{
11390 if (unlikely(!lapic_in_kernel(vcpu) ||
11391 kvm_event_needs_reinjection(vcpu) ||
11392 vcpu->arch.exception.pending))
11393 return false;
11394
11395 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
11396 return false;
11397
11398 /*
11399 * If interrupts are off we cannot even use an artificial
11400 * halt state.
11401 */
Paolo Bonzinic300ab92020-04-23 14:08:58 -040011402 return kvm_arch_interrupt_allowed(vcpu);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011403}
11404
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011405bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
Gleb Natapovaf585b92010-10-14 11:22:46 +020011406 struct kvm_async_pf *work)
11407{
Avi Kivity6389ee92010-11-29 16:12:30 +020011408 struct x86_exception fault;
11409
Sean Christopherson736c2912019-12-06 15:57:14 -080011410 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011411 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020011412
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011413 if (kvm_can_deliver_async_pf(vcpu) &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020011414 !apf_put_user_notpresent(vcpu)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020011415 fault.vector = PF_VECTOR;
11416 fault.error_code_valid = true;
11417 fault.error_code = 0;
11418 fault.nested_page_fault = false;
11419 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070011420 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020011421 kvm_inject_page_fault(vcpu, &fault);
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011422 return true;
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011423 } else {
11424 /*
11425 * It is not possible to deliver a paravirtualized asynchronous
11426 * page fault, but putting the guest in an artificial halt state
11427 * can be beneficial nevertheless: if an interrupt arrives, we
11428 * can deliver it timely and perhaps the guest will schedule
11429 * another process. When the instruction that triggered a page
11430 * fault is retried, hopefully the page will be ready in the host.
11431 */
11432 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011433 return false;
Gleb Natapov7c907052010-10-14 11:22:53 +020011434 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020011435}
11436
11437void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
11438 struct kvm_async_pf *work)
11439{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011440 struct kvm_lapic_irq irq = {
11441 .delivery_mode = APIC_DM_FIXED,
11442 .vector = vcpu->arch.apf.vec
11443 };
Avi Kivity6389ee92010-11-29 16:12:30 +020011444
chai wenf2e10662013-10-14 22:22:33 +080011445 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020011446 work->arch.token = ~0; /* broadcast wakeup */
11447 else
11448 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080011449 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020011450
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011451 if ((work->wakeup_all || work->notpresent_injected) &&
11452 kvm_pv_async_pf_enabled(vcpu) &&
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011453 !apf_put_user_ready(vcpu, work->arch.token)) {
11454 vcpu->arch.apf.pageready_pending = true;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011455 kvm_apic_set_irq(vcpu, &irq, NULL);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011456 }
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011457
Xiao Guangronge6d53e32010-11-01 17:01:28 +080011458 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030011459 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020011460}
11461
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011462void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu)
11463{
11464 kvm_make_request(KVM_REQ_APF_READY, vcpu);
11465 if (!vcpu->arch.apf.pageready_pending)
11466 kvm_vcpu_kick(vcpu);
11467}
11468
Vitaly Kuznetsov7c0ade62020-05-25 16:41:18 +020011469bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020011470{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011471 if (!kvm_pv_async_pf_enabled(vcpu))
Gleb Natapov7c907052010-10-14 11:22:53 +020011472 return true;
11473 else
Vitaly Kuznetsov2f15d022021-04-22 11:29:48 +020011474 return kvm_lapic_enabled(vcpu) && apf_pageready_slot_free(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011475}
11476
Paolo Bonzini5544eb92015-07-07 15:41:58 +020011477void kvm_arch_start_assignment(struct kvm *kvm)
11478{
11479 atomic_inc(&kvm->arch.assigned_device_count);
11480}
11481EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
11482
11483void kvm_arch_end_assignment(struct kvm *kvm)
11484{
11485 atomic_dec(&kvm->arch.assigned_device_count);
11486}
11487EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
11488
11489bool kvm_arch_has_assigned_device(struct kvm *kvm)
11490{
11491 return atomic_read(&kvm->arch.assigned_device_count);
11492}
11493EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
11494
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060011495void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
11496{
11497 atomic_inc(&kvm->arch.noncoherent_dma_count);
11498}
11499EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
11500
11501void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
11502{
11503 atomic_dec(&kvm->arch.noncoherent_dma_count);
11504}
11505EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
11506
11507bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
11508{
11509 return atomic_read(&kvm->arch.noncoherent_dma_count);
11510}
11511EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
11512
Alex Williamson14717e22016-05-05 11:58:35 -060011513bool kvm_arch_has_irq_bypass(void)
11514{
Sean Christopherson92735b12019-08-02 15:06:17 -070011515 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060011516}
11517
Feng Wu87276882015-09-18 22:29:40 +080011518int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
11519 struct irq_bypass_producer *prod)
11520{
11521 struct kvm_kernel_irqfd *irqfd =
11522 container_of(cons, struct kvm_kernel_irqfd, consumer);
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011523 int ret;
Feng Wu87276882015-09-18 22:29:40 +080011524
Alex Williamson14717e22016-05-05 11:58:35 -060011525 irqfd->producer = prod;
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011526 kvm_arch_start_assignment(irqfd->kvm);
Jason Baronb36464772021-01-14 22:27:56 -050011527 ret = static_call(kvm_x86_update_pi_irte)(irqfd->kvm,
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011528 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080011529
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011530 if (ret)
11531 kvm_arch_end_assignment(irqfd->kvm);
11532
11533 return ret;
Feng Wu87276882015-09-18 22:29:40 +080011534}
11535
11536void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
11537 struct irq_bypass_producer *prod)
11538{
11539 int ret;
11540 struct kvm_kernel_irqfd *irqfd =
11541 container_of(cons, struct kvm_kernel_irqfd, consumer);
11542
Feng Wu87276882015-09-18 22:29:40 +080011543 WARN_ON(irqfd->producer != prod);
11544 irqfd->producer = NULL;
11545
11546 /*
11547 * When producer of consumer is unregistered, we change back to
11548 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020011549 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080011550 * int this case doesn't want to receive the interrupts.
11551 */
Jason Baronb36464772021-01-14 22:27:56 -050011552 ret = static_call(kvm_x86_update_pi_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 0);
Feng Wu87276882015-09-18 22:29:40 +080011553 if (ret)
11554 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
11555 " fails: %d\n", irqfd->consumer.token, ret);
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011556
11557 kvm_arch_end_assignment(irqfd->kvm);
Feng Wu87276882015-09-18 22:29:40 +080011558}
11559
11560int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
11561 uint32_t guest_irq, bool set)
11562{
Jason Baronb36464772021-01-14 22:27:56 -050011563 return static_call(kvm_x86_update_pi_irte)(kvm, host_irq, guest_irq, set);
Feng Wu87276882015-09-18 22:29:40 +080011564}
11565
Feng Wu520040142016-01-25 16:53:33 +080011566bool kvm_vector_hashing_enabled(void)
11567{
11568 return vector_hashing;
11569}
Feng Wu520040142016-01-25 16:53:33 +080011570
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030011571bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
11572{
11573 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
11574}
11575EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
11576
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011577
11578int kvm_spec_ctrl_test_value(u64 value)
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011579{
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011580 /*
11581 * test that setting IA32_SPEC_CTRL to given value
11582 * is allowed by the host processor
11583 */
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011584
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011585 u64 saved_value;
11586 unsigned long flags;
11587 int ret = 0;
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011588
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011589 local_irq_save(flags);
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011590
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011591 if (rdmsrl_safe(MSR_IA32_SPEC_CTRL, &saved_value))
11592 ret = 1;
11593 else if (wrmsrl_safe(MSR_IA32_SPEC_CTRL, value))
11594 ret = 1;
11595 else
11596 wrmsrl(MSR_IA32_SPEC_CTRL, saved_value);
11597
11598 local_irq_restore(flags);
11599
11600 return ret;
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011601}
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011602EXPORT_SYMBOL_GPL(kvm_spec_ctrl_test_value);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030011603
Mohammed Gamal89786142020-07-10 17:48:03 +020011604void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code)
11605{
11606 struct x86_exception fault;
Paolo Bonzini19cf4b72020-08-17 13:53:04 -040011607 u32 access = error_code &
11608 (PFERR_WRITE_MASK | PFERR_FETCH_MASK | PFERR_USER_MASK);
Mohammed Gamal89786142020-07-10 17:48:03 +020011609
11610 if (!(error_code & PFERR_PRESENT_MASK) ||
Paolo Bonzini19cf4b72020-08-17 13:53:04 -040011611 vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, &fault) != UNMAPPED_GVA) {
Mohammed Gamal89786142020-07-10 17:48:03 +020011612 /*
11613 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page
11614 * tables probably do not match the TLB. Just proceed
11615 * with the error code that the processor gave.
11616 */
11617 fault.vector = PF_VECTOR;
11618 fault.error_code_valid = true;
11619 fault.error_code = error_code;
11620 fault.nested_page_fault = false;
11621 fault.address = gva;
11622 }
11623 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault);
11624}
11625EXPORT_SYMBOL_GPL(kvm_fixup_and_inject_pf_error);
Carsten Otte043405e2007-10-10 17:16:19 +020011626
Babu Moger3f3393b2020-09-11 14:29:05 -050011627/*
11628 * Handles kvm_read/write_guest_virt*() result and either injects #PF or returns
11629 * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value
11630 * indicates whether exit to userspace is needed.
11631 */
11632int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
11633 struct x86_exception *e)
11634{
11635 if (r == X86EMUL_PROPAGATE_FAULT) {
11636 kvm_inject_emulated_page_fault(vcpu, e);
11637 return 1;
11638 }
11639
11640 /*
11641 * In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED
11642 * while handling a VMX instruction KVM could've handled the request
11643 * correctly by exiting to userspace and performing I/O but there
11644 * doesn't seem to be a real use-case behind such requests, just return
11645 * KVM_EXIT_INTERNAL_ERROR for now.
11646 */
11647 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
11648 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
11649 vcpu->run->internal.ndata = 0;
11650
11651 return 0;
11652}
11653EXPORT_SYMBOL_GPL(kvm_handle_memory_failure);
11654
Babu Moger97150922020-09-11 14:29:12 -050011655int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva)
11656{
11657 bool pcid_enabled;
11658 struct x86_exception e;
11659 unsigned i;
11660 unsigned long roots_to_free = 0;
11661 struct {
11662 u64 pcid;
11663 u64 gla;
11664 } operand;
11665 int r;
11666
11667 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
11668 if (r != X86EMUL_CONTINUE)
11669 return kvm_handle_memory_failure(vcpu, r, &e);
11670
11671 if (operand.pcid >> 12 != 0) {
11672 kvm_inject_gp(vcpu, 0);
11673 return 1;
11674 }
11675
11676 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
11677
11678 switch (type) {
11679 case INVPCID_TYPE_INDIV_ADDR:
11680 if ((!pcid_enabled && (operand.pcid != 0)) ||
11681 is_noncanonical_address(operand.gla, vcpu)) {
11682 kvm_inject_gp(vcpu, 0);
11683 return 1;
11684 }
11685 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
11686 return kvm_skip_emulated_instruction(vcpu);
11687
11688 case INVPCID_TYPE_SINGLE_CTXT:
11689 if (!pcid_enabled && (operand.pcid != 0)) {
11690 kvm_inject_gp(vcpu, 0);
11691 return 1;
11692 }
11693
11694 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
11695 kvm_mmu_sync_roots(vcpu);
11696 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
11697 }
11698
11699 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
11700 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd)
11701 == operand.pcid)
11702 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
11703
11704 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
11705 /*
11706 * If neither the current cr3 nor any of the prev_roots use the
11707 * given PCID, then nothing needs to be done here because a
11708 * resync will happen anyway before switching to any other CR3.
11709 */
11710
11711 return kvm_skip_emulated_instruction(vcpu);
11712
11713 case INVPCID_TYPE_ALL_NON_GLOBAL:
11714 /*
11715 * Currently, KVM doesn't mark global entries in the shadow
11716 * page tables, so a non-global flush just degenerates to a
11717 * global flush. If needed, we could optimize this later by
11718 * keeping track of global entries in shadow page tables.
11719 */
11720
11721 fallthrough;
11722 case INVPCID_TYPE_ALL_INCL_GLOBAL:
Sean Christophersonf66c53b2021-03-04 17:10:58 -080011723 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
Babu Moger97150922020-09-11 14:29:12 -050011724 return kvm_skip_emulated_instruction(vcpu);
11725
11726 default:
11727 BUG(); /* We have already checked above that type <= 3 */
11728 }
11729}
11730EXPORT_SYMBOL_GPL(kvm_handle_invpcid);
11731
Tom Lendacky8f423a82020-12-10 11:09:53 -060011732static int complete_sev_es_emulated_mmio(struct kvm_vcpu *vcpu)
11733{
11734 struct kvm_run *run = vcpu->run;
11735 struct kvm_mmio_fragment *frag;
11736 unsigned int len;
11737
11738 BUG_ON(!vcpu->mmio_needed);
11739
11740 /* Complete previous fragment */
11741 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
11742 len = min(8u, frag->len);
11743 if (!vcpu->mmio_is_write)
11744 memcpy(frag->data, run->mmio.data, len);
11745
11746 if (frag->len <= 8) {
11747 /* Switch to the next fragment. */
11748 frag++;
11749 vcpu->mmio_cur_fragment++;
11750 } else {
11751 /* Go forward to the next mmio piece. */
11752 frag->data += len;
11753 frag->gpa += len;
11754 frag->len -= len;
11755 }
11756
11757 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
11758 vcpu->mmio_needed = 0;
11759
11760 // VMG change, at this point, we're always done
11761 // RIP has already been advanced
11762 return 1;
11763 }
11764
11765 // More MMIO is needed
11766 run->mmio.phys_addr = frag->gpa;
11767 run->mmio.len = min(8u, frag->len);
11768 run->mmio.is_write = vcpu->mmio_is_write;
11769 if (run->mmio.is_write)
11770 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
11771 run->exit_reason = KVM_EXIT_MMIO;
11772
11773 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio;
11774
11775 return 0;
11776}
11777
11778int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes,
11779 void *data)
11780{
11781 int handled;
11782 struct kvm_mmio_fragment *frag;
11783
11784 if (!data)
11785 return -EINVAL;
11786
11787 handled = write_emultor.read_write_mmio(vcpu, gpa, bytes, data);
11788 if (handled == bytes)
11789 return 1;
11790
11791 bytes -= handled;
11792 gpa += handled;
11793 data += handled;
11794
11795 /*TODO: Check if need to increment number of frags */
11796 frag = vcpu->mmio_fragments;
11797 vcpu->mmio_nr_fragments = 1;
11798 frag->len = bytes;
11799 frag->gpa = gpa;
11800 frag->data = data;
11801
11802 vcpu->mmio_needed = 1;
11803 vcpu->mmio_cur_fragment = 0;
11804
11805 vcpu->run->mmio.phys_addr = gpa;
11806 vcpu->run->mmio.len = min(8u, frag->len);
11807 vcpu->run->mmio.is_write = 1;
11808 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
11809 vcpu->run->exit_reason = KVM_EXIT_MMIO;
11810
11811 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio;
11812
11813 return 0;
11814}
11815EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_write);
11816
11817int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes,
11818 void *data)
11819{
11820 int handled;
11821 struct kvm_mmio_fragment *frag;
11822
11823 if (!data)
11824 return -EINVAL;
11825
11826 handled = read_emultor.read_write_mmio(vcpu, gpa, bytes, data);
11827 if (handled == bytes)
11828 return 1;
11829
11830 bytes -= handled;
11831 gpa += handled;
11832 data += handled;
11833
11834 /*TODO: Check if need to increment number of frags */
11835 frag = vcpu->mmio_fragments;
11836 vcpu->mmio_nr_fragments = 1;
11837 frag->len = bytes;
11838 frag->gpa = gpa;
11839 frag->data = data;
11840
11841 vcpu->mmio_needed = 1;
11842 vcpu->mmio_cur_fragment = 0;
11843
11844 vcpu->run->mmio.phys_addr = gpa;
11845 vcpu->run->mmio.len = min(8u, frag->len);
11846 vcpu->run->mmio.is_write = 0;
11847 vcpu->run->exit_reason = KVM_EXIT_MMIO;
11848
11849 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio;
11850
11851 return 0;
11852}
11853EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_read);
11854
Tom Lendacky7ed9abf2020-12-10 11:09:54 -060011855static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu)
11856{
11857 memcpy(vcpu->arch.guest_ins_data, vcpu->arch.pio_data,
11858 vcpu->arch.pio.count * vcpu->arch.pio.size);
11859 vcpu->arch.pio.count = 0;
11860
11861 return 1;
11862}
11863
11864static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size,
11865 unsigned int port, void *data, unsigned int count)
11866{
11867 int ret;
11868
11869 ret = emulator_pio_out_emulated(vcpu->arch.emulate_ctxt, size, port,
11870 data, count);
11871 if (ret)
11872 return ret;
11873
11874 vcpu->arch.pio.count = 0;
11875
11876 return 0;
11877}
11878
11879static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size,
11880 unsigned int port, void *data, unsigned int count)
11881{
11882 int ret;
11883
11884 ret = emulator_pio_in_emulated(vcpu->arch.emulate_ctxt, size, port,
11885 data, count);
11886 if (ret) {
11887 vcpu->arch.pio.count = 0;
11888 } else {
11889 vcpu->arch.guest_ins_data = data;
11890 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;
11891 }
11892
11893 return 0;
11894}
11895
11896int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
11897 unsigned int port, void *data, unsigned int count,
11898 int in)
11899{
11900 return in ? kvm_sev_es_ins(vcpu, size, port, data, count)
11901 : kvm_sev_es_outs(vcpu, size, port, data, count);
11902}
11903EXPORT_SYMBOL_GPL(kvm_sev_es_string_io);
11904
Lorenzo Bresciad95df952020-12-23 14:45:07 +000011905EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry);
Carsten Otte043405e2007-10-10 17:16:19 +020011906EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
11907EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
11908EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
11909EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
11910EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
11911EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
11912EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
11913EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
11914EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
11915EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070011916EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Carsten Otte043405e2007-10-10 17:16:19 +020011917EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
11918EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
11919EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
11920EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080011921EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Carsten Otte043405e2007-10-10 17:16:19 +020011922EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
11923EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
11924EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
11925EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050011926EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060011927EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);
Tom Lendackyd523ab6b2020-12-10 11:09:48 -060011928EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter);
11929EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit);
Tom Lendacky59e38b52020-12-10 11:09:52 -060011930EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter);
11931EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit);