blob: 4e07cae5663605e0ba5b010f79fb5c4a7eda6a8c [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>
Sergey Senozhatsky7d628742021-06-06 11:10:45 +090061#include <linux/suspend.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010062
Avi Kivityaec51dc2009-07-01 16:01:02 +030063#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080064
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020065#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080066#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020067#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080068#include <asm/mce.h>
Dave Hansen784a46612021-06-23 14:02:05 +020069#include <asm/pkru.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020070#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020071#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100072#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030073#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080074#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010075#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010076#include <asm/hypervisor.h>
Babu Moger97150922020-09-11 14:29:12 -050077#include <asm/tlbflush.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080078#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090079#include <asm/emulate_prefix.h>
Sean Christophersonfe7e9482021-04-12 16:21:43 +120080#include <asm/sgx.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000081#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020082
Dave Hansend1898b72016-06-01 10:42:20 -070083#define CREATE_TRACE_POINTS
84#include "trace.h"
85
Carsten Otte313a3dc2007-10-11 19:16:52 +020086#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080087#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080088u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
89EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080090
Avi Kivity0f65dd72011-04-20 13:37:53 +030091#define emul_to_vcpu(ctxt) \
Sean Christophersonc9b8b072020-02-18 15:29:48 -080092 ((struct kvm_vcpu *)(ctxt)->vcpu)
Avi Kivity0f65dd72011-04-20 13:37:53 +030093
Joerg Roedel50a37eb2008-01-31 14:57:38 +010094/* EFER defaults:
95 * - enable syscall per default because its emulated by KVM
96 * - enable LME and LMA per default on 64 bit KVM
97 */
98#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080099static
100u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100101#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +0800102static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100103#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +0200104
Sean Christophersonb11306b2019-12-10 14:44:13 -0800105static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
106
Ashish Kalra0dbb1122021-06-08 18:05:43 +0000107#define KVM_EXIT_HYPERCALL_VALID_MASK (1 << KVM_HC_MAP_GPA_RANGE)
108
Radim Krčmářc5192652016-07-12 22:09:28 +0200109#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
110 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200111
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300112static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300113static void process_nmi(struct kvm_vcpu *vcpu);
Jay Zhou1f7becf2021-01-18 16:47:20 +0800114static void process_smi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200115static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100116static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800117static void store_regs(struct kvm_vcpu *vcpu);
118static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200119
Maxim Levitsky6dba9402021-06-07 12:02:02 +0300120static int __set_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2);
121static void __get_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2);
122
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700123struct kvm_x86_ops kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300124EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800125
Jason Baron9af54712021-01-14 22:27:55 -0500126#define KVM_X86_OP(func) \
127 DEFINE_STATIC_CALL_NULL(kvm_x86_##func, \
128 *(((struct kvm_x86_ops *)0)->func));
129#define KVM_X86_OP_NULL KVM_X86_OP
130#include <asm/kvm-x86-ops.h>
131EXPORT_STATIC_CALL_GPL(kvm_x86_get_cs_db_l_bits);
132EXPORT_STATIC_CALL_GPL(kvm_x86_cache_reg);
133EXPORT_STATIC_CALL_GPL(kvm_x86_tlb_flush_current);
134
Paolo Bonzini893590c2015-11-06 11:46:24 +0100135static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030136module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200137
Like Xud8550662021-01-08 09:36:55 +0800138bool __read_mostly report_ignored_msrs = true;
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200139module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
Like Xud8550662021-01-08 09:36:55 +0800140EXPORT_SYMBOL_GPL(report_ignored_msrs);
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200141
Wanpeng Li4c276252018-05-05 04:02:32 -0700142unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200143module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
144
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300145static bool __read_mostly kvmclock_periodic_sync = true;
146module_param(kvmclock_periodic_sync, bool, S_IRUGO);
147
Paolo Bonzini893590c2015-11-06 11:46:24 +0100148bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100149EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100150u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100151EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800152u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
153EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
154u64 __read_mostly kvm_max_tsc_scaling_ratio;
155EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700156u64 __read_mostly kvm_default_tsc_scaling_ratio;
157EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +0800158bool __read_mostly kvm_has_bus_lock_exit;
159EXPORT_SYMBOL_GPL(kvm_has_bus_lock_exit);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100160
Zachary Amsdencc578282012-02-03 15:43:50 -0200161/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100162static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200163module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
164
Sean Christophersonc3941d92019-04-17 10:15:33 -0700165/*
166 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
Ingo Molnard9f6e122021-03-18 15:28:01 +0100167 * adaptive tuning starting from default advancement of 1000ns. '0' disables
Sean Christophersonc3941d92019-04-17 10:15:33 -0700168 * advancement entirely. Any other value is used as-is and disables adaptive
Ingo Molnard9f6e122021-03-18 15:28:01 +0100169 * tuning, i.e. allows privileged userspace to set an exact advancement time.
Sean Christophersonc3941d92019-04-17 10:15:33 -0700170 */
171static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800172module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500173
Feng Wu520040142016-01-25 16:53:33 +0800174static bool __read_mostly vector_hashing = true;
175module_param(vector_hashing, bool, S_IRUGO);
176
Liran Alonc4ae60e2018-03-12 13:12:47 +0200177bool __read_mostly enable_vmware_backdoor = false;
178module_param(enable_vmware_backdoor, bool, S_IRUGO);
179EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
180
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700181static bool __read_mostly force_emulation_prefix = false;
182module_param(force_emulation_prefix, bool, S_IRUGO);
183
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800184int __read_mostly pi_inject_timer = -1;
185module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
186
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700187/*
188 * Restoring the host value for MSRs that are only consumed when running in
189 * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
190 * returns to userspace, i.e. the kernel can run with the guest's value.
191 */
192#define KVM_MAX_NR_USER_RETURN_MSRS 16
Avi Kivity18863bd2009-09-07 11:12:18 +0300193
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700194struct kvm_user_return_msrs {
Avi Kivity18863bd2009-09-07 11:12:18 +0300195 struct user_return_notifier urn;
196 bool registered;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700197 struct kvm_user_return_msr_values {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800198 u64 host;
199 u64 curr;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700200 } values[KVM_MAX_NR_USER_RETURN_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300201};
202
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700203u32 __read_mostly kvm_nr_uret_msrs;
204EXPORT_SYMBOL_GPL(kvm_nr_uret_msrs);
205static u32 __read_mostly kvm_uret_msrs_list[KVM_MAX_NR_USER_RETURN_MSRS];
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700206static struct kvm_user_return_msrs __percpu *user_return_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300207
Sean Christophersoncfc48182020-03-02 15:56:23 -0800208#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
209 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
210 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
211 | XFEATURE_MASK_PKRU)
212
Sean Christopherson91661982020-03-02 15:57:06 -0800213u64 __read_mostly host_efer;
214EXPORT_SYMBOL_GPL(host_efer);
215
Mohammed Gamalb96e6502020-09-03 16:11:22 +0200216bool __read_mostly allow_smaller_maxphyaddr = 0;
Mohammed Gamal3edd6832020-07-10 17:48:11 +0200217EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr);
218
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +0200219bool __read_mostly enable_apicv = true;
220EXPORT_SYMBOL_GPL(enable_apicv);
221
Tom Lendacky86137772020-12-10 11:10:07 -0600222u64 __read_mostly host_xss;
223EXPORT_SYMBOL_GPL(host_xss);
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100224u64 __read_mostly supported_xss;
225EXPORT_SYMBOL_GPL(supported_xss);
Aaron Lewis139a12c2019-10-21 16:30:25 -0700226
Jing Zhangfcfe1ba2021-06-18 22:27:05 +0000227const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
228 KVM_GENERIC_VM_STATS(),
229 STATS_DESC_COUNTER(VM, mmu_shadow_zapped),
230 STATS_DESC_COUNTER(VM, mmu_pte_write),
231 STATS_DESC_COUNTER(VM, mmu_pde_zapped),
232 STATS_DESC_COUNTER(VM, mmu_flooded),
233 STATS_DESC_COUNTER(VM, mmu_recycled),
234 STATS_DESC_COUNTER(VM, mmu_cache_miss),
235 STATS_DESC_ICOUNTER(VM, mmu_unsync),
Mingwei Zhang71f51d22021-08-02 21:46:07 -0700236 STATS_DESC_ICOUNTER(VM, pages_4k),
237 STATS_DESC_ICOUNTER(VM, pages_2m),
238 STATS_DESC_ICOUNTER(VM, pages_1g),
Jing Zhangfcfe1ba2021-06-18 22:27:05 +0000239 STATS_DESC_ICOUNTER(VM, nx_lpage_splits),
Peter Xuec1cf692021-06-25 11:32:06 -0400240 STATS_DESC_PCOUNTER(VM, max_mmu_rmap_size),
Jing Zhangbc9e9e62021-06-23 17:28:46 -0400241 STATS_DESC_PCOUNTER(VM, max_mmu_page_hash_collisions)
Jing Zhangfcfe1ba2021-06-18 22:27:05 +0000242};
Jing Zhangfcfe1ba2021-06-18 22:27:05 +0000243
244const struct kvm_stats_header kvm_vm_stats_header = {
245 .name_size = KVM_STATS_NAME_SIZE,
246 .num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
247 .id_offset = sizeof(struct kvm_stats_header),
248 .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
249 .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
250 sizeof(kvm_vm_stats_desc),
251};
252
Jing Zhangce55c042021-06-18 22:27:06 +0000253const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
254 KVM_GENERIC_VCPU_STATS(),
255 STATS_DESC_COUNTER(VCPU, pf_fixed),
256 STATS_DESC_COUNTER(VCPU, pf_guest),
257 STATS_DESC_COUNTER(VCPU, tlb_flush),
258 STATS_DESC_COUNTER(VCPU, invlpg),
259 STATS_DESC_COUNTER(VCPU, exits),
260 STATS_DESC_COUNTER(VCPU, io_exits),
261 STATS_DESC_COUNTER(VCPU, mmio_exits),
262 STATS_DESC_COUNTER(VCPU, signal_exits),
263 STATS_DESC_COUNTER(VCPU, irq_window_exits),
264 STATS_DESC_COUNTER(VCPU, nmi_window_exits),
265 STATS_DESC_COUNTER(VCPU, l1d_flush),
266 STATS_DESC_COUNTER(VCPU, halt_exits),
267 STATS_DESC_COUNTER(VCPU, request_irq_exits),
268 STATS_DESC_COUNTER(VCPU, irq_exits),
269 STATS_DESC_COUNTER(VCPU, host_state_reload),
270 STATS_DESC_COUNTER(VCPU, fpu_reload),
271 STATS_DESC_COUNTER(VCPU, insn_emulation),
272 STATS_DESC_COUNTER(VCPU, insn_emulation_fail),
273 STATS_DESC_COUNTER(VCPU, hypercalls),
274 STATS_DESC_COUNTER(VCPU, irq_injections),
275 STATS_DESC_COUNTER(VCPU, nmi_injections),
276 STATS_DESC_COUNTER(VCPU, req_event),
277 STATS_DESC_COUNTER(VCPU, nested_run),
278 STATS_DESC_COUNTER(VCPU, directed_yield_attempted),
279 STATS_DESC_COUNTER(VCPU, directed_yield_successful),
280 STATS_DESC_ICOUNTER(VCPU, guest_mode)
281};
Jing Zhangce55c042021-06-18 22:27:06 +0000282
283const struct kvm_stats_header kvm_vcpu_stats_header = {
284 .name_size = KVM_STATS_NAME_SIZE,
285 .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
286 .id_offset = sizeof(struct kvm_stats_header),
287 .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
288 .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
289 sizeof(kvm_vcpu_stats_desc),
290};
291
Dexuan Cui2acf9232010-06-10 11:27:12 +0800292u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800293u64 __read_mostly supported_xcr0;
294EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800295
Sean Christopherson80fbd282020-06-08 11:02:18 -0700296static struct kmem_cache *x86_fpu_cache;
Marc Orrb666a4b2018-11-06 14:53:56 -0800297
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800298static struct kmem_cache *x86_emulator_cache;
299
Peter Xu6abe9c12020-06-22 18:04:41 -0400300/*
301 * When called, it means the previous get/set msr reached an invalid msr.
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200302 * Return true if we want to ignore/silent this failed msr access.
Peter Xu6abe9c12020-06-22 18:04:41 -0400303 */
Haiwei Lid6328262021-03-13 13:10:32 +0800304static bool kvm_msr_ignored_check(u32 msr, u64 data, bool write)
Peter Xu6abe9c12020-06-22 18:04:41 -0400305{
306 const char *op = write ? "wrmsr" : "rdmsr";
307
308 if (ignore_msrs) {
309 if (report_ignored_msrs)
Takashi Iwaid383b312020-10-30 16:14:14 +0100310 kvm_pr_unimpl("ignored %s: 0x%x data 0x%llx\n",
311 op, msr, data);
Peter Xu6abe9c12020-06-22 18:04:41 -0400312 /* Mask the error */
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200313 return true;
Peter Xu6abe9c12020-06-22 18:04:41 -0400314 } else {
Takashi Iwaid383b312020-10-30 16:14:14 +0100315 kvm_debug_ratelimited("unhandled %s: 0x%x data 0x%llx\n",
316 op, msr, data);
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200317 return false;
Peter Xu6abe9c12020-06-22 18:04:41 -0400318 }
319}
320
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800321static struct kmem_cache *kvm_alloc_emulator_cache(void)
322{
Sean Christopherson06add252020-02-18 15:29:50 -0800323 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
324 unsigned int size = sizeof(struct x86_emulate_ctxt);
325
326 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800327 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800328 SLAB_ACCOUNT, useroffset,
329 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800330}
331
Jan Kiszkab6785de2012-09-20 07:43:17 +0200332static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300333
Gleb Natapovaf585b92010-10-14 11:22:46 +0200334static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
335{
336 int i;
Peter Xudd03bca2020-04-16 11:58:59 -0400337 for (i = 0; i < ASYNC_PF_PER_VCPU; i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +0200338 vcpu->arch.apf.gfns[i] = ~0;
339}
340
Avi Kivity18863bd2009-09-07 11:12:18 +0300341static void kvm_on_user_return(struct user_return_notifier *urn)
342{
343 unsigned slot;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700344 struct kvm_user_return_msrs *msrs
345 = container_of(urn, struct kvm_user_return_msrs, urn);
346 struct kvm_user_return_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700347 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300348
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700349 /*
350 * Disabling irqs at this point since the following code could be
351 * interrupted and executed through kvm_arch_hardware_disable()
352 */
353 local_irq_save(flags);
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700354 if (msrs->registered) {
355 msrs->registered = false;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700356 user_return_notifier_unregister(urn);
357 }
358 local_irq_restore(flags);
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700359 for (slot = 0; slot < kvm_nr_uret_msrs; ++slot) {
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700360 values = &msrs->values[slot];
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800361 if (values->host != values->curr) {
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700362 wrmsrl(kvm_uret_msrs_list[slot], values->host);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800363 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300364 }
365 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300366}
367
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700368static int kvm_probe_user_return_msr(u32 msr)
Sean Christopherson5104d7f2021-05-04 10:17:24 -0700369{
370 u64 val;
371 int ret;
372
373 preempt_disable();
374 ret = rdmsrl_safe(msr, &val);
375 if (ret)
376 goto out;
377 ret = wrmsrl_safe(msr, val);
378out:
379 preempt_enable();
380 return ret;
381}
Sean Christopherson5104d7f2021-05-04 10:17:24 -0700382
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700383int kvm_add_user_return_msr(u32 msr)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800384{
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700385 BUG_ON(kvm_nr_uret_msrs >= KVM_MAX_NR_USER_RETURN_MSRS);
386
387 if (kvm_probe_user_return_msr(msr))
388 return -1;
389
390 kvm_uret_msrs_list[kvm_nr_uret_msrs] = msr;
391 return kvm_nr_uret_msrs++;
Avi Kivity18863bd2009-09-07 11:12:18 +0300392}
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700393EXPORT_SYMBOL_GPL(kvm_add_user_return_msr);
Avi Kivity18863bd2009-09-07 11:12:18 +0300394
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -0700395int kvm_find_user_return_msr(u32 msr)
396{
397 int i;
398
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700399 for (i = 0; i < kvm_nr_uret_msrs; ++i) {
400 if (kvm_uret_msrs_list[i] == msr)
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -0700401 return i;
402 }
403 return -1;
404}
405EXPORT_SYMBOL_GPL(kvm_find_user_return_msr);
406
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700407static void kvm_user_return_msr_cpu_online(void)
Avi Kivity18863bd2009-09-07 11:12:18 +0300408{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800409 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700410 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Sean Christopherson05c19c22019-11-22 12:04:50 -0800411 u64 value;
412 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300413
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700414 for (i = 0; i < kvm_nr_uret_msrs; ++i) {
415 rdmsrl_safe(kvm_uret_msrs_list[i], &value);
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700416 msrs->values[i].host = value;
417 msrs->values[i].curr = value;
Sean Christopherson05c19c22019-11-22 12:04:50 -0800418 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300419}
420
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700421int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300422{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200423 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700424 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700425 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300426
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700427 value = (value & mask) | (msrs->values[slot].host & ~mask);
428 if (value == msrs->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700429 return 0;
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700430 err = wrmsrl_safe(kvm_uret_msrs_list[slot], value);
Andy Honig8b3c3102014-08-27 11:16:44 -0700431 if (err)
432 return 1;
433
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700434 msrs->values[slot].curr = value;
435 if (!msrs->registered) {
436 msrs->urn.on_user_return = kvm_on_user_return;
437 user_return_notifier_register(&msrs->urn);
438 msrs->registered = true;
Avi Kivity18863bd2009-09-07 11:12:18 +0300439 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700440 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300441}
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700442EXPORT_SYMBOL_GPL(kvm_set_user_return_msr);
Avi Kivity18863bd2009-09-07 11:12:18 +0300443
Radim Krčmář13a34e02014-08-28 15:13:03 +0200444static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200445{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200446 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700447 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200448
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700449 if (msrs->registered)
450 kvm_on_user_return(&msrs->urn);
Avi Kivity3548bab2009-11-28 14:18:47 +0200451}
452
Carsten Otte6866b832007-10-29 16:09:10 +0100453u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
454{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300455 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100456}
457EXPORT_SYMBOL_GPL(kvm_get_apic_base);
458
Jim Mattson58871642018-05-09 16:56:04 -0400459enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
460{
461 return kvm_apic_mode(kvm_get_apic_base(vcpu));
462}
463EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
464
Jan Kiszka58cb6282014-01-24 16:48:44 +0100465int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100466{
Jim Mattson58871642018-05-09 16:56:04 -0400467 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
468 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Sean Christophersona8ac8642021-02-03 16:01:15 -0800469 u64 reserved_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu) | 0x2ff |
Radim Krčmářd6321d42017-08-05 00:12:49 +0200470 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100471
Jim Mattson58871642018-05-09 16:56:04 -0400472 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700473 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400474 if (!msr_info->host_initiated) {
475 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
476 return 1;
477 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
478 return 1;
479 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100480
481 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800482 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100483 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100484}
485EXPORT_SYMBOL_GPL(kvm_set_apic_base);
486
Sean Christophersonad0577c2021-08-09 10:39:54 -0700487/*
488 * Handle a fault on a hardware virtualization (VMX or SVM) instruction.
489 *
490 * Hardware virtualization extension instructions may fault if a reboot turns
491 * off virtualization while processes are running. Usually after catching the
492 * fault we just panic; during reboot instead the instruction is ignored.
493 */
494noinstr void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000495{
496 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200497 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000498}
499EXPORT_SYMBOL_GPL(kvm_spurious_fault);
500
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200501#define EXCPT_BENIGN 0
502#define EXCPT_CONTRIBUTORY 1
503#define EXCPT_PF 2
504
505static int exception_class(int vector)
506{
507 switch (vector) {
508 case PF_VECTOR:
509 return EXCPT_PF;
510 case DE_VECTOR:
511 case TS_VECTOR:
512 case NP_VECTOR:
513 case SS_VECTOR:
514 case GP_VECTOR:
515 return EXCPT_CONTRIBUTORY;
516 default:
517 break;
518 }
519 return EXCPT_BENIGN;
520}
521
Nadav Amitd6e8c852014-07-24 14:51:24 +0300522#define EXCPT_FAULT 0
523#define EXCPT_TRAP 1
524#define EXCPT_ABORT 2
525#define EXCPT_INTERRUPT 3
526
527static int exception_type(int vector)
528{
529 unsigned int mask;
530
531 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
532 return EXCPT_INTERRUPT;
533
534 mask = 1 << vector;
535
536 /* #DB is trap, as instruction watchpoints are handled elsewhere */
537 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
538 return EXCPT_TRAP;
539
540 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
541 return EXCPT_ABORT;
542
543 /* Reserved exceptions will result in fault */
544 return EXCPT_FAULT;
545}
546
Jim Mattsonda998b42018-10-16 14:29:22 -0700547void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
548{
549 unsigned nr = vcpu->arch.exception.nr;
550 bool has_payload = vcpu->arch.exception.has_payload;
551 unsigned long payload = vcpu->arch.exception.payload;
552
553 if (!has_payload)
554 return;
555
556 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700557 case DB_VECTOR:
558 /*
559 * "Certain debug exceptions may clear bit 0-3. The
560 * remaining contents of the DR6 register are never
561 * cleared by the processor".
562 */
563 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
564 /*
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800565 * In order to reflect the #DB exception payload in guest
566 * dr6, three components need to be considered: active low
567 * bit, FIXED_1 bits and active high bits (e.g. DR6_BD,
568 * DR6_BS and DR6_BT)
569 * DR6_ACTIVE_LOW contains the FIXED_1 and active low bits.
570 * In the target guest dr6:
571 * FIXED_1 bits should always be set.
572 * Active low bits should be cleared if 1-setting in payload.
573 * Active high bits should be set if 1-setting in payload.
574 *
575 * Note, the payload is compatible with the pending debug
576 * exceptions/exit qualification under VMX, that active_low bits
577 * are active high in payload.
578 * So they need to be flipped for DR6.
Jim Mattsonf10c7292018-10-16 14:29:23 -0700579 */
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800580 vcpu->arch.dr6 |= DR6_ACTIVE_LOW;
Jim Mattsonf10c7292018-10-16 14:29:23 -0700581 vcpu->arch.dr6 |= payload;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800582 vcpu->arch.dr6 ^= payload & DR6_ACTIVE_LOW;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800583
584 /*
585 * The #DB payload is defined as compatible with the 'pending
586 * debug exceptions' field under VMX, not DR6. While bit 12 is
587 * defined in the 'pending debug exceptions' field (enabled
588 * breakpoint), it is reserved and must be zero in DR6.
589 */
590 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700591 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700592 case PF_VECTOR:
593 vcpu->arch.cr2 = payload;
594 break;
595 }
596
597 vcpu->arch.exception.has_payload = false;
598 vcpu->arch.exception.payload = 0;
599}
600EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
601
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200602static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200603 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700604 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200605{
606 u32 prev_nr;
607 int class1, class2;
608
Avi Kivity3842d132010-07-27 12:30:24 +0300609 kvm_make_request(KVM_REQ_EVENT, vcpu);
610
Wanpeng Li664f8e22017-08-24 03:35:09 -0700611 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200612 queue:
Wanpeng Li664f8e22017-08-24 03:35:09 -0700613 if (reinject) {
614 /*
615 * On vmentry, vcpu->arch.exception.pending is only
616 * true if an event injection was blocked by
617 * nested_run_pending. In that case, however,
618 * vcpu_enter_guest requests an immediate exit,
619 * and the guest shouldn't proceed far enough to
620 * need reinjection.
621 */
622 WARN_ON_ONCE(vcpu->arch.exception.pending);
623 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700624 if (WARN_ON_ONCE(has_payload)) {
625 /*
626 * A reinjected event has already
627 * delivered its payload.
628 */
629 has_payload = false;
630 payload = 0;
631 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700632 } else {
633 vcpu->arch.exception.pending = true;
634 vcpu->arch.exception.injected = false;
635 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200636 vcpu->arch.exception.has_error_code = has_error;
637 vcpu->arch.exception.nr = nr;
638 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700639 vcpu->arch.exception.has_payload = has_payload;
640 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800641 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700642 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200643 return;
644 }
645
646 /* to check exception */
647 prev_nr = vcpu->arch.exception.nr;
648 if (prev_nr == DF_VECTOR) {
649 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300650 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200651 return;
652 }
653 class1 = exception_class(prev_nr);
654 class2 = exception_class(nr);
655 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
656 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700657 /*
658 * Generate double fault per SDM Table 5-5. Set
659 * exception.pending = true so that the double fault
660 * can trigger a nested vmexit.
661 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200662 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700663 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200664 vcpu->arch.exception.has_error_code = true;
665 vcpu->arch.exception.nr = DF_VECTOR;
666 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700667 vcpu->arch.exception.has_payload = false;
668 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200669 } else
670 /* replace previous exception with a new one in a hope
671 that instruction re-execution will regenerate lost
672 exception */
673 goto queue;
674}
675
Avi Kivity298101d2007-11-25 13:41:11 +0200676void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
677{
Jim Mattson91e86d22018-10-16 14:29:21 -0700678 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200679}
680EXPORT_SYMBOL_GPL(kvm_queue_exception);
681
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200682void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
683{
Jim Mattson91e86d22018-10-16 14:29:21 -0700684 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200685}
686EXPORT_SYMBOL_GPL(kvm_requeue_exception);
687
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400688void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
689 unsigned long payload)
Jim Mattsonf10c7292018-10-16 14:29:23 -0700690{
691 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
692}
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400693EXPORT_SYMBOL_GPL(kvm_queue_exception_p);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700694
Jim Mattsonda998b42018-10-16 14:29:22 -0700695static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
696 u32 error_code, unsigned long payload)
697{
698 kvm_multiple_exception(vcpu, nr, true, error_code,
699 true, payload, false);
700}
701
Kyle Huey6affcbe2016-11-29 12:40:40 -0800702int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200703{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100704 if (err)
705 kvm_inject_gp(vcpu, 0);
706 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800707 return kvm_skip_emulated_instruction(vcpu);
708
709 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100710}
711EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200712
Avi Kivity6389ee92010-11-29 16:12:30 +0200713void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200714{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200715 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700716 vcpu->arch.exception.nested_apf =
717 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700718 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700719 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700720 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
721 } else {
722 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
723 fault->address);
724 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200725}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300726EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200727
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700728bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
729 struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200730{
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400731 struct kvm_mmu *fault_mmu;
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700732 WARN_ON_ONCE(fault->vector != PF_VECTOR);
733
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400734 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu :
735 vcpu->arch.walk_mmu;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200736
Junaid Shahidee1fa202020-03-20 14:28:03 -0700737 /*
738 * Invalidate the TLB entry for the faulting address, if it exists,
739 * else the access will fault indefinitely (and to emulate hardware).
740 */
741 if ((fault->error_code & PFERR_PRESENT_MASK) &&
742 !(fault->error_code & PFERR_RSVD_MASK))
743 kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address,
744 fault_mmu->root_hpa);
745
746 fault_mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200747 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200748}
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700749EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200750
Sheng Yang3419ffc2008-05-15 09:52:48 +0800751void kvm_inject_nmi(struct kvm_vcpu *vcpu)
752{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300753 atomic_inc(&vcpu->arch.nmi_queued);
754 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800755}
756EXPORT_SYMBOL_GPL(kvm_inject_nmi);
757
Avi Kivity298101d2007-11-25 13:41:11 +0200758void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
759{
Jim Mattson91e86d22018-10-16 14:29:21 -0700760 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200761}
762EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
763
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200764void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
765{
Jim Mattson91e86d22018-10-16 14:29:21 -0700766 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200767}
768EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
769
Carsten Ottea03490e2007-10-29 16:09:35 +0100770/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300771 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
772 * a #GP and return false.
773 */
774bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200775{
Jason Baronb36464772021-01-14 22:27:56 -0500776 if (static_call(kvm_x86_get_cpl)(vcpu) <= required_cpl)
Avi Kivity0a79b002009-09-01 12:03:25 +0300777 return true;
778 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
779 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100780}
Avi Kivity0a79b002009-09-01 12:03:25 +0300781EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100782
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300783bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
784{
785 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
786 return true;
787
788 kvm_queue_exception(vcpu, UD_VECTOR);
789 return false;
790}
791EXPORT_SYMBOL_GPL(kvm_require_dr);
792
Carsten Ottea03490e2007-10-29 16:09:35 +0100793/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200794 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200795 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200796 * can read from guest physical or from the guest's guest physical memory.
797 */
798int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
799 gfn_t ngfn, void *data, int offset, int len,
800 u32 access)
801{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200802 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200803 gfn_t real_gfn;
804 gpa_t ngpa;
805
806 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200807 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200808 if (real_gfn == UNMAPPED_GVA)
809 return -EFAULT;
810
811 real_gfn = gpa_to_gfn(real_gfn);
812
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200813 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200814}
815EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
816
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700817static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
818{
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800819 return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2);
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700820}
821
Joerg Roedelec92fe42010-09-10 17:30:51 +0200822/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700823 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100824 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200825int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100826{
827 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
828 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
829 int i;
830 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200831 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100832
Joerg Roedelff03a072010-09-10 17:30:57 +0200833 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
834 offset * sizeof(u64), sizeof(pdpte),
835 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100836 if (ret < 0) {
837 ret = 0;
838 goto out;
839 }
840 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400841 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700842 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100843 ret = 0;
844 goto out;
845 }
846 }
847 ret = 1;
848
Joerg Roedelff03a072010-09-10 17:30:57 +0200849 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700850 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
Maxim Levitsky158a48e2021-06-07 12:02:03 +0300851 vcpu->arch.pdptrs_from_userspace = false;
852
Carsten Ottea03490e2007-10-29 16:09:35 +0100853out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100854
855 return ret;
856}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100857EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100858
Tom Lendackyf27ad382020-12-10 11:09:56 -0600859void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned long cr0)
860{
Tom Lendackyf27ad382020-12-10 11:09:56 -0600861 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
862 kvm_clear_async_pf_completion_queue(vcpu);
863 kvm_async_pf_hash_reset(vcpu);
864 }
865
Sean Christopherson20f632b2021-06-22 10:57:02 -0700866 if ((cr0 ^ old_cr0) & KVM_MMU_CR0_ROLE_BITS)
Tom Lendackyf27ad382020-12-10 11:09:56 -0600867 kvm_mmu_reset_context(vcpu);
868
869 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
870 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
871 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
872 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
873}
874EXPORT_SYMBOL_GPL(kvm_post_set_cr0);
875
Avi Kivity49a9b072010-06-10 17:02:14 +0300876int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100877{
Sheng Yangaad82702010-05-12 16:40:42 +0800878 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Jim Mattsond42e3fa2020-07-07 15:36:30 -0700879 unsigned long pdptr_bits = X86_CR0_CD | X86_CR0_NW | X86_CR0_PG;
Sheng Yangaad82702010-05-12 16:40:42 +0800880
Avi Kivityf9a48e62010-01-06 19:10:22 +0200881 cr0 |= X86_CR0_ET;
882
Gleb Natapovab344822010-01-21 15:28:46 +0200883#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300884 if (cr0 & 0xffffffff00000000UL)
885 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200886#endif
887
888 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100889
Gleb Natapov0f122442010-04-28 19:15:31 +0300890 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
891 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100892
Gleb Natapov0f122442010-04-28 19:15:31 +0300893 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
894 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100895
Carsten Ottea03490e2007-10-29 16:09:35 +0100896#ifdef CONFIG_X86_64
Sean Christopherson05487212020-07-13 18:57:32 -0700897 if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) &&
898 (cr0 & X86_CR0_PG)) {
899 int cs_db, cs_l;
Carsten Ottea03490e2007-10-29 16:09:35 +0100900
Sean Christopherson05487212020-07-13 18:57:32 -0700901 if (!is_pae(vcpu))
902 return 1;
Jason Baronb36464772021-01-14 22:27:56 -0500903 static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
Sean Christopherson05487212020-07-13 18:57:32 -0700904 if (cs_l)
Gleb Natapov0f122442010-04-28 19:15:31 +0300905 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100906 }
Sean Christopherson05487212020-07-13 18:57:32 -0700907#endif
908 if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) &&
909 is_pae(vcpu) && ((cr0 ^ old_cr0) & pdptr_bits) &&
910 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)))
911 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100912
Mao, Junjiead756a12012-07-02 01:18:48 +0000913 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
914 return 1;
915
Jason Baronb36464772021-01-14 22:27:56 -0500916 static_call(kvm_x86_set_cr0)(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100917
Tom Lendackyf27ad382020-12-10 11:09:56 -0600918 kvm_post_set_cr0(vcpu, old_cr0, cr0);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800919
Gleb Natapov0f122442010-04-28 19:15:31 +0300920 return 0;
921}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200922EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100923
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200924void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100925{
Avi Kivity49a9b072010-06-10 17:02:14 +0300926 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100927}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200928EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100929
Aaron Lewis139a12c2019-10-21 16:30:25 -0700930void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300931{
Tom Lendacky16809ec2020-12-10 11:10:08 -0600932 if (vcpu->arch.guest_state_protected)
933 return;
934
Aaron Lewis139a12c2019-10-21 16:30:25 -0700935 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300936
Aaron Lewis139a12c2019-10-21 16:30:25 -0700937 if (vcpu->arch.xcr0 != host_xcr0)
938 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
939
940 if (vcpu->arch.xsaves_enabled &&
941 vcpu->arch.ia32_xss != host_xss)
942 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
943 }
Babu Moger37486132020-05-12 18:59:06 -0500944
945 if (static_cpu_has(X86_FEATURE_PKU) &&
946 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
947 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) &&
948 vcpu->arch.pkru != vcpu->arch.host_pkru)
Thomas Gleixner72a6c082021-06-23 14:02:23 +0200949 write_pkru(vcpu->arch.pkru);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300950}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700951EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
952
953void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
954{
Tom Lendacky16809ec2020-12-10 11:10:08 -0600955 if (vcpu->arch.guest_state_protected)
956 return;
957
Babu Moger37486132020-05-12 18:59:06 -0500958 if (static_cpu_has(X86_FEATURE_PKU) &&
959 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
960 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) {
961 vcpu->arch.pkru = rdpkru();
962 if (vcpu->arch.pkru != vcpu->arch.host_pkru)
Thomas Gleixner72a6c082021-06-23 14:02:23 +0200963 write_pkru(vcpu->arch.host_pkru);
Babu Moger37486132020-05-12 18:59:06 -0500964 }
965
Aaron Lewis139a12c2019-10-21 16:30:25 -0700966 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
967
968 if (vcpu->arch.xcr0 != host_xcr0)
969 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
970
971 if (vcpu->arch.xsaves_enabled &&
972 vcpu->arch.ia32_xss != host_xss)
973 wrmsrl(MSR_IA32_XSS, host_xss);
974 }
975
976}
977EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300978
Fengguang Wu69b00492015-01-19 22:33:39 +0800979static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800980{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000981 u64 xcr0 = xcr;
982 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200983 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800984
985 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
986 if (index != XCR_XFEATURE_ENABLED_MASK)
987 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700988 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800989 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700990 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800991 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200992
993 /*
994 * Do not allow the guest to set bits that we do not support
995 * saving. However, xcr0 bit 0 is always set, even if the
996 * emulated CPU does not support XSAVE (see fx_init).
997 */
Dave Hansend91cab72015-09-02 16:31:26 -0700998 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200999 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +08001000 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +02001001
Dave Hansend91cab72015-09-02 16:31:26 -07001002 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
1003 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +00001004 return 1;
1005
Dave Hansend91cab72015-09-02 16:31:26 -07001006 if (xcr0 & XFEATURE_MASK_AVX512) {
1007 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +08001008 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -07001009 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +08001010 return 1;
1011 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08001012 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +00001013
Dave Hansend91cab72015-09-02 16:31:26 -07001014 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08001015 kvm_update_cpuid_runtime(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001016 return 0;
1017}
1018
Sean Christopherson92f98952021-02-04 16:57:46 -08001019int kvm_emulate_xsetbv(struct kvm_vcpu *vcpu)
Dexuan Cui2acf9232010-06-10 11:27:12 +08001020{
Sean Christopherson92f98952021-02-04 16:57:46 -08001021 if (static_call(kvm_x86_get_cpl)(vcpu) != 0 ||
1022 __kvm_set_xcr(vcpu, kvm_rcx_read(vcpu), kvm_read_edx_eax(vcpu))) {
1023 kvm_inject_gp(vcpu, 0);
1024 return 1;
1025 }
Paolo Bonzinibbefd4f2020-12-14 07:49:54 -05001026
Sean Christopherson92f98952021-02-04 16:57:46 -08001027 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001028}
Sean Christopherson92f98952021-02-04 16:57:46 -08001029EXPORT_SYMBOL_GPL(kvm_emulate_xsetbv);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001030
Sean Christophersonee69c922020-10-06 18:44:16 -07001031bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Wanpeng Li3ca94192019-09-18 17:50:10 +08001032{
Sean Christophersonb11306b2019-12-10 14:44:13 -08001033 if (cr4 & cr4_reserved_bits)
Sean Christophersonee69c922020-10-06 18:44:16 -07001034 return false;
Wanpeng Li3ca94192019-09-18 17:50:10 +08001035
Krish Sadhukhanb899c132020-07-08 00:39:55 +00001036 if (cr4 & vcpu->arch.cr4_guest_rsvd_bits)
Sean Christophersonee69c922020-10-06 18:44:16 -07001037 return false;
Wanpeng Li3ca94192019-09-18 17:50:10 +08001038
Jason Baronb36464772021-01-14 22:27:56 -05001039 return static_call(kvm_x86_is_valid_cr4)(vcpu, cr4);
Wanpeng Li3ca94192019-09-18 17:50:10 +08001040}
Sean Christophersonee69c922020-10-06 18:44:16 -07001041EXPORT_SYMBOL_GPL(kvm_is_valid_cr4);
Wanpeng Li3ca94192019-09-18 17:50:10 +08001042
Tom Lendacky5b51cb12020-12-10 11:09:57 -06001043void kvm_post_set_cr4(struct kvm_vcpu *vcpu, unsigned long old_cr4, unsigned long cr4)
1044{
Sean Christopherson20f632b2021-06-22 10:57:02 -07001045 if (((cr4 ^ old_cr4) & KVM_MMU_CR4_ROLE_BITS) ||
Tom Lendacky5b51cb12020-12-10 11:09:57 -06001046 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
1047 kvm_mmu_reset_context(vcpu);
1048}
1049EXPORT_SYMBOL_GPL(kvm_post_set_cr4);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001050
Avi Kivitya83b29c2010-06-10 17:02:15 +03001051int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +01001052{
Avi Kivityfc78f512009-12-07 12:16:48 +02001053 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +08001054 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Jim Mattsoncb957ad2020-08-17 11:16:54 -07001055 X86_CR4_SMEP;
Xiao Guangrong0be02262015-05-11 22:55:21 +08001056
Sean Christophersonee69c922020-10-06 18:44:16 -07001057 if (!kvm_is_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02001058 return 1;
1059
Carsten Ottea03490e2007-10-29 16:09:35 +01001060 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +03001061 if (!(cr4 & X86_CR4_PAE))
1062 return 1;
Sean Christophersond74fcfc2020-07-02 19:17:14 -07001063 if ((cr4 ^ old_cr4) & X86_CR4_LA57)
1064 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +03001065 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
1066 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +02001067 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
1068 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +03001069 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001070
Mao, Junjiead756a12012-07-02 01:18:48 +00001071 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +02001072 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +00001073 return 1;
1074
1075 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
1076 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
1077 return 1;
1078 }
1079
Jason Baronb36464772021-01-14 22:27:56 -05001080 static_call(kvm_x86_set_cr4)(vcpu, cr4);
Gleb Natapov0f122442010-04-28 19:15:31 +03001081
Tom Lendacky5b51cb12020-12-10 11:09:57 -06001082 kvm_post_set_cr4(vcpu, old_cr4, cr4);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001083
Gleb Natapov0f122442010-04-28 19:15:31 +03001084 return 0;
1085}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001086EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +01001087
Sean Christopherson21823fb2021-06-09 16:42:24 -07001088static void kvm_invalidate_pcid(struct kvm_vcpu *vcpu, unsigned long pcid)
1089{
1090 struct kvm_mmu *mmu = vcpu->arch.mmu;
1091 unsigned long roots_to_free = 0;
1092 int i;
1093
1094 /*
1095 * If neither the current CR3 nor any of the prev_roots use the given
1096 * PCID, then nothing needs to be done here because a resync will
1097 * happen anyway before switching to any other CR3.
1098 */
1099 if (kvm_get_active_pcid(vcpu) == pcid) {
Sean Christophersone62f1aa2021-06-09 16:42:32 -07001100 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Sean Christopherson21823fb2021-06-09 16:42:24 -07001101 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
1102 }
1103
1104 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
1105 if (kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd) == pcid)
1106 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
1107
1108 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
1109}
1110
Avi Kivity23902182010-06-10 17:02:16 +03001111int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +01001112{
Junaid Shahidade61e22018-06-27 14:59:15 -07001113 bool skip_tlb_flush = false;
Sean Christopherson21823fb2021-06-09 16:42:24 -07001114 unsigned long pcid = 0;
Paolo Bonziniac146232014-11-10 13:53:25 +01001115#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -07001116 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
1117
Junaid Shahidade61e22018-06-27 14:59:15 -07001118 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001119 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1120 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Sean Christopherson21823fb2021-06-09 16:42:24 -07001121 pcid = cr3 & X86_CR3_PCID_MASK;
Junaid Shahidade61e22018-06-27 14:59:15 -07001122 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001123#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001124
Sean Christophersonc7313152021-06-07 12:01:58 +03001125 /* PDPTRs are always reloaded for PAE paging. */
Sean Christopherson21823fb2021-06-09 16:42:24 -07001126 if (cr3 == kvm_read_cr3(vcpu) && !is_pae_paging(vcpu))
1127 goto handle_tlb_flush;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001128
Sean Christopherson886bbcc2021-04-21 19:21:21 -07001129 /*
1130 * Do not condition the GPA check on long mode, this helper is used to
1131 * stuff CR3, e.g. for RSM emulation, and there is no guarantee that
1132 * the current vCPU mode is accurate.
1133 */
1134 if (kvm_vcpu_is_illegal_gpa(vcpu, cr3))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001135 return 1;
Sean Christopherson886bbcc2021-04-21 19:21:21 -07001136
1137 if (is_pae_paging(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001138 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001139
Sean Christopherson21823fb2021-06-09 16:42:24 -07001140 if (cr3 != kvm_read_cr3(vcpu))
Sean Christophersonb5129102021-06-09 16:42:27 -07001141 kvm_mmu_new_pgd(vcpu, cr3);
Sean Christopherson21823fb2021-06-09 16:42:24 -07001142
Gleb Natapov0f122442010-04-28 19:15:31 +03001143 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001144 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001145
Sean Christopherson21823fb2021-06-09 16:42:24 -07001146handle_tlb_flush:
1147 /*
1148 * A load of CR3 that flushes the TLB flushes only the current PCID,
1149 * even if PCID is disabled, in which case PCID=0 is flushed. It's a
1150 * moot point in the end because _disabling_ PCID will flush all PCIDs,
1151 * and it's impossible to use a non-zero PCID when PCID is disabled,
1152 * i.e. only PCID=0 can be relevant.
1153 */
1154 if (!skip_tlb_flush)
1155 kvm_invalidate_pcid(vcpu, pcid);
1156
Gleb Natapov0f122442010-04-28 19:15:31 +03001157 return 0;
1158}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001159EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001160
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001161int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001162{
Gleb Natapov0f122442010-04-28 19:15:31 +03001163 if (cr8 & CR8_RESERVED_BITS)
1164 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001165 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001166 kvm_lapic_set_tpr(vcpu, cr8);
1167 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001168 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001169 return 0;
1170}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001171EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001172
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001173unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001174{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001175 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001176 return kvm_lapic_get_cr8(vcpu);
1177 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001178 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001179}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001180EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001181
Nadav Amitae561ed2015-04-02 03:10:37 +03001182static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1183{
1184 int i;
1185
1186 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1187 for (i = 0; i < KVM_NR_DB_REGS; i++)
1188 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Nadav Amitae561ed2015-04-02 03:10:37 +03001189 }
1190}
1191
Paolo Bonzini7c866632020-05-16 08:42:28 -04001192void kvm_update_dr7(struct kvm_vcpu *vcpu)
Jan Kiszkac8639012012-09-21 05:42:55 +02001193{
1194 unsigned long dr7;
1195
1196 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1197 dr7 = vcpu->arch.guest_debug_dr7;
1198 else
1199 dr7 = vcpu->arch.dr7;
Jason Baronb36464772021-01-14 22:27:56 -05001200 static_call(kvm_x86_set_dr7)(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001201 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1202 if (dr7 & DR7_BP_EN_MASK)
1203 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001204}
Paolo Bonzini7c866632020-05-16 08:42:28 -04001205EXPORT_SYMBOL_GPL(kvm_update_dr7);
Jan Kiszkac8639012012-09-21 05:42:55 +02001206
Nadav Amit6f43ed02014-07-15 17:37:46 +03001207static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1208{
1209 u64 fixed = DR6_FIXED_1;
1210
Radim Krčmářd6321d42017-08-05 00:12:49 +02001211 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001212 fixed |= DR6_RTM;
Chenyi Qiange8ea85f2021-02-02 17:04:32 +08001213
1214 if (!guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))
1215 fixed |= DR6_BUS_LOCK;
Nadav Amit6f43ed02014-07-15 17:37:46 +03001216 return fixed;
1217}
1218
Paolo Bonzini996ff542020-12-14 07:49:54 -05001219int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001220{
Marios Pomonisea740052019-12-11 12:47:52 -08001221 size_t size = ARRAY_SIZE(vcpu->arch.db);
1222
Gleb Natapov020df072010-04-13 10:05:23 +03001223 switch (dr) {
1224 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001225 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001226 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1227 vcpu->arch.eff_db[dr] = val;
1228 break;
1229 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001230 case 6:
Krish Sadhukhanf5f61452020-05-22 18:19:51 -04001231 if (!kvm_dr6_valid(val))
Paolo Bonzini996ff542020-12-14 07:49:54 -05001232 return 1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001233 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001234 break;
1235 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001236 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001237 if (!kvm_dr7_valid(val))
Paolo Bonzini996ff542020-12-14 07:49:54 -05001238 return 1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001239 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001240 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001241 break;
1242 }
1243
1244 return 0;
1245}
1246EXPORT_SYMBOL_GPL(kvm_set_dr);
1247
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05001248void kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001249{
Marios Pomonisea740052019-12-11 12:47:52 -08001250 size_t size = ARRAY_SIZE(vcpu->arch.db);
1251
Gleb Natapov020df072010-04-13 10:05:23 +03001252 switch (dr) {
1253 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001254 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001255 break;
1256 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001257 case 6:
Paolo Bonzini5679b802020-05-04 11:28:25 -04001258 *val = vcpu->arch.dr6;
Gleb Natapov020df072010-04-13 10:05:23 +03001259 break;
1260 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001261 default: /* 7 */
1262 *val = vcpu->arch.dr7;
1263 break;
1264 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001265}
Gleb Natapov020df072010-04-13 10:05:23 +03001266EXPORT_SYMBOL_GPL(kvm_get_dr);
1267
Sean Christophersonc483c452021-02-04 16:57:48 -08001268int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu)
Avi Kivity022cd0e2011-11-10 14:57:23 +02001269{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001270 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001271 u64 data;
Avi Kivity022cd0e2011-11-10 14:57:23 +02001272
Sean Christophersonc483c452021-02-04 16:57:48 -08001273 if (kvm_pmu_rdpmc(vcpu, ecx, &data)) {
1274 kvm_inject_gp(vcpu, 0);
1275 return 1;
1276 }
1277
Sean Christophersonde3cd112019-04-30 10:36:17 -07001278 kvm_rax_write(vcpu, (u32)data);
1279 kvm_rdx_write(vcpu, data >> 32);
Sean Christophersonc483c452021-02-04 16:57:48 -08001280 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001281}
Sean Christophersonc483c452021-02-04 16:57:48 -08001282EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001283
Carsten Otte043405e2007-10-10 17:16:19 +02001284/*
1285 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1286 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1287 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001288 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1289 * extract the supported MSRs from the related const lists.
1290 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001291 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001292 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001293 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001294 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001295
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001296static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001297 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001298 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001299#ifdef CONFIG_X86_64
1300 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1301#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001302 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001303 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001304 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001305 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1306 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1307 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1308 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1309 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1310 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001311 MSR_IA32_UMWAIT_CONTROL,
1312
Jim Mattsone2ada662019-08-21 11:20:04 -07001313 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1314 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1315 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1316 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1317 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1318 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1319 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1320 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1321 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1322 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1323 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1324 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1325 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001326 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1327 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1328 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1329 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1330 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1331 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1332 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1333 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1334 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001335};
1336
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001337static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001338static unsigned num_msrs_to_save;
1339
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001340static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001341 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1342 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1343 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1344 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001345 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001346 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1347 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001348 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001349 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001350 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001351 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001352 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001353 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001354 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1355 HV_X64_MSR_TSC_EMULATION_STATUS,
Jon Doronf97f5a52020-05-29 16:45:40 +03001356 HV_X64_MSR_SYNDBG_OPTIONS,
1357 HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS,
1358 HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER,
1359 HV_X64_MSR_SYNDBG_PENDING_BUFFER,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001360
1361 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02001362 MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001363
Will Auldba904632012-11-29 12:42:50 -08001364 MSR_IA32_TSC_ADJUST,
Dave Hansen09141ec2020-03-05 09:47:06 -08001365 MSR_IA32_TSC_DEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001366 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001367 MSR_IA32_PERF_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001368 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001369 MSR_IA32_MCG_STATUS,
1370 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001371 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001372 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001373 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001374 MSR_PLATFORM_INFO,
1375 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001376 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001377 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001378 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001379
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001380 /*
1381 * The following list leaves out MSRs whose values are determined
1382 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1383 * We always support the "true" VMX control MSRs, even if the host
1384 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001385 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001386 */
1387 MSR_IA32_VMX_BASIC,
1388 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1389 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1390 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1391 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1392 MSR_IA32_VMX_MISC,
1393 MSR_IA32_VMX_CR0_FIXED0,
1394 MSR_IA32_VMX_CR4_FIXED0,
1395 MSR_IA32_VMX_VMCS_ENUM,
1396 MSR_IA32_VMX_PROCBASED_CTLS2,
1397 MSR_IA32_VMX_EPT_VPID_CAP,
1398 MSR_IA32_VMX_VMFUNC,
1399
Borislav Petkov191c8132019-04-18 18:32:50 +02001400 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001401 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001402};
1403
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001404static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001405static unsigned num_emulated_msrs;
1406
Tom Lendacky801e4592018-02-21 13:39:51 -06001407/*
1408 * List of msr numbers which are used to expose MSR-based features that
1409 * can be used by a hypervisor to validate requested CPU features.
1410 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001411static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001412 MSR_IA32_VMX_BASIC,
1413 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1414 MSR_IA32_VMX_PINBASED_CTLS,
1415 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1416 MSR_IA32_VMX_PROCBASED_CTLS,
1417 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1418 MSR_IA32_VMX_EXIT_CTLS,
1419 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1420 MSR_IA32_VMX_ENTRY_CTLS,
1421 MSR_IA32_VMX_MISC,
1422 MSR_IA32_VMX_CR0_FIXED0,
1423 MSR_IA32_VMX_CR0_FIXED1,
1424 MSR_IA32_VMX_CR4_FIXED0,
1425 MSR_IA32_VMX_CR4_FIXED1,
1426 MSR_IA32_VMX_VMCS_ENUM,
1427 MSR_IA32_VMX_PROCBASED_CTLS2,
1428 MSR_IA32_VMX_EPT_VPID_CAP,
1429 MSR_IA32_VMX_VMFUNC,
1430
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001431 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001432 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001433 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001434 MSR_IA32_PERF_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001435};
1436
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001437static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001438static unsigned int num_msr_based_features;
1439
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001440static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001441{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001442 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001443
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001444 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1445 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001446
1447 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001448 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1449 * the nested hypervisor runs with NX huge pages. If it is not,
Ingo Molnard9f6e122021-03-18 15:28:01 +01001450 * L1 is anyway vulnerable to ITLB_MULTIHIT exploits from other
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001451 * L1 guests, so it need not worry about its own (L2) guests.
1452 */
1453 data |= ARCH_CAP_PSCHANGE_MC_NO;
1454
1455 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001456 * If we're doing cache flushes (either "always" or "cond")
1457 * we will do one whenever the guest does a vmlaunch/vmresume.
1458 * If an outer hypervisor is doing the cache flush for us
1459 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1460 * capability to the guest too, and if EPT is disabled we're not
1461 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1462 * require a nested hypervisor to do a flush of its own.
1463 */
1464 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1465 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1466
Paolo Bonzini0c549142019-08-19 17:24:07 +02001467 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1468 data |= ARCH_CAP_RDCL_NO;
1469 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1470 data |= ARCH_CAP_SSB_NO;
1471 if (!boot_cpu_has_bug(X86_BUG_MDS))
1472 data |= ARCH_CAP_MDS_NO;
1473
Paolo Bonzini71316362021-01-28 11:45:00 -05001474 if (!boot_cpu_has(X86_FEATURE_RTM)) {
1475 /*
1476 * If RTM=0 because the kernel has disabled TSX, the host might
1477 * have TAA_NO or TSX_CTRL. Clear TAA_NO (the guest sees RTM=0
1478 * and therefore knows that there cannot be TAA) but keep
1479 * TSX_CTRL: some buggy userspaces leave it set on tsx=on hosts,
1480 * and we want to allow migrating those guests to tsx=off hosts.
1481 */
1482 data &= ~ARCH_CAP_TAA_NO;
1483 } else if (!boot_cpu_has_bug(X86_BUG_TAA)) {
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001484 data |= ARCH_CAP_TAA_NO;
Paolo Bonzini71316362021-01-28 11:45:00 -05001485 } else {
1486 /*
1487 * Nothing to do here; we emulate TSX_CTRL if present on the
1488 * host so the guest can choose between disabling TSX or
1489 * using VERW to clear CPU buffers.
1490 */
1491 }
Pawan Guptae1d38b62019-10-23 12:23:33 +02001492
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001493 return data;
1494}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001495
Wanpeng Li66421c12018-02-28 14:03:30 +08001496static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1497{
1498 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001499 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001500 msr->data = kvm_get_arch_capabilities();
1501 break;
1502 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001503 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001504 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001505 default:
Jason Baronb36464772021-01-14 22:27:56 -05001506 return static_call(kvm_x86_get_msr_feature)(msr);
Wanpeng Li66421c12018-02-28 14:03:30 +08001507 }
1508 return 0;
1509}
1510
Tom Lendacky801e4592018-02-21 13:39:51 -06001511static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1512{
1513 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001514 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001515
1516 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001517 r = kvm_get_msr_feature(&msr);
Peter Xu12bc2132020-06-22 18:04:42 -04001518
1519 if (r == KVM_MSR_RET_INVALID) {
1520 /* Unconditionally clear the output for simplicity */
1521 *data = 0;
Haiwei Lid6328262021-03-13 13:10:32 +08001522 if (kvm_msr_ignored_check(index, 0, false))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001523 r = 0;
Peter Xu12bc2132020-06-22 18:04:42 -04001524 }
1525
Wanpeng Li66421c12018-02-28 14:03:30 +08001526 if (r)
1527 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001528
1529 *data = msr.data;
1530
1531 return 0;
1532}
1533
Sean Christopherson11988492019-04-02 08:19:15 -07001534static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1535{
1536 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1537 return false;
1538
1539 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1540 return false;
1541
Sean Christopherson0a629562019-04-02 08:19:16 -07001542 if (efer & (EFER_LME | EFER_LMA) &&
1543 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1544 return false;
1545
1546 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1547 return false;
1548
Sean Christopherson11988492019-04-02 08:19:15 -07001549 return true;
1550
1551}
Jan Kiszka384bb782013-04-20 10:52:36 +02001552bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001553{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001554 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001555 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001556
Sean Christopherson11988492019-04-02 08:19:15 -07001557 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001558}
1559EXPORT_SYMBOL_GPL(kvm_valid_efer);
1560
Sean Christopherson11988492019-04-02 08:19:15 -07001561static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001562{
1563 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001564 u64 efer = msr_info->data;
Maxim Levitsky72f211e2020-10-01 14:29:53 +03001565 int r;
Jan Kiszka384bb782013-04-20 10:52:36 +02001566
Sean Christopherson11988492019-04-02 08:19:15 -07001567 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001568 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001569
Sean Christopherson11988492019-04-02 08:19:15 -07001570 if (!msr_info->host_initiated) {
1571 if (!__kvm_valid_efer(vcpu, efer))
1572 return 1;
1573
1574 if (is_paging(vcpu) &&
1575 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1576 return 1;
1577 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001578
Carsten Otte15c4a642007-10-30 18:44:17 +01001579 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001580 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001581
Jason Baronb36464772021-01-14 22:27:56 -05001582 r = static_call(kvm_x86_set_efer)(vcpu, efer);
Maxim Levitsky72f211e2020-10-01 14:29:53 +03001583 if (r) {
1584 WARN_ON(r > 0);
1585 return r;
1586 }
Sheng Yanga3d204e2010-05-12 16:40:40 +08001587
Sheng Yangaad82702010-05-12 16:40:42 +08001588 /* Update reserved bits */
1589 if ((efer ^ old_efer) & EFER_NX)
1590 kvm_mmu_reset_context(vcpu);
1591
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001592 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001593}
1594
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001595void kvm_enable_efer_bits(u64 mask)
1596{
1597 efer_reserved_bits &= ~mask;
1598}
1599EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1600
Alexander Graf51de8152020-09-25 16:34:17 +02001601bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type)
1602{
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001603 struct kvm_x86_msr_filter *msr_filter;
1604 struct msr_bitmap_range *ranges;
Alexander Graf1a1552542020-09-25 16:34:21 +02001605 struct kvm *kvm = vcpu->kvm;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001606 bool allowed;
Alexander Graf1a1552542020-09-25 16:34:21 +02001607 int idx;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001608 u32 i;
Alexander Graf1a1552542020-09-25 16:34:21 +02001609
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001610 /* x2APIC MSRs do not support filtering. */
1611 if (index >= 0x800 && index <= 0x8ff)
Alexander Graf1a1552542020-09-25 16:34:21 +02001612 return true;
1613
Alexander Graf1a1552542020-09-25 16:34:21 +02001614 idx = srcu_read_lock(&kvm->srcu);
1615
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001616 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu);
1617 if (!msr_filter) {
1618 allowed = true;
1619 goto out;
1620 }
1621
1622 allowed = msr_filter->default_allow;
1623 ranges = msr_filter->ranges;
1624
1625 for (i = 0; i < msr_filter->count; i++) {
Alexander Graf1a1552542020-09-25 16:34:21 +02001626 u32 start = ranges[i].base;
1627 u32 end = start + ranges[i].nmsrs;
1628 u32 flags = ranges[i].flags;
1629 unsigned long *bitmap = ranges[i].bitmap;
1630
1631 if ((index >= start) && (index < end) && (flags & type)) {
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001632 allowed = !!test_bit(index - start, bitmap);
Alexander Graf1a1552542020-09-25 16:34:21 +02001633 break;
1634 }
1635 }
1636
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001637out:
Alexander Graf1a1552542020-09-25 16:34:21 +02001638 srcu_read_unlock(&kvm->srcu, idx);
1639
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001640 return allowed;
Alexander Graf51de8152020-09-25 16:34:17 +02001641}
1642EXPORT_SYMBOL_GPL(kvm_msr_allowed);
1643
Carsten Otte15c4a642007-10-30 18:44:17 +01001644/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001645 * Write @data into the MSR specified by @index. Select MSR specific fault
1646 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001647 * Returns 0 on success, non-0 otherwise.
1648 * Assumes vcpu_load() was already called.
1649 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001650static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1651 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001652{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001653 struct msr_data msr;
1654
Alexander Graf1a1552542020-09-25 16:34:21 +02001655 if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_WRITE))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001656 return KVM_MSR_RET_FILTERED;
Alexander Graf1a1552542020-09-25 16:34:21 +02001657
Sean Christophersonf20935d2019-09-05 14:22:54 -07001658 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001659 case MSR_FS_BASE:
1660 case MSR_GS_BASE:
1661 case MSR_KERNEL_GS_BASE:
1662 case MSR_CSTAR:
1663 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001664 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001665 return 1;
1666 break;
1667 case MSR_IA32_SYSENTER_EIP:
1668 case MSR_IA32_SYSENTER_ESP:
1669 /*
1670 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1671 * non-canonical address is written on Intel but not on
1672 * AMD (which ignores the top 32-bits, because it does
1673 * not implement 64-bit SYSENTER).
1674 *
1675 * 64-bit code should hence be able to write a non-canonical
1676 * value on AMD. Making the address canonical ensures that
1677 * vmentry does not fail on Intel after writing a non-canonical
1678 * value, and that something deterministic happens if the guest
1679 * invokes 64-bit SYSENTER.
1680 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001681 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Sean Christopherson61a05d42021-05-04 10:17:33 -07001682 break;
1683 case MSR_TSC_AUX:
1684 if (!kvm_is_supported_user_return_msr(MSR_TSC_AUX))
1685 return 1;
1686
1687 if (!host_initiated &&
1688 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) &&
1689 !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))
1690 return 1;
1691
1692 /*
1693 * Per Intel's SDM, bits 63:32 are reserved, but AMD's APM has
1694 * incomplete and conflicting architectural behavior. Current
1695 * AMD CPUs completely ignore bits 63:32, i.e. they aren't
1696 * reserved and always read as zeros. Enforce Intel's reserved
1697 * bits check if and only if the guest CPU is Intel, and clear
1698 * the bits in all other cases. This ensures cross-vendor
1699 * migration will provide consistent behavior for the guest.
1700 */
1701 if (guest_cpuid_is_intel(vcpu) && (data >> 32) != 0)
1702 return 1;
1703
1704 data = (u32)data;
1705 break;
Nadav Amit854e8bb2014-09-16 03:24:05 +03001706 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001707
1708 msr.data = data;
1709 msr.index = index;
1710 msr.host_initiated = host_initiated;
1711
Jason Baronb36464772021-01-14 22:27:56 -05001712 return static_call(kvm_x86_set_msr)(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001713}
1714
Peter Xu6abe9c12020-06-22 18:04:41 -04001715static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu,
1716 u32 index, u64 data, bool host_initiated)
1717{
1718 int ret = __kvm_set_msr(vcpu, index, data, host_initiated);
1719
1720 if (ret == KVM_MSR_RET_INVALID)
Haiwei Lid6328262021-03-13 13:10:32 +08001721 if (kvm_msr_ignored_check(index, data, true))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001722 ret = 0;
Peter Xu6abe9c12020-06-22 18:04:41 -04001723
1724 return ret;
1725}
1726
Sean Christophersonf20935d2019-09-05 14:22:54 -07001727/*
1728 * Read the MSR specified by @index into @data. Select MSR specific fault
1729 * checks are bypassed if @host_initiated is %true.
1730 * Returns 0 on success, non-0 otherwise.
1731 * Assumes vcpu_load() was already called.
1732 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001733int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1734 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001735{
1736 struct msr_data msr;
1737 int ret;
1738
Alexander Graf1a1552542020-09-25 16:34:21 +02001739 if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_READ))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001740 return KVM_MSR_RET_FILTERED;
Alexander Graf1a1552542020-09-25 16:34:21 +02001741
Sean Christopherson61a05d42021-05-04 10:17:33 -07001742 switch (index) {
1743 case MSR_TSC_AUX:
1744 if (!kvm_is_supported_user_return_msr(MSR_TSC_AUX))
1745 return 1;
1746
1747 if (!host_initiated &&
1748 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) &&
1749 !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))
1750 return 1;
1751 break;
1752 }
1753
Sean Christophersonf20935d2019-09-05 14:22:54 -07001754 msr.index = index;
1755 msr.host_initiated = host_initiated;
1756
Jason Baronb36464772021-01-14 22:27:56 -05001757 ret = static_call(kvm_x86_get_msr)(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001758 if (!ret)
1759 *data = msr.data;
1760 return ret;
1761}
1762
Peter Xu6abe9c12020-06-22 18:04:41 -04001763static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu,
1764 u32 index, u64 *data, bool host_initiated)
1765{
1766 int ret = __kvm_get_msr(vcpu, index, data, host_initiated);
1767
1768 if (ret == KVM_MSR_RET_INVALID) {
1769 /* Unconditionally clear *data for simplicity */
1770 *data = 0;
Haiwei Lid6328262021-03-13 13:10:32 +08001771 if (kvm_msr_ignored_check(index, 0, false))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001772 ret = 0;
Peter Xu6abe9c12020-06-22 18:04:41 -04001773 }
1774
1775 return ret;
1776}
1777
Sean Christophersonf20935d2019-09-05 14:22:54 -07001778int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1779{
Peter Xu6abe9c12020-06-22 18:04:41 -04001780 return kvm_get_msr_ignored_check(vcpu, index, data, false);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001781}
1782EXPORT_SYMBOL_GPL(kvm_get_msr);
1783
1784int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1785{
Peter Xu6abe9c12020-06-22 18:04:41 -04001786 return kvm_set_msr_ignored_check(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001787}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001788EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001789
Paolo Bonzini8b474422020-12-14 07:44:46 -05001790static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu)
Alexander Graf1ae09952020-09-25 16:34:16 +02001791{
Paolo Bonzini8b474422020-12-14 07:44:46 -05001792 int err = vcpu->run->msr.error;
1793 if (!err) {
Alexander Graf1ae09952020-09-25 16:34:16 +02001794 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data);
1795 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32);
1796 }
1797
Jason Baronb36464772021-01-14 22:27:56 -05001798 return static_call(kvm_x86_complete_emulated_msr)(vcpu, err);
Alexander Graf1ae09952020-09-25 16:34:16 +02001799}
1800
1801static int complete_emulated_wrmsr(struct kvm_vcpu *vcpu)
1802{
Jason Baronb36464772021-01-14 22:27:56 -05001803 return static_call(kvm_x86_complete_emulated_msr)(vcpu, vcpu->run->msr.error);
Alexander Graf1ae09952020-09-25 16:34:16 +02001804}
1805
1806static u64 kvm_msr_reason(int r)
1807{
1808 switch (r) {
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001809 case KVM_MSR_RET_INVALID:
Alexander Graf1ae09952020-09-25 16:34:16 +02001810 return KVM_MSR_EXIT_REASON_UNKNOWN;
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001811 case KVM_MSR_RET_FILTERED:
Alexander Graf1a1552542020-09-25 16:34:21 +02001812 return KVM_MSR_EXIT_REASON_FILTER;
Alexander Graf1ae09952020-09-25 16:34:16 +02001813 default:
1814 return KVM_MSR_EXIT_REASON_INVAL;
1815 }
1816}
1817
1818static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index,
1819 u32 exit_reason, u64 data,
1820 int (*completion)(struct kvm_vcpu *vcpu),
1821 int r)
1822{
1823 u64 msr_reason = kvm_msr_reason(r);
1824
1825 /* Check if the user wanted to know about this MSR fault */
1826 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason))
1827 return 0;
1828
1829 vcpu->run->exit_reason = exit_reason;
1830 vcpu->run->msr.error = 0;
1831 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad));
1832 vcpu->run->msr.reason = msr_reason;
1833 vcpu->run->msr.index = index;
1834 vcpu->run->msr.data = data;
1835 vcpu->arch.complete_userspace_io = completion;
1836
1837 return 1;
1838}
1839
1840static int kvm_get_msr_user_space(struct kvm_vcpu *vcpu, u32 index, int r)
1841{
1842 return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_RDMSR, 0,
1843 complete_emulated_rdmsr, r);
1844}
1845
1846static int kvm_set_msr_user_space(struct kvm_vcpu *vcpu, u32 index, u64 data, int r)
1847{
1848 return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_WRMSR, data,
1849 complete_emulated_wrmsr, r);
1850}
1851
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001852int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1853{
1854 u32 ecx = kvm_rcx_read(vcpu);
1855 u64 data;
Alexander Graf1ae09952020-09-25 16:34:16 +02001856 int r;
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001857
Alexander Graf1ae09952020-09-25 16:34:16 +02001858 r = kvm_get_msr(vcpu, ecx, &data);
1859
1860 /* MSR read failed? See if we should ask user space */
1861 if (r && kvm_get_msr_user_space(vcpu, ecx, r)) {
1862 /* Bounce to user space */
1863 return 0;
1864 }
1865
Paolo Bonzini8b474422020-12-14 07:44:46 -05001866 if (!r) {
1867 trace_kvm_msr_read(ecx, data);
1868
1869 kvm_rax_write(vcpu, data & -1u);
1870 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1871 } else {
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001872 trace_kvm_msr_read_ex(ecx);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001873 }
1874
Jason Baronb36464772021-01-14 22:27:56 -05001875 return static_call(kvm_x86_complete_emulated_msr)(vcpu, r);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001876}
1877EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1878
1879int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1880{
1881 u32 ecx = kvm_rcx_read(vcpu);
1882 u64 data = kvm_read_edx_eax(vcpu);
Alexander Graf1ae09952020-09-25 16:34:16 +02001883 int r;
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001884
Alexander Graf1ae09952020-09-25 16:34:16 +02001885 r = kvm_set_msr(vcpu, ecx, data);
1886
1887 /* MSR write failed? See if we should ask user space */
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03001888 if (r && kvm_set_msr_user_space(vcpu, ecx, data, r))
Alexander Graf1ae09952020-09-25 16:34:16 +02001889 /* Bounce to user space */
1890 return 0;
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03001891
1892 /* Signal all other negative errors to userspace */
1893 if (r < 0)
1894 return r;
Alexander Graf1ae09952020-09-25 16:34:16 +02001895
Paolo Bonzini8b474422020-12-14 07:44:46 -05001896 if (!r)
1897 trace_kvm_msr_write(ecx, data);
1898 else
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001899 trace_kvm_msr_write_ex(ecx, data);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001900
Jason Baronb36464772021-01-14 22:27:56 -05001901 return static_call(kvm_x86_complete_emulated_msr)(vcpu, r);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001902}
1903EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1904
Sean Christopherson5ff3a352021-02-04 16:57:47 -08001905int kvm_emulate_as_nop(struct kvm_vcpu *vcpu)
1906{
1907 return kvm_skip_emulated_instruction(vcpu);
1908}
1909EXPORT_SYMBOL_GPL(kvm_emulate_as_nop);
1910
1911int kvm_emulate_invd(struct kvm_vcpu *vcpu)
1912{
1913 /* Treat an INVD instruction as a NOP and just skip it. */
1914 return kvm_emulate_as_nop(vcpu);
1915}
1916EXPORT_SYMBOL_GPL(kvm_emulate_invd);
1917
1918int kvm_emulate_mwait(struct kvm_vcpu *vcpu)
1919{
1920 pr_warn_once("kvm: MWAIT instruction emulated as NOP!\n");
1921 return kvm_emulate_as_nop(vcpu);
1922}
1923EXPORT_SYMBOL_GPL(kvm_emulate_mwait);
1924
1925int kvm_handle_invalid_op(struct kvm_vcpu *vcpu)
1926{
1927 kvm_queue_exception(vcpu, UD_VECTOR);
1928 return 1;
1929}
1930EXPORT_SYMBOL_GPL(kvm_handle_invalid_op);
1931
1932int kvm_emulate_monitor(struct kvm_vcpu *vcpu)
1933{
1934 pr_warn_once("kvm: MONITOR instruction emulated as NOP!\n");
1935 return kvm_emulate_as_nop(vcpu);
1936}
1937EXPORT_SYMBOL_GPL(kvm_emulate_monitor);
1938
Paolo Bonzinid89d04a2021-02-02 10:44:23 -05001939static inline bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001940{
Frederic Weisbecker4ae7dc92021-02-01 00:05:48 +01001941 xfer_to_guest_mode_prepare();
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001942 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02001943 xfer_to_guest_mode_work_pending();
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001944}
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001945
Carsten Otte313a3dc2007-10-11 19:16:52 +02001946/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001947 * The fast path for frequent and performance sensitive wrmsr emulation,
1948 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1949 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1950 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1951 * other cases which must be called after interrupts are enabled on the host.
1952 */
1953static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1954{
Wanpeng Lie1be9ac2020-03-26 10:20:01 +08001955 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1956 return 1;
1957
1958 if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001959 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
Wanpeng Li4064a4c2020-04-02 16:20:26 +08001960 ((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
1961 ((u32)(data >> 32) != X2APIC_BROADCAST)) {
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001962
Wanpeng Lid5361672020-03-26 10:20:02 +08001963 data &= ~(1 << 12);
1964 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001965 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001966 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1967 trace_kvm_apic_write(APIC_ICR, (u32)data);
1968 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001969 }
1970
1971 return 1;
1972}
1973
Wanpeng Liae95f562020-04-28 14:23:28 +08001974static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data)
1975{
1976 if (!kvm_can_use_hv_timer(vcpu))
1977 return 1;
1978
1979 kvm_set_lapic_tscdeadline_msr(vcpu, data);
1980 return 0;
1981}
1982
Wanpeng Li404d5d72020-04-28 14:23:25 +08001983fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001984{
1985 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001986 u64 data;
Wanpeng Li404d5d72020-04-28 14:23:25 +08001987 fastpath_t ret = EXIT_FASTPATH_NONE;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001988
1989 switch (msr) {
1990 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001991 data = kvm_read_edx_eax(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08001992 if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) {
1993 kvm_skip_emulated_instruction(vcpu);
1994 ret = EXIT_FASTPATH_EXIT_HANDLED;
Haiwei Li80bc97f2020-05-18 09:31:38 +08001995 }
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001996 break;
Dave Hansen09141ec2020-03-05 09:47:06 -08001997 case MSR_IA32_TSC_DEADLINE:
Wanpeng Liae95f562020-04-28 14:23:28 +08001998 data = kvm_read_edx_eax(vcpu);
1999 if (!handle_fastpath_set_tscdeadline(vcpu, data)) {
2000 kvm_skip_emulated_instruction(vcpu);
2001 ret = EXIT_FASTPATH_REENTER_GUEST;
2002 }
2003 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08002004 default:
Wanpeng Li404d5d72020-04-28 14:23:25 +08002005 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08002006 }
2007
Wanpeng Li404d5d72020-04-28 14:23:25 +08002008 if (ret != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08002009 trace_kvm_msr_write(msr, data);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08002010
Wanpeng Li404d5d72020-04-28 14:23:25 +08002011 return ret;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08002012}
2013EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
2014
2015/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02002016 * Adapt set_msr() to msr_io()'s calling convention
2017 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002018static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2019{
Peter Xu6abe9c12020-06-22 18:04:41 -04002020 return kvm_get_msr_ignored_check(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002021}
2022
Carsten Otte313a3dc2007-10-11 19:16:52 +02002023static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2024{
Peter Xu6abe9c12020-06-22 18:04:41 -04002025 return kvm_set_msr_ignored_check(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002026}
2027
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002028#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002029struct pvclock_clock {
2030 int vclock_mode;
2031 u64 cycle_last;
2032 u64 mask;
2033 u32 mult;
2034 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002035 u64 base_cycles;
2036 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002037};
2038
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002039struct pvclock_gtod_data {
2040 seqcount_t seq;
2041
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002042 struct pvclock_clock clock; /* extract of a clocksource struct */
2043 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002044
Paolo Bonzini917f9472020-01-22 14:32:20 +01002045 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002046 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002047};
2048
2049static struct pvclock_gtod_data pvclock_gtod_data;
2050
2051static void update_pvclock_gtod(struct timekeeper *tk)
2052{
2053 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
2054
2055 write_seqcount_begin(&vdata->seq);
2056
2057 /* copy pvclock gtod data */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002058 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Peter Zijlstra876e7882015-03-19 10:09:06 +01002059 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
2060 vdata->clock.mask = tk->tkr_mono.mask;
2061 vdata->clock.mult = tk->tkr_mono.mult;
2062 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002063 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
2064 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002065
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002066 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002067 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
2068 vdata->raw_clock.mask = tk->tkr_raw.mask;
2069 vdata->raw_clock.mult = tk->tkr_raw.mult;
2070 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002071 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
2072 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002073
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002074 vdata->wall_time_sec = tk->xtime_sec;
2075
Paolo Bonzini917f9472020-01-22 14:32:20 +01002076 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002077
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002078 write_seqcount_end(&vdata->seq);
2079}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002080
2081static s64 get_kvmclock_base_ns(void)
2082{
2083 /* Count up from boot time, but with the frequency of the raw clock. */
2084 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
2085}
2086#else
2087static s64 get_kvmclock_base_ns(void)
2088{
2089 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
2090 return ktime_get_boottime_ns();
2091}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002092#endif
2093
Joao Martins629b5342018-06-28 15:06:43 -04002094void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock, int sec_hi_ofs)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002095{
Avi Kivity9ed3c442010-05-04 15:00:37 +03002096 int version;
2097 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002098 struct pvclock_wall_clock wc;
Joao Martins629b5342018-06-28 15:06:43 -04002099 u32 wc_sec_hi;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002100 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002101
2102 if (!wall_clock)
2103 return;
2104
Avi Kivity9ed3c442010-05-04 15:00:37 +03002105 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
2106 if (r)
2107 return;
2108
2109 if (version & 1)
2110 ++version; /* first time write, random junk */
2111
2112 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002113
Nicholas Krause1dab1342015-12-30 13:08:46 -05002114 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
2115 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002116
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002117 /*
2118 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10002119 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002120 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002121 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002122 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002123
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002124 wc.nsec = do_div(wall_nsec, 1000000000);
2125 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002126 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002127
2128 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
2129
Joao Martins629b5342018-06-28 15:06:43 -04002130 if (sec_hi_ofs) {
2131 wc_sec_hi = wall_nsec >> 32;
2132 kvm_write_guest(kvm, wall_clock + sec_hi_ofs,
2133 &wc_sec_hi, sizeof(wc_sec_hi));
2134 }
2135
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002136 version++;
2137 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002138}
2139
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00002140static void kvm_write_system_time(struct kvm_vcpu *vcpu, gpa_t system_time,
2141 bool old_msr, bool host_initiated)
2142{
2143 struct kvm_arch *ka = &vcpu->kvm->arch;
2144
2145 if (vcpu->vcpu_id == 0 && !host_initiated) {
Oliver Upton1e293d12020-10-27 16:10:43 -07002146 if (ka->boot_vcpu_runs_old_kvmclock != old_msr)
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00002147 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
2148
2149 ka->boot_vcpu_runs_old_kvmclock = old_msr;
2150 }
2151
2152 vcpu->arch.time = system_time;
2153 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
2154
2155 /* we verify if the enable bit is set... */
2156 vcpu->arch.pv_time_enabled = false;
2157 if (!(system_time & 1))
2158 return;
2159
2160 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
2161 &vcpu->arch.pv_time, system_time & ~1ULL,
2162 sizeof(struct pvclock_vcpu_time_info)))
2163 vcpu->arch.pv_time_enabled = true;
2164
2165 return;
2166}
2167
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002168static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
2169{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01002170 do_shl32_div32(dividend, divisor);
2171 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002172}
2173
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002174static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002175 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002176{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002177 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002178 int32_t shift = 0;
2179 uint64_t tps64;
2180 uint32_t tps32;
2181
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002182 tps64 = base_hz;
2183 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02002184 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002185 tps64 >>= 1;
2186 shift--;
2187 }
2188
2189 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02002190 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
2191 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002192 scaled64 >>= 1;
2193 else
2194 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002195 shift++;
2196 }
2197
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002198 *pshift = shift;
2199 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002200}
2201
Marcelo Tosattid8281992012-11-27 23:29:01 -02002202#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002203static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002204#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002205
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002206static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08002207static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002208
Zachary Amsdencc578282012-02-03 15:43:50 -02002209static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10002210{
Zachary Amsdencc578282012-02-03 15:43:50 -02002211 u64 v = (u64)khz * (1000000 + ppm);
2212 do_div(v, 1000000);
2213 return v;
2214}
2215
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002216static void kvm_vcpu_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 l1_multiplier);
2217
Haozhong Zhang381d5852015-10-20 15:39:04 +08002218static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
2219{
2220 u64 ratio;
2221
2222 /* Guest TSC same frequency as host TSC? */
2223 if (!scale) {
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002224 kvm_vcpu_write_tsc_multiplier(vcpu, kvm_default_tsc_scaling_ratio);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002225 return 0;
2226 }
2227
2228 /* TSC scaling supported? */
2229 if (!kvm_has_tsc_control) {
2230 if (user_tsc_khz > tsc_khz) {
2231 vcpu->arch.tsc_catchup = 1;
2232 vcpu->arch.tsc_always_catchup = 1;
2233 return 0;
2234 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02002235 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08002236 return -1;
2237 }
2238 }
2239
2240 /* TSC scaling required - calculate ratio */
2241 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
2242 user_tsc_khz, tsc_khz);
2243
2244 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02002245 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
2246 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002247 return -1;
2248 }
2249
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002250 kvm_vcpu_write_tsc_multiplier(vcpu, ratio);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002251 return 0;
2252}
2253
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002254static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02002255{
2256 u32 thresh_lo, thresh_hi;
2257 int use_scaling = 0;
2258
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002259 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002260 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08002261 /* set tsc_scaling_ratio to a safe value */
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002262 kvm_vcpu_write_tsc_multiplier(vcpu, kvm_default_tsc_scaling_ratio);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002263 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08002264 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002265
Zachary Amsdenc2855452010-09-18 14:38:15 -10002266 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002267 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02002268 &vcpu->arch.virtual_tsc_shift,
2269 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002270 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02002271
2272 /*
2273 * Compute the variation in TSC rate which is acceptable
2274 * within the range of tolerance and decide if the
2275 * rate being applied is within that bounds of the hardware
2276 * rate. If so, no scaling or compensation need be done.
2277 */
2278 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
2279 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002280 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
2281 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 -02002282 use_scaling = 1;
2283 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002284 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002285}
2286
2287static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
2288{
Zachary Amsdene26101b2012-02-03 15:43:57 -02002289 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02002290 vcpu->arch.virtual_tsc_mult,
2291 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002292 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002293 return tsc;
2294}
2295
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002296static inline int gtod_is_based_on_tsc(int mode)
2297{
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002298 return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002299}
2300
Fengguang Wu69b00492015-01-19 22:33:39 +08002301static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002302{
2303#ifdef CONFIG_X86_64
2304 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002305 struct kvm_arch *ka = &vcpu->kvm->arch;
2306 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2307
2308 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2309 atomic_read(&vcpu->kvm->online_vcpus));
2310
Marcelo Tosatti7f187922014-11-04 21:30:44 -02002311 /*
2312 * Once the masterclock is enabled, always perform request in
2313 * order to update it.
2314 *
2315 * In order to enable masterclock, the host clocksource must be TSC
2316 * and the vcpus need to have matched TSCs. When that happens,
2317 * perform request to enable masterclock.
2318 */
2319 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002320 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002321 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
2322
2323 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
2324 atomic_read(&vcpu->kvm->online_vcpus),
2325 ka->use_master_clock, gtod->clock.vclock_mode);
2326#endif
2327}
2328
Haozhong Zhang35181e82015-10-20 15:39:03 +08002329/*
2330 * Multiply tsc by a fixed point number represented by ratio.
2331 *
2332 * The most significant 64-N bits (mult) of ratio represent the
2333 * integral part of the fixed point number; the remaining N bits
2334 * (frac) represent the fractional part, ie. ratio represents a fixed
2335 * point number (mult + frac * 2^(-N)).
2336 *
2337 * N equals to kvm_tsc_scaling_ratio_frac_bits.
2338 */
2339static inline u64 __scale_tsc(u64 ratio, u64 tsc)
2340{
2341 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
2342}
2343
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002344u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc, u64 ratio)
Haozhong Zhang35181e82015-10-20 15:39:03 +08002345{
2346 u64 _tsc = tsc;
Haozhong Zhang35181e82015-10-20 15:39:03 +08002347
2348 if (ratio != kvm_default_tsc_scaling_ratio)
2349 _tsc = __scale_tsc(ratio, tsc);
2350
2351 return _tsc;
2352}
2353EXPORT_SYMBOL_GPL(kvm_scale_tsc);
2354
Ilias Stamatis9b399df2021-05-26 19:44:10 +01002355static u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
Haozhong Zhang07c14192015-10-20 15:39:05 +08002356{
2357 u64 tsc;
2358
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002359 tsc = kvm_scale_tsc(vcpu, rdtsc(), vcpu->arch.l1_tsc_scaling_ratio);
Haozhong Zhang07c14192015-10-20 15:39:05 +08002360
2361 return target_tsc - tsc;
2362}
2363
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002364u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
2365{
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002366 return vcpu->arch.l1_tsc_offset +
2367 kvm_scale_tsc(vcpu, host_tsc, vcpu->arch.l1_tsc_scaling_ratio);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002368}
2369EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
2370
Ilias Stamatis83150f22021-05-26 19:44:14 +01002371u64 kvm_calc_nested_tsc_offset(u64 l1_offset, u64 l2_offset, u64 l2_multiplier)
2372{
2373 u64 nested_offset;
2374
2375 if (l2_multiplier == kvm_default_tsc_scaling_ratio)
2376 nested_offset = l1_offset;
2377 else
2378 nested_offset = mul_s64_u64_shr((s64) l1_offset, l2_multiplier,
2379 kvm_tsc_scaling_ratio_frac_bits);
2380
2381 nested_offset += l2_offset;
2382 return nested_offset;
2383}
2384EXPORT_SYMBOL_GPL(kvm_calc_nested_tsc_offset);
2385
2386u64 kvm_calc_nested_tsc_multiplier(u64 l1_multiplier, u64 l2_multiplier)
2387{
2388 if (l2_multiplier != kvm_default_tsc_scaling_ratio)
2389 return mul_u64_u64_shr(l1_multiplier, l2_multiplier,
2390 kvm_tsc_scaling_ratio_frac_bits);
2391
2392 return l1_multiplier;
2393}
2394EXPORT_SYMBOL_GPL(kvm_calc_nested_tsc_multiplier);
2395
Ilias Stamatisedcfe542021-05-26 19:44:15 +01002396static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 l1_offset)
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002397{
Ilias Stamatisedcfe542021-05-26 19:44:15 +01002398 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2399 vcpu->arch.l1_tsc_offset,
2400 l1_offset);
2401
2402 vcpu->arch.l1_tsc_offset = l1_offset;
2403
2404 /*
2405 * If we are here because L1 chose not to trap WRMSR to TSC then
2406 * according to the spec this should set L1's TSC (as opposed to
2407 * setting L1's offset for L2).
2408 */
2409 if (is_guest_mode(vcpu))
2410 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
2411 l1_offset,
2412 static_call(kvm_x86_get_l2_tsc_offset)(vcpu),
2413 static_call(kvm_x86_get_l2_tsc_multiplier)(vcpu));
2414 else
2415 vcpu->arch.tsc_offset = l1_offset;
2416
2417 static_call(kvm_x86_write_tsc_offset)(vcpu, vcpu->arch.tsc_offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002418}
2419
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002420static void kvm_vcpu_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 l1_multiplier)
2421{
2422 vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier;
2423
2424 /* Userspace is changing the multiplier while L2 is active */
2425 if (is_guest_mode(vcpu))
2426 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier(
2427 l1_multiplier,
2428 static_call(kvm_x86_get_l2_tsc_multiplier)(vcpu));
2429 else
2430 vcpu->arch.tsc_scaling_ratio = l1_multiplier;
2431
2432 if (kvm_has_tsc_control)
2433 static_call(kvm_x86_write_tsc_multiplier)(
2434 vcpu, vcpu->arch.tsc_scaling_ratio);
2435}
2436
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002437static inline bool kvm_check_tsc_unstable(void)
2438{
2439#ifdef CONFIG_X86_64
2440 /*
2441 * TSC is marked unstable when we're running on Hyper-V,
2442 * 'TSC page' clocksource is good.
2443 */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002444 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002445 return false;
2446#endif
2447 return check_tsc_unstable();
2448}
2449
Paolo Bonzini0c899c22020-09-24 14:45:27 +02002450static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data)
Zachary Amsden99e3e302010-08-19 22:07:17 -10002451{
2452 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002453 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002454 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002455 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002456 bool already_matched;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002457 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002458
Jan Kiszka038f8c12011-02-04 10:49:11 +01002459 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Ilias Stamatis9b399df2021-05-26 19:44:10 +01002460 offset = kvm_compute_l1_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002461 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002462 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002463
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002464 if (vcpu->arch.virtual_tsc_khz) {
Paolo Bonzini0c899c22020-09-24 14:45:27 +02002465 if (data == 0) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03002466 /*
2467 * detection of vcpu initialization -- need to sync
2468 * with other vCPUs. This particularly helps to keep
2469 * kvm_clock stable after CPU hotplug
2470 */
2471 synchronizing = true;
2472 } else {
2473 u64 tsc_exp = kvm->arch.last_tsc_write +
2474 nsec_to_cycles(vcpu, elapsed);
2475 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
2476 /*
2477 * Special case: TSC write with a small delta (1 second)
2478 * of virtual cycle time against real time is
2479 * interpreted as an attempt to synchronize the CPU.
2480 */
2481 synchronizing = data < tsc_exp + tsc_hz &&
2482 data + tsc_hz > tsc_exp;
2483 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002484 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002485
2486 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002487 * For a reliable TSC, we can match TSC offsets, and for an unstable
2488 * TSC, we add elapsed time in this computation. We could let the
2489 * compensation code attempt to catch up if we fall behind, but
2490 * it's better to try to match offsets from the beginning.
2491 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002492 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002493 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002494 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002495 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002496 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002497 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002498 data += delta;
Ilias Stamatis9b399df2021-05-26 19:44:10 +01002499 offset = kvm_compute_l1_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002500 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002501 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002502 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002503 } else {
2504 /*
2505 * We split periods of matched TSC writes into generations.
2506 * For each generation, we track the original measured
2507 * nanosecond time, offset, and write, so if TSCs are in
2508 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002509 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002510 *
2511 * These values are tracked in kvm->arch.cur_xxx variables.
2512 */
2513 kvm->arch.cur_tsc_generation++;
2514 kvm->arch.cur_tsc_nsec = ns;
2515 kvm->arch.cur_tsc_write = data;
2516 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002517 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002518 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002519
2520 /*
2521 * We also track th most recent recorded KHZ, write and time to
2522 * allow the matching interval to be extended at each write.
2523 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002524 kvm->arch.last_tsc_nsec = ns;
2525 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002526 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002527
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002528 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002529
2530 /* Keep track of which generation this VCPU has synchronized to */
2531 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2532 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2533 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2534
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002535 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002536 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002537
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002538 spin_lock_irqsave(&kvm->arch.pvclock_gtod_sync_lock, flags);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002539 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002540 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002541 } else if (!already_matched) {
2542 kvm->arch.nr_vcpus_matched_tsc++;
2543 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002544
2545 kvm_track_tsc_matching(vcpu);
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002546 spin_unlock_irqrestore(&kvm->arch.pvclock_gtod_sync_lock, flags);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002547}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002548
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002549static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2550 s64 adjustment)
2551{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002552 u64 tsc_offset = vcpu->arch.l1_tsc_offset;
Leonid Shatz326e7422018-11-06 12:14:25 +02002553 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002554}
2555
2556static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2557{
Ilias Stamatis805d7052021-05-26 19:44:09 +01002558 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002559 WARN_ON(adjustment < 0);
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002560 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment,
2561 vcpu->arch.l1_tsc_scaling_ratio);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002562 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002563}
2564
Marcelo Tosattid8281992012-11-27 23:29:01 -02002565#ifdef CONFIG_X86_64
2566
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002567static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002568{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002569 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002570 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002571
2572 if (likely(ret >= last))
2573 return ret;
2574
2575 /*
2576 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002577 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002578 * very likely) and there's a data dependence, so force GCC
2579 * to generate a branch instead. I don't barrier() because
2580 * we don't actually need a barrier, and if this function
2581 * ever gets inlined it will generate worse code.
2582 */
2583 asm volatile ("");
2584 return last;
2585}
2586
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002587static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2588 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002589{
2590 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002591 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002592
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002593 switch (clock->vclock_mode) {
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002594 case VDSO_CLOCKMODE_HVCLOCK:
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002595 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2596 tsc_timestamp);
2597 if (tsc_pg_val != U64_MAX) {
2598 /* TSC page valid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002599 *mode = VDSO_CLOCKMODE_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002600 v = (tsc_pg_val - clock->cycle_last) &
2601 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002602 } else {
2603 /* TSC page invalid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002604 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002605 }
2606 break;
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002607 case VDSO_CLOCKMODE_TSC:
2608 *mode = VDSO_CLOCKMODE_TSC;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002609 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002610 v = (*tsc_timestamp - clock->cycle_last) &
2611 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002612 break;
2613 default:
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002614 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002615 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002616
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002617 if (*mode == VDSO_CLOCKMODE_NONE)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002618 *tsc_timestamp = v = 0;
2619
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002620 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002621}
2622
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002623static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002624{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002625 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002626 unsigned long seq;
2627 int mode;
2628 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002629
Marcelo Tosattid8281992012-11-27 23:29:01 -02002630 do {
2631 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002632 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002633 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002634 ns >>= gtod->raw_clock.shift;
2635 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002636 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002637 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002638
2639 return mode;
2640}
2641
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002642static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002643{
2644 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2645 unsigned long seq;
2646 int mode;
2647 u64 ns;
2648
2649 do {
2650 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002651 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002652 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002653 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002654 ns >>= gtod->clock.shift;
2655 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2656
2657 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2658 ts->tv_nsec = ns;
2659
2660 return mode;
2661}
2662
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002663/* returns true if host is using TSC based clocksource */
2664static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002665{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002666 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002667 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002668 return false;
2669
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002670 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002671 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002672}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002673
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002674/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002675static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002676 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002677{
2678 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002679 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002680 return false;
2681
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002682 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002683}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002684#endif
2685
2686/*
2687 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002688 * Assuming a stable TSC across physical CPUS, and a stable TSC
2689 * across virtual CPUs, the following condition is possible.
2690 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002691 * CPUs at the next numbered event.
2692 *
2693 * "timespecX" represents host monotonic time. "tscX" represents
2694 * RDTSC value.
2695 *
2696 * VCPU0 on CPU0 | VCPU1 on CPU1
2697 *
2698 * 1. read timespec0,tsc0
2699 * 2. | timespec1 = timespec0 + N
2700 * | tsc1 = tsc0 + M
2701 * 3. transition to guest | transition to guest
2702 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2703 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2704 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2705 *
2706 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2707 *
2708 * - ret0 < ret1
2709 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2710 * ...
2711 * - 0 < N - M => M < N
2712 *
2713 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2714 * always the case (the difference between two distinct xtime instances
2715 * might be smaller then the difference between corresponding TSC reads,
2716 * when updating guest vcpus pvclock areas).
2717 *
2718 * To avoid that problem, do not allow visibility of distinct
2719 * system_timestamp/tsc_timestamp values simultaneously: use a master
2720 * copy of host monotonic time values. Update that master copy
2721 * in lockstep.
2722 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002723 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002724 *
2725 */
2726
2727static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2728{
2729#ifdef CONFIG_X86_64
2730 struct kvm_arch *ka = &kvm->arch;
2731 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002732 bool host_tsc_clocksource, vcpus_matched;
2733
2734 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2735 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002736
2737 /*
2738 * If the host uses TSC clock, then passthrough TSC as stable
2739 * to the guest.
2740 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002741 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002742 &ka->master_kernel_ns,
2743 &ka->master_cycle_now);
2744
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002745 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002746 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002747 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002748
Marcelo Tosattid8281992012-11-27 23:29:01 -02002749 if (ka->use_master_clock)
2750 atomic_set(&kvm_guest_has_master_clock, 1);
2751
2752 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002753 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2754 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002755#endif
2756}
2757
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002758void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2759{
2760 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2761}
2762
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002763static void kvm_gen_update_masterclock(struct kvm *kvm)
2764{
2765#ifdef CONFIG_X86_64
2766 int i;
2767 struct kvm_vcpu *vcpu;
2768 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002769 unsigned long flags;
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002770
Vitaly Kuznetsove880c6e2021-03-16 15:37:34 +01002771 kvm_hv_invalidate_tsc_page(kvm);
2772
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002773 kvm_make_mclock_inprogress_request(kvm);
Paolo Bonzinic2c647f2021-03-25 14:05:11 -04002774
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002775 /* no guest entries from this point */
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002776 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002777 pvclock_update_vm_gtod_copy(kvm);
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002778 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002779
2780 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002781 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002782
2783 /* guest entries allowed */
2784 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002785 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002786#endif
2787}
2788
Marcelo Tosattie891a322017-04-17 12:51:37 -03002789u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002790{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002791 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002792 struct pvclock_vcpu_time_info hv_clock;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002793 unsigned long flags;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002794 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002795
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002796 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Paolo Bonzini8b953442016-11-16 18:31:30 +01002797 if (!ka->use_master_clock) {
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002798 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002799 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002800 }
2801
Paolo Bonzini8b953442016-11-16 18:31:30 +01002802 hv_clock.tsc_timestamp = ka->master_cycle_now;
2803 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002804 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Paolo Bonzini8b953442016-11-16 18:31:30 +01002805
Wanpeng Lie2c22062017-05-11 18:12:05 -07002806 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2807 get_cpu();
2808
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002809 if (__this_cpu_read(cpu_tsc_khz)) {
2810 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2811 &hv_clock.tsc_shift,
2812 &hv_clock.tsc_to_system_mul);
2813 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2814 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002815 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002816
2817 put_cpu();
2818
2819 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002820}
2821
Joao Martinsaa096aa2019-02-01 13:01:45 -05002822static void kvm_setup_pvclock_page(struct kvm_vcpu *v,
2823 struct gfn_to_hva_cache *cache,
2824 unsigned int offset)
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002825{
2826 struct kvm_vcpu_arch *vcpu = &v->arch;
2827 struct pvclock_vcpu_time_info guest_hv_clock;
2828
Joao Martinsaa096aa2019-02-01 13:01:45 -05002829 if (unlikely(kvm_read_guest_offset_cached(v->kvm, cache,
2830 &guest_hv_clock, offset, sizeof(guest_hv_clock))))
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002831 return;
2832
2833 /* This VCPU is paused, but it's legal for a guest to read another
2834 * VCPU's kvmclock, so we really have to follow the specification where
2835 * it says that version is odd if data is being modified, and even after
2836 * it is consistent.
2837 *
2838 * Version field updates must be kept separate. This is because
2839 * kvm_write_guest_cached might use a "rep movs" instruction, and
2840 * writes within a string instruction are weakly ordered. So there
2841 * are three writes overall.
2842 *
2843 * As a small optimization, only write the version field in the first
2844 * and third write. The vcpu->pv_time cache is still valid, because the
2845 * version field is the first in the struct.
2846 */
2847 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2848
Liran Alon51c4b8b2017-11-05 16:11:30 +02002849 if (guest_hv_clock.version & 1)
2850 ++guest_hv_clock.version; /* first time write, random junk */
2851
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002852 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Joao Martinsaa096aa2019-02-01 13:01:45 -05002853 kvm_write_guest_offset_cached(v->kvm, cache,
2854 &vcpu->hv_clock, offset,
2855 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002856
2857 smp_wmb();
2858
2859 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2860 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2861
2862 if (vcpu->pvclock_set_guest_stopped_request) {
2863 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2864 vcpu->pvclock_set_guest_stopped_request = false;
2865 }
2866
2867 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2868
Joao Martinsaa096aa2019-02-01 13:01:45 -05002869 kvm_write_guest_offset_cached(v->kvm, cache,
2870 &vcpu->hv_clock, offset,
2871 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002872
2873 smp_wmb();
2874
2875 vcpu->hv_clock.version++;
Joao Martinsaa096aa2019-02-01 13:01:45 -05002876 kvm_write_guest_offset_cached(v->kvm, cache,
2877 &vcpu->hv_clock, offset,
2878 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002879}
2880
Zachary Amsden34c238a2010-09-18 14:38:14 -10002881static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002882{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002883 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002884 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002885 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002886 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002887 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002888 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002889 bool use_master_clock;
2890
2891 kernel_ns = 0;
2892 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002893
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002894 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002895 * If the host uses TSC clock, then passthrough TSC as stable
2896 * to the guest.
2897 */
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002898 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002899 use_master_clock = ka->use_master_clock;
2900 if (use_master_clock) {
2901 host_tsc = ka->master_cycle_now;
2902 kernel_ns = ka->master_kernel_ns;
2903 }
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002904 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002905
2906 /* Keep irq disabled to prevent changes to the clock */
2907 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002908 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2909 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002910 local_irq_restore(flags);
2911 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2912 return 1;
2913 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002914 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002915 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002916 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002917 }
2918
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002919 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002920
2921 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002922 * We may have to catch up the TSC to match elapsed wall clock
2923 * time for two reasons, even if kvmclock is used.
2924 * 1) CPU could have been running below the maximum TSC rate
2925 * 2) Broken TSC compensation resets the base at each VCPU
2926 * entry to avoid unknown leaps of TSC even when running
2927 * again on the same CPU. This may cause apparent elapsed
2928 * time to disappear, and the guest to stand still or run
2929 * very slowly.
2930 */
2931 if (vcpu->tsc_catchup) {
2932 u64 tsc = compute_guest_tsc(v, kernel_ns);
2933 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002934 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002935 tsc_timestamp = tsc;
2936 }
2937 }
2938
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002939 local_irq_restore(flags);
2940
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002941 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002942
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002943 if (kvm_has_tsc_control)
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002944 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz,
2945 v->arch.l1_tsc_scaling_ratio);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002946
2947 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002948 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002949 &vcpu->hv_clock.tsc_shift,
2950 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002951 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002952 }
2953
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002954 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002955 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002956 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002957
Marcelo Tosattid8281992012-11-27 23:29:01 -02002958 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002959 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002960 if (use_master_clock)
2961 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2962
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002963 vcpu->hv_clock.flags = pvclock_flags;
2964
Paolo Bonzini095cf552016-02-08 12:54:12 +01002965 if (vcpu->pv_time_enabled)
Joao Martinsaa096aa2019-02-01 13:01:45 -05002966 kvm_setup_pvclock_page(v, &vcpu->pv_time, 0);
2967 if (vcpu->xen.vcpu_info_set)
2968 kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_info_cache,
2969 offsetof(struct compat_vcpu_info, time));
Joao Martinsf2340cd2018-07-23 11:20:57 -04002970 if (vcpu->xen.vcpu_time_info_set)
2971 kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0);
Paolo Bonzini095cf552016-02-08 12:54:12 +01002972 if (v == kvm_get_vcpu(v->kvm, 0))
2973 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002974 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002975}
2976
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002977/*
2978 * kvmclock updates which are isolated to a given vcpu, such as
2979 * vcpu->cpu migration, should not allow system_timestamp from
2980 * the rest of the vcpus to remain static. Otherwise ntp frequency
2981 * correction applies to one vcpu's system_timestamp but not
2982 * the others.
2983 *
2984 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002985 * We need to rate-limit these requests though, as they can
2986 * considerably slow guests that have a large number of vcpus.
2987 * The time for a remote vcpu to update its kvmclock is bound
2988 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002989 */
2990
Andrew Jones7e44e442014-02-28 12:52:54 +01002991#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2992
2993static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002994{
2995 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002996 struct delayed_work *dwork = to_delayed_work(work);
2997 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2998 kvmclock_update_work);
2999 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003000 struct kvm_vcpu *vcpu;
3001
3002 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003003 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003004 kvm_vcpu_kick(vcpu);
3005 }
3006}
3007
Andrew Jones7e44e442014-02-28 12:52:54 +01003008static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
3009{
3010 struct kvm *kvm = v->kvm;
3011
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003012 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01003013 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
3014 KVMCLOCK_UPDATE_DELAY);
3015}
3016
Andrew Jones332967a2014-02-28 12:52:55 +01003017#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
3018
3019static void kvmclock_sync_fn(struct work_struct *work)
3020{
3021 struct delayed_work *dwork = to_delayed_work(work);
3022 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
3023 kvmclock_sync_work);
3024 struct kvm *kvm = container_of(ka, struct kvm, arch);
3025
Marcelo Tosatti630994b2015-05-12 22:42:04 -03003026 if (!kvmclock_periodic_sync)
3027 return;
3028
Andrew Jones332967a2014-02-28 12:52:55 +01003029 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
3030 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
3031 KVMCLOCK_SYNC_PERIOD);
3032}
3033
Borislav Petkov191c8132019-04-18 18:32:50 +02003034/*
3035 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
3036 */
3037static bool can_set_mci_status(struct kvm_vcpu *vcpu)
3038{
3039 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08003040 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02003041 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
3042
3043 return false;
3044}
3045
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003046static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08003047{
3048 u64 mcg_cap = vcpu->arch.mcg_cap;
3049 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003050 u32 msr = msr_info->index;
3051 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08003052
3053 switch (msr) {
3054 case MSR_IA32_MCG_STATUS:
3055 vcpu->arch.mcg_status = data;
3056 break;
3057 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003058 if (!(mcg_cap & MCG_CTL_P) &&
3059 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08003060 return 1;
3061 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02003062 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08003063 vcpu->arch.mcg_ctl = data;
3064 break;
3065 default:
3066 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003067 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003068 u32 offset = array_index_nospec(
3069 msr - MSR_IA32_MC0_CTL,
3070 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3071
Andre Przywara114be422010-03-24 17:46:42 +01003072 /* only 0 or all 1s can be written to IA32_MCi_CTL
3073 * some Linux kernels though clear bit 10 in bank 4 to
3074 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
3075 * this to avoid an uncatched #GP in the guest
3076 */
Huang Ying890ca9a2009-05-11 16:48:15 +08003077 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01003078 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08003079 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02003080
3081 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003082 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02003083 (offset & 0x3) == 1 && data != 0) {
3084 if (!can_set_mci_status(vcpu))
3085 return -1;
3086 }
3087
Huang Ying890ca9a2009-05-11 16:48:15 +08003088 vcpu->arch.mce_banks[offset] = data;
3089 break;
3090 }
3091 return 1;
3092 }
3093 return 0;
3094}
3095
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003096static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu)
3097{
3098 u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
3099
3100 return (vcpu->arch.apf.msr_en_val & mask) == mask;
3101}
3102
Gleb Natapov344d9582010-10-14 11:22:50 +02003103static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
3104{
3105 gpa_t gpa = data & ~0x3f;
3106
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003107 /* Bits 4:5 are reserved, Should be zero */
3108 if (data & 0x30)
Gleb Natapov344d9582010-10-14 11:22:50 +02003109 return 1;
3110
Oliver Upton66570e92020-08-18 15:24:28 +00003111 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_VMEXIT) &&
3112 (data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT))
3113 return 1;
3114
3115 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT) &&
3116 (data & KVM_ASYNC_PF_DELIVERY_AS_INT))
3117 return 1;
3118
Wanpeng Li9d3c4472020-06-29 18:26:31 +08003119 if (!lapic_in_kernel(vcpu))
Vitaly Kuznetsovd831de12020-09-11 11:31:47 +02003120 return data ? 1 : 0;
Wanpeng Li9d3c4472020-06-29 18:26:31 +08003121
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003122 vcpu->arch.apf.msr_en_val = data;
Gleb Natapov344d9582010-10-14 11:22:50 +02003123
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003124 if (!kvm_pv_async_pf_enabled(vcpu)) {
Gleb Natapov344d9582010-10-14 11:22:50 +02003125 kvm_clear_async_pf_completion_queue(vcpu);
3126 kvm_async_pf_hash_reset(vcpu);
3127 return 0;
3128 }
3129
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003130 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003131 sizeof(u64)))
Gleb Natapov344d9582010-10-14 11:22:50 +02003132 return 1;
3133
Gleb Natapov6adba522010-10-14 11:22:55 +02003134 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07003135 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003136
Gleb Natapov344d9582010-10-14 11:22:50 +02003137 kvm_async_pf_wakeup_all(vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003138
3139 return 0;
3140}
3141
3142static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data)
3143{
3144 /* Bits 8-63 are reserved */
3145 if (data >> 8)
3146 return 1;
3147
3148 if (!lapic_in_kernel(vcpu))
3149 return 1;
3150
3151 vcpu->arch.apf.msr_int_val = data;
3152
3153 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK;
3154
Gleb Natapov344d9582010-10-14 11:22:50 +02003155 return 0;
3156}
3157
Glauber Costa12f9a482011-02-01 14:16:40 -05003158static void kvmclock_reset(struct kvm_vcpu *vcpu)
3159{
Andy Honig0b794592013-02-20 14:48:10 -08003160 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02003161 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05003162}
3163
Sean Christopherson77809382020-03-20 14:28:18 -07003164static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08003165{
3166 ++vcpu->stat.tlb_flush;
Jason Baronb36464772021-01-14 22:27:56 -05003167 static_call(kvm_x86_tlb_flush_all)(vcpu);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08003168}
3169
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04003170static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
3171{
3172 ++vcpu->stat.tlb_flush;
Lai Jiangshanb53e84e2021-06-01 01:22:56 +08003173
3174 if (!tdp_enabled) {
3175 /*
3176 * A TLB flush on behalf of the guest is equivalent to
3177 * INVPCID(all), toggling CR4.PGE, etc., which requires
3178 * a forced sync of the shadow page tables. Unload the
3179 * entire MMU here and the subsequent load will sync the
3180 * shadow page tables, and also flush the TLB.
3181 */
3182 kvm_mmu_unload(vcpu);
3183 return;
3184 }
3185
Jason Baronb36464772021-01-14 22:27:56 -05003186 static_call(kvm_x86_tlb_flush_guest)(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04003187}
3188
Glauber Costac9aaa892011-07-11 15:28:14 -04003189static void record_steal_time(struct kvm_vcpu *vcpu)
3190{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003191 struct kvm_host_map map;
3192 struct kvm_steal_time *st;
3193
David Woodhouse30b5c852021-03-01 12:53:09 +00003194 if (kvm_xen_msr_enabled(vcpu->kvm)) {
3195 kvm_xen_runstate_set_running(vcpu);
3196 return;
3197 }
3198
Glauber Costac9aaa892011-07-11 15:28:14 -04003199 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3200 return;
3201
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003202 /* -EAGAIN is returned in atomic context so we can just return. */
3203 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
3204 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04003205 return;
3206
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003207 st = map.hva +
3208 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3209
Wanpeng Lif38a7b72017-12-12 17:33:04 -08003210 /*
3211 * Doing a TLB flush here, on the guest's behalf, can avoid
3212 * expensive IPIs.
3213 */
Oliver Upton66570e92020-08-18 15:24:28 +00003214 if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) {
Lai Jiangshanaf3511f2021-06-01 01:46:28 +08003215 u8 st_preempted = xchg(&st->preempted, 0);
3216
Oliver Upton66570e92020-08-18 15:24:28 +00003217 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Lai Jiangshanaf3511f2021-06-01 01:46:28 +08003218 st_preempted & KVM_VCPU_FLUSH_TLB);
3219 if (st_preempted & KVM_VCPU_FLUSH_TLB)
Oliver Upton66570e92020-08-18 15:24:28 +00003220 kvm_vcpu_flush_tlb_guest(vcpu);
Wanpeng Li1eff0ad2021-05-18 05:00:33 -07003221 } else {
3222 st->preempted = 0;
Oliver Upton66570e92020-08-18 15:24:28 +00003223 }
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003224
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003225 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003226
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003227 if (st->version & 1)
3228 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003229
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003230 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003231
3232 smp_wmb();
3233
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003234 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08003235 vcpu->arch.st.last_steal;
3236 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003237
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003238 smp_wmb();
3239
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003240 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04003241
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003242 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04003243}
3244
Will Auld8fe8ab42012-11-29 12:42:12 -08003245int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003246{
Gleb Natapov57537852012-01-15 14:17:22 +02003247 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08003248 u32 msr = msr_info->index;
3249 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02003250
Joao Martins1232f8e2018-06-13 06:10:37 -04003251 if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr)
Joao Martins23200b72018-06-13 09:55:44 -04003252 return kvm_xen_write_hypercall_page(vcpu, data);
Joao Martins1232f8e2018-06-13 06:10:37 -04003253
Carsten Otte15c4a642007-10-30 18:44:17 +01003254 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01003255 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003256 case MSR_IA32_UCODE_WRITE:
3257 case MSR_VM_HSAVE_PA:
3258 case MSR_AMD64_PATCH_LOADER:
3259 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02003260 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003261 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003262 break;
3263
Wanpeng Li518e7b92018-02-28 14:03:31 +08003264 case MSR_IA32_UCODE_REV:
3265 if (msr_info->host_initiated)
3266 vcpu->arch.microcode_version = data;
3267 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003268 case MSR_IA32_ARCH_CAPABILITIES:
3269 if (!msr_info->host_initiated)
3270 return 1;
3271 vcpu->arch.arch_capabilities = data;
3272 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02003273 case MSR_IA32_PERF_CAPABILITIES: {
3274 struct kvm_msr_entry msr_ent = {.index = msr, .data = 0};
3275
3276 if (!msr_info->host_initiated)
3277 return 1;
3278 if (guest_cpuid_has(vcpu, X86_FEATURE_PDCM) && kvm_get_msr_feature(&msr_ent))
3279 return 1;
3280 if (data & ~msr_ent.data)
3281 return 1;
3282
3283 vcpu->arch.perf_capabilities = data;
3284
3285 return 0;
3286 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003287 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07003288 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02003289 case MSR_K7_HWCR:
3290 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01003291 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08003292 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02003293
3294 /* Handle McStatusWrEn */
3295 if (data == BIT_ULL(18)) {
3296 vcpu->arch.msr_hwcr = data;
3297 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003298 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
3299 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02003300 return 1;
3301 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003302 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003303 case MSR_FAM10H_MMIO_CONF_BASE:
3304 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003305 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
3306 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003307 return 1;
3308 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003309 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003310 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003311 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003312 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01003313 return kvm_set_apic_base(vcpu, msr_info);
Xiaoyao Libf10bd02020-06-16 15:33:07 +08003314 case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003315 return kvm_x2apic_msr_write(vcpu, msr, data);
Dave Hansen09141ec2020-03-05 09:47:06 -08003316 case MSR_IA32_TSC_DEADLINE:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003317 kvm_set_lapic_tscdeadline_msr(vcpu, data);
3318 break;
Will Auldba904632012-11-29 12:42:50 -08003319 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003320 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08003321 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06003322 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08003323 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08003324 }
3325 vcpu->arch.ia32_tsc_adjust_msr = data;
3326 }
3327 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003328 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08003329 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
3330 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
3331 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
3332 return 1;
3333 vcpu->arch.ia32_misc_enable_msr = data;
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08003334 kvm_update_cpuid_runtime(vcpu);
Wanpeng Li511a85562019-05-21 14:06:54 +08003335 } else {
3336 vcpu->arch.ia32_misc_enable_msr = data;
3337 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003338 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003339 case MSR_IA32_SMBASE:
3340 if (!msr_info->host_initiated)
3341 return 1;
3342 vcpu->arch.smbase = data;
3343 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003344 case MSR_IA32_POWER_CTL:
3345 vcpu->arch.msr_ia32_power_ctl = data;
3346 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003347 case MSR_IA32_TSC:
Paolo Bonzini0c899c22020-09-24 14:45:27 +02003348 if (msr_info->host_initiated) {
3349 kvm_synchronize_tsc(vcpu, data);
3350 } else {
Ilias Stamatis9b399df2021-05-26 19:44:10 +01003351 u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset;
Paolo Bonzini0c899c22020-09-24 14:45:27 +02003352 adjust_tsc_offset_guest(vcpu, adj);
3353 vcpu->arch.ia32_tsc_adjust_msr += adj;
3354 }
Paolo Bonzinidd259932018-04-13 11:38:35 +02003355 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003356 case MSR_IA32_XSS:
3357 if (!msr_info->host_initiated &&
3358 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3359 return 1;
3360 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08003361 * KVM supports exposing PT to the guest, but does not support
3362 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
3363 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003364 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01003365 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003366 return 1;
3367 vcpu->arch.ia32_xss = data;
3368 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003369 case MSR_SMI_COUNT:
3370 if (!msr_info->host_initiated)
3371 return 1;
3372 vcpu->arch.smi_count = data;
3373 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003374 case MSR_KVM_WALL_CLOCK_NEW:
Oliver Upton66570e92020-08-18 15:24:28 +00003375 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3376 return 1;
3377
Joao Martins629b5342018-06-28 15:06:43 -04003378 vcpu->kvm->arch.wall_clock = data;
3379 kvm_write_wall_clock(vcpu->kvm, data, 0);
Oliver Upton66570e92020-08-18 15:24:28 +00003380 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003381 case MSR_KVM_WALL_CLOCK:
Oliver Upton66570e92020-08-18 15:24:28 +00003382 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3383 return 1;
3384
Joao Martins629b5342018-06-28 15:06:43 -04003385 vcpu->kvm->arch.wall_clock = data;
3386 kvm_write_wall_clock(vcpu->kvm, data, 0);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003387 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003388 case MSR_KVM_SYSTEM_TIME_NEW:
Oliver Upton66570e92020-08-18 15:24:28 +00003389 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3390 return 1;
3391
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003392 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003393 break;
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003394 case MSR_KVM_SYSTEM_TIME:
Oliver Upton66570e92020-08-18 15:24:28 +00003395 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3396 return 1;
3397
3398 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated);
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003399 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003400 case MSR_KVM_ASYNC_PF_EN:
Oliver Upton66570e92020-08-18 15:24:28 +00003401 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3402 return 1;
3403
Gleb Natapov344d9582010-10-14 11:22:50 +02003404 if (kvm_pv_enable_async_pf(vcpu, data))
3405 return 1;
3406 break;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003407 case MSR_KVM_ASYNC_PF_INT:
Oliver Upton66570e92020-08-18 15:24:28 +00003408 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
3409 return 1;
3410
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003411 if (kvm_pv_enable_async_pf_int(vcpu, data))
3412 return 1;
3413 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003414 case MSR_KVM_ASYNC_PF_ACK:
Vitaly Kuznetsov0a31df62021-07-22 14:30:18 +02003415 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
Oliver Upton66570e92020-08-18 15:24:28 +00003416 return 1;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003417 if (data & 0x1) {
3418 vcpu->arch.apf.pageready_pending = false;
3419 kvm_check_async_pf_completion(vcpu);
3420 }
3421 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003422 case MSR_KVM_STEAL_TIME:
Oliver Upton66570e92020-08-18 15:24:28 +00003423 if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME))
3424 return 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04003425
3426 if (unlikely(!sched_info_on()))
3427 return 1;
3428
3429 if (data & KVM_STEAL_RESERVED_MASK)
3430 return 1;
3431
Glauber Costac9aaa892011-07-11 15:28:14 -04003432 vcpu->arch.st.msr_val = data;
3433
3434 if (!(data & KVM_MSR_ENABLED))
3435 break;
3436
Glauber Costac9aaa892011-07-11 15:28:14 -04003437 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
3438
3439 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003440 case MSR_KVM_PV_EOI_EN:
Oliver Upton66570e92020-08-18 15:24:28 +00003441 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI))
3442 return 1;
3443
Ladi Prosek72bbf932018-10-16 18:49:59 +02003444 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003445 return 1;
3446 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003447
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003448 case MSR_KVM_POLL_CONTROL:
Oliver Upton66570e92020-08-18 15:24:28 +00003449 if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL))
3450 return 1;
3451
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003452 /* only enable bit supported */
3453 if (data & (-1ULL << 1))
3454 return 1;
3455
3456 vcpu->arch.msr_kvm_poll_control = data;
3457 break;
3458
Huang Ying890ca9a2009-05-11 16:48:15 +08003459 case MSR_IA32_MCG_CTL:
3460 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003461 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003462 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02003463
Wei Huang6912ac32015-06-12 01:34:56 -04003464 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3465 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003466 pr = true;
3467 fallthrough;
Wei Huang6912ac32015-06-12 01:34:56 -04003468 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3469 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003470 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003471 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02003472
3473 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03003474 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
3475 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02003476 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003477 case MSR_K7_CLK_CTL:
3478 /*
3479 * Ignore all writes to this no longer documented MSR.
3480 * Writes are only relevant for old K7 processors,
3481 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08003482 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003483 * affected processor models on the command line, hence
3484 * the need to ignore the workaround.
3485 */
3486 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003487 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003488 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3489 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003490 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3491 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003492 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003493 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3494 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3495 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003496 return kvm_hv_set_msr_common(vcpu, msr, data,
3497 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003498 case MSR_IA32_BBL_CR_CTL3:
3499 /* Drop writes to this legacy MSR -- see rdmsr
3500 * counterpart for further detail.
3501 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003502 if (report_ignored_msrs)
3503 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
3504 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05003505 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003506 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003507 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003508 return 1;
3509 vcpu->arch.osvw.length = data;
3510 break;
3511 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003512 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003513 return 1;
3514 vcpu->arch.osvw.status = data;
3515 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003516 case MSR_PLATFORM_INFO:
3517 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07003518 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
3519 cpuid_fault_enabled(vcpu)))
3520 return 1;
3521 vcpu->arch.msr_platform_info = data;
3522 break;
3523 case MSR_MISC_FEATURES_ENABLES:
3524 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
3525 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3526 !supports_cpuid_fault(vcpu)))
3527 return 1;
3528 vcpu->arch.msr_misc_features_enables = data;
3529 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003530 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003531 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003532 return kvm_pmu_set_msr(vcpu, msr_info);
Peter Xu6abe9c12020-06-22 18:04:41 -04003533 return KVM_MSR_RET_INVALID;
Carsten Otte15c4a642007-10-30 18:44:17 +01003534 }
3535 return 0;
3536}
3537EXPORT_SYMBOL_GPL(kvm_set_msr_common);
3538
Paolo Bonzini44883f02018-07-26 13:01:52 +02003539static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003540{
3541 u64 data;
3542 u64 mcg_cap = vcpu->arch.mcg_cap;
3543 unsigned bank_num = mcg_cap & 0xff;
3544
3545 switch (msr) {
3546 case MSR_IA32_P5_MC_ADDR:
3547 case MSR_IA32_P5_MC_TYPE:
3548 data = 0;
3549 break;
3550 case MSR_IA32_MCG_CAP:
3551 data = vcpu->arch.mcg_cap;
3552 break;
3553 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003554 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003555 return 1;
3556 data = vcpu->arch.mcg_ctl;
3557 break;
3558 case MSR_IA32_MCG_STATUS:
3559 data = vcpu->arch.mcg_status;
3560 break;
3561 default:
3562 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003563 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003564 u32 offset = array_index_nospec(
3565 msr - MSR_IA32_MC0_CTL,
3566 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3567
Huang Ying890ca9a2009-05-11 16:48:15 +08003568 data = vcpu->arch.mce_banks[offset];
3569 break;
3570 }
3571 return 1;
3572 }
3573 *pdata = data;
3574 return 0;
3575}
3576
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003577int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003578{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003579 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003580 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003581 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003582 case MSR_IA32_LASTBRANCHFROMIP:
3583 case MSR_IA32_LASTBRANCHTOIP:
3584 case MSR_IA32_LASTINTFROMIP:
3585 case MSR_IA32_LASTINTTOIP:
Brijesh Singh059e5c32021-04-27 06:16:36 -05003586 case MSR_AMD64_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003587 case MSR_K8_TSEG_ADDR:
3588 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003589 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003590 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003591 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003592 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003593 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003594 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003595 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003596 case MSR_F15H_EX_CFG:
Venkatesh Srinivas2ca1a062020-04-16 11:42:54 -07003597 /*
3598 * Intel Sandy Bridge CPUs must support the RAPL (running average power
3599 * limit) MSRs. Just return 0, as we do not want to expose the host
3600 * data here. Do not conditionalize this on CPUID, as KVM does not do
3601 * so for existing CPU-specific MSRs.
3602 */
3603 case MSR_RAPL_POWER_UNIT:
3604 case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */
3605 case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */
3606 case MSR_PKG_ENERGY_STATUS: /* Total package */
3607 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003608 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003609 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003610 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Vitaly Kuznetsovc28fa562021-03-29 14:48:04 +02003611 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
3612 return kvm_pmu_get_msr(vcpu, msr_info);
3613 if (!msr_info->host_initiated)
3614 return 1;
3615 msr_info->data = 0;
3616 break;
Wei Huang6912ac32015-06-12 01:34:56 -04003617 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3618 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3619 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3620 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003621 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003622 return kvm_pmu_get_msr(vcpu, msr_info);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003623 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003624 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003625 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003626 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003627 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003628 case MSR_IA32_ARCH_CAPABILITIES:
3629 if (!msr_info->host_initiated &&
3630 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3631 return 1;
3632 msr_info->data = vcpu->arch.arch_capabilities;
3633 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02003634 case MSR_IA32_PERF_CAPABILITIES:
3635 if (!msr_info->host_initiated &&
3636 !guest_cpuid_has(vcpu, X86_FEATURE_PDCM))
3637 return 1;
3638 msr_info->data = vcpu->arch.perf_capabilities;
3639 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003640 case MSR_IA32_POWER_CTL:
3641 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3642 break;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003643 case MSR_IA32_TSC: {
3644 /*
3645 * Intel SDM states that MSR_IA32_TSC read adds the TSC offset
3646 * even when not intercepted. AMD manual doesn't explicitly
3647 * state this but appears to behave the same.
3648 *
Maxim Levitskyee6fa052020-09-21 13:38:05 +03003649 * On userspace reads and writes, however, we unconditionally
Paolo Bonzinic0623f52020-10-21 18:05:58 -04003650 * return L1's TSC value to ensure backwards-compatible
Maxim Levitskyee6fa052020-09-21 13:38:05 +03003651 * behavior for migration.
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003652 */
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01003653 u64 offset, ratio;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003654
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01003655 if (msr_info->host_initiated) {
3656 offset = vcpu->arch.l1_tsc_offset;
3657 ratio = vcpu->arch.l1_tsc_scaling_ratio;
3658 } else {
3659 offset = vcpu->arch.tsc_offset;
3660 ratio = vcpu->arch.tsc_scaling_ratio;
3661 }
3662
3663 msr_info->data = kvm_scale_tsc(vcpu, rdtsc(), ratio) + offset;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003664 break;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003665 }
Avi Kivity9ba075a2008-05-26 20:06:35 +03003666 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003667 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003668 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003669 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003670 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003671 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003672 /*
3673 * MSR_EBC_FREQUENCY_ID
3674 * Conservative value valid for even the basic CPU models.
3675 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3676 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3677 * and 266MHz for model 3, or 4. Set Core Clock
3678 * Frequency to System Bus Frequency Ratio to 1 (bits
3679 * 31:24) even though these are only valid for CPU
3680 * models > 2, however guests may end up dividing or
3681 * multiplying by zero otherwise.
3682 */
3683 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003684 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003685 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003686 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003687 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003688 break;
Xiaoyao Libf10bd02020-06-16 15:33:07 +08003689 case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003690 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Dave Hansen09141ec2020-03-05 09:47:06 -08003691 case MSR_IA32_TSC_DEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003692 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003693 break;
Will Auldba904632012-11-29 12:42:50 -08003694 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003695 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003696 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003697 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003698 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003699 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003700 case MSR_IA32_SMBASE:
3701 if (!msr_info->host_initiated)
3702 return 1;
3703 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003704 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003705 case MSR_SMI_COUNT:
3706 msr_info->data = vcpu->arch.smi_count;
3707 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003708 case MSR_IA32_PERF_STATUS:
3709 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003710 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003711 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003712 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003713 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003714 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003715 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003716 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003717 case MSR_KVM_WALL_CLOCK:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003718 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3719 return 1;
3720
3721 msr_info->data = vcpu->kvm->arch.wall_clock;
3722 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003723 case MSR_KVM_WALL_CLOCK_NEW:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003724 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3725 return 1;
3726
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003727 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003728 break;
3729 case MSR_KVM_SYSTEM_TIME:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003730 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3731 return 1;
3732
3733 msr_info->data = vcpu->arch.time;
3734 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003735 case MSR_KVM_SYSTEM_TIME_NEW:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003736 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3737 return 1;
3738
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003739 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003740 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003741 case MSR_KVM_ASYNC_PF_EN:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003742 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3743 return 1;
3744
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003745 msr_info->data = vcpu->arch.apf.msr_en_val;
3746 break;
3747 case MSR_KVM_ASYNC_PF_INT:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003748 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
3749 return 1;
3750
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003751 msr_info->data = vcpu->arch.apf.msr_int_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003752 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003753 case MSR_KVM_ASYNC_PF_ACK:
Vitaly Kuznetsov0a31df62021-07-22 14:30:18 +02003754 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
Oliver Upton1930e5d2020-10-27 16:10:41 -07003755 return 1;
3756
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003757 msr_info->data = 0;
3758 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003759 case MSR_KVM_STEAL_TIME:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003760 if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME))
3761 return 1;
3762
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003763 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003764 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003765 case MSR_KVM_PV_EOI_EN:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003766 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI))
3767 return 1;
3768
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003769 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003770 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003771 case MSR_KVM_POLL_CONTROL:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003772 if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL))
3773 return 1;
3774
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003775 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3776 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003777 case MSR_IA32_P5_MC_ADDR:
3778 case MSR_IA32_P5_MC_TYPE:
3779 case MSR_IA32_MCG_CAP:
3780 case MSR_IA32_MCG_CTL:
3781 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003782 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003783 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3784 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003785 case MSR_IA32_XSS:
3786 if (!msr_info->host_initiated &&
3787 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3788 return 1;
3789 msr_info->data = vcpu->arch.ia32_xss;
3790 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003791 case MSR_K7_CLK_CTL:
3792 /*
3793 * Provide expected ramp-up count for K7. All other
3794 * are set to zero, indicating minimum divisors for
3795 * every field.
3796 *
3797 * This prevents guest kernels on AMD host with CPU
3798 * type 6, model 8 and higher from exploding due to
3799 * the rdmsr failing.
3800 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003801 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003802 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003803 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003804 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3805 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003806 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3807 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003808 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003809 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3810 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3811 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003812 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003813 msr_info->index, &msr_info->data,
3814 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003815 case MSR_IA32_BBL_CR_CTL3:
3816 /* This legacy MSR exists but isn't fully documented in current
3817 * silicon. It is however accessed by winxp in very narrow
3818 * scenarios where it sets bit #19, itself documented as
3819 * a "reserved" bit. Best effort attempt to source coherent
3820 * read data here should the balance of the register be
3821 * interpreted by the guest:
3822 *
3823 * L2 cache control register 3: 64GB range, 256KB size,
3824 * enabled, latency 0x1, configured
3825 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003826 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003827 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003828 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003829 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003830 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003831 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003832 break;
3833 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003834 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003835 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003836 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003837 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003838 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003839 if (!msr_info->host_initiated &&
3840 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3841 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003842 msr_info->data = vcpu->arch.msr_platform_info;
3843 break;
3844 case MSR_MISC_FEATURES_ENABLES:
3845 msr_info->data = vcpu->arch.msr_misc_features_enables;
3846 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003847 case MSR_K7_HWCR:
3848 msr_info->data = vcpu->arch.msr_hwcr;
3849 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003850 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003851 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003852 return kvm_pmu_get_msr(vcpu, msr_info);
Peter Xu6abe9c12020-06-22 18:04:41 -04003853 return KVM_MSR_RET_INVALID;
Carsten Otte15c4a642007-10-30 18:44:17 +01003854 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003855 return 0;
3856}
3857EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3858
Carsten Otte313a3dc2007-10-11 19:16:52 +02003859/*
3860 * Read or write a bunch of msrs. All parameters are kernel addresses.
3861 *
3862 * @return number of msrs set successfully.
3863 */
3864static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3865 struct kvm_msr_entry *entries,
3866 int (*do_msr)(struct kvm_vcpu *vcpu,
3867 unsigned index, u64 *data))
3868{
Tom Lendacky801e4592018-02-21 13:39:51 -06003869 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003870
Carsten Otte313a3dc2007-10-11 19:16:52 +02003871 for (i = 0; i < msrs->nmsrs; ++i)
3872 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3873 break;
3874
Carsten Otte313a3dc2007-10-11 19:16:52 +02003875 return i;
3876}
3877
3878/*
3879 * Read or write a bunch of msrs. Parameters are user addresses.
3880 *
3881 * @return number of msrs set successfully.
3882 */
3883static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3884 int (*do_msr)(struct kvm_vcpu *vcpu,
3885 unsigned index, u64 *data),
3886 int writeback)
3887{
3888 struct kvm_msrs msrs;
3889 struct kvm_msr_entry *entries;
3890 int r, n;
3891 unsigned size;
3892
3893 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003894 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003895 goto out;
3896
3897 r = -E2BIG;
3898 if (msrs.nmsrs >= MAX_IO_MSRS)
3899 goto out;
3900
Carsten Otte313a3dc2007-10-11 19:16:52 +02003901 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003902 entries = memdup_user(user_msrs->entries, size);
3903 if (IS_ERR(entries)) {
3904 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003905 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003906 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003907
3908 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3909 if (r < 0)
3910 goto out_free;
3911
3912 r = -EFAULT;
3913 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3914 goto out_free;
3915
3916 r = n;
3917
3918out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003919 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003920out:
3921 return r;
3922}
3923
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003924static inline bool kvm_can_mwait_in_guest(void)
3925{
3926 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003927 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3928 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003929}
3930
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02003931static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu,
3932 struct kvm_cpuid2 __user *cpuid_arg)
3933{
3934 struct kvm_cpuid2 cpuid;
3935 int r;
3936
3937 r = -EFAULT;
3938 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
3939 return r;
3940
3941 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries);
3942 if (r)
3943 return r;
3944
3945 r = -EFAULT;
3946 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
3947 return r;
3948
3949 return 0;
3950}
3951
Alexander Graf784aa3d2014-07-14 18:27:35 +02003952int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003953{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003954 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003955
3956 switch (ext) {
3957 case KVM_CAP_IRQCHIP:
3958 case KVM_CAP_HLT:
3959 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003960 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003961 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003962 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003963 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003964 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003965 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003966 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003967 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003968 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003969 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003970 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003971 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003972 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003973 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003974 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003975 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003976 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003977 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003978 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003979 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003980 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003981 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003982 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003983 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003984 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003985 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003986 case KVM_CAP_HYPERV_CPUID:
Vitaly Kuznetsov644f7062021-05-21 11:51:36 +02003987 case KVM_CAP_HYPERV_ENFORCE_CPUID:
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02003988 case KVM_CAP_SYS_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003989 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003990 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003991 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003992 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003993 case KVM_CAP_ASYNC_PF:
Vitaly Kuznetsov72de5fa4c2020-05-25 16:41:22 +02003994 case KVM_CAP_ASYNC_PF_INT:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003995 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003996 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003997 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003998 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003999 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01004000 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03004001 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02004002 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07004003 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01004004 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07004005 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06004006 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004007 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07004008 case KVM_CAP_EXCEPTION_PAYLOAD:
Peter Xub9b27822020-05-05 11:47:50 -04004009 case KVM_CAP_SET_GUEST_DEBUG:
Jim Mattson1aa561b2020-06-03 16:56:21 -07004010 case KVM_CAP_LAST_CPU:
Alexander Graf1ae09952020-09-25 16:34:16 +02004011 case KVM_CAP_X86_USER_SPACE_MSR:
Alexander Graf1a1552542020-09-25 16:34:21 +02004012 case KVM_CAP_X86_MSR_FILTER:
Oliver Upton66570e92020-08-18 15:24:28 +00004013 case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
Sean Christophersonfe7e9482021-04-12 16:21:43 +12004014#ifdef CONFIG_X86_SGX_KVM
4015 case KVM_CAP_SGX_ATTRIBUTE:
4016#endif
Nathan Tempelman54526d12021-04-08 22:32:14 +00004017 case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
Maxim Levitsky6dba9402021-06-07 12:02:02 +03004018 case KVM_CAP_SREGS2:
Aaron Lewis19238e72021-05-10 07:48:33 -07004019 case KVM_CAP_EXIT_ON_EMULATION_FAILURE:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08004020 r = 1;
4021 break;
Ashish Kalra0dbb1122021-06-08 18:05:43 +00004022 case KVM_CAP_EXIT_HYPERCALL:
4023 r = KVM_EXIT_HYPERCALL_VALID_MASK;
4024 break;
Maxim Levitsky7e582cc2021-04-01 16:54:45 +03004025 case KVM_CAP_SET_GUEST_DEBUG2:
4026 return KVM_GUESTDBG_VALID_MASK;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05004027#ifdef CONFIG_KVM_XEN
Joao Martins23200b72018-06-13 09:55:44 -04004028 case KVM_CAP_XEN_HVM:
4029 r = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR |
David Woodhouse8d4e7e82020-12-04 01:02:04 +00004030 KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL |
4031 KVM_XEN_HVM_CONFIG_SHARED_INFO;
David Woodhouse30b5c852021-03-01 12:53:09 +00004032 if (sched_info_on())
4033 r |= KVM_XEN_HVM_CONFIG_RUNSTATE;
Joao Martins23200b72018-06-13 09:55:44 -04004034 break;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05004035#endif
Ken Hofsass01643c52018-01-31 16:03:36 -08004036 case KVM_CAP_SYNC_REGS:
4037 r = KVM_SYNC_X86_VALID_FIELDS;
4038 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01004039 case KVM_CAP_ADJUST_CLOCK:
4040 r = KVM_CLOCK_TSC_STABLE;
4041 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004042 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08004043 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
4044 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004045 if(kvm_can_mwait_in_guest())
4046 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02004047 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004048 case KVM_CAP_X86_SMM:
4049 /* SMBASE is usually relocated above 1M on modern chipsets,
4050 * and SMM handlers might indeed rely on 4G segment limits,
4051 * so do not report SMM to be available if real mode is
4052 * emulated via vm86 mode. Still, do not go to great lengths
4053 * to avoid userspace's usage of the feature, because it is a
4054 * fringe case that is not enabled except via specific settings
4055 * of the module parameters.
4056 */
Jason Baronb36464772021-01-14 22:27:56 -05004057 r = static_call(kvm_x86_has_emulated_msr)(kvm, MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004058 break;
Avi Kivity774ead32007-12-26 13:57:04 +02004059 case KVM_CAP_VAPIC:
Jason Baronb36464772021-01-14 22:27:56 -05004060 r = !static_call(kvm_x86_cpu_has_accelerated_tpr)();
Avi Kivity774ead32007-12-26 13:57:04 +02004061 break;
Avi Kivityf7252302008-02-20 11:53:16 +02004062 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03004063 r = KVM_SOFT_MAX_VCPUS;
4064 break;
4065 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02004066 r = KVM_MAX_VCPUS;
4067 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02004068 case KVM_CAP_MAX_VCPU_ID:
4069 r = KVM_MAX_VCPU_ID;
4070 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03004071 case KVM_CAP_PV_MMU: /* obsolete */
4072 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004073 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08004074 case KVM_CAP_MCE:
4075 r = KVM_MAX_MCE_BANKS;
4076 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004077 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02004078 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004079 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004080 case KVM_CAP_TSC_CONTROL:
4081 r = kvm_has_tsc_control;
4082 break;
Radim Krčmář371313132016-07-12 22:09:27 +02004083 case KVM_CAP_X2APIC_API:
4084 r = KVM_X2APIC_API_VALID_FLAGS;
4085 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004086 case KVM_CAP_NESTED_STATE:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004087 r = kvm_x86_ops.nested_ops->get_state ?
4088 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004089 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004090 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004091 r = kvm_x86_ops.enable_direct_tlbflush != NULL;
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02004092 break;
4093 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004094 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004095 break;
Mohammed Gamal3edd6832020-07-10 17:48:11 +02004096 case KVM_CAP_SMALLER_MAXPHYADDR:
4097 r = (int) allow_smaller_maxphyaddr;
4098 break;
Andrew Jones004a0122020-08-04 19:06:04 +02004099 case KVM_CAP_STEAL_TIME:
4100 r = sched_info_on();
4101 break;
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08004102 case KVM_CAP_X86_BUS_LOCK_EXIT:
4103 if (kvm_has_bus_lock_exit)
4104 r = KVM_BUS_LOCK_DETECTION_OFF |
4105 KVM_BUS_LOCK_DETECTION_EXIT;
4106 else
4107 r = 0;
4108 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08004109 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08004110 break;
4111 }
4112 return r;
4113
4114}
4115
Carsten Otte043405e2007-10-10 17:16:19 +02004116long kvm_arch_dev_ioctl(struct file *filp,
4117 unsigned int ioctl, unsigned long arg)
4118{
4119 void __user *argp = (void __user *)arg;
4120 long r;
4121
4122 switch (ioctl) {
4123 case KVM_GET_MSR_INDEX_LIST: {
4124 struct kvm_msr_list __user *user_msr_list = argp;
4125 struct kvm_msr_list msr_list;
4126 unsigned n;
4127
4128 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004129 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02004130 goto out;
4131 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004132 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004133 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02004134 goto out;
4135 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02004136 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02004137 goto out;
4138 r = -EFAULT;
4139 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
4140 num_msrs_to_save * sizeof(u32)))
4141 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02004142 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02004143 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004144 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02004145 goto out;
4146 r = 0;
4147 break;
4148 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02004149 case KVM_GET_SUPPORTED_CPUID:
4150 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02004151 struct kvm_cpuid2 __user *cpuid_arg = argp;
4152 struct kvm_cpuid2 cpuid;
4153
4154 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004155 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02004156 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02004157
4158 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
4159 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02004160 if (r)
4161 goto out;
4162
4163 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004164 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02004165 goto out;
4166 r = 0;
4167 break;
4168 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08004169 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08004170 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004171 if (copy_to_user(argp, &kvm_mce_cap_supported,
4172 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004173 goto out;
4174 r = 0;
4175 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004176 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
4177 struct kvm_msr_list __user *user_msr_list = argp;
4178 struct kvm_msr_list msr_list;
4179 unsigned int n;
4180
4181 r = -EFAULT;
4182 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
4183 goto out;
4184 n = msr_list.nmsrs;
4185 msr_list.nmsrs = num_msr_based_features;
4186 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
4187 goto out;
4188 r = -E2BIG;
4189 if (n < msr_list.nmsrs)
4190 goto out;
4191 r = -EFAULT;
4192 if (copy_to_user(user_msr_list->indices, &msr_based_features,
4193 num_msr_based_features * sizeof(u32)))
4194 goto out;
4195 r = 0;
4196 break;
4197 }
4198 case KVM_GET_MSRS:
4199 r = msr_io(NULL, argp, do_get_msr_feature, 1);
4200 break;
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02004201 case KVM_GET_SUPPORTED_HV_CPUID:
4202 r = kvm_ioctl_get_supported_hv_cpuid(NULL, argp);
4203 break;
Carsten Otte043405e2007-10-10 17:16:19 +02004204 default:
4205 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08004206 break;
Carsten Otte043405e2007-10-10 17:16:19 +02004207 }
4208out:
4209 return r;
4210}
4211
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004212static void wbinvd_ipi(void *garbage)
4213{
4214 wbinvd();
4215}
4216
4217static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
4218{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06004219 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004220}
4221
Carsten Otte313a3dc2007-10-11 19:16:52 +02004222void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
4223{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004224 /* Address WBINVD may be executed by guest */
4225 if (need_emulate_wbinvd(vcpu)) {
Jason Baronb36464772021-01-14 22:27:56 -05004226 if (static_call(kvm_x86_has_wbinvd_exit)())
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004227 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
4228 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
4229 smp_call_function_single(vcpu->cpu,
4230 wbinvd_ipi, NULL, 1);
4231 }
4232
Jason Baronb36464772021-01-14 22:27:56 -05004233 static_call(kvm_x86_vcpu_load)(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02004234
Babu Moger37486132020-05-12 18:59:06 -05004235 /* Save host pkru register if supported */
4236 vcpu->arch.host_pkru = read_pkru();
4237
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02004238 /* Apply any externally detected TSC adjustments (due to suspend) */
4239 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
4240 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
4241 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08004242 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02004243 }
4244
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01004245 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02004246 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02004247 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10004248 if (tsc_delta < 0)
4249 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07004250
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01004251 if (kvm_check_tsc_unstable()) {
Ilias Stamatis9b399df2021-05-26 19:44:10 +01004252 u64 offset = kvm_compute_l1_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02004253 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04004254 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10004255 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10004256 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02004257
4258 if (kvm_lapic_hv_timer_in_use(vcpu))
4259 kvm_lapic_restart_hv_timer(vcpu);
4260
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02004261 /*
4262 * On a host with synchronized TSC, there is no need to update
4263 * kvmclock on vcpu->cpu migration
4264 */
4265 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03004266 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10004267 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02004268 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10004269 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10004270 }
Glauber Costac9aaa892011-07-11 15:28:14 -04004271
Glauber Costac9aaa892011-07-11 15:28:14 -04004272 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004273}
4274
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004275static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
4276{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004277 struct kvm_host_map map;
4278 struct kvm_steal_time *st;
4279
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004280 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
4281 return;
4282
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00004283 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00004284 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004285
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004286 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
4287 &vcpu->arch.st.cache, true))
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004288 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004289
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004290 st = map.hva +
4291 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
4292
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00004293 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004294
4295 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004296}
4297
Carsten Otte313a3dc2007-10-11 19:16:52 +02004298void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
4299{
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004300 int idx;
4301
Tom Lendackyf1c63662020-12-14 10:29:50 -05004302 if (vcpu->preempted && !vcpu->arch.guest_state_protected)
Jason Baronb36464772021-01-14 22:27:56 -05004303 vcpu->arch.preempted_in_kernel = !static_call(kvm_x86_get_cpl)(vcpu);
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004304
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004305 /*
4306 * Take the srcu lock as memslots will be accessed to check the gfn
4307 * cache generation against the memslots generation.
4308 */
4309 idx = srcu_read_lock(&vcpu->kvm->srcu);
David Woodhouse30b5c852021-03-01 12:53:09 +00004310 if (kvm_xen_msr_enabled(vcpu->kvm))
4311 kvm_xen_runstate_set_preempted(vcpu);
4312 else
4313 kvm_steal_time_set_preempted(vcpu);
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004314 srcu_read_unlock(&vcpu->kvm->srcu, idx);
David Woodhouse30b5c852021-03-01 12:53:09 +00004315
Jason Baronb36464772021-01-14 22:27:56 -05004316 static_call(kvm_x86_vcpu_put)(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02004317 vcpu->arch.last_host_tsc = rdtsc();
Carsten Otte313a3dc2007-10-11 19:16:52 +02004318}
4319
Carsten Otte313a3dc2007-10-11 19:16:52 +02004320static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
4321 struct kvm_lapic_state *s)
4322{
Liran Alonfa59cc02017-12-24 18:12:53 +02004323 if (vcpu->arch.apicv_active)
Jason Baronb36464772021-01-14 22:27:56 -05004324 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004325
Radim Krčmářa92e2542016-07-12 22:09:22 +02004326 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004327}
4328
4329static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
4330 struct kvm_lapic_state *s)
4331{
Radim Krčmářa92e2542016-07-12 22:09:22 +02004332 int r;
4333
4334 r = kvm_apic_set_state(vcpu, s);
4335 if (r)
4336 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03004337 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004338
4339 return 0;
4340}
4341
Matt Gingell127a4572015-11-17 17:32:05 +01004342static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
4343{
Paolo Bonzini71cc8492020-11-27 09:18:20 +01004344 /*
4345 * We can accept userspace's request for interrupt injection
4346 * as long as we have a place to store the interrupt number.
4347 * The actual injection will happen when the CPU is able to
4348 * deliver the interrupt.
4349 */
4350 if (kvm_cpu_has_extint(vcpu))
4351 return false;
4352
4353 /* Acknowledging ExtINT does not happen if LINT0 is masked. */
Matt Gingell127a4572015-11-17 17:32:05 +01004354 return (!lapic_in_kernel(vcpu) ||
4355 kvm_apic_accept_pic_intr(vcpu));
4356}
4357
Matt Gingell782d4222015-11-16 15:26:00 -08004358static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
4359{
Paolo Bonzinifa7a5492021-07-14 17:37:49 -04004360 /*
4361 * Do not cause an interrupt window exit if an exception
4362 * is pending or an event needs reinjection; userspace
4363 * might want to inject the interrupt manually using KVM_SET_REGS
4364 * or KVM_SET_SREGS. For that to work, we must be at an
4365 * instruction boundary and with no events half-injected.
4366 */
4367 return (kvm_arch_interrupt_allowed(vcpu) &&
4368 kvm_cpu_accept_dm_intr(vcpu) &&
4369 !kvm_event_needs_reinjection(vcpu) &&
4370 !vcpu->arch.exception.pending);
Matt Gingell782d4222015-11-16 15:26:00 -08004371}
4372
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004373static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
4374 struct kvm_interrupt *irq)
4375{
Chen Gang02cdb502013-02-27 11:33:25 +08004376 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004377 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004378
4379 if (!irqchip_in_kernel(vcpu->kvm)) {
4380 kvm_queue_interrupt(vcpu, irq->irq, false);
4381 kvm_make_request(KVM_REQ_EVENT, vcpu);
4382 return 0;
4383 }
4384
4385 /*
4386 * With in-kernel LAPIC, we only use this to inject EXTINT, so
4387 * fail for in-kernel 8259.
4388 */
4389 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004390 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004391
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004392 if (vcpu->arch.pending_external_vector != -1)
4393 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004394
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004395 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08004396 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004397 return 0;
4398}
4399
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004400static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
4401{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004402 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004403
4404 return 0;
4405}
4406
Paolo Bonzinif0778252015-04-01 15:06:40 +02004407static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
4408{
Paolo Bonzini64d60672015-05-07 11:36:11 +02004409 kvm_make_request(KVM_REQ_SMI, vcpu);
4410
Paolo Bonzinif0778252015-04-01 15:06:40 +02004411 return 0;
4412}
4413
Avi Kivityb209749f2007-10-22 16:50:39 +02004414static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
4415 struct kvm_tpr_access_ctl *tac)
4416{
4417 if (tac->flags)
4418 return -EINVAL;
4419 vcpu->arch.tpr_access_reporting = !!tac->enabled;
4420 return 0;
4421}
4422
Huang Ying890ca9a2009-05-11 16:48:15 +08004423static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
4424 u64 mcg_cap)
4425{
4426 int r;
4427 unsigned bank_num = mcg_cap & 0xff, bank;
4428
4429 r = -EINVAL;
Jim Mattsonc4e0e4ab2020-05-11 15:56:16 -07004430 if (!bank_num || bank_num > KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08004431 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004432 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08004433 goto out;
4434 r = 0;
4435 vcpu->arch.mcg_cap = mcg_cap;
4436 /* Init IA32_MCG_CTL to all 1s */
4437 if (mcg_cap & MCG_CTL_P)
4438 vcpu->arch.mcg_ctl = ~(u64)0;
4439 /* Init IA32_MCi_CTL to all 1s */
4440 for (bank = 0; bank < bank_num; bank++)
4441 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004442
Jason Baronb36464772021-01-14 22:27:56 -05004443 static_call(kvm_x86_setup_mce)(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08004444out:
4445 return r;
4446}
4447
4448static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
4449 struct kvm_x86_mce *mce)
4450{
4451 u64 mcg_cap = vcpu->arch.mcg_cap;
4452 unsigned bank_num = mcg_cap & 0xff;
4453 u64 *banks = vcpu->arch.mce_banks;
4454
4455 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
4456 return -EINVAL;
4457 /*
4458 * if IA32_MCG_CTL is not all 1s, the uncorrected error
4459 * reporting is disabled
4460 */
4461 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
4462 vcpu->arch.mcg_ctl != ~(u64)0)
4463 return 0;
4464 banks += 4 * mce->bank;
4465 /*
4466 * if IA32_MCi_CTL is not all 1s, the uncorrected error
4467 * reporting is disabled for the bank
4468 */
4469 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
4470 return 0;
4471 if (mce->status & MCI_STATUS_UC) {
4472 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02004473 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03004474 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08004475 return 0;
4476 }
4477 if (banks[1] & MCI_STATUS_VAL)
4478 mce->status |= MCI_STATUS_OVER;
4479 banks[2] = mce->addr;
4480 banks[3] = mce->misc;
4481 vcpu->arch.mcg_status = mce->mcg_status;
4482 banks[1] = mce->status;
4483 kvm_queue_exception(vcpu, MC_VECTOR);
4484 } else if (!(banks[1] & MCI_STATUS_VAL)
4485 || !(banks[1] & MCI_STATUS_UC)) {
4486 if (banks[1] & MCI_STATUS_VAL)
4487 mce->status |= MCI_STATUS_OVER;
4488 banks[2] = mce->addr;
4489 banks[3] = mce->misc;
4490 banks[1] = mce->status;
4491 } else
4492 banks[1] |= MCI_STATUS_OVER;
4493 return 0;
4494}
4495
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004496static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
4497 struct kvm_vcpu_events *events)
4498{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004499 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07004500
Jay Zhou1f7becf2021-01-18 16:47:20 +08004501 if (kvm_check_request(KVM_REQ_SMI, vcpu))
4502 process_smi(vcpu);
4503
Wanpeng Li664f8e22017-08-24 03:35:09 -07004504 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08004505 * In guest mode, payload delivery should be deferred,
4506 * so that the L1 hypervisor can intercept #PF before
4507 * CR2 is modified (or intercept #DB before DR6 is
4508 * modified under nVMX). Unless the per-VM capability,
4509 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
4510 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
4511 * opportunistically defer the exception payload, deliver it if the
4512 * capability hasn't been requested before processing a
4513 * KVM_GET_VCPU_EVENTS.
4514 */
4515 if (!vcpu->kvm->arch.exception_payload_enabled &&
4516 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
4517 kvm_deliver_exception_payload(vcpu);
4518
4519 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07004520 * The API doesn't provide the instruction length for software
4521 * exceptions, so don't report them. As long as the guest RIP
4522 * isn't advanced, we should expect to encounter the exception
4523 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07004524 */
Jim Mattson59073aa2018-10-16 14:29:20 -07004525 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
4526 events->exception.injected = 0;
4527 events->exception.pending = 0;
4528 } else {
4529 events->exception.injected = vcpu->arch.exception.injected;
4530 events->exception.pending = vcpu->arch.exception.pending;
4531 /*
4532 * For ABI compatibility, deliberately conflate
4533 * pending and injected exceptions when
4534 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
4535 */
4536 if (!vcpu->kvm->arch.exception_payload_enabled)
4537 events->exception.injected |=
4538 vcpu->arch.exception.pending;
4539 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004540 events->exception.nr = vcpu->arch.exception.nr;
4541 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
4542 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07004543 events->exception_has_payload = vcpu->arch.exception.has_payload;
4544 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004545
Jan Kiszka03b82a32010-02-15 10:45:41 +01004546 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03004547 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004548 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01004549 events->interrupt.soft = 0;
Jason Baronb36464772021-01-14 22:27:56 -05004550 events->interrupt.shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004551
4552 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004553 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jason Baronb36464772021-01-14 22:27:56 -05004554 events->nmi.masked = static_call(kvm_x86_get_nmi_mask)(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004555 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004556
Jan Kiszka66450a22013-03-13 12:42:34 +01004557 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004558
Paolo Bonzinif0778252015-04-01 15:06:40 +02004559 events->smi.smm = is_smm(vcpu);
4560 events->smi.pending = vcpu->arch.smi_pending;
4561 events->smi.smm_inside_nmi =
4562 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
4563 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
4564
Jan Kiszkadab4b912009-12-06 18:24:15 +01004565 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02004566 | KVM_VCPUEVENT_VALID_SHADOW
4567 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07004568 if (vcpu->kvm->arch.exception_payload_enabled)
4569 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
4570
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004571 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004572}
4573
Sean Christophersondc872752021-06-09 11:56:15 -07004574static void kvm_smm_changed(struct kvm_vcpu *vcpu, bool entering_smm);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004575
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004576static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
4577 struct kvm_vcpu_events *events)
4578{
Jan Kiszkadab4b912009-12-06 18:24:15 +01004579 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01004580 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02004581 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07004582 | KVM_VCPUEVENT_VALID_SMM
4583 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004584 return -EINVAL;
4585
Jim Mattson59073aa2018-10-16 14:29:20 -07004586 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
4587 if (!vcpu->kvm->arch.exception_payload_enabled)
4588 return -EINVAL;
4589 if (events->exception.pending)
4590 events->exception.injected = 0;
4591 else
4592 events->exception_has_payload = 0;
4593 } else {
4594 events->exception.pending = 0;
4595 events->exception_has_payload = 0;
4596 }
4597
4598 if ((events->exception.injected || events->exception.pending) &&
4599 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02004600 return -EINVAL;
4601
David Hildenbrand28bf2882017-03-23 11:46:03 +01004602 /* INITs are latched while in SMM */
4603 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
4604 (events->smi.smm || events->smi.pending) &&
4605 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4606 return -EINVAL;
4607
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004608 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07004609 vcpu->arch.exception.injected = events->exception.injected;
4610 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004611 vcpu->arch.exception.nr = events->exception.nr;
4612 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
4613 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07004614 vcpu->arch.exception.has_payload = events->exception_has_payload;
4615 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004616
Liran Alon04140b42018-03-23 03:01:31 +03004617 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004618 vcpu->arch.interrupt.nr = events->interrupt.nr;
4619 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01004620 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
Jason Baronb36464772021-01-14 22:27:56 -05004621 static_call(kvm_x86_set_interrupt_shadow)(vcpu,
4622 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004623
4624 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01004625 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
4626 vcpu->arch.nmi_pending = events->nmi.pending;
Jason Baronb36464772021-01-14 22:27:56 -05004627 static_call(kvm_x86_set_nmi_mask)(vcpu, events->nmi.masked);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004628
Jan Kiszka66450a22013-03-13 12:42:34 +01004629 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004630 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01004631 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004632
Paolo Bonzinif0778252015-04-01 15:06:40 +02004633 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersondc872752021-06-09 11:56:15 -07004634 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm)
4635 kvm_smm_changed(vcpu, events->smi.smm);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004636
Paolo Bonzinif0778252015-04-01 15:06:40 +02004637 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004638
4639 if (events->smi.smm) {
4640 if (events->smi.smm_inside_nmi)
4641 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02004642 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004643 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02004644 }
4645
4646 if (lapic_in_kernel(vcpu)) {
4647 if (events->smi.latched_init)
4648 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
4649 else
4650 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02004651 }
4652 }
4653
Avi Kivity3842d132010-07-27 12:30:24 +03004654 kvm_make_request(KVM_REQ_EVENT, vcpu);
4655
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004656 return 0;
4657}
4658
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004659static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
4660 struct kvm_debugregs *dbgregs)
4661{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004662 unsigned long val;
4663
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004664 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004665 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004666 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004667 dbgregs->dr7 = vcpu->arch.dr7;
4668 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004669 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004670}
4671
4672static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
4673 struct kvm_debugregs *dbgregs)
4674{
4675 if (dbgregs->flags)
4676 return -EINVAL;
4677
Paolo Bonzinifd238002020-11-13 08:31:09 -05004678 if (!kvm_dr6_valid(dbgregs->dr6))
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004679 return -EINVAL;
Paolo Bonzinifd238002020-11-13 08:31:09 -05004680 if (!kvm_dr7_valid(dbgregs->dr7))
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004681 return -EINVAL;
4682
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004683 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004684 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004685 vcpu->arch.dr6 = dbgregs->dr6;
4686 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004687 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004688
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004689 return 0;
4690}
4691
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004692#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4693
4694static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4695{
Marc Orrb666a4b2018-11-06 14:53:56 -08004696 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004697 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004698 u64 valid;
4699
4700 /*
4701 * Copy legacy XSAVE area, to avoid complications with CPUID
4702 * leaves 0 and 1 in the loop below.
4703 */
4704 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4705
4706 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004707 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004708 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4709
4710 /*
4711 * Copy each region from the possibly compacted offset to the
4712 * non-compacted offset.
4713 */
Dave Hansend91cab72015-09-02 16:31:26 -07004714 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004715 while (valid) {
Dave Hansen71ef4532021-06-23 14:01:49 +02004716 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004717 u64 xfeature_mask = valid & -valid;
4718 int xfeature_nr = fls64(xfeature_mask) - 1;
Dave Hansen71ef4532021-06-23 14:01:49 +02004719 void *src;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004720
Dave Hansen71ef4532021-06-23 14:01:49 +02004721 cpuid_count(XSTATE_CPUID, xfeature_nr,
4722 &size, &offset, &ecx, &edx);
4723
4724 if (xfeature_nr == XFEATURE_PKRU) {
4725 memcpy(dest + offset, &vcpu->arch.pkru,
4726 sizeof(vcpu->arch.pkru));
4727 } else {
4728 src = get_xsave_addr(xsave, xfeature_nr);
4729 if (src)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004730 memcpy(dest + offset, src, size);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004731 }
4732
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004733 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004734 }
4735}
4736
4737static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4738{
Marc Orrb666a4b2018-11-06 14:53:56 -08004739 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004740 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4741 u64 valid;
4742
4743 /*
4744 * Copy legacy XSAVE area, to avoid complications with CPUID
4745 * leaves 0 and 1 in the loop below.
4746 */
4747 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4748
4749 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004750 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004751 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004752 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004753
4754 /*
4755 * Copy each region from the non-compacted offset to the
4756 * possibly compacted offset.
4757 */
Dave Hansend91cab72015-09-02 16:31:26 -07004758 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004759 while (valid) {
Dave Hansen71ef4532021-06-23 14:01:49 +02004760 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004761 u64 xfeature_mask = valid & -valid;
4762 int xfeature_nr = fls64(xfeature_mask) - 1;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004763
Dave Hansen71ef4532021-06-23 14:01:49 +02004764 cpuid_count(XSTATE_CPUID, xfeature_nr,
4765 &size, &offset, &ecx, &edx);
4766
4767 if (xfeature_nr == XFEATURE_PKRU) {
4768 memcpy(&vcpu->arch.pkru, src + offset,
4769 sizeof(vcpu->arch.pkru));
4770 } else {
4771 void *dest = get_xsave_addr(xsave, xfeature_nr);
4772
4773 if (dest)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004774 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004775 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004776
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004777 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004778 }
4779}
4780
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004781static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4782 struct kvm_xsave *guest_xsave)
4783{
Tom Lendackyed02b212020-12-10 11:10:01 -06004784 if (!vcpu->arch.guest_fpu)
4785 return;
4786
Borislav Petkovd366bf72016-04-04 22:25:02 +02004787 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004788 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4789 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004790 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004791 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004792 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004793 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004794 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004795 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004796 }
4797}
4798
Wanpeng Lia5758132017-05-11 02:58:55 -07004799#define XSAVE_MXCSR_OFFSET 24
4800
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004801static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4802 struct kvm_xsave *guest_xsave)
4803{
Tom Lendackyed02b212020-12-10 11:10:01 -06004804 u64 xstate_bv;
4805 u32 mxcsr;
4806
4807 if (!vcpu->arch.guest_fpu)
4808 return 0;
4809
4810 xstate_bv = *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
4811 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004812
Borislav Petkovd366bf72016-04-04 22:25:02 +02004813 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004814 /*
4815 * Here we allow setting states that are not present in
4816 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4817 * with old userspace.
4818 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004819 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004820 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004821 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004822 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004823 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4824 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004825 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004826 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004827 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004828 }
4829 return 0;
4830}
4831
4832static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4833 struct kvm_xcrs *guest_xcrs)
4834{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004835 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004836 guest_xcrs->nr_xcrs = 0;
4837 return;
4838 }
4839
4840 guest_xcrs->nr_xcrs = 1;
4841 guest_xcrs->flags = 0;
4842 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4843 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4844}
4845
4846static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4847 struct kvm_xcrs *guest_xcrs)
4848{
4849 int i, r = 0;
4850
Borislav Petkovd366bf72016-04-04 22:25:02 +02004851 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004852 return -EINVAL;
4853
4854 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4855 return -EINVAL;
4856
4857 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4858 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004859 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004860 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004861 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004862 break;
4863 }
4864 if (r)
4865 r = -EINVAL;
4866 return r;
4867}
4868
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004869/*
4870 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4871 * stopped by the hypervisor. This function will be called from the host only.
4872 * EINVAL is returned when the host attempts to set the flag for a guest that
4873 * does not support pv clocks.
4874 */
4875static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4876{
Andy Honig0b794592013-02-20 14:48:10 -08004877 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004878 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004879 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004880 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4881 return 0;
4882}
4883
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004884static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4885 struct kvm_enable_cap *cap)
4886{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004887 int r;
4888 uint16_t vmcs_version;
4889 void __user *user_ptr;
4890
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004891 if (cap->flags)
4892 return -EINVAL;
4893
4894 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004895 case KVM_CAP_HYPERV_SYNIC2:
4896 if (cap->args[0])
4897 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004898 fallthrough;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004899
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004900 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004901 if (!irqchip_in_kernel(vcpu->kvm))
4902 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004903 return kvm_hv_activate_synic(vcpu, cap->cap ==
4904 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004905 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004906 if (!kvm_x86_ops.nested_ops->enable_evmcs)
Sean Christopherson51589172018-12-03 13:53:10 -08004907 return -ENOTTY;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004908 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004909 if (!r) {
4910 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4911 if (copy_to_user(user_ptr, &vmcs_version,
4912 sizeof(vmcs_version)))
4913 r = -EFAULT;
4914 }
4915 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004916 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004917 if (!kvm_x86_ops.enable_direct_tlbflush)
Tianyu Lan344c6c82019-08-22 22:30:20 +08004918 return -ENOTTY;
4919
Jason Baronb36464772021-01-14 22:27:56 -05004920 return static_call(kvm_x86_enable_direct_tlbflush)(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004921
Vitaly Kuznetsov644f7062021-05-21 11:51:36 +02004922 case KVM_CAP_HYPERV_ENFORCE_CPUID:
4923 return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]);
4924
Oliver Upton66570e92020-08-18 15:24:28 +00004925 case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
4926 vcpu->arch.pv_cpuid.enforce = cap->args[0];
Oliver Upton01b4f512020-10-27 16:10:42 -07004927 if (vcpu->arch.pv_cpuid.enforce)
4928 kvm_update_pv_runtime(vcpu);
Oliver Upton66570e92020-08-18 15:24:28 +00004929
4930 return 0;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004931 default:
4932 return -EINVAL;
4933 }
4934}
4935
Carsten Otte313a3dc2007-10-11 19:16:52 +02004936long kvm_arch_vcpu_ioctl(struct file *filp,
4937 unsigned int ioctl, unsigned long arg)
4938{
4939 struct kvm_vcpu *vcpu = filp->private_data;
4940 void __user *argp = (void __user *)arg;
4941 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004942 union {
Maxim Levitsky6dba9402021-06-07 12:02:02 +03004943 struct kvm_sregs2 *sregs2;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004944 struct kvm_lapic_state *lapic;
4945 struct kvm_xsave *xsave;
4946 struct kvm_xcrs *xcrs;
4947 void *buffer;
4948 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004949
Christoffer Dall9b0624712017-12-04 21:35:36 +01004950 vcpu_load(vcpu);
4951
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004952 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004953 switch (ioctl) {
4954 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004955 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004956 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004957 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004958 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4959 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004960
Dave Hansenb772ff32008-08-11 10:01:47 -07004961 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004962 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004963 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004964 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004965 if (r)
4966 goto out;
4967 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004968 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004969 goto out;
4970 r = 0;
4971 break;
4972 }
4973 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004974 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004975 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004976 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004977 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004978 if (IS_ERR(u.lapic)) {
4979 r = PTR_ERR(u.lapic);
4980 goto out_nofree;
4981 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004982
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004983 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004984 break;
4985 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004986 case KVM_INTERRUPT: {
4987 struct kvm_interrupt irq;
4988
4989 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004990 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004991 goto out;
4992 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004993 break;
4994 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004995 case KVM_NMI: {
4996 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004997 break;
4998 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004999 case KVM_SMI: {
5000 r = kvm_vcpu_ioctl_smi(vcpu);
5001 break;
5002 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02005003 case KVM_SET_CPUID: {
5004 struct kvm_cpuid __user *cpuid_arg = argp;
5005 struct kvm_cpuid cpuid;
5006
5007 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005008 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02005009 goto out;
5010 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005011 break;
5012 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02005013 case KVM_SET_CPUID2: {
5014 struct kvm_cpuid2 __user *cpuid_arg = argp;
5015 struct kvm_cpuid2 cpuid;
5016
5017 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005018 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02005019 goto out;
5020 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00005021 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02005022 break;
5023 }
5024 case KVM_GET_CPUID2: {
5025 struct kvm_cpuid2 __user *cpuid_arg = argp;
5026 struct kvm_cpuid2 cpuid;
5027
5028 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005029 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02005030 goto out;
5031 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00005032 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02005033 if (r)
5034 goto out;
5035 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005036 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02005037 goto out;
5038 r = 0;
5039 break;
5040 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005041 case KVM_GET_MSRS: {
5042 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005043 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06005044 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005045 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06005046 }
5047 case KVM_SET_MSRS: {
5048 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005049 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06005050 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005051 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06005052 }
Avi Kivityb209749f2007-10-22 16:50:39 +02005053 case KVM_TPR_ACCESS_REPORTING: {
5054 struct kvm_tpr_access_ctl tac;
5055
5056 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005057 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02005058 goto out;
5059 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
5060 if (r)
5061 goto out;
5062 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005063 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02005064 goto out;
5065 r = 0;
5066 break;
5067 };
Avi Kivityb93463a2007-10-25 16:52:32 +02005068 case KVM_SET_VAPIC_ADDR: {
5069 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01005070 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02005071
5072 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005073 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02005074 goto out;
5075 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005076 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02005077 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01005078 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08005079 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01005080 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02005081 break;
5082 }
Huang Ying890ca9a2009-05-11 16:48:15 +08005083 case KVM_X86_SETUP_MCE: {
5084 u64 mcg_cap;
5085
5086 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005087 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08005088 goto out;
5089 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
5090 break;
5091 }
5092 case KVM_X86_SET_MCE: {
5093 struct kvm_x86_mce mce;
5094
5095 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005096 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08005097 goto out;
5098 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
5099 break;
5100 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005101 case KVM_GET_VCPU_EVENTS: {
5102 struct kvm_vcpu_events events;
5103
5104 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
5105
5106 r = -EFAULT;
5107 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
5108 break;
5109 r = 0;
5110 break;
5111 }
5112 case KVM_SET_VCPU_EVENTS: {
5113 struct kvm_vcpu_events events;
5114
5115 r = -EFAULT;
5116 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
5117 break;
5118
5119 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
5120 break;
5121 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01005122 case KVM_GET_DEBUGREGS: {
5123 struct kvm_debugregs dbgregs;
5124
5125 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
5126
5127 r = -EFAULT;
5128 if (copy_to_user(argp, &dbgregs,
5129 sizeof(struct kvm_debugregs)))
5130 break;
5131 r = 0;
5132 break;
5133 }
5134 case KVM_SET_DEBUGREGS: {
5135 struct kvm_debugregs dbgregs;
5136
5137 r = -EFAULT;
5138 if (copy_from_user(&dbgregs, argp,
5139 sizeof(struct kvm_debugregs)))
5140 break;
5141
5142 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
5143 break;
5144 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005145 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08005146 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005147 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005148 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005149 break;
5150
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005151 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005152
5153 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005154 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005155 break;
5156 r = 0;
5157 break;
5158 }
5159 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02005160 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01005161 if (IS_ERR(u.xsave)) {
5162 r = PTR_ERR(u.xsave);
5163 goto out_nofree;
5164 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005165
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005166 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005167 break;
5168 }
5169 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08005170 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005171 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005172 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005173 break;
5174
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005175 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005176
5177 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005178 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005179 sizeof(struct kvm_xcrs)))
5180 break;
5181 r = 0;
5182 break;
5183 }
5184 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02005185 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01005186 if (IS_ERR(u.xcrs)) {
5187 r = PTR_ERR(u.xcrs);
5188 goto out_nofree;
5189 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005190
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005191 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005192 break;
5193 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01005194 case KVM_SET_TSC_KHZ: {
5195 u32 user_tsc_khz;
5196
5197 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01005198 user_tsc_khz = (u32)arg;
5199
Marcelo Tosatti26769f92020-06-16 08:47:41 -03005200 if (kvm_has_tsc_control &&
5201 user_tsc_khz >= kvm_max_guest_tsc_khz)
Joerg Roedel92a1f122011-03-25 09:44:51 +01005202 goto out;
5203
Zachary Amsdencc578282012-02-03 15:43:50 -02005204 if (user_tsc_khz == 0)
5205 user_tsc_khz = tsc_khz;
5206
Haozhong Zhang381d5852015-10-20 15:39:04 +08005207 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
5208 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01005209
Joerg Roedel92a1f122011-03-25 09:44:51 +01005210 goto out;
5211 }
5212 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02005213 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01005214 goto out;
5215 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05005216 case KVM_KVMCLOCK_CTRL: {
5217 r = kvm_set_guest_paused(vcpu);
5218 goto out;
5219 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03005220 case KVM_ENABLE_CAP: {
5221 struct kvm_enable_cap cap;
5222
5223 r = -EFAULT;
5224 if (copy_from_user(&cap, argp, sizeof(cap)))
5225 goto out;
5226 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
5227 break;
5228 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005229 case KVM_GET_NESTED_STATE: {
5230 struct kvm_nested_state __user *user_kvm_nested_state = argp;
5231 u32 user_data_size;
5232
5233 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04005234 if (!kvm_x86_ops.nested_ops->get_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005235 break;
5236
5237 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03005238 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005239 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03005240 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005241
Paolo Bonzini33b22172020-04-17 10:24:18 -04005242 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state,
5243 user_data_size);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005244 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03005245 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005246
5247 if (r > user_data_size) {
5248 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03005249 r = -EFAULT;
5250 else
5251 r = -E2BIG;
5252 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005253 }
Liran Alon26b471c2018-09-16 14:28:20 +03005254
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005255 r = 0;
5256 break;
5257 }
5258 case KVM_SET_NESTED_STATE: {
5259 struct kvm_nested_state __user *user_kvm_nested_state = argp;
5260 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08005261 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005262
5263 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04005264 if (!kvm_x86_ops.nested_ops->set_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005265 break;
5266
Liran Alon26b471c2018-09-16 14:28:20 +03005267 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005268 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03005269 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005270
Liran Alon26b471c2018-09-16 14:28:20 +03005271 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005272 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03005273 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005274
5275 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02005276 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04005277 | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING
5278 | KVM_STATE_NESTED_GIF_SET))
Liran Alon26b471c2018-09-16 14:28:20 +03005279 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005280
5281 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02005282 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
5283 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03005284 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005285
Sean Christophersonad5996d2019-11-22 08:58:18 -08005286 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini33b22172020-04-17 10:24:18 -04005287 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08005288 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005289 break;
5290 }
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02005291 case KVM_GET_SUPPORTED_HV_CPUID:
5292 r = kvm_ioctl_get_supported_hv_cpuid(vcpu, argp);
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01005293 break;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005294#ifdef CONFIG_KVM_XEN
David Woodhouse3e324612021-02-02 16:53:25 +00005295 case KVM_XEN_VCPU_GET_ATTR: {
5296 struct kvm_xen_vcpu_attr xva;
5297
5298 r = -EFAULT;
5299 if (copy_from_user(&xva, argp, sizeof(xva)))
5300 goto out;
5301 r = kvm_xen_vcpu_get_attr(vcpu, &xva);
5302 if (!r && copy_to_user(argp, &xva, sizeof(xva)))
5303 r = -EFAULT;
5304 break;
5305 }
5306 case KVM_XEN_VCPU_SET_ATTR: {
5307 struct kvm_xen_vcpu_attr xva;
5308
5309 r = -EFAULT;
5310 if (copy_from_user(&xva, argp, sizeof(xva)))
5311 goto out;
5312 r = kvm_xen_vcpu_set_attr(vcpu, &xva);
5313 break;
5314 }
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005315#endif
Maxim Levitsky6dba9402021-06-07 12:02:02 +03005316 case KVM_GET_SREGS2: {
5317 u.sregs2 = kzalloc(sizeof(struct kvm_sregs2), GFP_KERNEL);
5318 r = -ENOMEM;
5319 if (!u.sregs2)
5320 goto out;
5321 __get_sregs2(vcpu, u.sregs2);
5322 r = -EFAULT;
5323 if (copy_to_user(argp, u.sregs2, sizeof(struct kvm_sregs2)))
5324 goto out;
5325 r = 0;
5326 break;
5327 }
5328 case KVM_SET_SREGS2: {
5329 u.sregs2 = memdup_user(argp, sizeof(struct kvm_sregs2));
5330 if (IS_ERR(u.sregs2)) {
5331 r = PTR_ERR(u.sregs2);
5332 u.sregs2 = NULL;
5333 goto out;
5334 }
5335 r = __set_sregs2(vcpu, u.sregs2);
5336 break;
5337 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02005338 default:
5339 r = -EINVAL;
5340 }
5341out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005342 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01005343out_nofree:
5344 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005345 return r;
5346}
5347
Souptick Joarder1499fa82018-04-19 00:49:58 +05305348vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01005349{
5350 return VM_FAULT_SIGBUS;
5351}
5352
Carsten Otte1fe779f2007-10-29 16:08:35 +01005353static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
5354{
5355 int ret;
5356
5357 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08005358 return -EINVAL;
Jason Baronb36464772021-01-14 22:27:56 -05005359 ret = static_call(kvm_x86_set_tss_addr)(kvm, addr);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005360 return ret;
5361}
5362
Sheng Yangb927a3c2009-07-21 10:42:48 +08005363static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
5364 u64 ident_addr)
5365{
Jason Baronb36464772021-01-14 22:27:56 -05005366 return static_call(kvm_x86_set_identity_map_addr)(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08005367}
5368
Carsten Otte1fe779f2007-10-29 16:08:35 +01005369static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005370 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01005371{
5372 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
5373 return -EINVAL;
5374
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005375 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005376
5377 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005378 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005379
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005380 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005381 return 0;
5382}
5383
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005384static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01005385{
Dave Hansen39de71e2010-08-19 18:11:14 -07005386 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005387}
5388
Carsten Otte1fe779f2007-10-29 16:08:35 +01005389static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
5390{
David Hildenbrand90bca052017-04-07 10:50:23 +02005391 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005392 int r;
5393
5394 r = 0;
5395 switch (chip->chip_id) {
5396 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02005397 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01005398 sizeof(struct kvm_pic_state));
5399 break;
5400 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02005401 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01005402 sizeof(struct kvm_pic_state));
5403 break;
5404 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02005405 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005406 break;
5407 default:
5408 r = -EINVAL;
5409 break;
5410 }
5411 return r;
5412}
5413
5414static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
5415{
David Hildenbrand90bca052017-04-07 10:50:23 +02005416 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005417 int r;
5418
5419 r = 0;
5420 switch (chip->chip_id) {
5421 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02005422 spin_lock(&pic->lock);
5423 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01005424 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02005425 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005426 break;
5427 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02005428 spin_lock(&pic->lock);
5429 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01005430 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02005431 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005432 break;
5433 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02005434 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005435 break;
5436 default:
5437 r = -EINVAL;
5438 break;
5439 }
David Hildenbrand90bca052017-04-07 10:50:23 +02005440 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005441 return r;
5442}
5443
Sheng Yange0f63cb2008-03-04 00:50:59 +08005444static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
5445{
Radim Krčmář34f39412016-03-02 22:56:50 +01005446 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
5447
5448 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
5449
5450 mutex_lock(&kps->lock);
5451 memcpy(ps, &kps->channels, sizeof(*ps));
5452 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305453 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08005454}
5455
5456static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
5457{
Andrew Honig01856042015-11-18 14:50:23 -08005458 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01005459 struct kvm_pit *pit = kvm->arch.vpit;
5460
5461 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01005462 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08005463 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01005464 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
5465 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305466 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04005467}
5468
5469static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
5470{
Beth Kone9f42752009-07-07 11:50:38 -04005471 mutex_lock(&kvm->arch.vpit->pit_state.lock);
5472 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
5473 sizeof(ps->channels));
5474 ps->flags = kvm->arch.vpit->pit_state.flags;
5475 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005476 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305477 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04005478}
5479
5480static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
5481{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305482 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08005483 int i;
Beth Kone9f42752009-07-07 11:50:38 -04005484 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01005485 struct kvm_pit *pit = kvm->arch.vpit;
5486
5487 mutex_lock(&pit->pit_state.lock);
5488 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04005489 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
5490 if (!prev_legacy && cur_legacy)
5491 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01005492 memcpy(&pit->pit_state.channels, &ps->channels,
5493 sizeof(pit->pit_state.channels));
5494 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08005495 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01005496 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01005497 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01005498 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305499 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08005500}
5501
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005502static int kvm_vm_ioctl_reinject(struct kvm *kvm,
5503 struct kvm_reinject_control *control)
5504{
Radim Krčmář71474e22016-03-02 22:56:45 +01005505 struct kvm_pit *pit = kvm->arch.vpit;
5506
Radim Krčmář71474e22016-03-02 22:56:45 +01005507 /* pit->pit_state.lock was overloaded to prevent userspace from getting
5508 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
5509 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
5510 */
5511 mutex_lock(&pit->pit_state.lock);
5512 kvm_pit_set_reinject(pit, control->pit_reinject);
5513 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01005514
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005515 return 0;
5516}
5517
Sean Christopherson0dff0842020-02-18 13:07:29 -08005518void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08005519{
Sean Christophersona018eba2021-02-12 16:50:10 -08005520
Kai Huang88178fd2015-01-28 10:54:27 +08005521 /*
Sean Christophersona018eba2021-02-12 16:50:10 -08005522 * Flush all CPUs' dirty log buffers to the dirty_bitmap. Called
5523 * before reporting dirty_bitmap to userspace. KVM flushes the buffers
5524 * on all VM-Exits, thus we only need to kick running vCPUs to force a
5525 * VM-Exit.
Kai Huang88178fd2015-01-28 10:54:27 +08005526 */
Sean Christophersona018eba2021-02-12 16:50:10 -08005527 struct kvm_vcpu *vcpu;
5528 int i;
5529
5530 kvm_for_each_vcpu(i, vcpu, kvm)
5531 kvm_vcpu_kick(vcpu);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08005532}
5533
Yang Zhangaa2fbe62013-04-11 19:21:40 +08005534int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
5535 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04005536{
5537 if (!irqchip_in_kernel(kvm))
5538 return -ENXIO;
5539
5540 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08005541 irq_event->irq, irq_event->level,
5542 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04005543 return 0;
5544}
5545
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01005546int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
5547 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03005548{
5549 int r;
5550
5551 if (cap->flags)
5552 return -EINVAL;
5553
5554 switch (cap->cap) {
5555 case KVM_CAP_DISABLE_QUIRKS:
5556 kvm->arch.disabled_quirks = cap->args[0];
5557 r = 0;
5558 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07005559 case KVM_CAP_SPLIT_IRQCHIP: {
5560 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07005561 r = -EINVAL;
5562 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
5563 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07005564 r = -EEXIST;
5565 if (irqchip_in_kernel(kvm))
5566 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02005567 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07005568 goto split_irqchip_unlock;
5569 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02005570 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07005571 goto split_irqchip_unlock;
5572 /* Pairs with irqchip_in_kernel. */
5573 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005574 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07005575 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07005576 r = 0;
5577split_irqchip_unlock:
5578 mutex_unlock(&kvm->lock);
5579 break;
5580 }
Radim Krčmář371313132016-07-12 22:09:27 +02005581 case KVM_CAP_X2APIC_API:
5582 r = -EINVAL;
5583 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
5584 break;
5585
5586 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
5587 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02005588 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
5589 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02005590
5591 r = 0;
5592 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005593 case KVM_CAP_X86_DISABLE_EXITS:
5594 r = -EINVAL;
5595 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
5596 break;
5597
5598 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
5599 kvm_can_mwait_in_guest())
5600 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03005601 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07005602 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005603 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
5604 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08005605 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
5606 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005607 r = 0;
5608 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07005609 case KVM_CAP_MSR_PLATFORM_INFO:
5610 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
5611 r = 0;
5612 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07005613 case KVM_CAP_EXCEPTION_PAYLOAD:
5614 kvm->arch.exception_payload_enabled = cap->args[0];
5615 r = 0;
5616 break;
Alexander Graf1ae09952020-09-25 16:34:16 +02005617 case KVM_CAP_X86_USER_SPACE_MSR:
5618 kvm->arch.user_space_msr_mask = cap->args[0];
5619 r = 0;
5620 break;
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005621 case KVM_CAP_X86_BUS_LOCK_EXIT:
5622 r = -EINVAL;
5623 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE)
5624 break;
5625
5626 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) &&
5627 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT))
5628 break;
5629
5630 if (kvm_has_bus_lock_exit &&
5631 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)
5632 kvm->arch.bus_lock_detection_enabled = true;
5633 r = 0;
5634 break;
Sean Christophersonfe7e9482021-04-12 16:21:43 +12005635#ifdef CONFIG_X86_SGX_KVM
5636 case KVM_CAP_SGX_ATTRIBUTE: {
5637 unsigned long allowed_attributes = 0;
5638
5639 r = sgx_set_attribute(&allowed_attributes, cap->args[0]);
5640 if (r)
5641 break;
5642
5643 /* KVM only supports the PROVISIONKEY privileged attribute. */
5644 if ((allowed_attributes & SGX_ATTR_PROVISIONKEY) &&
5645 !(allowed_attributes & ~SGX_ATTR_PROVISIONKEY))
5646 kvm->arch.sgx_provisioning_allowed = true;
5647 else
5648 r = -EINVAL;
5649 break;
5650 }
5651#endif
Nathan Tempelman54526d12021-04-08 22:32:14 +00005652 case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
5653 r = -EINVAL;
5654 if (kvm_x86_ops.vm_copy_enc_context_from)
5655 r = kvm_x86_ops.vm_copy_enc_context_from(kvm, cap->args[0]);
5656 return r;
Ashish Kalra0dbb1122021-06-08 18:05:43 +00005657 case KVM_CAP_EXIT_HYPERCALL:
5658 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) {
5659 r = -EINVAL;
5660 break;
5661 }
5662 kvm->arch.hypercall_exit_enabled = cap->args[0];
5663 r = 0;
5664 break;
Aaron Lewis19238e72021-05-10 07:48:33 -07005665 case KVM_CAP_EXIT_ON_EMULATION_FAILURE:
5666 r = -EINVAL;
5667 if (cap->args[0] & ~1)
5668 break;
5669 kvm->arch.exit_on_emulation_error = cap->args[0];
5670 r = 0;
5671 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03005672 default:
5673 r = -EINVAL;
5674 break;
5675 }
5676 return r;
5677}
5678
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005679static struct kvm_x86_msr_filter *kvm_alloc_msr_filter(bool default_allow)
Alexander Graf1a1552542020-09-25 16:34:21 +02005680{
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005681 struct kvm_x86_msr_filter *msr_filter;
Alexander Graf1a1552542020-09-25 16:34:21 +02005682
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005683 msr_filter = kzalloc(sizeof(*msr_filter), GFP_KERNEL_ACCOUNT);
5684 if (!msr_filter)
5685 return NULL;
Alexander Graf1a1552542020-09-25 16:34:21 +02005686
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005687 msr_filter->default_allow = default_allow;
5688 return msr_filter;
Alexander Graf1a1552542020-09-25 16:34:21 +02005689}
5690
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005691static void kvm_free_msr_filter(struct kvm_x86_msr_filter *msr_filter)
Alexander Graf1a1552542020-09-25 16:34:21 +02005692{
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005693 u32 i;
5694
5695 if (!msr_filter)
5696 return;
5697
5698 for (i = 0; i < msr_filter->count; i++)
5699 kfree(msr_filter->ranges[i].bitmap);
5700
5701 kfree(msr_filter);
5702}
5703
5704static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,
5705 struct kvm_msr_filter_range *user_range)
5706{
Alexander Graf1a1552542020-09-25 16:34:21 +02005707 unsigned long *bitmap = NULL;
5708 size_t bitmap_size;
Alexander Graf1a1552542020-09-25 16:34:21 +02005709
5710 if (!user_range->nmsrs)
5711 return 0;
5712
Siddharth Chandrasekaranaca35282021-05-03 14:21:11 +02005713 if (user_range->flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE))
5714 return -EINVAL;
5715
5716 if (!user_range->flags)
5717 return -EINVAL;
5718
Alexander Graf1a1552542020-09-25 16:34:21 +02005719 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long);
5720 if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE)
5721 return -EINVAL;
5722
5723 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size);
5724 if (IS_ERR(bitmap))
5725 return PTR_ERR(bitmap);
5726
Siddharth Chandrasekaranaca35282021-05-03 14:21:11 +02005727 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) {
Alexander Graf1a1552542020-09-25 16:34:21 +02005728 .flags = user_range->flags,
5729 .base = user_range->base,
5730 .nmsrs = user_range->nmsrs,
5731 .bitmap = bitmap,
5732 };
5733
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005734 msr_filter->count++;
Alexander Graf1a1552542020-09-25 16:34:21 +02005735 return 0;
Alexander Graf1a1552542020-09-25 16:34:21 +02005736}
5737
5738static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp)
5739{
5740 struct kvm_msr_filter __user *user_msr_filter = argp;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005741 struct kvm_x86_msr_filter *new_filter, *old_filter;
Alexander Graf1a1552542020-09-25 16:34:21 +02005742 struct kvm_msr_filter filter;
5743 bool default_allow;
Paolo Bonzini043248b2020-10-20 10:57:01 -04005744 bool empty = true;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005745 int r = 0;
Alexander Graf1a1552542020-09-25 16:34:21 +02005746 u32 i;
5747
5748 if (copy_from_user(&filter, user_msr_filter, sizeof(filter)))
5749 return -EFAULT;
5750
Paolo Bonzini043248b2020-10-20 10:57:01 -04005751 for (i = 0; i < ARRAY_SIZE(filter.ranges); i++)
5752 empty &= !filter.ranges[i].nmsrs;
Alexander Graf1a1552542020-09-25 16:34:21 +02005753
5754 default_allow = !(filter.flags & KVM_MSR_FILTER_DEFAULT_DENY);
Paolo Bonzini043248b2020-10-20 10:57:01 -04005755 if (empty && !default_allow)
5756 return -EINVAL;
5757
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005758 new_filter = kvm_alloc_msr_filter(default_allow);
5759 if (!new_filter)
5760 return -ENOMEM;
Paolo Bonzini043248b2020-10-20 10:57:01 -04005761
Alexander Graf1a1552542020-09-25 16:34:21 +02005762 for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) {
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005763 r = kvm_add_msr_filter(new_filter, &filter.ranges[i]);
5764 if (r) {
5765 kvm_free_msr_filter(new_filter);
5766 return r;
5767 }
Alexander Graf1a1552542020-09-25 16:34:21 +02005768 }
5769
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005770 mutex_lock(&kvm->lock);
5771
5772 /* The per-VM filter is protected by kvm->lock... */
5773 old_filter = srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1);
5774
5775 rcu_assign_pointer(kvm->arch.msr_filter, new_filter);
5776 synchronize_srcu(&kvm->srcu);
5777
5778 kvm_free_msr_filter(old_filter);
5779
Alexander Graf1a1552542020-09-25 16:34:21 +02005780 kvm_make_all_cpus_request(kvm, KVM_REQ_MSR_FILTER_CHANGED);
5781 mutex_unlock(&kvm->lock);
5782
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005783 return 0;
Alexander Graf1a1552542020-09-25 16:34:21 +02005784}
5785
Sergey Senozhatsky7d628742021-06-06 11:10:45 +09005786#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER
5787static int kvm_arch_suspend_notifier(struct kvm *kvm)
5788{
5789 struct kvm_vcpu *vcpu;
5790 int i, ret = 0;
5791
5792 mutex_lock(&kvm->lock);
5793 kvm_for_each_vcpu(i, vcpu, kvm) {
5794 if (!vcpu->arch.pv_time_enabled)
5795 continue;
5796
5797 ret = kvm_set_guest_paused(vcpu);
5798 if (ret) {
5799 kvm_err("Failed to pause guest VCPU%d: %d\n",
5800 vcpu->vcpu_id, ret);
5801 break;
5802 }
5803 }
5804 mutex_unlock(&kvm->lock);
5805
5806 return ret ? NOTIFY_BAD : NOTIFY_DONE;
5807}
5808
5809int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state)
5810{
5811 switch (state) {
5812 case PM_HIBERNATION_PREPARE:
5813 case PM_SUSPEND_PREPARE:
5814 return kvm_arch_suspend_notifier(kvm);
5815 }
5816
5817 return NOTIFY_DONE;
5818}
5819#endif /* CONFIG_HAVE_KVM_PM_NOTIFIER */
5820
Carsten Otte1fe779f2007-10-29 16:08:35 +01005821long kvm_arch_vm_ioctl(struct file *filp,
5822 unsigned int ioctl, unsigned long arg)
5823{
5824 struct kvm *kvm = filp->private_data;
5825 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03005826 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07005827 /*
5828 * This union makes it completely explicit to gcc-3.x
5829 * that these two variables' stack usage should be
5830 * combined, not added together.
5831 */
5832 union {
5833 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04005834 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005835 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07005836 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005837
5838 switch (ioctl) {
5839 case KVM_SET_TSS_ADDR:
5840 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005841 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005842 case KVM_SET_IDENTITY_MAP_ADDR: {
5843 u64 ident_addr;
5844
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005845 mutex_lock(&kvm->lock);
5846 r = -EINVAL;
5847 if (kvm->created_vcpus)
5848 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005849 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005850 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005851 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005852 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005853set_identity_unlock:
5854 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08005855 break;
5856 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01005857 case KVM_SET_NR_MMU_PAGES:
5858 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005859 break;
5860 case KVM_GET_NR_MMU_PAGES:
5861 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
5862 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005863 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005864 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01005865
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005866 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01005867 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005868 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005869
Avi Kivity3e515702012-03-05 14:23:29 +02005870 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02005871 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02005872 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005873
5874 r = kvm_pic_init(kvm);
5875 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005876 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005877
5878 r = kvm_ioapic_init(kvm);
5879 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01005880 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005881 goto create_irqchip_unlock;
5882 }
5883
Avi Kivity399ec802008-11-19 13:58:46 +02005884 r = kvm_setup_default_irq_routing(kvm);
5885 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08005886 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005887 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005888 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02005889 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01005890 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005891 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005892 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005893 create_irqchip_unlock:
5894 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005895 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005896 }
Sheng Yang78376992008-01-28 05:10:22 +08005897 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005898 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
5899 goto create_pit;
5900 case KVM_CREATE_PIT2:
5901 r = -EFAULT;
5902 if (copy_from_user(&u.pit_config, argp,
5903 sizeof(struct kvm_pit_config)))
5904 goto out;
5905 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005906 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02005907 r = -EEXIST;
5908 if (kvm->arch.vpit)
5909 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08005910 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005911 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08005912 if (kvm->arch.vpit)
5913 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02005914 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005915 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08005916 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005917 case KVM_GET_IRQCHIP: {
5918 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005919 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005920
Sasha Levinff5c2c02011-12-04 19:36:29 +02005921 chip = memdup_user(argp, sizeof(*chip));
5922 if (IS_ERR(chip)) {
5923 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005924 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005925 }
5926
Carsten Otte1fe779f2007-10-29 16:08:35 +01005927 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005928 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005929 goto get_irqchip_out;
5930 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5931 if (r)
5932 goto get_irqchip_out;
5933 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005934 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005935 goto get_irqchip_out;
5936 r = 0;
5937 get_irqchip_out:
5938 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005939 break;
5940 }
5941 case KVM_SET_IRQCHIP: {
5942 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005943 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005944
Sasha Levinff5c2c02011-12-04 19:36:29 +02005945 chip = memdup_user(argp, sizeof(*chip));
5946 if (IS_ERR(chip)) {
5947 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005948 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005949 }
5950
Carsten Otte1fe779f2007-10-29 16:08:35 +01005951 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005952 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005953 goto set_irqchip_out;
5954 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005955 set_irqchip_out:
5956 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005957 break;
5958 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005959 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005960 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005961 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005962 goto out;
5963 r = -ENXIO;
5964 if (!kvm->arch.vpit)
5965 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005966 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005967 if (r)
5968 goto out;
5969 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005970 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005971 goto out;
5972 r = 0;
5973 break;
5974 }
5975 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005976 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005977 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005978 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005979 mutex_lock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005980 r = -ENXIO;
5981 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005982 goto set_pit_out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005983 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005984set_pit_out:
5985 mutex_unlock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005986 break;
5987 }
Beth Kone9f42752009-07-07 11:50:38 -04005988 case KVM_GET_PIT2: {
5989 r = -ENXIO;
5990 if (!kvm->arch.vpit)
5991 goto out;
5992 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5993 if (r)
5994 goto out;
5995 r = -EFAULT;
5996 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5997 goto out;
5998 r = 0;
5999 break;
6000 }
6001 case KVM_SET_PIT2: {
6002 r = -EFAULT;
6003 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
6004 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07006005 mutex_lock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04006006 r = -ENXIO;
6007 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07006008 goto set_pit2_out;
Beth Kone9f42752009-07-07 11:50:38 -04006009 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07006010set_pit2_out:
6011 mutex_unlock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04006012 break;
6013 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02006014 case KVM_REINJECT_CONTROL: {
6015 struct kvm_reinject_control control;
6016 r = -EFAULT;
6017 if (copy_from_user(&control, argp, sizeof(control)))
6018 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08006019 r = -ENXIO;
6020 if (!kvm->arch.vpit)
6021 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02006022 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02006023 break;
6024 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02006025 case KVM_SET_BOOT_CPU_ID:
6026 r = 0;
6027 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02006028 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02006029 r = -EBUSY;
6030 else
6031 kvm->arch.bsp_vcpu_id = arg;
6032 mutex_unlock(&kvm->lock);
6033 break;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05006034#ifdef CONFIG_KVM_XEN
Ed Swierkffde22a2009-10-15 15:21:43 -07006035 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02006036 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07006037 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02006038 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07006039 goto out;
David Woodhouse78e98782021-02-02 13:19:35 +00006040 r = kvm_xen_hvm_config(kvm, &xhc);
Ed Swierkffde22a2009-10-15 15:21:43 -07006041 break;
6042 }
Joao Martinsa76b9642020-12-03 15:52:25 +00006043 case KVM_XEN_HVM_GET_ATTR: {
6044 struct kvm_xen_hvm_attr xha;
6045
6046 r = -EFAULT;
6047 if (copy_from_user(&xha, argp, sizeof(xha)))
Hannes Edercded19f2009-02-21 02:19:13 +01006048 goto out;
Joao Martinsa76b9642020-12-03 15:52:25 +00006049 r = kvm_xen_hvm_get_attr(kvm, &xha);
6050 if (!r && copy_to_user(argp, &xha, sizeof(xha)))
6051 r = -EFAULT;
6052 break;
6053 }
6054 case KVM_XEN_HVM_SET_ATTR: {
6055 struct kvm_xen_hvm_attr xha;
6056
6057 r = -EFAULT;
6058 if (copy_from_user(&xha, argp, sizeof(xha)))
6059 goto out;
6060 r = kvm_xen_hvm_set_attr(kvm, &xha);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006061 break;
6062 }
Paolo Bonzinib59b1532021-02-26 04:54:45 -05006063#endif
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006064 case KVM_SET_CLOCK: {
Vitaly Kuznetsov77fcbe82021-03-31 14:41:29 +02006065 struct kvm_arch *ka = &kvm->arch;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006066 struct kvm_clock_data user_ns;
6067 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006068
6069 r = -EFAULT;
6070 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
6071 goto out;
6072
6073 r = -EINVAL;
6074 if (user_ns.flags)
6075 goto out;
6076
6077 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02006078 /*
6079 * TODO: userspace has to take care of races with VCPU_RUN, so
6080 * kvm_gen_update_masterclock() can be cut down to locked
6081 * pvclock_update_vm_gtod_copy().
6082 */
6083 kvm_gen_update_masterclock(kvm);
Vitaly Kuznetsov77fcbe82021-03-31 14:41:29 +02006084
6085 /*
6086 * This pairs with kvm_guest_time_update(): when masterclock is
6087 * in use, we use master_kernel_ns + kvmclock_offset to set
6088 * unsigned 'system_time' so if we use get_kvmclock_ns() (which
6089 * is slightly ahead) here we risk going negative on unsigned
6090 * 'system_time' when 'user_ns.clock' is very small.
6091 */
6092 spin_lock_irq(&ka->pvclock_gtod_sync_lock);
6093 if (kvm->arch.use_master_clock)
6094 now_ns = ka->master_kernel_ns;
6095 else
6096 now_ns = get_kvmclock_base_ns();
6097 ka->kvmclock_offset = user_ns.clock - now_ns;
6098 spin_unlock_irq(&ka->pvclock_gtod_sync_lock);
6099
Radim Krčmář0bc48be2017-05-16 22:50:00 +02006100 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006101 break;
6102 }
6103 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006104 struct kvm_clock_data user_ns;
6105 u64 now_ns;
6106
Marcelo Tosattie891a322017-04-17 12:51:37 -03006107 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02006108 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01006109 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04006110 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006111
6112 r = -EFAULT;
6113 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
6114 goto out;
6115 r = 0;
6116 break;
6117 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06006118 case KVM_MEMORY_ENCRYPT_OP: {
6119 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006120 if (kvm_x86_ops.mem_enc_op)
Jason Baronb36464772021-01-14 22:27:56 -05006121 r = static_call(kvm_x86_mem_enc_op)(kvm, argp);
Brijesh Singh5acc5c02017-12-04 10:57:26 -06006122 break;
6123 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06006124 case KVM_MEMORY_ENCRYPT_REG_REGION: {
6125 struct kvm_enc_region region;
6126
6127 r = -EFAULT;
6128 if (copy_from_user(&region, argp, sizeof(region)))
6129 goto out;
6130
6131 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006132 if (kvm_x86_ops.mem_enc_reg_region)
Jason Baronb36464772021-01-14 22:27:56 -05006133 r = static_call(kvm_x86_mem_enc_reg_region)(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06006134 break;
6135 }
6136 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
6137 struct kvm_enc_region region;
6138
6139 r = -EFAULT;
6140 if (copy_from_user(&region, argp, sizeof(region)))
6141 goto out;
6142
6143 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006144 if (kvm_x86_ops.mem_enc_unreg_region)
Jason Baronb36464772021-01-14 22:27:56 -05006145 r = static_call(kvm_x86_mem_enc_unreg_region)(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06006146 break;
6147 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03006148 case KVM_HYPERV_EVENTFD: {
6149 struct kvm_hyperv_eventfd hvevfd;
6150
6151 r = -EFAULT;
6152 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
6153 goto out;
6154 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
6155 break;
6156 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07006157 case KVM_SET_PMU_EVENT_FILTER:
6158 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
6159 break;
Alexander Graf1a1552542020-09-25 16:34:21 +02006160 case KVM_X86_SET_MSR_FILTER:
6161 r = kvm_vm_ioctl_set_msr_filter(kvm, argp);
6162 break;
Carsten Otte043405e2007-10-10 17:16:19 +02006163 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02006164 r = -ENOTTY;
Carsten Otte043405e2007-10-10 17:16:19 +02006165 }
6166out:
6167 return r;
6168}
6169
6170static void kvm_init_msr_list(void)
6171{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006172 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02006173 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006174 unsigned i;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006175
Jim Mattsone2ada662019-08-21 11:20:04 -07006176 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006177 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006178
6179 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07006180
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08006181 num_msrs_to_save = 0;
6182 num_emulated_msrs = 0;
6183 num_msr_based_features = 0;
6184
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006185 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
6186 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006187 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006188
6189 /*
6190 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01006191 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006192 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006193 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006194 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03006195 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006196 continue;
6197 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01006198 case MSR_TSC_AUX:
Sean Christopherson36fa06f2021-05-04 10:17:26 -07006199 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP) &&
6200 !kvm_cpu_cap_has(X86_FEATURE_RDPID))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01006201 continue;
6202 break;
Maxim Levitskyf4cfcd22020-05-23 19:14:55 +03006203 case MSR_IA32_UMWAIT_CONTROL:
6204 if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG))
6205 continue;
6206 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08006207 case MSR_IA32_RTIT_CTL:
6208 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006209 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08006210 continue;
6211 break;
6212 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006213 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08006214 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
6215 continue;
6216 break;
6217 case MSR_IA32_RTIT_OUTPUT_BASE:
6218 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006219 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08006220 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
6221 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
6222 continue;
6223 break;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08006224 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006225 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006226 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08006227 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
6228 continue;
6229 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02006230 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006231 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006232 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
6233 continue;
6234 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02006235 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006236 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006237 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
6238 continue;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08006239 break;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006240 default:
6241 break;
6242 }
6243
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006244 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Ottebbd9b642007-10-30 18:44:21 +01006245 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02006246
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006247 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
Jason Baronb36464772021-01-14 22:27:56 -05006248 if (!static_call(kvm_x86_has_emulated_msr)(NULL, emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02006249 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02006250
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006251 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02006252 }
Tom Lendacky801e4592018-02-21 13:39:51 -06006253
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006254 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06006255 struct kvm_msr_entry msr;
6256
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006257 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08006258 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06006259 continue;
6260
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006261 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06006262 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01006263}
6264
6265static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
6266 const void *v)
6267{
Avi Kivity70252a12010-01-19 12:51:22 +02006268 int handled = 0;
6269 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006270
Avi Kivity70252a12010-01-19 12:51:22 +02006271 do {
6272 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01006273 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006274 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
6275 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02006276 break;
6277 handled += n;
6278 addr += n;
6279 len -= n;
6280 v += n;
6281 } while (len);
6282
6283 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006284}
6285
6286static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
6287{
Avi Kivity70252a12010-01-19 12:51:22 +02006288 int handled = 0;
6289 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006290
Avi Kivity70252a12010-01-19 12:51:22 +02006291 do {
6292 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01006293 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006294 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
6295 addr, n, v))
6296 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02006297 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006298 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02006299 handled += n;
6300 addr += n;
6301 len -= n;
6302 v += n;
6303 } while (len);
6304
6305 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006306}
6307
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006308static void kvm_set_segment(struct kvm_vcpu *vcpu,
6309 struct kvm_segment *var, int seg)
6310{
Jason Baronb36464772021-01-14 22:27:56 -05006311 static_call(kvm_x86_set_segment)(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006312}
6313
6314void kvm_get_segment(struct kvm_vcpu *vcpu,
6315 struct kvm_segment *var, int seg)
6316{
Jason Baronb36464772021-01-14 22:27:56 -05006317 static_call(kvm_x86_get_segment)(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006318}
6319
Paolo Bonzini54987b72014-09-02 13:23:06 +02006320gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
6321 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02006322{
6323 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02006324
6325 BUG_ON(!mmu_is_nested(vcpu));
6326
6327 /* NPT walks are always user-walks */
6328 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006329 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02006330
6331 return t_gpa;
6332}
6333
Avi Kivityab9ae312010-11-22 17:53:26 +02006334gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
6335 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006336{
Jason Baronb36464772021-01-14 22:27:56 -05006337 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02006338 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006339}
Sean Christopherson54f958c2021-04-12 16:21:33 +12006340EXPORT_SYMBOL_GPL(kvm_mmu_gva_to_gpa_read);
Gleb Natapov1871c602010-02-10 14:21:32 +02006341
Avi Kivityab9ae312010-11-22 17:53:26 +02006342 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
6343 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006344{
Jason Baronb36464772021-01-14 22:27:56 -05006345 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02006346 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02006347 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006348}
6349
Avi Kivityab9ae312010-11-22 17:53:26 +02006350gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
6351 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006352{
Jason Baronb36464772021-01-14 22:27:56 -05006353 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02006354 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02006355 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006356}
Sean Christopherson54f958c2021-04-12 16:21:33 +12006357EXPORT_SYMBOL_GPL(kvm_mmu_gva_to_gpa_write);
Gleb Natapov1871c602010-02-10 14:21:32 +02006358
6359/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02006360gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
6361 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006362{
Avi Kivityab9ae312010-11-22 17:53:26 +02006363 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006364}
6365
6366static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
6367 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006368 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006369{
6370 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006371 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006372
6373 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02006374 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02006375 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006376 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02006377 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006378 int ret;
6379
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006380 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02006381 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006382 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
6383 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006384 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006385 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006386 goto out;
6387 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01006388
Izik Eidus77c20022008-12-29 01:42:19 +02006389 bytes -= toread;
6390 data += toread;
6391 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006392 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006393out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006394 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006395}
Izik Eidus77c20022008-12-29 01:42:19 +02006396
Gleb Natapov1871c602010-02-10 14:21:32 +02006397/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03006398static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
6399 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006400 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006401{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006402 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Jason Baronb36464772021-01-14 22:27:56 -05006403 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02006404 unsigned offset;
6405 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006406
Paolo Bonzini44583cb2014-05-13 14:02:13 +02006407 /* Inline kvm_read_guest_virt_helper for speed. */
6408 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
6409 exception);
6410 if (unlikely(gpa == UNMAPPED_GVA))
6411 return X86EMUL_PROPAGATE_FAULT;
6412
6413 offset = addr & (PAGE_SIZE-1);
6414 if (WARN_ON(offset + bytes > PAGE_SIZE))
6415 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006416 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
6417 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02006418 if (unlikely(ret < 0))
6419 return X86EMUL_IO_NEEDED;
6420
6421 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02006422}
6423
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006424int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03006425 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006426 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006427{
Jason Baronb36464772021-01-14 22:27:56 -05006428 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006429
Paolo Bonzini353c0952019-01-29 18:41:16 +01006430 /*
6431 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
6432 * is returned, but our callers are not ready for that and they blindly
6433 * call kvm_inject_page_fault. Ensure that they at least do not leak
6434 * uninitialized kernel stack memory into cr2 and error code.
6435 */
6436 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02006437 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006438 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006439}
Nadav Har'El064aea72011-05-25 23:04:56 +03006440EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02006441
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006442static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
6443 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006444 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02006445{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006446 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006447 u32 access = 0;
6448
Jason Baronb36464772021-01-14 22:27:56 -05006449 if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006450 access |= PFERR_USER_MASK;
6451
6452 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006453}
6454
Radim Krčmář7a036a62015-10-30 16:36:24 +01006455static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
6456 unsigned long addr, void *val, unsigned int bytes)
6457{
6458 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6459 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
6460
6461 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
6462}
6463
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006464static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
6465 struct kvm_vcpu *vcpu, u32 access,
6466 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02006467{
6468 void *data = val;
6469 int r = X86EMUL_CONTINUE;
6470
6471 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02006472 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006473 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02006474 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02006475 unsigned offset = addr & (PAGE_SIZE-1);
6476 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
6477 int ret;
6478
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006479 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02006480 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006481 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02006482 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006483 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02006484 goto out;
6485 }
6486
6487 bytes -= towrite;
6488 data += towrite;
6489 addr += towrite;
6490 }
6491out:
6492 return r;
6493}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006494
6495static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006496 unsigned int bytes, struct x86_exception *exception,
6497 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006498{
6499 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006500 u32 access = PFERR_WRITE_MASK;
6501
Jason Baronb36464772021-01-14 22:27:56 -05006502 if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006503 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006504
6505 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006506 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006507}
6508
6509int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
6510 unsigned int bytes, struct x86_exception *exception)
6511{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006512 /* kvm_write_guest_virt_system can pull in tons of pages. */
6513 vcpu->arch.l1tf_flush_l1d = true;
6514
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006515 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
6516 PFERR_WRITE_MASK, exception);
6517}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006518EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02006519
Wanpeng Li082d06e2018-04-03 16:28:48 -07006520int handle_ud(struct kvm_vcpu *vcpu)
6521{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09006522 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006523 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006524 char sig[5]; /* ud2; .ascii "kvm" */
6525 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07006526
Jason Baronb36464772021-01-14 22:27:56 -05006527 if (unlikely(!static_call(kvm_x86_can_emulate_instruction)(vcpu, NULL, 0)))
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07006528 return 1;
6529
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006530 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006531 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
6532 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09006533 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006534 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07006535 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006536 }
6537
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006538 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07006539}
6540EXPORT_SYMBOL_GPL(handle_ud);
6541
Tom Lendacky0f89b202016-12-14 14:59:23 -05006542static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
6543 gpa_t gpa, bool write)
6544{
6545 /* For APIC access vmexit */
6546 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
6547 return 1;
6548
6549 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
6550 trace_vcpu_match_mmio(gva, gpa, write, true);
6551 return 1;
6552 }
6553
6554 return 0;
6555}
6556
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006557static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
6558 gpa_t *gpa, struct x86_exception *exception,
6559 bool write)
6560{
Jason Baronb36464772021-01-14 22:27:56 -05006561 u32 access = ((static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0)
Avi Kivity97d64b72012-09-12 14:52:00 +03006562 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006563
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08006564 /*
6565 * currently PKRU is only applied to ept enabled guest so
6566 * there is no pkey in EPT page table for L1 guest or EPT
6567 * shadow page table for L2 guest.
6568 */
Sean Christopherson908b7d42021-07-13 09:33:04 -07006569 if (vcpu_match_mmio_gva(vcpu, gva) && (!is_paging(vcpu) ||
6570 !permission_fault(vcpu, vcpu->arch.walk_mmu,
6571 vcpu->arch.mmio_access, 0, access))) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08006572 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
6573 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08006574 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08006575 return 1;
6576 }
6577
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006578 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
6579
6580 if (*gpa == UNMAPPED_GVA)
6581 return -1;
6582
Tom Lendacky0f89b202016-12-14 14:59:23 -05006583 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006584}
6585
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006586int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006587 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02006588{
6589 int ret;
6590
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006591 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02006592 if (ret < 0)
6593 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08006594 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02006595 return 1;
6596}
6597
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006598struct read_write_emulator_ops {
6599 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
6600 int bytes);
6601 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
6602 void *val, int bytes);
6603 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
6604 int bytes, void *val);
6605 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
6606 void *val, int bytes);
6607 bool write;
6608};
6609
6610static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
6611{
6612 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006613 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006614 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006615 vcpu->mmio_read_completed = 0;
6616 return 1;
6617 }
6618
6619 return 0;
6620}
6621
6622static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
6623 void *val, int bytes)
6624{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006625 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006626}
6627
6628static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
6629 void *val, int bytes)
6630{
6631 return emulator_write_phys(vcpu, gpa, val, bytes);
6632}
6633
6634static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
6635{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006636 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006637 return vcpu_mmio_write(vcpu, gpa, bytes, val);
6638}
6639
6640static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
6641 void *val, int bytes)
6642{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006643 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006644 return X86EMUL_IO_NEEDED;
6645}
6646
6647static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
6648 void *val, int bytes)
6649{
Avi Kivityf78146b2012-04-18 19:22:47 +03006650 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
6651
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006652 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006653 return X86EMUL_CONTINUE;
6654}
6655
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006656static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006657 .read_write_prepare = read_prepare,
6658 .read_write_emulate = read_emulate,
6659 .read_write_mmio = vcpu_mmio_read,
6660 .read_write_exit_mmio = read_exit_mmio,
6661};
6662
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006663static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006664 .read_write_emulate = write_emulate,
6665 .read_write_mmio = write_mmio,
6666 .read_write_exit_mmio = write_exit_mmio,
6667 .write = true,
6668};
6669
Xiao Guangrong22388a32011-07-13 14:32:31 +08006670static int emulator_read_write_onepage(unsigned long addr, void *val,
6671 unsigned int bytes,
6672 struct x86_exception *exception,
6673 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006674 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006675{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006676 gpa_t gpa;
6677 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08006678 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03006679 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006680 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05006681
6682 /*
6683 * If the exit was due to a NPF we may already have a GPA.
6684 * If the GPA is present, use it to avoid the GVA to GPA table walk.
6685 * Note, this cannot be used on string operations since string
6686 * operation using rep will only have the initial GPA from the NPF
6687 * occurred.
6688 */
Sean Christopherson744e6992020-02-18 15:03:09 -08006689 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
6690 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
6691 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02006692 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
6693 } else {
6694 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
6695 if (ret < 0)
6696 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05006697 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08006698
Brijesh Singh618232e2017-08-17 18:36:57 +02006699 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01006700 return X86EMUL_CONTINUE;
6701
Carsten Ottebbd9b642007-10-30 18:44:21 +01006702 /*
6703 * Is this MMIO handled locally?
6704 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08006705 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02006706 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006707 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006708
Avi Kivity70252a12010-01-19 12:51:22 +02006709 gpa += handled;
6710 bytes -= handled;
6711 val += handled;
6712
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006713 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
6714 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
6715 frag->gpa = gpa;
6716 frag->data = val;
6717 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03006718 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006719}
6720
Xiubo Li52eb5a62015-03-13 17:39:45 +08006721static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
6722 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08006723 void *val, unsigned int bytes,
6724 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006725 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006726{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006727 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03006728 gpa_t gpa;
6729 int rc;
6730
6731 if (ops->read_write_prepare &&
6732 ops->read_write_prepare(vcpu, val, bytes))
6733 return X86EMUL_CONTINUE;
6734
6735 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006736
Carsten Ottebbd9b642007-10-30 18:44:21 +01006737 /* Crossing a page boundary? */
6738 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03006739 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006740
6741 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08006742 rc = emulator_read_write_onepage(addr, val, now, exception,
6743 vcpu, ops);
6744
Carsten Ottebbd9b642007-10-30 18:44:21 +01006745 if (rc != X86EMUL_CONTINUE)
6746 return rc;
6747 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02006748 if (ctxt->mode != X86EMUL_MODE_PROT64)
6749 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006750 val += now;
6751 bytes -= now;
6752 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08006753
Avi Kivityf78146b2012-04-18 19:22:47 +03006754 rc = emulator_read_write_onepage(addr, val, bytes, exception,
6755 vcpu, ops);
6756 if (rc != X86EMUL_CONTINUE)
6757 return rc;
6758
6759 if (!vcpu->mmio_nr_fragments)
6760 return rc;
6761
6762 gpa = vcpu->mmio_fragments[0].gpa;
6763
6764 vcpu->mmio_needed = 1;
6765 vcpu->mmio_cur_fragment = 0;
6766
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006767 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03006768 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
6769 vcpu->run->exit_reason = KVM_EXIT_MMIO;
6770 vcpu->run->mmio.phys_addr = gpa;
6771
6772 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08006773}
6774
6775static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
6776 unsigned long addr,
6777 void *val,
6778 unsigned int bytes,
6779 struct x86_exception *exception)
6780{
6781 return emulator_read_write(ctxt, addr, val, bytes,
6782 exception, &read_emultor);
6783}
6784
Xiubo Li52eb5a62015-03-13 17:39:45 +08006785static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08006786 unsigned long addr,
6787 const void *val,
6788 unsigned int bytes,
6789 struct x86_exception *exception)
6790{
6791 return emulator_read_write(ctxt, addr, (void *)val, bytes,
6792 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006793}
Carsten Ottebbd9b642007-10-30 18:44:21 +01006794
Avi Kivitydaea3e72010-03-15 13:59:54 +02006795#define CMPXCHG_TYPE(t, ptr, old, new) \
6796 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
6797
6798#ifdef CONFIG_X86_64
6799# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
6800#else
6801# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01006802 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02006803#endif
6804
Avi Kivity0f65dd72011-04-20 13:37:53 +03006805static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
6806 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006807 const void *old,
6808 const void *new,
6809 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03006810 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006811{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006812 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006813 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02006814 u64 page_line_mask;
Avi Kivitydaea3e72010-03-15 13:59:54 +02006815 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02006816 char *kaddr;
6817 bool exchanged;
6818
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006819 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02006820 if (bytes > 8 || (bytes & (bytes - 1)))
6821 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006822
Avi Kivitydaea3e72010-03-15 13:59:54 +02006823 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006824
Avi Kivitydaea3e72010-03-15 13:59:54 +02006825 if (gpa == UNMAPPED_GVA ||
6826 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
6827 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006828
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02006829 /*
6830 * Emulate the atomic as a straight write to avoid #AC if SLD is
6831 * enabled in the host and the access splits a cache line.
6832 */
6833 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
6834 page_line_mask = ~(cache_line_size() - 1);
6835 else
6836 page_line_mask = PAGE_MASK;
6837
6838 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask))
Avi Kivitydaea3e72010-03-15 13:59:54 +02006839 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006840
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006841 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08006842 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02006843
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006844 kaddr = map.hva + offset_in_page(gpa);
6845
Avi Kivitydaea3e72010-03-15 13:59:54 +02006846 switch (bytes) {
6847 case 1:
6848 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
6849 break;
6850 case 2:
6851 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
6852 break;
6853 case 4:
6854 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
6855 break;
6856 case 8:
6857 exchanged = CMPXCHG64(kaddr, old, new);
6858 break;
6859 default:
6860 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006861 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006862
6863 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02006864
6865 if (!exchanged)
6866 return X86EMUL_CMPXCHG_FAILED;
6867
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08006868 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03006869
6870 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02006871
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006872emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02006873 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006874
Avi Kivity0f65dd72011-04-20 13:37:53 +03006875 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006876}
6877
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006878static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
6879{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006880 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006881
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006882 for (i = 0; i < vcpu->arch.pio.count; i++) {
6883 if (vcpu->arch.pio.in)
6884 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
6885 vcpu->arch.pio.size, pd);
6886 else
6887 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
6888 vcpu->arch.pio.port, vcpu->arch.pio.size,
6889 pd);
6890 if (r)
6891 break;
6892 pd += vcpu->arch.pio.size;
6893 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006894 return r;
6895}
6896
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006897static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
6898 unsigned short port, void *val,
6899 unsigned int count, bool in)
6900{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006901 vcpu->arch.pio.port = port;
6902 vcpu->arch.pio.in = in;
6903 vcpu->arch.pio.count = count;
6904 vcpu->arch.pio.size = size;
6905
6906 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
6907 vcpu->arch.pio.count = 0;
6908 return 1;
6909 }
6910
6911 vcpu->run->exit_reason = KVM_EXIT_IO;
6912 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
6913 vcpu->run->io.size = size;
6914 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
6915 vcpu->run->io.count = count;
6916 vcpu->run->io.port = port;
6917
6918 return 0;
6919}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006920
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006921static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6922 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006923{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006924 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03006925
Gleb Natapov79729952010-03-18 15:20:24 +02006926 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006927 goto data_avail;
6928
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006929 memset(vcpu->arch.pio_data, 0, size * count);
6930
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006931 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
6932 if (ret) {
6933data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006934 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006935 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02006936 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006937 return 1;
6938 }
6939
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006940 return 0;
6941}
6942
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006943static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
6944 int size, unsigned short port, void *val,
6945 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006946{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006947 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006948
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006949}
6950
6951static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
6952 unsigned short port, const void *val,
6953 unsigned int count)
6954{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006955 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006956 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006957 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006958}
6959
Carsten Ottebbd9b642007-10-30 18:44:21 +01006960static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
6961 int size, unsigned short port,
6962 const void *val, unsigned int count)
6963{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006964 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006965}
6966
6967static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
6968{
Jason Baronb36464772021-01-14 22:27:56 -05006969 return static_call(kvm_x86_get_segment_base)(vcpu, seg);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006970}
6971
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006972static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006973{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006974 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006975}
6976
Jiang Biaoae6a2372016-11-07 08:54:51 +08006977static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006978{
6979 if (!need_emulate_wbinvd(vcpu))
6980 return X86EMUL_CONTINUE;
6981
Jason Baronb36464772021-01-14 22:27:56 -05006982 if (static_call(kvm_x86_has_wbinvd_exit)()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01006983 int cpu = get_cpu();
6984
6985 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Wanpeng Lic2162e12021-03-12 10:45:51 +08006986 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006987 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006988 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006989 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006990 } else
6991 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006992 return X86EMUL_CONTINUE;
6993}
Joel Schopp5cb56052015-03-02 13:43:31 -06006994
6995int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6996{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006997 kvm_emulate_wbinvd_noskip(vcpu);
6998 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006999}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007000EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
7001
Joel Schopp5cb56052015-03-02 13:43:31 -06007002
7003
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03007004static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
7005{
Joel Schopp5cb56052015-03-02 13:43:31 -06007006 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03007007}
7008
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05007009static void emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
7010 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01007011{
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05007012 kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007013}
7014
Xiubo Li52eb5a62015-03-13 17:39:45 +08007015static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
7016 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01007017{
Gleb Natapov338dbc92010-04-28 19:15:32 +03007018
Paolo Bonzini996ff542020-12-14 07:49:54 -05007019 return kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007020}
7021
Gleb Natapov52a46612010-03-18 15:20:03 +02007022static u64 mk_cr_64(u64 curr_cr, u32 new_val)
7023{
7024 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
7025}
7026
Avi Kivity717746e2011-04-20 13:37:53 +03007027static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02007028{
Avi Kivity717746e2011-04-20 13:37:53 +03007029 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02007030 unsigned long value;
7031
7032 switch (cr) {
7033 case 0:
7034 value = kvm_read_cr0(vcpu);
7035 break;
7036 case 2:
7037 value = vcpu->arch.cr2;
7038 break;
7039 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02007040 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02007041 break;
7042 case 4:
7043 value = kvm_read_cr4(vcpu);
7044 break;
7045 case 8:
7046 value = kvm_get_cr8(vcpu);
7047 break;
7048 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03007049 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02007050 return 0;
7051 }
7052
7053 return value;
7054}
7055
Avi Kivity717746e2011-04-20 13:37:53 +03007056static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02007057{
Avi Kivity717746e2011-04-20 13:37:53 +03007058 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03007059 int res = 0;
7060
Gleb Natapov52a46612010-03-18 15:20:03 +02007061 switch (cr) {
7062 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03007063 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02007064 break;
7065 case 2:
7066 vcpu->arch.cr2 = val;
7067 break;
7068 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03007069 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02007070 break;
7071 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03007072 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02007073 break;
7074 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007075 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02007076 break;
7077 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03007078 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03007079 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02007080 }
Gleb Natapov0f122442010-04-28 19:15:31 +03007081
7082 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02007083}
7084
Avi Kivity717746e2011-04-20 13:37:53 +03007085static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02007086{
Jason Baronb36464772021-01-14 22:27:56 -05007087 return static_call(kvm_x86_get_cpl)(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02007088}
7089
Avi Kivity4bff1e862011-04-20 13:37:53 +03007090static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007091{
Jason Baronb36464772021-01-14 22:27:56 -05007092 static_call(kvm_x86_get_gdt)(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007093}
7094
Avi Kivity4bff1e862011-04-20 13:37:53 +03007095static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03007096{
Jason Baronb36464772021-01-14 22:27:56 -05007097 static_call(kvm_x86_get_idt)(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03007098}
7099
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03007100static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
7101{
Jason Baronb36464772021-01-14 22:27:56 -05007102 static_call(kvm_x86_set_gdt)(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03007103}
7104
7105static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
7106{
Jason Baronb36464772021-01-14 22:27:56 -05007107 static_call(kvm_x86_set_idt)(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03007108}
7109
Avi Kivity4bff1e862011-04-20 13:37:53 +03007110static unsigned long emulator_get_cached_segment_base(
7111 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03007112{
Avi Kivity4bff1e862011-04-20 13:37:53 +03007113 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03007114}
7115
Avi Kivity1aa36612011-04-27 13:20:30 +03007116static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
7117 struct desc_struct *desc, u32 *base3,
7118 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007119{
7120 struct kvm_segment var;
7121
Avi Kivity4bff1e862011-04-20 13:37:53 +03007122 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03007123 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007124
Gleb Natapov378a8b02013-01-21 15:36:48 +02007125 if (var.unusable) {
7126 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02007127 if (base3)
7128 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007129 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02007130 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007131
7132 if (var.g)
7133 var.limit >>= 12;
7134 set_desc_limit(desc, var.limit);
7135 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02007136#ifdef CONFIG_X86_64
7137 if (base3)
7138 *base3 = var.base >> 32;
7139#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007140 desc->type = var.type;
7141 desc->s = var.s;
7142 desc->dpl = var.dpl;
7143 desc->p = var.present;
7144 desc->avl = var.avl;
7145 desc->l = var.l;
7146 desc->d = var.db;
7147 desc->g = var.g;
7148
7149 return true;
7150}
7151
Avi Kivity1aa36612011-04-27 13:20:30 +03007152static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
7153 struct desc_struct *desc, u32 base3,
7154 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007155{
Avi Kivity4bff1e862011-04-20 13:37:53 +03007156 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007157 struct kvm_segment var;
7158
Avi Kivity1aa36612011-04-27 13:20:30 +03007159 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007160 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02007161#ifdef CONFIG_X86_64
7162 var.base |= ((u64)base3) << 32;
7163#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007164 var.limit = get_desc_limit(desc);
7165 if (desc->g)
7166 var.limit = (var.limit << 12) | 0xfff;
7167 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007168 var.dpl = desc->dpl;
7169 var.db = desc->d;
7170 var.s = desc->s;
7171 var.l = desc->l;
7172 var.g = desc->g;
7173 var.avl = desc->avl;
7174 var.present = desc->p;
7175 var.unusable = !var.present;
7176 var.padding = 0;
7177
7178 kvm_set_segment(vcpu, &var, seg);
7179 return;
7180}
7181
Avi Kivity717746e2011-04-20 13:37:53 +03007182static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
7183 u32 msr_index, u64 *pdata)
7184{
Alexander Graf1ae09952020-09-25 16:34:16 +02007185 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7186 int r;
7187
7188 r = kvm_get_msr(vcpu, msr_index, pdata);
7189
7190 if (r && kvm_get_msr_user_space(vcpu, msr_index, r)) {
7191 /* Bounce to user space */
7192 return X86EMUL_IO_NEEDED;
7193 }
7194
7195 return r;
Avi Kivity717746e2011-04-20 13:37:53 +03007196}
7197
7198static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
7199 u32 msr_index, u64 data)
7200{
Alexander Graf1ae09952020-09-25 16:34:16 +02007201 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7202 int r;
7203
7204 r = kvm_set_msr(vcpu, msr_index, data);
7205
7206 if (r && kvm_set_msr_user_space(vcpu, msr_index, data, r)) {
7207 /* Bounce to user space */
7208 return X86EMUL_IO_NEEDED;
7209 }
7210
7211 return r;
Avi Kivity717746e2011-04-20 13:37:53 +03007212}
7213
Paolo Bonzini64d60672015-05-07 11:36:11 +02007214static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
7215{
7216 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7217
7218 return vcpu->arch.smbase;
7219}
7220
7221static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
7222{
7223 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7224
7225 vcpu->arch.smbase = smbase;
7226}
7227
Nadav Amit67f4d422014-06-02 18:34:09 +03007228static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
7229 u32 pmc)
7230{
Like Xu98ff80f2019-10-27 18:52:40 +08007231 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03007232}
7233
Avi Kivity222d21a2011-11-10 14:57:30 +02007234static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
7235 u32 pmc, u64 *pdata)
7236{
Wei Huangc6702c92015-06-19 13:44:45 +02007237 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02007238}
7239
Avi Kivity6c3287f2011-04-20 15:43:05 +03007240static void emulator_halt(struct x86_emulate_ctxt *ctxt)
7241{
7242 emul_to_vcpu(ctxt)->arch.halt_request = 1;
7243}
7244
Avi Kivity29535382011-04-20 13:37:53 +03007245static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007246 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02007247 enum x86_intercept_stage stage)
7248{
Jason Baronb36464772021-01-14 22:27:56 -05007249 return static_call(kvm_x86_check_intercept)(emul_to_vcpu(ctxt), info, stage,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007250 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02007251}
7252
Yu Zhange911eb32017-08-24 20:27:52 +08007253static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08007254 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
7255 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01007256{
Sean Christophersonf91af512020-03-04 17:34:37 -08007257 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01007258}
7259
Sean Christopherson5ae78e92019-12-17 13:32:38 -08007260static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
7261{
7262 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
7263}
7264
7265static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
7266{
7267 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
7268}
7269
7270static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
7271{
7272 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
7273}
7274
Avi Kivitydd856ef2012-08-27 23:46:17 +03007275static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
7276{
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07007277 return kvm_register_read_raw(emul_to_vcpu(ctxt), reg);
Avi Kivitydd856ef2012-08-27 23:46:17 +03007278}
7279
7280static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
7281{
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07007282 kvm_register_write_raw(emul_to_vcpu(ctxt), reg, val);
Avi Kivitydd856ef2012-08-27 23:46:17 +03007283}
7284
Nadav Amit801806d2015-01-26 09:32:23 +02007285static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
7286{
Jason Baronb36464772021-01-14 22:27:56 -05007287 static_call(kvm_x86_set_nmi_mask)(emul_to_vcpu(ctxt), masked);
Nadav Amit801806d2015-01-26 09:32:23 +02007288}
7289
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007290static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
7291{
7292 return emul_to_vcpu(ctxt)->arch.hflags;
7293}
7294
Sean Christophersonedce4652021-06-09 11:56:13 -07007295static void emulator_exiting_smm(struct x86_emulate_ctxt *ctxt)
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007296{
Sean Christopherson78fcb2c2021-06-09 11:56:11 -07007297 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7298
Sean Christophersondc872752021-06-09 11:56:15 -07007299 kvm_smm_changed(vcpu, false);
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007300}
7301
Sean Christophersonecc513e2021-06-09 11:56:19 -07007302static int emulator_leave_smm(struct x86_emulate_ctxt *ctxt,
Sean Christophersoned193212019-04-02 08:03:09 -07007303 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007304{
Sean Christophersonecc513e2021-06-09 11:56:19 -07007305 return static_call(kvm_x86_leave_smm)(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007306}
7307
Sean Christopherson25b17222021-06-09 11:56:12 -07007308static void emulator_triple_fault(struct x86_emulate_ctxt *ctxt)
7309{
7310 kvm_make_request(KVM_REQ_TRIPLE_FAULT, emul_to_vcpu(ctxt));
7311}
7312
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02007313static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
7314{
7315 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
7316}
7317
Mathias Krause0225fb52012-08-30 01:30:16 +02007318static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03007319 .read_gpr = emulator_read_gpr,
7320 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007321 .read_std = emulator_read_std,
7322 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01007323 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02007324 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01007325 .read_emulated = emulator_read_emulated,
7326 .write_emulated = emulator_write_emulated,
7327 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03007328 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007329 .pio_in_emulated = emulator_pio_in_emulated,
7330 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03007331 .get_segment = emulator_get_segment,
7332 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03007333 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007334 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03007335 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03007336 .set_gdt = emulator_set_gdt,
7337 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02007338 .get_cr = emulator_get_cr,
7339 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02007340 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03007341 .get_dr = emulator_get_dr,
7342 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02007343 .get_smbase = emulator_get_smbase,
7344 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03007345 .set_msr = emulator_set_msr,
7346 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03007347 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02007348 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03007349 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03007350 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03007351 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02007352 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01007353 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08007354 .guest_has_long_mode = emulator_guest_has_long_mode,
7355 .guest_has_movbe = emulator_guest_has_movbe,
7356 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02007357 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007358 .get_hflags = emulator_get_hflags,
Sean Christophersonedce4652021-06-09 11:56:13 -07007359 .exiting_smm = emulator_exiting_smm,
Sean Christophersonecc513e2021-06-09 11:56:19 -07007360 .leave_smm = emulator_leave_smm,
Sean Christopherson25b17222021-06-09 11:56:12 -07007361 .triple_fault = emulator_triple_fault,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02007362 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01007363};
7364
Gleb Natapov95cb2292010-04-28 19:15:43 +03007365static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
7366{
Jason Baronb36464772021-01-14 22:27:56 -05007367 u32 int_shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03007368 /*
7369 * an sti; sti; sequence only disable interrupts for the first
7370 * instruction. So, if the last instruction, be it emulated or
7371 * not, left the system with the INT_STI flag enabled, it
7372 * means that the last instruction is an sti. We should not
7373 * leave the flag on in this case. The same goes for mov ss
7374 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02007375 if (int_shadow & mask)
7376 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007377 if (unlikely(int_shadow || mask)) {
Jason Baronb36464772021-01-14 22:27:56 -05007378 static_call(kvm_x86_set_interrupt_shadow)(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007379 if (!mask)
7380 kvm_make_request(KVM_REQ_EVENT, vcpu);
7381 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03007382}
7383
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007384static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03007385{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007386 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02007387 if (ctxt->exception.vector == PF_VECTOR)
Sean Christopherson53b3d8e2020-03-20 14:28:01 -07007388 return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007389
7390 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02007391 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
7392 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03007393 else
Avi Kivityda9cb572010-11-22 17:53:21 +02007394 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007395 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03007396}
7397
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007398static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
7399{
7400 struct x86_emulate_ctxt *ctxt;
7401
7402 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
7403 if (!ctxt) {
7404 pr_err("kvm: failed to allocate vcpu's emulator\n");
7405 return NULL;
7406 }
7407
7408 ctxt->vcpu = vcpu;
7409 ctxt->ops = &emulate_ops;
7410 vcpu->arch.emulate_ctxt = ctxt;
7411
7412 return ctxt;
7413}
7414
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007415static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
7416{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007417 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007418 int cs_db, cs_l;
7419
Jason Baronb36464772021-01-14 22:27:56 -05007420 static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007421
Sean Christopherson744e6992020-02-18 15:03:09 -08007422 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007423 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007424 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
7425
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007426 ctxt->eip = kvm_rip_read(vcpu);
7427 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
7428 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03007429 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007430 cs_db ? X86EMUL_MODE_PROT32 :
7431 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02007432 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007433 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
7434 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007435
Wanpeng Lida6393c2021-05-27 17:01:36 -07007436 ctxt->interruptibility = 0;
7437 ctxt->have_exception = false;
7438 ctxt->exception.vector = -1;
7439 ctxt->perm_ok = false;
7440
Avi Kivitydd856ef2012-08-27 23:46:17 +03007441 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007442 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007443}
7444
Sean Christopherson9497e1f2019-08-27 14:40:36 -07007445void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02007446{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007447 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02007448 int ret;
7449
7450 init_emulate_ctxt(vcpu);
7451
Avi Kivity9dac77f2011-06-01 15:34:25 +03007452 ctxt->op_bytes = 2;
7453 ctxt->ad_bytes = 2;
7454 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007455 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02007456
Sean Christopherson9497e1f2019-08-27 14:40:36 -07007457 if (ret != X86EMUL_CONTINUE) {
7458 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7459 } else {
7460 ctxt->eip = ctxt->_eip;
7461 kvm_rip_write(vcpu, ctxt->eip);
7462 kvm_set_rflags(vcpu, ctxt->eflags);
7463 }
Mohammed Gamal63995652010-09-19 14:34:06 +02007464}
7465EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
7466
Aaron Lewis19238e72021-05-10 07:48:33 -07007467static void prepare_emulation_failure_exit(struct kvm_vcpu *vcpu)
7468{
7469 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
7470 u32 insn_size = ctxt->fetch.end - ctxt->fetch.data;
7471 struct kvm_run *run = vcpu->run;
7472
7473 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7474 run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION;
7475 run->emulation_failure.ndata = 0;
7476 run->emulation_failure.flags = 0;
7477
7478 if (insn_size) {
7479 run->emulation_failure.ndata = 3;
7480 run->emulation_failure.flags |=
7481 KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES;
7482 run->emulation_failure.insn_size = insn_size;
7483 memset(run->emulation_failure.insn_bytes, 0x90,
7484 sizeof(run->emulation_failure.insn_bytes));
7485 memcpy(run->emulation_failure.insn_bytes,
7486 ctxt->fetch.data, insn_size);
7487 }
7488}
7489
Liran Alone2366172018-03-12 13:12:49 +02007490static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007491{
Aaron Lewis19238e72021-05-10 07:48:33 -07007492 struct kvm *kvm = vcpu->kvm;
7493
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007494 ++vcpu->stat.insn_emulation_fail;
7495 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02007496
Sean Christopherson42cbf062019-08-27 14:40:31 -07007497 if (emulation_type & EMULTYPE_VMWARE_GP) {
7498 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007499 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07007500 }
Liran Alone2366172018-03-12 13:12:49 +02007501
Aaron Lewis19238e72021-05-10 07:48:33 -07007502 if (kvm->arch.exit_on_emulation_error ||
7503 (emulation_type & EMULTYPE_SKIP)) {
7504 prepare_emulation_failure_exit(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007505 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07007506 }
7507
Sean Christopherson22da61c2019-08-27 14:40:28 -07007508 kvm_queue_exception(vcpu, UD_VECTOR);
7509
Jason Baronb36464772021-01-14 22:27:56 -05007510 if (!is_guest_mode(vcpu) && static_call(kvm_x86_get_cpl)(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01007511 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7512 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7513 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007514 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01007515 }
Liran Alone2366172018-03-12 13:12:49 +02007516
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007517 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007518}
7519
Sean Christopherson736c2912019-12-06 15:57:14 -08007520static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03007521 bool write_fault_to_shadow_pgtable,
7522 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03007523{
Sean Christopherson736c2912019-12-06 15:57:14 -08007524 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08007525 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03007526
Sean Christopherson92daa482020-02-18 15:03:08 -08007527 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03007528 return false;
7529
Sean Christopherson92daa482020-02-18 15:03:08 -08007530 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
7531 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07007532 return false;
7533
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007534 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007535 /*
7536 * Write permission should be allowed since only
7537 * write access need to be emulated.
7538 */
Sean Christopherson736c2912019-12-06 15:57:14 -08007539 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03007540
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007541 /*
7542 * If the mapping is invalid in guest, let cpu retry
7543 * it to generate fault.
7544 */
7545 if (gpa == UNMAPPED_GVA)
7546 return true;
7547 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03007548
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08007549 /*
7550 * Do not retry the unhandleable instruction if it faults on the
7551 * readonly host memory, otherwise it will goto a infinite loop:
7552 * retry instruction -> write #PF -> emulation fail -> retry
7553 * instruction -> ...
7554 */
7555 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007556
7557 /*
7558 * If the instruction failed on the error pfn, it can not be fixed,
7559 * report the error to userspace.
7560 */
7561 if (is_error_noslot_pfn(pfn))
7562 return false;
7563
7564 kvm_release_pfn_clean(pfn);
7565
7566 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007567 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007568 unsigned int indirect_shadow_pages;
7569
Ben Gardon531810c2021-02-02 10:57:24 -08007570 write_lock(&vcpu->kvm->mmu_lock);
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007571 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
Ben Gardon531810c2021-02-02 10:57:24 -08007572 write_unlock(&vcpu->kvm->mmu_lock);
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007573
7574 if (indirect_shadow_pages)
7575 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
7576
Gleb Natapova6f177e2010-07-08 12:41:12 +03007577 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08007578 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03007579
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007580 /*
7581 * if emulation was due to access to shadowed page table
7582 * and it failed try to unshadow page and re-enter the
7583 * guest to let CPU execute the instruction.
7584 */
7585 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007586
7587 /*
7588 * If the access faults on its page table, it can not
7589 * be fixed by unprotecting shadow page and it should
7590 * be reported to userspace.
7591 */
7592 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03007593}
7594
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007595static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08007596 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007597{
7598 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08007599 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007600
7601 last_retry_eip = vcpu->arch.last_retry_eip;
7602 last_retry_addr = vcpu->arch.last_retry_addr;
7603
7604 /*
7605 * If the emulation is caused by #PF and it is non-page_table
7606 * writing instruction, it means the VM-EXIT is caused by shadow
7607 * page protected, we can zap the shadow page and retry this
7608 * instruction directly.
7609 *
7610 * Note: if the guest uses a non-page-table modifying instruction
7611 * on the PDE that points to the instruction, then we will unmap
7612 * the instruction and go to an infinite loop. So, we cache the
7613 * last retried eip and the last fault address, if we meet the eip
7614 * and the address again, we can break out of the potential infinite
7615 * loop.
7616 */
7617 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
7618
Sean Christopherson92daa482020-02-18 15:03:08 -08007619 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007620 return false;
7621
Sean Christopherson92daa482020-02-18 15:03:08 -08007622 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
7623 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07007624 return false;
7625
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007626 if (x86_page_table_writing_insn(ctxt))
7627 return false;
7628
Sean Christopherson736c2912019-12-06 15:57:14 -08007629 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007630 return false;
7631
7632 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08007633 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007634
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007635 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08007636 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007637
Xiao Guangrong22368022013-01-13 23:44:12 +08007638 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007639
7640 return true;
7641}
7642
Gleb Natapov716d51a2012-09-03 15:24:26 +03007643static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
7644static int complete_emulated_pio(struct kvm_vcpu *vcpu);
7645
Sean Christophersondc872752021-06-09 11:56:15 -07007646static void kvm_smm_changed(struct kvm_vcpu *vcpu, bool entering_smm)
Paolo Bonzinia5845392015-04-01 18:18:53 +02007647{
Sean Christopherson1270e642021-06-09 11:56:17 -07007648 trace_kvm_smm_transition(vcpu->vcpu_id, vcpu->arch.smbase, entering_smm);
Sean Christopherson0d7ee6f2021-06-09 11:56:16 -07007649
Sean Christophersondc872752021-06-09 11:56:15 -07007650 if (entering_smm) {
7651 vcpu->arch.hflags |= HF_SMM_MASK;
7652 } else {
7653 vcpu->arch.hflags &= ~(HF_SMM_MASK | HF_SMM_INSIDE_NMI_MASK);
7654
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007655 /* Process a latched INIT or SMI, if any. */
7656 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007657 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02007658
7659 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007660}
7661
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007662static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
7663 unsigned long *db)
7664{
7665 u32 dr6 = 0;
7666 int i;
7667 u32 enable, rwlen;
7668
7669 enable = dr7;
7670 rwlen = dr7 >> 16;
7671 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
7672 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
7673 dr6 |= (1 << i);
7674 return dr6;
7675}
7676
Sean Christopherson120c2c42019-08-27 14:40:29 -07007677static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007678{
7679 struct kvm_run *kvm_run = vcpu->run;
7680
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007681 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08007682 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW;
Peter Xud5d260c2020-05-05 16:49:59 -04007683 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007684 kvm_run->debug.arch.exception = DB_VECTOR;
7685 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007686 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007687 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07007688 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007689 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007690}
7691
Kyle Huey6affcbe2016-11-29 12:40:40 -08007692int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
7693{
Jason Baronb36464772021-01-14 22:27:56 -05007694 unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02007695 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007696
Jason Baronb36464772021-01-14 22:27:56 -05007697 r = static_call(kvm_x86_skip_emulated_instruction)(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007698 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02007699 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007700
7701 /*
7702 * rflags is the old, "raw" value of the flags. The new value has
7703 * not been saved yet.
7704 *
7705 * This is correct even for TF set by the guest, because "the
7706 * processor will not generate this exception after the instruction
7707 * that sets the TF flag".
7708 */
7709 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07007710 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007711 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007712}
7713EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
7714
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007715static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
7716{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007717 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
7718 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02007719 struct kvm_run *kvm_run = vcpu->run;
7720 unsigned long eip = kvm_get_linear_rip(vcpu);
7721 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007722 vcpu->arch.guest_debug_dr7,
7723 vcpu->arch.eff_db);
7724
7725 if (dr6 != 0) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08007726 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW;
Nadav Amit82b32772014-11-02 11:54:45 +02007727 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007728 kvm_run->debug.arch.exception = DB_VECTOR;
7729 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007730 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007731 return true;
7732 }
7733 }
7734
Nadav Amit4161a562014-07-17 01:19:31 +03007735 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
7736 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02007737 unsigned long eip = kvm_get_linear_rip(vcpu);
7738 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007739 vcpu->arch.dr7,
7740 vcpu->arch.db);
7741
7742 if (dr6 != 0) {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04007743 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007744 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007745 return true;
7746 }
7747 }
7748
7749 return false;
7750}
7751
Liran Alon04789b62018-03-12 13:12:50 +02007752static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
7753{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02007754 switch (ctxt->opcode_len) {
7755 case 1:
7756 switch (ctxt->b) {
7757 case 0xe4: /* IN */
7758 case 0xe5:
7759 case 0xec:
7760 case 0xed:
7761 case 0xe6: /* OUT */
7762 case 0xe7:
7763 case 0xee:
7764 case 0xef:
7765 case 0x6c: /* INS */
7766 case 0x6d:
7767 case 0x6e: /* OUTS */
7768 case 0x6f:
7769 return true;
7770 }
7771 break;
7772 case 2:
7773 switch (ctxt->b) {
7774 case 0x33: /* RDPMC */
7775 return true;
7776 }
7777 break;
Liran Alon04789b62018-03-12 13:12:50 +02007778 }
7779
7780 return false;
7781}
7782
Wei Huang4aa26912021-01-26 03:18:28 -05007783/*
7784 * Decode to be emulated instruction. Return EMULATION_OK if success.
7785 */
7786int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
7787 void *insn, int insn_len)
7788{
7789 int r = EMULATION_OK;
7790 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
7791
7792 init_emulate_ctxt(vcpu);
7793
7794 /*
7795 * We will reenter on the same instruction since we do not set
7796 * complete_userspace_io. This does not handle watchpoints yet,
7797 * those would be handled in the emulate_ops.
7798 */
7799 if (!(emulation_type & EMULTYPE_SKIP) &&
7800 kvm_vcpu_check_breakpoint(vcpu, &r))
7801 return r;
7802
Wanpeng Lib35491e2021-05-27 17:01:37 -07007803 r = x86_decode_insn(ctxt, insn, insn_len, emulation_type);
Wei Huang4aa26912021-01-26 03:18:28 -05007804
7805 trace_kvm_emulate_insn_start(vcpu);
7806 ++vcpu->stat.insn_emulation;
7807
7808 return r;
7809}
7810EXPORT_SYMBOL_GPL(x86_decode_emulated_instruction);
7811
Sean Christopherson736c2912019-12-06 15:57:14 -08007812int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
7813 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01007814{
Gleb Natapov95cb2292010-04-28 19:15:43 +03007815 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007816 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007817 bool writeback = true;
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007818 bool write_fault_to_spt;
7819
Jason Baronb36464772021-01-14 22:27:56 -05007820 if (unlikely(!static_call(kvm_x86_can_emulate_instruction)(vcpu, insn, insn_len)))
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007821 return 1;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007822
Paolo Bonzinic595cee2018-07-02 13:07:14 +02007823 vcpu->arch.l1tf_flush_l1d = true;
7824
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007825 /*
7826 * Clear write_fault_to_shadow_pgtable here to ensure it is
7827 * never reused.
7828 */
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007829 write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007830 vcpu->arch.write_fault_to_shadow_pgtable = false;
Gleb Natapov8d7d81022011-04-12 12:36:21 +03007831
Sheng Yang571008d2008-01-02 14:49:22 +08007832 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Wei Huang4aa26912021-01-26 03:18:28 -05007833 kvm_clear_exception_queue(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007834
Wei Huang4aa26912021-01-26 03:18:28 -05007835 r = x86_decode_emulated_instruction(vcpu, emulation_type,
7836 insn, insn_len);
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09007837 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07007838 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07007839 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
7840 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007841 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07007842 }
Sean Christopherson736c2912019-12-06 15:57:14 -08007843 if (reexecute_instruction(vcpu, cr2_or_gpa,
7844 write_fault_to_spt,
7845 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007846 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00007847 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00007848 /*
7849 * #UD should result in just EMULATION_FAILED, and trap-like
7850 * exception should not be encountered during decode.
7851 */
7852 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
7853 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00007854 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007855 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00007856 }
Liran Alone2366172018-03-12 13:12:49 +02007857 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007858 }
7859 }
7860
Sean Christopherson42cbf062019-08-27 14:40:31 -07007861 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
7862 !is_vmware_backdoor_opcode(ctxt)) {
7863 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007864 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07007865 }
Liran Alon04789b62018-03-12 13:12:50 +02007866
Sean Christopherson1957aa62019-08-27 14:40:39 -07007867 /*
7868 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
7869 * for kvm_skip_emulated_instruction(). The caller is responsible for
7870 * updating interruptibility state and injecting single-step #DBs.
7871 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03007872 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03007873 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03007874 if (ctxt->eflags & X86_EFLAGS_RF)
7875 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007876 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03007877 }
7878
Sean Christopherson736c2912019-12-06 15:57:14 -08007879 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007880 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007881
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007882 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03007883 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007884 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
7885 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03007886 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007887 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03007888
Gleb Natapov5cd21912010-03-18 15:20:26 +02007889restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08007890 if (emulation_type & EMULTYPE_PF) {
7891 /* Save the faulting GPA (cr2) in the address field */
7892 ctxt->exception.address = cr2_or_gpa;
7893
7894 /* With shadow page tables, cr2 contains a GVA or nGPA. */
7895 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08007896 ctxt->gpa_available = true;
7897 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08007898 }
7899 } else {
7900 /* Sanitize the address out of an abundance of paranoia. */
7901 ctxt->exception.address = 0;
7902 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05007903
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007904 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03007905
Joerg Roedel775fde82011-04-04 12:39:24 +02007906 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007907 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02007908
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007909 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08007910 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03007911 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007912 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03007913
Liran Alone2366172018-03-12 13:12:49 +02007914 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007915 }
7916
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007917 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007918 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007919 if (inject_emulated_exception(vcpu))
7920 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007921 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02007922 if (!vcpu->arch.pio.in) {
7923 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03007924 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02007925 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007926 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007927 vcpu->arch.complete_userspace_io = complete_emulated_pio;
7928 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007929 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007930 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07007931 ++vcpu->stat.mmio_exits;
7932
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007933 if (!vcpu->mmio_is_write)
7934 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007935 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007936 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007937 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03007938 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007939 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007940 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03007941
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007942 if (writeback) {
Jason Baronb36464772021-01-14 22:27:56 -05007943 unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007944 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007945 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02007946 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007947 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
7948 kvm_rip_write(vcpu, ctxt->eip);
Felipe Franciosi384dea12020-05-19 08:11:22 +00007949 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
Sean Christopherson120c2c42019-08-27 14:40:29 -07007950 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007951 if (kvm_x86_ops.update_emulated_instruction)
Jason Baronb36464772021-01-14 22:27:56 -05007952 static_call(kvm_x86_update_emulated_instruction)(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02007953 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007954 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007955
7956 /*
7957 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
7958 * do nothing, and it will be requested again as soon as
7959 * the shadow expires. But we still need to check here,
7960 * because POPF has no interrupt shadow.
7961 */
7962 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
7963 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007964 } else
7965 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03007966
Gleb Natapove85d28f2010-07-29 15:11:52 +03007967 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007968}
Sean Christophersonc60658d2018-08-23 13:56:53 -07007969
7970int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
7971{
7972 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
7973}
7974EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
7975
7976int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
7977 void *insn, int insn_len)
7978{
7979 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
7980}
7981EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007982
Sean Christopherson8764ed52019-04-29 07:04:15 -07007983static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
7984{
7985 vcpu->arch.pio.count = 0;
7986 return 1;
7987}
7988
Sean Christopherson45def772019-03-11 20:01:05 -07007989static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
7990{
7991 vcpu->arch.pio.count = 0;
7992
7993 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
7994 return 1;
7995
7996 return kvm_skip_emulated_instruction(vcpu);
7997}
7998
Sean Christophersondca7f122018-03-08 08:57:27 -08007999static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
8000 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01008001{
Sean Christophersonde3cd112019-04-30 10:36:17 -07008002 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08008003 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
8004
Sean Christopherson8764ed52019-04-29 07:04:15 -07008005 if (ret)
8006 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07008007
Sean Christopherson8764ed52019-04-29 07:04:15 -07008008 /*
8009 * Workaround userspace that relies on old KVM behavior of %rip being
8010 * incremented prior to exiting to userspace to handle "OUT 0x7e".
8011 */
8012 if (port == 0x7e &&
8013 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
8014 vcpu->arch.complete_userspace_io =
8015 complete_fast_pio_out_port_0x7e;
8016 kvm_skip_emulated_instruction(vcpu);
8017 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07008018 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
8019 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
8020 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07008021 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01008022}
Carsten Ottede7d7892007-10-30 18:44:25 +01008023
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008024static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
8025{
8026 unsigned long val;
8027
8028 /* We should only ever be called with arch.pio.count equal to 1 */
8029 BUG_ON(vcpu->arch.pio.count != 1);
8030
Sean Christopherson45def772019-03-11 20:01:05 -07008031 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
8032 vcpu->arch.pio.count = 0;
8033 return 1;
8034 }
8035
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008036 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07008037 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008038
8039 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08008040 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008041 * the copy and tracing
8042 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08008043 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008044 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008045
Sean Christopherson45def772019-03-11 20:01:05 -07008046 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008047}
8048
Sean Christophersondca7f122018-03-08 08:57:27 -08008049static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
8050 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008051{
8052 unsigned long val;
8053 int ret;
8054
8055 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07008056 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008057
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08008058 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008059 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07008060 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008061 return ret;
8062 }
8063
Sean Christopherson45def772019-03-11 20:01:05 -07008064 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008065 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
8066
8067 return 0;
8068}
Sean Christophersondca7f122018-03-08 08:57:27 -08008069
8070int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
8071{
Sean Christopherson45def772019-03-11 20:01:05 -07008072 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08008073
Sean Christophersondca7f122018-03-08 08:57:27 -08008074 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07008075 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08008076 else
Sean Christopherson45def772019-03-11 20:01:05 -07008077 ret = kvm_fast_pio_out(vcpu, size, port);
8078 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08008079}
8080EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05008081
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008082static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008083{
Tejun Heo0a3aee02010-12-18 16:28:55 +01008084 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008085 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008086}
8087
8088static void tsc_khz_changed(void *data)
8089{
8090 struct cpufreq_freqs *freq = data;
8091 unsigned long khz = 0;
8092
8093 if (data)
8094 khz = freq->new;
8095 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
8096 khz = cpufreq_quick_get(raw_smp_processor_id());
8097 if (!khz)
8098 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01008099 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008100}
8101
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008102#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008103static void kvm_hyperv_tsc_notifier(void)
8104{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008105 struct kvm *kvm;
8106 struct kvm_vcpu *vcpu;
8107 int cpu;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04008108 unsigned long flags;
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008109
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008110 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008111 list_for_each_entry(kvm, &vm_list, vm_list)
8112 kvm_make_mclock_inprogress_request(kvm);
8113
8114 hyperv_stop_tsc_emulation();
8115
8116 /* TSC frequency always matches when on Hyper-V */
8117 for_each_present_cpu(cpu)
8118 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
8119 kvm_max_guest_tsc_khz = tsc_khz;
8120
8121 list_for_each_entry(kvm, &vm_list, vm_list) {
8122 struct kvm_arch *ka = &kvm->arch;
8123
Paolo Bonzinia83829f2021-03-25 14:11:14 -04008124 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008125 pvclock_update_vm_gtod_copy(kvm);
Paolo Bonzinia83829f2021-03-25 14:11:14 -04008126 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008127
8128 kvm_for_each_vcpu(cpu, vcpu, kvm)
8129 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
8130
8131 kvm_for_each_vcpu(cpu, vcpu, kvm)
8132 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008133 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008134 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008135}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008136#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008137
Viresh Kumardf240142019-04-29 15:03:58 +05308138static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008139{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008140 struct kvm *kvm;
8141 struct kvm_vcpu *vcpu;
8142 int i, send_ipi = 0;
8143
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008144 /*
8145 * We allow guests to temporarily run on slowing clocks,
8146 * provided we notify them after, or to run on accelerating
8147 * clocks, provided we notify them before. Thus time never
8148 * goes backwards.
8149 *
8150 * However, we have a problem. We can't atomically update
8151 * the frequency of a given CPU from this function; it is
8152 * merely a notifier, which can be called from any CPU.
8153 * Changing the TSC frequency at arbitrary points in time
8154 * requires a recomputation of local variables related to
8155 * the TSC for each VCPU. We must flag these local variables
8156 * to be updated and be sure the update takes place with the
8157 * new frequency before any guests proceed.
8158 *
8159 * Unfortunately, the combination of hotplug CPU and frequency
8160 * change creates an intractable locking scenario; the order
8161 * of when these callouts happen is undefined with respect to
8162 * CPU hotplug, and they can race with each other. As such,
8163 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
8164 * undefined; you can actually have a CPU frequency change take
8165 * place in between the computation of X and the setting of the
8166 * variable. To protect against this problem, all updates of
8167 * the per_cpu tsc_khz variable are done in an interrupt
8168 * protected IPI, and all callers wishing to update the value
8169 * must wait for a synchronous IPI to complete (which is trivial
8170 * if the caller is on the CPU already). This establishes the
8171 * necessary total order on variable updates.
8172 *
8173 * Note that because a guest time update may take place
8174 * anytime after the setting of the VCPU's request bit, the
8175 * correct TSC value must be set before the request. However,
8176 * to ensure the update actually makes it to any guest which
8177 * starts running in hardware virtualization between the set
8178 * and the acquisition of the spinlock, we must also ping the
8179 * CPU after setting the request bit.
8180 *
8181 */
8182
Viresh Kumardf240142019-04-29 15:03:58 +05308183 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008184
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008185 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008186 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03008187 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05308188 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008189 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10008190 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008191 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008192 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008193 }
8194 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008195 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008196
8197 if (freq->old < freq->new && send_ipi) {
8198 /*
8199 * We upscale the frequency. Must make the guest
8200 * doesn't see old kvmclock values while running with
8201 * the new frequency, otherwise we risk the guest sees
8202 * time go backwards.
8203 *
8204 * In case we update the frequency for another cpu
8205 * (which might be in guest context) send an interrupt
8206 * to kick the cpu out of guest context. Next time
8207 * guest context is entered kvmclock will be updated,
8208 * so the guest will not see stale values.
8209 */
Viresh Kumardf240142019-04-29 15:03:58 +05308210 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008211 }
Viresh Kumardf240142019-04-29 15:03:58 +05308212}
8213
8214static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
8215 void *data)
8216{
8217 struct cpufreq_freqs *freq = data;
8218 int cpu;
8219
8220 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
8221 return 0;
8222 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
8223 return 0;
8224
8225 for_each_cpu(cpu, freq->policy->cpus)
8226 __kvmclock_cpufreq_notifier(freq, cpu);
8227
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008228 return 0;
8229}
8230
8231static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008232 .notifier_call = kvmclock_cpufreq_notifier
8233};
8234
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008235static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008236{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008237 tsc_khz_changed(NULL);
8238 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008239}
8240
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008241static void kvm_timer_init(void)
8242{
Zachary Amsdenc2855452010-09-18 14:38:15 -10008243 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05308244
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008245 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10008246#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01008247 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02008248 int cpu;
8249
Avi Kivity3e26f232010-12-16 12:16:34 +02008250 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01008251 policy = cpufreq_cpu_get(cpu);
Wanpeng Li9a119972020-03-02 15:15:36 +08008252 if (policy) {
8253 if (policy->cpuinfo.max_freq)
8254 max_tsc_khz = policy->cpuinfo.max_freq;
8255 cpufreq_cpu_put(policy);
8256 }
Avi Kivity3e26f232010-12-16 12:16:34 +02008257 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10008258#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008259 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
8260 CPUFREQ_TRANSITION_NOTIFIER);
8261 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05308262
Thomas Gleixner73c1b412016-12-21 20:19:54 +01008263 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008264 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008265}
8266
Andi Kleendd60d212017-07-25 17:20:32 -07008267DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
8268EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008269
Gleb Natapovf5132b02011-11-10 14:57:22 +02008270int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008271{
Alex,Shi086c9852011-10-20 15:34:01 +08008272 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008273}
8274
8275static int kvm_is_user_mode(void)
8276{
8277 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008278
Alex,Shi086c9852011-10-20 15:34:01 +08008279 if (__this_cpu_read(current_vcpu))
Jason Baronb36464772021-01-14 22:27:56 -05008280 user_mode = static_call(kvm_x86_get_cpl)(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008281
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008282 return user_mode != 0;
8283}
8284
8285static unsigned long kvm_get_guest_ip(void)
8286{
8287 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008288
Alex,Shi086c9852011-10-20 15:34:01 +08008289 if (__this_cpu_read(current_vcpu))
8290 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008291
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008292 return ip;
8293}
8294
Luwei Kang8479e042019-02-18 19:26:07 -05008295static void kvm_handle_intel_pt_intr(void)
8296{
8297 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
8298
8299 kvm_make_request(KVM_REQ_PMI, vcpu);
8300 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
8301 (unsigned long *)&vcpu->arch.pmu.global_status);
8302}
8303
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008304static struct perf_guest_info_callbacks kvm_guest_cbs = {
8305 .is_in_guest = kvm_is_in_guest,
8306 .is_user_mode = kvm_is_user_mode,
8307 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05008308 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008309};
8310
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008311#ifdef CONFIG_X86_64
8312static void pvclock_gtod_update_fn(struct work_struct *work)
8313{
Marcelo Tosattid8281992012-11-27 23:29:01 -02008314 struct kvm *kvm;
8315
8316 struct kvm_vcpu *vcpu;
8317 int i;
8318
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008319 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008320 list_for_each_entry(kvm, &vm_list, vm_list)
8321 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08008322 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008323 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008324 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008325}
8326
8327static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
8328
8329/*
Thomas Gleixner3f804f62021-05-06 15:21:37 +02008330 * Indirection to move queue_work() out of the tk_core.seq write held
8331 * region to prevent possible deadlocks against time accessors which
8332 * are invoked with work related locks held.
8333 */
8334static void pvclock_irq_work_fn(struct irq_work *w)
8335{
8336 queue_work(system_long_wq, &pvclock_gtod_work);
8337}
8338
8339static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_irq_work_fn);
8340
8341/*
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008342 * Notification about pvclock gtod data update.
8343 */
8344static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
8345 void *priv)
8346{
8347 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
8348 struct timekeeper *tk = priv;
8349
8350 update_pvclock_gtod(tk);
8351
Thomas Gleixner3f804f62021-05-06 15:21:37 +02008352 /*
8353 * Disable master clock if host does not trust, or does not use,
8354 * TSC based clocksource. Delegate queue_work() to irq_work as
8355 * this is invoked with tk_core.seq write held.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008356 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01008357 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008358 atomic_read(&kvm_guest_has_master_clock) != 0)
Thomas Gleixner3f804f62021-05-06 15:21:37 +02008359 irq_work_queue(&pvclock_irq_work);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008360 return 0;
8361}
8362
8363static struct notifier_block pvclock_gtod_notifier = {
8364 .notifier_call = pvclock_gtod_notify,
8365};
8366#endif
8367
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008368int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02008369{
Sean Christophersond008dfd2020-03-21 13:25:56 -07008370 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008371 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008372
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008373 if (kvm_x86_ops.hardware_enable) {
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008374 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008375 r = -EEXIST;
8376 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008377 }
8378
8379 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01008380 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008381 r = -EOPNOTSUPP;
8382 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008383 }
8384 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01008385 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008386 r = -EOPNOTSUPP;
8387 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008388 }
8389
Marc Orrb666a4b2018-11-06 14:53:56 -08008390 /*
8391 * KVM explicitly assumes that the guest has an FPU and
8392 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
8393 * vCPU's FPU state as a fxregs_state struct.
8394 */
8395 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
8396 printk(KERN_ERR "kvm: inadequate fpu\n");
8397 r = -EOPNOTSUPP;
8398 goto out;
8399 }
8400
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008401 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01008402 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08008403 __alignof__(struct fpu), SLAB_ACCOUNT,
8404 NULL);
8405 if (!x86_fpu_cache) {
8406 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
8407 goto out;
8408 }
8409
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008410 x86_emulator_cache = kvm_alloc_emulator_cache();
8411 if (!x86_emulator_cache) {
8412 pr_err("kvm: failed to allocate cache for x86 emulator\n");
8413 goto out_free_x86_fpu_cache;
8414 }
8415
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07008416 user_return_msrs = alloc_percpu(struct kvm_user_return_msrs);
8417 if (!user_return_msrs) {
8418 printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008419 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008420 }
Sean Christophersone5fda4b2021-05-04 10:17:32 -07008421 kvm_nr_uret_msrs = 0;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008422
Avi Kivity97db56c2008-01-13 13:23:56 +02008423 r = kvm_mmu_module_init();
8424 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008425 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02008426
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008427 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008428
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008429 perf_register_guest_info_callbacks(&kvm_guest_cbs);
8430
Sean Christophersoncfc48182020-03-02 15:56:23 -08008431 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08008432 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08008433 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
8434 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08008435
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08008436 if (pi_inject_timer == -1)
8437 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008438#ifdef CONFIG_X86_64
8439 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008440
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008441 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008442 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008443#endif
8444
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008445 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008446
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008447out_free_percpu:
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07008448 free_percpu(user_return_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008449out_free_x86_emulator_cache:
8450 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08008451out_free_x86_fpu_cache:
8452 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008453out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008454 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02008455}
Hollis Blanchard8776e512007-10-31 17:24:24 -05008456
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008457void kvm_arch_exit(void)
8458{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008459#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008460 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008461 clear_hv_tscchange_cb();
8462#endif
David Matlackcef84c32016-12-16 14:30:36 -08008463 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008464 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
8465
Jan Kiszka888d2562009-04-17 19:24:58 +02008466 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
8467 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
8468 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008469 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008470#ifdef CONFIG_X86_64
8471 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
Thomas Gleixner3f804f62021-05-06 15:21:37 +02008472 irq_work_sync(&pvclock_irq_work);
Thomas Gleixner594b27e2021-05-05 23:48:17 +02008473 cancel_work_sync(&pvclock_gtod_work);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008474#endif
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008475 kvm_x86_ops.hardware_enable = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008476 kvm_mmu_module_exit();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07008477 free_percpu(user_return_msrs);
Wanpeng Lidfdc0a72021-06-10 21:59:33 -07008478 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08008479 kmem_cache_destroy(x86_fpu_cache);
Paolo Bonzinib59b1532021-02-26 04:54:45 -05008480#ifdef CONFIG_KVM_XEN
Paolo Bonzinic462f852021-02-26 04:49:06 -05008481 static_key_deferred_flush(&kvm_xen_enabled);
David Woodhouse7d6bbeb2021-02-02 15:48:05 +00008482 WARN_ON(static_branch_unlikely(&kvm_xen_enabled.key));
Paolo Bonzinib59b1532021-02-26 04:54:45 -05008483#endif
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008484}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008485
Paolo Bonzini872f36e2021-01-08 05:54:44 -05008486static int __kvm_vcpu_halt(struct kvm_vcpu *vcpu, int state, int reason)
Hollis Blanchard8776e512007-10-31 17:24:24 -05008487{
8488 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02008489 if (lapic_in_kernel(vcpu)) {
Tom Lendacky647daca2021-01-04 14:20:01 -06008490 vcpu->arch.mp_state = state;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008491 return 1;
8492 } else {
Tom Lendacky647daca2021-01-04 14:20:01 -06008493 vcpu->run->exit_reason = reason;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008494 return 0;
8495 }
8496}
Tom Lendacky647daca2021-01-04 14:20:01 -06008497
8498int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
8499{
8500 return __kvm_vcpu_halt(vcpu, KVM_MP_STATE_HALTED, KVM_EXIT_HLT);
8501}
Joel Schopp5cb56052015-03-02 13:43:31 -06008502EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
8503
8504int kvm_emulate_halt(struct kvm_vcpu *vcpu)
8505{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008506 int ret = kvm_skip_emulated_instruction(vcpu);
8507 /*
8508 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
8509 * KVM_EXIT_DEBUG here.
8510 */
8511 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06008512}
Hollis Blanchard8776e512007-10-31 17:24:24 -05008513EXPORT_SYMBOL_GPL(kvm_emulate_halt);
8514
Tom Lendacky647daca2021-01-04 14:20:01 -06008515int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu)
8516{
8517 int ret = kvm_skip_emulated_instruction(vcpu);
8518
8519 return __kvm_vcpu_halt(vcpu, KVM_MP_STATE_AP_RESET_HOLD, KVM_EXIT_AP_RESET_HOLD) && ret;
8520}
8521EXPORT_SYMBOL_GPL(kvm_emulate_ap_reset_hold);
8522
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008523#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008524static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
8525 unsigned long clock_type)
8526{
8527 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02008528 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01008529 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008530 int ret;
8531
8532 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
8533 return -KVM_EOPNOTSUPP;
8534
YANG LI7ca7f3b2021-01-11 17:32:58 +08008535 if (!kvm_get_walltime_and_clockread(&ts, &cycle))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008536 return -KVM_EOPNOTSUPP;
8537
8538 clock_pairing.sec = ts.tv_sec;
8539 clock_pairing.nsec = ts.tv_nsec;
8540 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
8541 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02008542 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008543
8544 ret = 0;
8545 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
8546 sizeof(struct kvm_clock_pairing)))
8547 ret = -KVM_EFAULT;
8548
8549 return ret;
8550}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008551#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008552
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308553/*
8554 * kvm_pv_kick_cpu_op: Kick a vcpu.
8555 *
8556 * @apicid - apicid of vcpu to be kicked.
8557 */
8558static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
8559{
Raghavendra K T24d21662013-08-26 14:18:35 +05308560 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308561
Peter Xu150a84f2019-12-04 20:07:21 +01008562 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01008563 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08008564 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05308565 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06008566 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308567
Raghavendra K T24d21662013-08-26 14:18:35 +05308568 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08008569 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308570}
8571
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008572bool kvm_apicv_activated(struct kvm *kvm)
8573{
8574 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
8575}
8576EXPORT_SYMBOL_GPL(kvm_apicv_activated);
8577
Vitaly Kuznetsov4651fc52021-06-09 17:09:09 +02008578static void kvm_apicv_init(struct kvm *kvm)
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008579{
Maxim Levitskyb0a16372021-08-10 23:52:44 +03008580 mutex_init(&kvm->arch.apicv_update_lock);
8581
Vitaly Kuznetsov4651fc52021-06-09 17:09:09 +02008582 if (enable_apicv)
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008583 clear_bit(APICV_INHIBIT_REASON_DISABLE,
8584 &kvm->arch.apicv_inhibit_reasons);
8585 else
8586 set_bit(APICV_INHIBIT_REASON_DISABLE,
8587 &kvm->arch.apicv_inhibit_reasons);
8588}
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008589
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008590static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id)
Wanpeng Li71506292019-06-11 20:23:49 +08008591{
8592 struct kvm_vcpu *target = NULL;
8593 struct kvm_apic_map *map;
8594
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008595 vcpu->stat.directed_yield_attempted++;
8596
Wanpeng Li72b268a2021-05-18 05:00:32 -07008597 if (single_task_running())
8598 goto no_yield;
8599
Wanpeng Li71506292019-06-11 20:23:49 +08008600 rcu_read_lock();
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008601 map = rcu_dereference(vcpu->kvm->arch.apic_map);
Wanpeng Li71506292019-06-11 20:23:49 +08008602
8603 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
8604 target = map->phys_map[dest_id]->vcpu;
8605
8606 rcu_read_unlock();
8607
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008608 if (!target || !READ_ONCE(target->ready))
8609 goto no_yield;
8610
Wanpeng Lia1fa4cb2021-04-09 12:18:31 +08008611 /* Ignore requests to yield to self */
8612 if (vcpu == target)
8613 goto no_yield;
8614
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008615 if (kvm_vcpu_yield_to(target) <= 0)
8616 goto no_yield;
8617
8618 vcpu->stat.directed_yield_successful++;
8619
8620no_yield:
8621 return;
Wanpeng Li71506292019-06-11 20:23:49 +08008622}
8623
Ashish Kalra0dbb1122021-06-08 18:05:43 +00008624static int complete_hypercall_exit(struct kvm_vcpu *vcpu)
8625{
8626 u64 ret = vcpu->run->hypercall.ret;
8627
8628 if (!is_64_bit_mode(vcpu))
8629 ret = (u32)ret;
8630 kvm_rax_write(vcpu, ret);
8631 ++vcpu->stat.hypercalls;
8632 return kvm_skip_emulated_instruction(vcpu);
8633}
8634
Hollis Blanchard8776e512007-10-31 17:24:24 -05008635int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
8636{
8637 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03008638 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008639
Joao Martins23200b72018-06-13 09:55:44 -04008640 if (kvm_xen_hypercall_enabled(vcpu->kvm))
8641 return kvm_xen_hypercall(vcpu);
8642
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01008643 if (kvm_hv_hypercall_enabled(vcpu))
Radim Krčmář696ca772018-05-24 17:50:56 +02008644 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02008645
Sean Christophersonde3cd112019-04-30 10:36:17 -07008646 nr = kvm_rax_read(vcpu);
8647 a0 = kvm_rbx_read(vcpu);
8648 a1 = kvm_rcx_read(vcpu);
8649 a2 = kvm_rdx_read(vcpu);
8650 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008651
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008652 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04008653
Nadav Amita449c7a2014-06-18 17:19:24 +03008654 op_64_bit = is_64_bit_mode(vcpu);
8655 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05008656 nr &= 0xFFFFFFFF;
8657 a0 &= 0xFFFFFFFF;
8658 a1 &= 0xFFFFFFFF;
8659 a2 &= 0xFFFFFFFF;
8660 a3 &= 0xFFFFFFFF;
8661 }
8662
Jason Baronb36464772021-01-14 22:27:56 -05008663 if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
Jan Kiszka07708c42009-08-03 18:43:28 +02008664 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02008665 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02008666 }
8667
Oliver Upton66570e92020-08-18 15:24:28 +00008668 ret = -KVM_ENOSYS;
8669
Hollis Blanchard8776e512007-10-31 17:24:24 -05008670 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02008671 case KVM_HC_VAPIC_POLL_IRQ:
8672 ret = 0;
8673 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308674 case KVM_HC_KICK_CPU:
Oliver Upton66570e92020-08-18 15:24:28 +00008675 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_UNHALT))
8676 break;
8677
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308678 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008679 kvm_sched_yield(vcpu, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308680 ret = 0;
8681 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008682#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008683 case KVM_HC_CLOCK_PAIRING:
8684 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
8685 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08008686#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08008687 case KVM_HC_SEND_IPI:
Oliver Upton66570e92020-08-18 15:24:28 +00008688 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SEND_IPI))
8689 break;
8690
Wanpeng Li4180bf12018-07-23 14:39:54 +08008691 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
8692 break;
Wanpeng Li71506292019-06-11 20:23:49 +08008693 case KVM_HC_SCHED_YIELD:
Oliver Upton66570e92020-08-18 15:24:28 +00008694 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SCHED_YIELD))
8695 break;
8696
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008697 kvm_sched_yield(vcpu, a0);
Wanpeng Li71506292019-06-11 20:23:49 +08008698 ret = 0;
8699 break;
Ashish Kalra0dbb1122021-06-08 18:05:43 +00008700 case KVM_HC_MAP_GPA_RANGE: {
8701 u64 gpa = a0, npages = a1, attrs = a2;
8702
8703 ret = -KVM_ENOSYS;
8704 if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE)))
8705 break;
8706
8707 if (!PAGE_ALIGNED(gpa) || !npages ||
8708 gpa_to_gfn(gpa) + npages <= gpa_to_gfn(gpa)) {
8709 ret = -KVM_EINVAL;
8710 break;
8711 }
8712
8713 vcpu->run->exit_reason = KVM_EXIT_HYPERCALL;
8714 vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE;
8715 vcpu->run->hypercall.args[0] = gpa;
8716 vcpu->run->hypercall.args[1] = npages;
8717 vcpu->run->hypercall.args[2] = attrs;
8718 vcpu->run->hypercall.longmode = op_64_bit;
8719 vcpu->arch.complete_userspace_io = complete_hypercall_exit;
8720 return 0;
8721 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05008722 default:
8723 ret = -KVM_ENOSYS;
8724 break;
8725 }
Radim Krčmář696ca772018-05-24 17:50:56 +02008726out:
Nadav Amita449c7a2014-06-18 17:19:24 +03008727 if (!op_64_bit)
8728 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07008729 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03008730
Amit Shahf11c3a82008-02-21 01:00:30 +05308731 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03008732 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008733}
8734EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
8735
Jan Kiszkab6785de2012-09-20 07:43:17 +02008736static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05008737{
Avi Kivityd6aa1002011-04-20 15:47:13 +03008738 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008739 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008740 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008741
Jason Baronb36464772021-01-14 22:27:56 -05008742 static_call(kvm_x86_patch_hypercall)(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008743
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01008744 return emulator_write_emulated(ctxt, rip, instruction, 3,
8745 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008746}
8747
Avi Kivity851ba692009-08-24 11:10:17 +03008748static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008749{
Matt Gingell782d4222015-11-16 15:26:00 -08008750 return vcpu->run->request_interrupt_window &&
8751 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008752}
8753
Avi Kivity851ba692009-08-24 11:10:17 +03008754static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008755{
Avi Kivity851ba692009-08-24 11:10:17 +03008756 struct kvm_run *kvm_run = vcpu->run;
8757
Tom Lendackyf1c63662020-12-14 10:29:50 -05008758 /*
8759 * if_flag is obsolete and useless, so do not bother
8760 * setting it for SEV-ES guests. Userspace can just
8761 * use kvm_run->ready_for_interrupt_injection.
8762 */
8763 kvm_run->if_flag = !vcpu->arch.guest_state_protected
8764 && (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
8765
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008766 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008767 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01008768 kvm_run->ready_for_interrupt_injection =
8769 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08008770 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Chenyi Qiang15aad3b2020-11-06 17:03:13 +08008771
8772 if (is_smm(vcpu))
8773 kvm_run->flags |= KVM_RUN_X86_SMM;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008774}
8775
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008776static void update_cr8_intercept(struct kvm_vcpu *vcpu)
8777{
8778 int max_irr, tpr;
8779
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008780 if (!kvm_x86_ops.update_cr8_intercept)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008781 return;
8782
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008783 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03008784 return;
8785
Andrey Smetanind62caab2015-11-10 15:36:33 +03008786 if (vcpu->arch.apicv_active)
8787 return;
8788
Gleb Natapov8db3baa2009-05-11 13:35:54 +03008789 if (!vcpu->arch.apic->vapic_addr)
8790 max_irr = kvm_lapic_find_highest_irr(vcpu);
8791 else
8792 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008793
8794 if (max_irr != -1)
8795 max_irr >>= 4;
8796
8797 tpr = kvm_lapic_get_cr8(vcpu);
8798
Jason Baronb36464772021-01-14 22:27:56 -05008799 static_call(kvm_x86_update_cr8_intercept)(vcpu, tpr, max_irr);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008800}
8801
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008802
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08008803int kvm_check_nested_events(struct kvm_vcpu *vcpu)
8804{
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08008805 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
8806 kvm_x86_ops.nested_ops->triple_fault(vcpu);
8807 return 1;
8808 }
8809
8810 return kvm_x86_ops.nested_ops->check_events(vcpu);
8811}
8812
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008813static void kvm_inject_exception(struct kvm_vcpu *vcpu)
8814{
8815 if (vcpu->arch.exception.error_code && !is_protmode(vcpu))
8816 vcpu->arch.exception.error_code = false;
8817 static_call(kvm_x86_queue_exception)(vcpu);
8818}
8819
Jim Mattsona5f69092021-06-04 10:26:03 -07008820static int inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008821{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008822 int r;
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008823 bool can_inject = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008824
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008825 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07008826
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008827 if (vcpu->arch.exception.injected) {
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008828 kvm_inject_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008829 can_inject = false;
8830 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07008831 /*
Liran Alona042c262018-03-23 03:01:32 +03008832 * Do not inject an NMI or interrupt if there is a pending
8833 * exception. Exceptions and interrupts are recognized at
8834 * instruction boundaries, i.e. the start of an instruction.
8835 * Trap-like exceptions, e.g. #DB, have higher priority than
8836 * NMIs and interrupts, i.e. traps are recognized before an
8837 * NMI/interrupt that's pending on the same instruction.
8838 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
8839 * priority, but are only generated (pended) during instruction
8840 * execution, i.e. a pending fault-like exception means the
8841 * fault occurred on the *previous* instruction and must be
8842 * serviced prior to recognizing any new events in order to
8843 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07008844 */
Liran Alon1a680e32018-03-23 03:01:33 +03008845 else if (!vcpu->arch.exception.pending) {
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008846 if (vcpu->arch.nmi_injected) {
Jason Baronb36464772021-01-14 22:27:56 -05008847 static_call(kvm_x86_set_nmi)(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008848 can_inject = false;
8849 } else if (vcpu->arch.interrupt.injected) {
Jason Baronb36464772021-01-14 22:27:56 -05008850 static_call(kvm_x86_set_irq)(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008851 can_inject = false;
8852 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07008853 }
8854
Sean Christopherson3b82b8d2020-04-22 19:25:47 -07008855 WARN_ON_ONCE(vcpu->arch.exception.injected &&
8856 vcpu->arch.exception.pending);
8857
Liran Alon1a680e32018-03-23 03:01:33 +03008858 /*
8859 * Call check_nested_events() even if we reinjected a previous event
8860 * in order for caller to determine if it should require immediate-exit
8861 * from L2 to L1 due to pending L1 events which require exit
8862 * from L2 to L1.
8863 */
Paolo Bonzini56083bd2020-04-17 10:32:53 -04008864 if (is_guest_mode(vcpu)) {
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08008865 r = kvm_check_nested_events(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008866 if (r < 0)
Jim Mattsona5f69092021-06-04 10:26:03 -07008867 goto out;
Wanpeng Li664f8e22017-08-24 03:35:09 -07008868 }
8869
8870 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03008871 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02008872 trace_kvm_inj_exception(vcpu->arch.exception.nr,
8873 vcpu->arch.exception.has_error_code,
8874 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03008875
Wanpeng Li664f8e22017-08-24 03:35:09 -07008876 vcpu->arch.exception.pending = false;
8877 vcpu->arch.exception.injected = true;
8878
Nadav Amitd6e8c852014-07-24 14:51:24 +03008879 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
8880 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
8881 X86_EFLAGS_RF);
8882
Jim Mattsonf10c7292018-10-16 14:29:23 -07008883 if (vcpu->arch.exception.nr == DB_VECTOR) {
Jim Mattsonf10c7292018-10-16 14:29:23 -07008884 kvm_deliver_exception_payload(vcpu);
8885 if (vcpu->arch.dr7 & DR7_GD) {
8886 vcpu->arch.dr7 &= ~DR7_GD;
8887 kvm_update_dr7(vcpu);
8888 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03008889 }
8890
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008891 kvm_inject_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008892 can_inject = false;
Liran Alon1a680e32018-03-23 03:01:33 +03008893 }
8894
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008895 /*
8896 * Finally, inject interrupt events. If an event cannot be injected
8897 * due to architectural conditions (e.g. IF=0) a window-open exit
8898 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending
8899 * and can architecturally be injected, but we cannot do it right now:
8900 * an interrupt could have arrived just now and we have to inject it
8901 * as a vmexit, or there could already an event in the queue, which is
8902 * indicated by can_inject. In that case we request an immediate exit
8903 * in order to make progress and get back here for another iteration.
8904 * The kvm_x86_ops hooks communicate this by returning -EBUSY.
8905 */
8906 if (vcpu->arch.smi_pending) {
Jason Baronb36464772021-01-14 22:27:56 -05008907 r = can_inject ? static_call(kvm_x86_smi_allowed)(vcpu, true) : -EBUSY;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008908 if (r < 0)
Jim Mattsona5f69092021-06-04 10:26:03 -07008909 goto out;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008910 if (r) {
8911 vcpu->arch.smi_pending = false;
8912 ++vcpu->arch.smi_count;
8913 enter_smm(vcpu);
8914 can_inject = false;
8915 } else
Jason Baronb36464772021-01-14 22:27:56 -05008916 static_call(kvm_x86_enable_smi_window)(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008917 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008918
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008919 if (vcpu->arch.nmi_pending) {
Jason Baronb36464772021-01-14 22:27:56 -05008920 r = can_inject ? static_call(kvm_x86_nmi_allowed)(vcpu, true) : -EBUSY;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008921 if (r < 0)
Jim Mattsona5f69092021-06-04 10:26:03 -07008922 goto out;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008923 if (r) {
8924 --vcpu->arch.nmi_pending;
8925 vcpu->arch.nmi_injected = true;
Jason Baronb36464772021-01-14 22:27:56 -05008926 static_call(kvm_x86_set_nmi)(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008927 can_inject = false;
Jason Baronb36464772021-01-14 22:27:56 -05008928 WARN_ON(static_call(kvm_x86_nmi_allowed)(vcpu, true) < 0);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008929 }
8930 if (vcpu->arch.nmi_pending)
Jason Baronb36464772021-01-14 22:27:56 -05008931 static_call(kvm_x86_enable_nmi_window)(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008932 }
8933
8934 if (kvm_cpu_has_injectable_intr(vcpu)) {
Jason Baronb36464772021-01-14 22:27:56 -05008935 r = can_inject ? static_call(kvm_x86_interrupt_allowed)(vcpu, true) : -EBUSY;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008936 if (r < 0)
Jim Mattsona5f69092021-06-04 10:26:03 -07008937 goto out;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008938 if (r) {
8939 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false);
Jason Baronb36464772021-01-14 22:27:56 -05008940 static_call(kvm_x86_set_irq)(vcpu);
8941 WARN_ON(static_call(kvm_x86_interrupt_allowed)(vcpu, true) < 0);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008942 }
8943 if (kvm_cpu_has_injectable_intr(vcpu))
Jason Baronb36464772021-01-14 22:27:56 -05008944 static_call(kvm_x86_enable_irq_window)(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008945 }
8946
8947 if (is_guest_mode(vcpu) &&
8948 kvm_x86_ops.nested_ops->hv_timer_pending &&
8949 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
8950 *req_immediate_exit = true;
8951
8952 WARN_ON(vcpu->arch.exception.pending);
Jim Mattsona5f69092021-06-04 10:26:03 -07008953 return 0;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008954
Jim Mattsona5f69092021-06-04 10:26:03 -07008955out:
8956 if (r == -EBUSY) {
8957 *req_immediate_exit = true;
8958 r = 0;
8959 }
8960 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008961}
8962
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008963static void process_nmi(struct kvm_vcpu *vcpu)
8964{
8965 unsigned limit = 2;
8966
8967 /*
8968 * x86 is limited to one NMI running, and one NMI pending after it.
8969 * If an NMI is already in progress, limit further NMIs to just one.
8970 * Otherwise, allow two (and we'll inject the first one immediately).
8971 */
Jason Baronb36464772021-01-14 22:27:56 -05008972 if (static_call(kvm_x86_get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008973 limit = 1;
8974
8975 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
8976 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
8977 kvm_make_request(KVM_REQ_EVENT, vcpu);
8978}
8979
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008980static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008981{
8982 u32 flags = 0;
8983 flags |= seg->g << 23;
8984 flags |= seg->db << 22;
8985 flags |= seg->l << 21;
8986 flags |= seg->avl << 20;
8987 flags |= seg->present << 15;
8988 flags |= seg->dpl << 13;
8989 flags |= seg->s << 12;
8990 flags |= seg->type << 8;
8991 return flags;
8992}
8993
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008994static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008995{
8996 struct kvm_segment seg;
8997 int offset;
8998
8999 kvm_get_segment(vcpu, &seg, n);
9000 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
9001
9002 if (n < 3)
9003 offset = 0x7f84 + n * 12;
9004 else
9005 offset = 0x7f2c + (n - 3) * 12;
9006
9007 put_smstate(u32, buf, offset + 8, seg.base);
9008 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009009 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009010}
9011
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06009012#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009013static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009014{
9015 struct kvm_segment seg;
9016 int offset;
9017 u16 flags;
9018
9019 kvm_get_segment(vcpu, &seg, n);
9020 offset = 0x7e00 + n * 16;
9021
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009022 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009023 put_smstate(u16, buf, offset, seg.selector);
9024 put_smstate(u16, buf, offset + 2, flags);
9025 put_smstate(u32, buf, offset + 4, seg.limit);
9026 put_smstate(u64, buf, offset + 8, seg.base);
9027}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06009028#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009029
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009030static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009031{
9032 struct desc_ptr dt;
9033 struct kvm_segment seg;
9034 unsigned long val;
9035 int i;
9036
9037 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
9038 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
9039 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
9040 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
9041
9042 for (i = 0; i < 8; i++)
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07009043 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read_raw(vcpu, i));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009044
9045 kvm_get_dr(vcpu, 6, &val);
9046 put_smstate(u32, buf, 0x7fcc, (u32)val);
9047 kvm_get_dr(vcpu, 7, &val);
9048 put_smstate(u32, buf, 0x7fc8, (u32)val);
9049
9050 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
9051 put_smstate(u32, buf, 0x7fc4, seg.selector);
9052 put_smstate(u32, buf, 0x7f64, seg.base);
9053 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009054 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009055
9056 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
9057 put_smstate(u32, buf, 0x7fc0, seg.selector);
9058 put_smstate(u32, buf, 0x7f80, seg.base);
9059 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009060 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009061
Jason Baronb36464772021-01-14 22:27:56 -05009062 static_call(kvm_x86_get_gdt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009063 put_smstate(u32, buf, 0x7f74, dt.address);
9064 put_smstate(u32, buf, 0x7f70, dt.size);
9065
Jason Baronb36464772021-01-14 22:27:56 -05009066 static_call(kvm_x86_get_idt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009067 put_smstate(u32, buf, 0x7f58, dt.address);
9068 put_smstate(u32, buf, 0x7f54, dt.size);
9069
9070 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009071 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009072
9073 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
9074
9075 /* revision id */
9076 put_smstate(u32, buf, 0x7efc, 0x00020000);
9077 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
9078}
9079
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009080#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009081static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009082{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009083 struct desc_ptr dt;
9084 struct kvm_segment seg;
9085 unsigned long val;
9086 int i;
9087
9088 for (i = 0; i < 16; i++)
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07009089 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read_raw(vcpu, i));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009090
9091 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
9092 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
9093
9094 kvm_get_dr(vcpu, 6, &val);
9095 put_smstate(u64, buf, 0x7f68, val);
9096 kvm_get_dr(vcpu, 7, &val);
9097 put_smstate(u64, buf, 0x7f60, val);
9098
9099 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
9100 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
9101 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
9102
9103 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
9104
9105 /* revision id */
9106 put_smstate(u32, buf, 0x7efc, 0x00020064);
9107
9108 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
9109
9110 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
9111 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009112 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009113 put_smstate(u32, buf, 0x7e94, seg.limit);
9114 put_smstate(u64, buf, 0x7e98, seg.base);
9115
Jason Baronb36464772021-01-14 22:27:56 -05009116 static_call(kvm_x86_get_idt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009117 put_smstate(u32, buf, 0x7e84, dt.size);
9118 put_smstate(u64, buf, 0x7e88, dt.address);
9119
9120 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
9121 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009122 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009123 put_smstate(u32, buf, 0x7e74, seg.limit);
9124 put_smstate(u64, buf, 0x7e78, seg.base);
9125
Jason Baronb36464772021-01-14 22:27:56 -05009126 static_call(kvm_x86_get_gdt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009127 put_smstate(u32, buf, 0x7e64, dt.size);
9128 put_smstate(u64, buf, 0x7e68, dt.address);
9129
9130 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009131 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009132}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009133#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009134
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009135static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02009136{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009137 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02009138 struct desc_ptr dt;
Sean Christophersondbc47392021-06-22 10:56:59 -07009139 unsigned long cr0;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009140 char buf[512];
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009141
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009142 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009143#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02009144 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009145 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009146 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009147#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009148 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009149
Ladi Prosek0234bf82017-10-11 16:54:40 +02009150 /*
Sean Christophersonecc513e2021-06-09 11:56:19 -07009151 * Give enter_smm() a chance to make ISA-specific changes to the vCPU
9152 * state (e.g. leave guest mode) after we've saved the state into the
9153 * SMM state-save area.
Ladi Prosek0234bf82017-10-11 16:54:40 +02009154 */
Sean Christophersonecc513e2021-06-09 11:56:19 -07009155 static_call(kvm_x86_enter_smm)(vcpu, buf);
Ladi Prosek0234bf82017-10-11 16:54:40 +02009156
Sean Christophersondc872752021-06-09 11:56:15 -07009157 kvm_smm_changed(vcpu, true);
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009158 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009159
Jason Baronb36464772021-01-14 22:27:56 -05009160 if (static_call(kvm_x86_get_nmi_mask)(vcpu))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009161 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
9162 else
Jason Baronb36464772021-01-14 22:27:56 -05009163 static_call(kvm_x86_set_nmi_mask)(vcpu, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009164
9165 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
9166 kvm_rip_write(vcpu, 0x8000);
9167
9168 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
Jason Baronb36464772021-01-14 22:27:56 -05009169 static_call(kvm_x86_set_cr0)(vcpu, cr0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009170 vcpu->arch.cr0 = cr0;
9171
Jason Baronb36464772021-01-14 22:27:56 -05009172 static_call(kvm_x86_set_cr4)(vcpu, 0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009173
Paolo Bonzini18c36262015-08-07 12:27:54 +02009174 /* Undocumented: IDT limit is set to zero on entry to SMM. */
9175 dt.address = dt.size = 0;
Jason Baronb36464772021-01-14 22:27:56 -05009176 static_call(kvm_x86_set_idt)(vcpu, &dt);
Paolo Bonzini18c36262015-08-07 12:27:54 +02009177
Paolo Bonzini996ff542020-12-14 07:49:54 -05009178 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009179
9180 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
9181 cs.base = vcpu->arch.smbase;
9182
9183 ds.selector = 0;
9184 ds.base = 0;
9185
9186 cs.limit = ds.limit = 0xffffffff;
9187 cs.type = ds.type = 0x3;
9188 cs.dpl = ds.dpl = 0;
9189 cs.db = ds.db = 0;
9190 cs.s = ds.s = 1;
9191 cs.l = ds.l = 0;
9192 cs.g = ds.g = 1;
9193 cs.avl = ds.avl = 0;
9194 cs.present = ds.present = 1;
9195 cs.unusable = ds.unusable = 0;
9196 cs.padding = ds.padding = 0;
9197
9198 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9199 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
9200 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
9201 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
9202 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
9203 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
9204
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009205#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02009206 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Jason Baronb36464772021-01-14 22:27:56 -05009207 static_call(kvm_x86_set_efer)(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009208#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009209
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08009210 kvm_update_cpuid_runtime(vcpu);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009211 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009212}
9213
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009214static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009215{
9216 vcpu->arch.smi_pending = true;
9217 kvm_make_request(KVM_REQ_EVENT, vcpu);
9218}
9219
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05009220void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
9221 unsigned long *vcpu_bitmap)
9222{
9223 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05009224
9225 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
9226
Mao Wenandb5a95e2019-11-19 11:06:40 +08009227 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -05009228 NULL, vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05009229
9230 free_cpumask_var(cpus);
9231}
9232
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01009233void kvm_make_scan_ioapic_request(struct kvm *kvm)
9234{
9235 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
9236}
9237
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009238void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
9239{
Maxim Levitsky06ef8132021-08-10 23:52:48 +03009240 bool activate;
9241
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009242 if (!lapic_in_kernel(vcpu))
9243 return;
9244
Maxim Levitskyb0a16372021-08-10 23:52:44 +03009245 mutex_lock(&vcpu->kvm->arch.apicv_update_lock);
9246
Maxim Levitsky06ef8132021-08-10 23:52:48 +03009247 activate = kvm_apicv_activated(vcpu->kvm);
9248 if (vcpu->arch.apicv_active == activate)
9249 goto out;
9250
9251 vcpu->arch.apicv_active = activate;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009252 kvm_apic_update_apicv(vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05009253 static_call(kvm_x86_refresh_apicv_exec_ctrl)(vcpu);
Vitaly Kuznetsovbca66db2021-06-09 17:09:10 +02009254
9255 /*
9256 * When APICv gets disabled, we may still have injected interrupts
9257 * pending. At the same time, KVM_REQ_EVENT may not be set as APICv was
9258 * still active when the interrupt got accepted. Make sure
9259 * inject_pending_event() is called to check for that.
9260 */
9261 if (!vcpu->arch.apicv_active)
9262 kvm_make_request(KVM_REQ_EVENT, vcpu);
Maxim Levitskyb0a16372021-08-10 23:52:44 +03009263
Maxim Levitsky06ef8132021-08-10 23:52:48 +03009264out:
Maxim Levitskyb0a16372021-08-10 23:52:44 +03009265 mutex_unlock(&vcpu->kvm->arch.apicv_update_lock);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009266}
9267EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
9268
Maxim Levitskyb0a16372021-08-10 23:52:44 +03009269void __kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009270{
Maxim Levitskyb0a16372021-08-10 23:52:44 +03009271 unsigned long old, new;
Paolo Bonzini8e205a62020-03-14 12:29:23 +01009272
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009273 if (!kvm_x86_ops.check_apicv_inhibit_reasons ||
Jason Baronb36464772021-01-14 22:27:56 -05009274 !static_call(kvm_x86_check_apicv_inhibit_reasons)(bit))
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06009275 return;
9276
Maxim Levitskyb0a16372021-08-10 23:52:44 +03009277 old = new = kvm->arch.apicv_inhibit_reasons;
9278
9279 if (activate)
9280 __clear_bit(bit, &new);
9281 else
9282 __set_bit(bit, &new);
Paolo Bonzini8e205a62020-03-14 12:29:23 +01009283
Maxim Levitsky36222b12021-08-10 23:52:43 +03009284 if (!!old != !!new) {
9285 trace_kvm_apicv_update_request(activate, bit);
9286 kvm_make_all_cpus_request(kvm, KVM_REQ_APICV_UPDATE);
Maxim Levitskyb0a16372021-08-10 23:52:44 +03009287 kvm->arch.apicv_inhibit_reasons = new;
Maxim Levitsky36222b12021-08-10 23:52:43 +03009288 if (new) {
9289 unsigned long gfn = gpa_to_gfn(APIC_DEFAULT_PHYS_BASE);
Maxim Levitsky36222b12021-08-10 23:52:43 +03009290 kvm_zap_gfn_range(kvm, gfn, gfn+1);
9291 }
Maxim Levitskyb0a16372021-08-10 23:52:44 +03009292 } else
9293 kvm->arch.apicv_inhibit_reasons = new;
9294}
9295EXPORT_SYMBOL_GPL(__kvm_request_apicv_update);
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05009296
Maxim Levitskyb0a16372021-08-10 23:52:44 +03009297void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
9298{
9299 mutex_lock(&kvm->arch.apicv_update_lock);
9300 __kvm_request_apicv_update(kvm, activate, bit);
9301 mutex_unlock(&kvm->arch.apicv_update_lock);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009302}
9303EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
9304
Yang Zhang3d81bc72013-04-11 19:25:13 +08009305static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009306{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08009307 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009308 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009309
Andrey Smetanin63086302015-11-10 15:36:32 +03009310 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009311
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07009312 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03009313 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02009314 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02009315 if (vcpu->arch.apicv_active)
Jason Baronb36464772021-01-14 22:27:56 -05009316 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08009317 if (ioapic_in_kernel(vcpu->kvm))
9318 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02009319 }
Liran Alone40ff1d2018-03-21 02:50:31 +02009320
9321 if (is_guest_mode(vcpu))
9322 vcpu->arch.load_eoi_exitmap_pending = true;
9323 else
9324 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
9325}
9326
9327static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
9328{
9329 u64 eoi_exit_bitmap[4];
9330
9331 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
9332 return;
9333
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +01009334 if (to_hv_vcpu(vcpu))
9335 bitmap_or((ulong *)eoi_exit_bitmap,
9336 vcpu->arch.ioapic_handled_vectors,
9337 to_hv_synic(vcpu)->vec_bitmap, 256);
9338
Jason Baronb36464772021-01-14 22:27:56 -05009339 static_call(kvm_x86_load_eoi_exitmap)(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009340}
9341
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +09009342void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
9343 unsigned long start, unsigned long end)
Radim Krčmářb1394e72017-11-30 19:05:45 +01009344{
9345 unsigned long apic_address;
9346
9347 /*
9348 * The physical address of apic access page is stored in the VMCS.
9349 * Update it when it becomes invalid.
9350 */
9351 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
9352 if (start <= apic_address && apic_address < end)
9353 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
9354}
9355
Tang Chen4256f432014-09-24 15:57:54 +08009356void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
9357{
Paolo Bonzini35754c92015-07-29 12:05:37 +02009358 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02009359 return;
9360
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009361 if (!kvm_x86_ops.set_apic_access_page_addr)
Tang Chen4256f432014-09-24 15:57:54 +08009362 return;
9363
Jason Baronb36464772021-01-14 22:27:56 -05009364 static_call(kvm_x86_set_apic_access_page_addr)(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08009365}
Tang Chen4256f432014-09-24 15:57:54 +08009366
Sean Christophersond264ee02018-08-27 15:21:12 -07009367void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
9368{
9369 smp_send_reschedule(vcpu->cpu);
9370}
9371EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
9372
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09009373/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01009374 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09009375 * exiting to the userspace. Otherwise, the value will be returned to the
9376 * userspace.
9377 */
Avi Kivity851ba692009-08-24 11:10:17 +03009378static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009379{
9380 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08009381 bool req_int_win =
9382 dm_request_for_irq_injection(vcpu) &&
9383 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08009384 fastpath_t exit_fastpath;
Matt Gingell62a193e2015-11-16 15:26:07 -08009385
Jan Kiszka730dca42013-04-28 10:50:52 +02009386 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009387
Peter Xufb04a1e2020-09-30 21:22:22 -04009388 /* Forbid vmenter if vcpu dirty ring is soft-full */
9389 if (unlikely(vcpu->kvm->dirty_ring_size &&
9390 kvm_dirty_ring_soft_full(&vcpu->dirty_ring))) {
9391 vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL;
9392 trace_kvm_dirty_ring_exit(vcpu);
9393 r = 0;
9394 goto out;
9395 }
9396
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02009397 if (kvm_request_pending(vcpu)) {
Sean Christopherson67369272021-07-02 15:04:25 -07009398 if (kvm_check_request(KVM_REQ_VM_BUGGED, vcpu)) {
9399 r = -EIO;
9400 goto out;
9401 }
Paolo Bonzini729c15c2020-09-22 06:53:57 -04009402 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
Paolo Bonzini9a78e152021-01-08 11:43:08 -05009403 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) {
Jim Mattson671ddc72019-10-15 10:44:05 -07009404 r = 0;
9405 goto out;
9406 }
9407 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03009408 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05009409 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03009410 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03009411 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009412 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
9413 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03009414 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
9415 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10009416 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
9417 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10009418 if (unlikely(r))
9419 goto out;
9420 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03009421 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03009422 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05009423 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
9424 kvm_mmu_load_pgd(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07009425 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
Sean Christopherson77809382020-03-20 14:28:18 -07009426 kvm_vcpu_flush_tlb_all(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07009427
9428 /* Flushing all ASIDs flushes the current ASID... */
9429 kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
9430 }
9431 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
9432 kvm_vcpu_flush_tlb_current(vcpu);
Sean Christopherson07ffaf32021-06-09 16:42:21 -07009433 if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu))
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04009434 kvm_vcpu_flush_tlb_guest(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07009435
Avi Kivitya8eeb042010-05-10 12:34:53 +03009436 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009437 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02009438 r = 0;
9439 goto out;
9440 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03009441 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08009442 if (is_guest_mode(vcpu)) {
9443 kvm_x86_ops.nested_ops->triple_fault(vcpu);
9444 } else {
9445 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
9446 vcpu->mmio_needed = 0;
9447 r = 0;
9448 goto out;
9449 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01009450 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02009451 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
9452 /* Page is swapped out. Do synthetic halt */
9453 vcpu->arch.apf.halted = true;
9454 r = 1;
9455 goto out;
9456 }
Glauber Costac9aaa892011-07-11 15:28:14 -04009457 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
9458 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009459 if (kvm_check_request(KVM_REQ_SMI, vcpu))
9460 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009461 if (kvm_check_request(KVM_REQ_NMI, vcpu))
9462 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009463 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02009464 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009465 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02009466 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07009467 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
9468 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
9469 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03009470 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07009471 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
9472 vcpu->run->eoi.vector =
9473 vcpu->arch.pending_ioapic_eoi;
9474 r = 0;
9475 goto out;
9476 }
9477 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08009478 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
9479 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02009480 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
9481 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08009482 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
9483 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03009484 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
9485 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
9486 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
9487 r = 0;
9488 goto out;
9489 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03009490 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
9491 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
9492 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
9493 r = 0;
9494 goto out;
9495 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03009496 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01009497 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
9498
Andrey Smetanindb3975712015-11-10 15:36:35 +03009499 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01009500 vcpu->run->hyperv = hv_vcpu->exit;
Andrey Smetanindb3975712015-11-10 15:36:35 +03009501 r = 0;
9502 goto out;
9503 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03009504
9505 /*
9506 * KVM_REQ_HV_STIMER has to be processed after
9507 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
9508 * depend on the guest clock being up-to-date
9509 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009510 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
9511 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009512 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
9513 kvm_vcpu_update_apicv(vcpu);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02009514 if (kvm_check_request(KVM_REQ_APF_READY, vcpu))
9515 kvm_check_async_pf_completion(vcpu);
Alexander Graf1a1552542020-09-25 16:34:21 +02009516 if (kvm_check_request(KVM_REQ_MSR_FILTER_CHANGED, vcpu))
Jason Baronb36464772021-01-14 22:27:56 -05009517 static_call(kvm_x86_msr_filter_changed)(vcpu);
Makarand Sonarea85863c2021-02-12 16:50:12 -08009518
9519 if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu))
9520 static_call(kvm_x86_update_cpu_dirty_logging)(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02009521 }
Avi Kivityb93463a2007-10-25 16:52:32 +02009522
David Woodhouse40da8cc2020-12-09 20:08:30 +00009523 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
9524 kvm_xen_has_interrupt(vcpu)) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01009525 ++vcpu->stat.req_event;
Jim Mattson4fe09bc2021-06-04 10:26:04 -07009526 r = kvm_apic_accept_events(vcpu);
9527 if (r < 0) {
9528 r = 0;
9529 goto out;
9530 }
Jan Kiszka66450a22013-03-13 12:42:34 +01009531 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
9532 r = 1;
9533 goto out;
9534 }
9535
Jim Mattsona5f69092021-06-04 10:26:03 -07009536 r = inject_pending_event(vcpu, &req_immediate_exit);
9537 if (r < 0) {
9538 r = 0;
9539 goto out;
9540 }
Paolo Bonzinic9d40912020-05-22 11:21:49 -04009541 if (req_int_win)
Jason Baronb36464772021-01-14 22:27:56 -05009542 static_call(kvm_x86_enable_irq_window)(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03009543
Avi Kivity3842d132010-07-27 12:30:24 +03009544 if (kvm_lapic_enabled(vcpu)) {
9545 update_cr8_intercept(vcpu);
9546 kvm_lapic_sync_to_vapic(vcpu);
9547 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009548 }
Avi Kivityb93463a2007-10-25 16:52:32 +02009549
Avi Kivityd8368af2012-05-14 18:07:56 +03009550 r = kvm_mmu_reload(vcpu);
9551 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009552 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03009553 }
9554
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009555 preempt_disable();
9556
Jason Baronb36464772021-01-14 22:27:56 -05009557 static_call(kvm_x86_prepare_guest_switch)(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009558
9559 /*
9560 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
9561 * IPI are then delayed after guest entry, which ensures that they
9562 * result in virtual interrupt delivery.
9563 */
9564 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009565 vcpu->mode = IN_GUEST_MODE;
9566
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02009567 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
9568
Lan Tianyu0f127d12016-03-13 11:10:29 +08009569 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009570 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02009571 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009572 *
Luwei Kang81b01662019-01-31 16:52:02 +08009573 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009574 * pairs with the memory barrier implicit in pi_test_and_set_on
9575 * (see vmx_deliver_posted_interrupt).
9576 *
9577 * 3) This also orders the write to mode from any reads to the page
9578 * tables done while the VCPU is running. Please see the comment
9579 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009580 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02009581 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009582
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009583 /*
9584 * This handles the case where a posted interrupt was
9585 * notified with kvm_vcpu_kick.
9586 */
Liran Alonfa59cc02017-12-24 18:12:53 +02009587 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
Jason Baronb36464772021-01-14 22:27:56 -05009588 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009589
Wanpeng Li5a9f5442020-04-28 14:23:26 +08009590 if (kvm_vcpu_exit_request(vcpu)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009591 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009592 smp_wmb();
9593 local_irq_enable();
9594 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02009595 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009596 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009597 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009598 }
9599
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009600 if (req_immediate_exit) {
9601 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05009602 static_call(kvm_x86_request_immediate_exit)(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009603 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03009604
Sean Christopherson2620fe22020-01-17 11:30:51 -08009605 fpregs_assert_state_consistent();
9606 if (test_thread_flag(TIF_NEED_FPU_LOAD))
9607 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02009608
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009609 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009610 set_debugreg(0, 7);
9611 set_debugreg(vcpu->arch.eff_db[0], 0);
9612 set_debugreg(vcpu->arch.eff_db[1], 1);
9613 set_debugreg(vcpu->arch.eff_db[2], 2);
9614 set_debugreg(vcpu->arch.eff_db[3], 3);
Lai Jiangshanf85d4012021-06-29 01:26:32 +08009615 } else if (unlikely(hw_breakpoint_active())) {
9616 set_debugreg(0, 7);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009617 }
9618
Paolo Bonzinid89d04a2021-02-02 10:44:23 -05009619 for (;;) {
9620 exit_fastpath = static_call(kvm_x86_run)(vcpu);
9621 if (likely(exit_fastpath != EXIT_FASTPATH_REENTER_GUEST))
9622 break;
9623
9624 if (unlikely(kvm_vcpu_exit_request(vcpu))) {
9625 exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
9626 break;
9627 }
9628
9629 if (vcpu->arch.apicv_active)
9630 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
9631 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009632
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009633 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009634 * Do this here before restoring debug registers on the host. And
9635 * since we do this before handling the vmexit, a DR access vmexit
9636 * can (a) read the correct value of the debug registers, (b) set
9637 * KVM_DEBUGREG_WONT_EXIT again.
9638 */
9639 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009640 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
Jason Baronb36464772021-01-14 22:27:56 -05009641 static_call(kvm_x86_sync_dirty_debug_regs)(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01009642 kvm_update_dr0123(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01009643 kvm_update_dr7(vcpu);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009644 }
9645
9646 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009647 * If the guest has used debug registers, at least dr7
9648 * will be disabled while returning to the host.
9649 * If we don't have active breakpoints in the host, we don't
9650 * care about the messed up debug address registers. But if
9651 * we have some of them active, restore the old state.
9652 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01009653 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009654 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009655
Jim Mattsonc9671182020-06-03 16:56:23 -07009656 vcpu->arch.last_vmentry_cpu = vcpu->cpu;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08009657 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10009658
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009659 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03009660 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08009661
Jason Baronb36464772021-01-14 22:27:56 -05009662 static_call(kvm_x86_handle_exit_irqoff)(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009663
Sean Christophersond7a08882019-07-10 09:07:34 -07009664 /*
9665 * Consume any pending interrupts, including the possible source of
9666 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
9667 * An instruction is required after local_irq_enable() to fully unblock
9668 * interrupts on processors that implement an interrupt shadow, the
9669 * stat.exits increment will do nicely.
9670 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009671 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07009672 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009673 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07009674 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009675 kvm_after_interrupt(vcpu);
9676
Wanpeng Li16045712021-05-04 17:27:30 -07009677 /*
9678 * Wait until after servicing IRQs to account guest time so that any
9679 * ticks that occurred while running the guest are properly accounted
9680 * to the guest. Waiting until IRQs are enabled degrades the accuracy
9681 * of accounting via context tracking, but the loss of accuracy is
9682 * acceptable for all known use cases.
9683 */
9684 vtime_account_guest_exit();
9685
Wanpeng Liec0671d2019-05-20 16:18:08 +08009686 if (lapic_in_kernel(vcpu)) {
9687 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
9688 if (delta != S64_MIN) {
9689 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
9690 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
9691 }
9692 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009693
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009694 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009695 preempt_enable();
9696
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009697 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03009698
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009699 /*
9700 * Profile KVM exit RIPs:
9701 */
9702 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009703 unsigned long rip = kvm_rip_read(vcpu);
9704 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009705 }
9706
Zachary Amsdencc578282012-02-03 15:43:50 -02009707 if (unlikely(vcpu->arch.tsc_always_catchup))
9708 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02009709
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03009710 if (vcpu->arch.apic_attention)
9711 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02009712
Jason Baronb36464772021-01-14 22:27:56 -05009713 r = static_call(kvm_x86_handle_exit)(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009714 return r;
9715
9716cancel_injection:
Sean Christopherson8081ad02020-04-22 19:25:40 -07009717 if (req_immediate_exit)
9718 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05009719 static_call(kvm_x86_cancel_injection)(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03009720 if (unlikely(vcpu->arch.apic_attention))
9721 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009722out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03009723 return r;
9724}
9725
Paolo Bonzini362c6982015-02-06 12:48:04 +01009726static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
9727{
Feng Wubf9f6ac2015-09-18 22:29:55 +08009728 if (!kvm_arch_vcpu_runnable(vcpu) &&
Jason Baronb36464772021-01-14 22:27:56 -05009729 (!kvm_x86_ops.pre_block || static_call(kvm_x86_pre_block)(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01009730 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
9731 kvm_vcpu_block(vcpu);
9732 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009733
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009734 if (kvm_x86_ops.post_block)
Jason Baronb36464772021-01-14 22:27:56 -05009735 static_call(kvm_x86_post_block)(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009736
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01009737 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
9738 return 1;
9739 }
Gleb Natapov09cec752009-03-23 15:11:44 +02009740
Jim Mattson4fe09bc2021-06-04 10:26:04 -07009741 if (kvm_apic_accept_events(vcpu) < 0)
9742 return 0;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009743 switch(vcpu->arch.mp_state) {
9744 case KVM_MP_STATE_HALTED:
Tom Lendacky647daca2021-01-04 14:20:01 -06009745 case KVM_MP_STATE_AP_RESET_HOLD:
Paolo Bonzini362c6982015-02-06 12:48:04 +01009746 vcpu->arch.pv.pv_unhalted = false;
9747 vcpu->arch.mp_state =
9748 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009749 fallthrough;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009750 case KVM_MP_STATE_RUNNABLE:
9751 vcpu->arch.apf.halted = false;
9752 break;
9753 case KVM_MP_STATE_INIT_RECEIVED:
9754 break;
9755 default:
9756 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009757 }
9758 return 1;
9759}
9760
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009761static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
9762{
Paolo Bonzini56083bd2020-04-17 10:32:53 -04009763 if (is_guest_mode(vcpu))
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08009764 kvm_check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01009765
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009766 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
9767 !vcpu->arch.apf.halted);
9768}
9769
Paolo Bonzini362c6982015-02-06 12:48:04 +01009770static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03009771{
9772 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009773 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03009774
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009775 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009776 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03009777
Paolo Bonzini362c6982015-02-06 12:48:04 +01009778 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02009779 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009780 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009781 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01009782 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009783 }
9784
Gleb Natapov09cec752009-03-23 15:11:44 +02009785 if (r <= 0)
9786 break;
9787
Marcelo Tosatti084071d2021-05-25 10:41:17 -03009788 kvm_clear_request(KVM_REQ_UNBLOCK, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02009789 if (kvm_cpu_has_pending_timer(vcpu))
9790 kvm_inject_pending_timer_irqs(vcpu);
9791
Matt Gingell782d4222015-11-16 15:26:00 -08009792 if (dm_request_for_irq_injection(vcpu) &&
9793 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02009794 r = 0;
9795 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02009796 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009797 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02009798 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02009799
Thomas Gleixnerf3020b82020-07-30 09:19:01 +02009800 if (__xfer_to_guest_mode_work_pending()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009801 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02009802 r = xfer_to_guest_mode_handle_work(vcpu);
9803 if (r)
9804 return r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009805 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03009806 }
9807 }
9808
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009809 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009810
9811 return r;
9812}
9813
Gleb Natapov716d51a2012-09-03 15:24:26 +03009814static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
9815{
9816 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009817
Gleb Natapov716d51a2012-09-03 15:24:26 +03009818 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07009819 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009820 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009821 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03009822}
9823
9824static int complete_emulated_pio(struct kvm_vcpu *vcpu)
9825{
9826 BUG_ON(!vcpu->arch.pio.count);
9827
9828 return complete_emulated_io(vcpu);
9829}
9830
Avi Kivityf78146b2012-04-18 19:22:47 +03009831/*
9832 * Implements the following, as a state machine:
9833 *
9834 * read:
9835 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009836 * for each mmio piece in the fragment
9837 * write gpa, len
9838 * exit
9839 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03009840 * execute insn
9841 *
9842 * write:
9843 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009844 * for each mmio piece in the fragment
9845 * write gpa, len
9846 * copy data
9847 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03009848 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03009849static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02009850{
9851 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03009852 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009853 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02009854
Gleb Natapov716d51a2012-09-03 15:24:26 +03009855 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02009856
Gleb Natapov716d51a2012-09-03 15:24:26 +03009857 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009858 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
9859 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009860 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009861 memcpy(frag->data, run->mmio.data, len);
9862
9863 if (frag->len <= 8) {
9864 /* Switch to the next fragment. */
9865 frag++;
9866 vcpu->mmio_cur_fragment++;
9867 } else {
9868 /* Go forward to the next mmio piece. */
9869 frag->data += len;
9870 frag->gpa += len;
9871 frag->len -= len;
9872 }
9873
Andrew Honiga08d3b32014-02-27 19:35:14 +01009874 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03009875 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02009876
9877 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02009878 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03009879 return 1;
9880 vcpu->mmio_read_completed = 1;
9881 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02009882 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009883
Gleb Natapov716d51a2012-09-03 15:24:26 +03009884 run->exit_reason = KVM_EXIT_MMIO;
9885 run->mmio.phys_addr = frag->gpa;
9886 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009887 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
9888 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009889 run->mmio.is_write = vcpu->mmio_is_write;
9890 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
9891 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02009892}
9893
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009894static void kvm_save_current_fpu(struct fpu *fpu)
9895{
9896 /*
9897 * If the target FPU state is not resident in the CPU registers, just
9898 * memcpy() from current, else save CPU state directly to the target.
9899 */
9900 if (test_thread_flag(TIF_NEED_FPU_LOAD))
9901 memcpy(&fpu->state, &current->thread.fpu.state,
9902 fpu_kernel_xstate_size);
9903 else
Thomas Gleixnerebe72342021-06-23 14:01:59 +02009904 save_fpregs_to_fpstate(fpu);
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009905}
9906
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009907/* Swap (qemu) user FPU context for the guest FPU context. */
9908static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
9909{
Rik van Riel5f409e22019-04-03 18:41:52 +02009910 fpregs_lock();
9911
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009912 kvm_save_current_fpu(vcpu->arch.user_fpu);
9913
Tom Lendackyed02b212020-12-10 11:10:01 -06009914 /*
9915 * Guests with protected state can't have it set by the hypervisor,
9916 * so skip trying to set it.
9917 */
9918 if (vcpu->arch.guest_fpu)
9919 /* PKRU is separately restored in kvm_x86_ops.run. */
Thomas Gleixner1c61fad2021-06-23 14:02:01 +02009920 __restore_fpregs_from_fpstate(&vcpu->arch.guest_fpu->state,
Tom Lendackyed02b212020-12-10 11:10:01 -06009921 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02009922
9923 fpregs_mark_activate();
9924 fpregs_unlock();
9925
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009926 trace_kvm_fpu(1);
9927}
9928
9929/* When vcpu_run ends, restore user space FPU context. */
9930static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
9931{
Rik van Riel5f409e22019-04-03 18:41:52 +02009932 fpregs_lock();
9933
Tom Lendackyed02b212020-12-10 11:10:01 -06009934 /*
9935 * Guests with protected state can't have it read by the hypervisor,
9936 * so skip trying to save it.
9937 */
9938 if (vcpu->arch.guest_fpu)
9939 kvm_save_current_fpu(vcpu->arch.guest_fpu);
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009940
Thomas Gleixner1c61fad2021-06-23 14:02:01 +02009941 restore_fpregs_from_fpstate(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02009942
9943 fpregs_mark_activate();
9944 fpregs_unlock();
9945
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009946 ++vcpu->stat.fpu_reload;
9947 trace_kvm_fpu(0);
9948}
9949
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009950int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009951{
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009952 struct kvm_run *kvm_run = vcpu->run;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009953 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009954
Christoffer Dallaccb7572017-12-04 21:35:25 +01009955 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01009956 kvm_sigset_activate(vcpu);
Chenyi Qiang15aad3b2020-11-06 17:03:13 +08009957 kvm_run->flags = 0;
Peter Xu5663d8f2017-12-12 17:15:02 +01009958 kvm_load_guest_fpu(vcpu);
9959
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009960 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02009961 if (kvm_run->immediate_exit) {
9962 r = -EINTR;
9963 goto out;
9964 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009965 kvm_vcpu_block(vcpu);
Jim Mattson4fe09bc2021-06-04 10:26:04 -07009966 if (kvm_apic_accept_events(vcpu) < 0) {
9967 r = 0;
9968 goto out;
9969 }
Radim Krčmář72875d82017-04-26 22:32:19 +02009970 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009971 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02009972 if (signal_pending(current)) {
9973 r = -EINTR;
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009974 kvm_run->exit_reason = KVM_EXIT_INTR;
Jan H. Schönherra0595002017-09-06 00:27:19 +02009975 ++vcpu->stat.signal_exits;
9976 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009977 goto out;
9978 }
9979
Sean Christophersone489a4a2021-07-02 15:04:29 -07009980 if ((kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) ||
9981 (kvm_run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)) {
Ken Hofsass01643c52018-01-31 16:03:36 -08009982 r = -EINVAL;
9983 goto out;
9984 }
9985
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009986 if (kvm_run->kvm_dirty_regs) {
Ken Hofsass01643c52018-01-31 16:03:36 -08009987 r = sync_regs(vcpu);
9988 if (r != 0)
9989 goto out;
9990 }
9991
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009992 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02009993 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01009994 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
9995 r = -EINVAL;
9996 goto out;
9997 }
9998 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009999
Gleb Natapov716d51a2012-09-03 15:24:26 +030010000 if (unlikely(vcpu->arch.complete_userspace_io)) {
10001 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
10002 vcpu->arch.complete_userspace_io = NULL;
10003 r = cui(vcpu);
10004 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +010010005 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +030010006 } else
10007 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +020010008
Paolo Bonzini460df4c2017-02-08 11:50:15 +010010009 if (kvm_run->immediate_exit)
10010 r = -EINTR;
10011 else
10012 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010013
10014out:
Peter Xu5663d8f2017-12-12 17:15:02 +010010015 kvm_put_guest_fpu(vcpu);
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +080010016 if (kvm_run->kvm_valid_regs)
Ken Hofsass01643c52018-01-31 16:03:36 -080010017 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -030010018 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +010010019 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010020
Christoffer Dallaccb7572017-12-04 21:35:25 +010010021 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010022 return r;
10023}
10024
Ken Hofsass01643c52018-01-31 16:03:36 -080010025static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010026{
Gleb Natapov7ae441e2011-03-31 12:06:41 +020010027 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
10028 /*
10029 * We are here if userspace calls get_regs() in the middle of
10030 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +080010031 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +020010032 * that usually, but some bad designed PV devices (vmware
10033 * backdoor interface) need this to work
10034 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010035 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +020010036 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
10037 }
Sean Christophersonde3cd112019-04-30 10:36:17 -070010038 regs->rax = kvm_rax_read(vcpu);
10039 regs->rbx = kvm_rbx_read(vcpu);
10040 regs->rcx = kvm_rcx_read(vcpu);
10041 regs->rdx = kvm_rdx_read(vcpu);
10042 regs->rsi = kvm_rsi_read(vcpu);
10043 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +020010044 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -070010045 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010046#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -070010047 regs->r8 = kvm_r8_read(vcpu);
10048 regs->r9 = kvm_r9_read(vcpu);
10049 regs->r10 = kvm_r10_read(vcpu);
10050 regs->r11 = kvm_r11_read(vcpu);
10051 regs->r12 = kvm_r12_read(vcpu);
10052 regs->r13 = kvm_r13_read(vcpu);
10053 regs->r14 = kvm_r14_read(vcpu);
10054 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010055#endif
10056
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010057 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +020010058 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -080010059}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010060
Ken Hofsass01643c52018-01-31 16:03:36 -080010061int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
10062{
10063 vcpu_load(vcpu);
10064 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +010010065 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010066 return 0;
10067}
10068
Ken Hofsass01643c52018-01-31 16:03:36 -080010069static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010070{
Gleb Natapov7ae441e2011-03-31 12:06:41 +020010071 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
10072 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
10073
Sean Christophersonde3cd112019-04-30 10:36:17 -070010074 kvm_rax_write(vcpu, regs->rax);
10075 kvm_rbx_write(vcpu, regs->rbx);
10076 kvm_rcx_write(vcpu, regs->rcx);
10077 kvm_rdx_write(vcpu, regs->rdx);
10078 kvm_rsi_write(vcpu, regs->rsi);
10079 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +020010080 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -070010081 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010082#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -070010083 kvm_r8_write(vcpu, regs->r8);
10084 kvm_r9_write(vcpu, regs->r9);
10085 kvm_r10_write(vcpu, regs->r10);
10086 kvm_r11_write(vcpu, regs->r11);
10087 kvm_r12_write(vcpu, regs->r12);
10088 kvm_r13_write(vcpu, regs->r13);
10089 kvm_r14_write(vcpu, regs->r14);
10090 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010091#endif
10092
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010093 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -080010094 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010095
Jan Kiszkab4f14ab2008-04-30 17:59:04 +020010096 vcpu->arch.exception.pending = false;
10097
Avi Kivity3842d132010-07-27 12:30:24 +030010098 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -080010099}
Avi Kivity3842d132010-07-27 12:30:24 +030010100
Ken Hofsass01643c52018-01-31 16:03:36 -080010101int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
10102{
10103 vcpu_load(vcpu);
10104 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +010010105 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010106 return 0;
10107}
10108
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010109void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
10110{
10111 struct kvm_segment cs;
10112
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +020010113 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010114 *db = cs.db;
10115 *l = cs.l;
10116}
10117EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
10118
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010119static void __get_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010120{
Gleb Natapov89a27f42010-02-16 10:51:48 +020010121 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010122
Tom Lendacky52657132020-12-10 11:09:59 -060010123 if (vcpu->arch.guest_state_protected)
10124 goto skip_protected_regs;
10125
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +020010126 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
10127 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
10128 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
10129 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
10130 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
10131 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010132
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +020010133 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
10134 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010135
Jason Baronb36464772021-01-14 22:27:56 -050010136 static_call(kvm_x86_get_idt)(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +020010137 sregs->idt.limit = dt.size;
10138 sregs->idt.base = dt.address;
Jason Baronb36464772021-01-14 22:27:56 -050010139 static_call(kvm_x86_get_gdt)(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +020010140 sregs->gdt.limit = dt.size;
10141 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010142
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010143 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +020010144 sregs->cr3 = kvm_read_cr3(vcpu);
Tom Lendacky52657132020-12-10 11:09:59 -060010145
10146skip_protected_regs:
10147 sregs->cr0 = kvm_read_cr0(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +020010148 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +020010149 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +020010150 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010151 sregs->apic_base = kvm_get_apic_base(vcpu);
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010152}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010153
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010154static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
10155{
10156 __get_sregs_common(vcpu, sregs);
10157
10158 if (vcpu->arch.guest_state_protected)
10159 return;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010160
Liran Alon04140b42018-03-23 03:01:31 +030010161 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +030010162 set_bit(vcpu->arch.interrupt.nr,
10163 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -080010164}
Gleb Natapov16d7a192009-04-21 17:45:10 +030010165
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010166static void __get_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2)
10167{
10168 int i;
10169
10170 __get_sregs_common(vcpu, (struct kvm_sregs *)sregs2);
10171
10172 if (vcpu->arch.guest_state_protected)
10173 return;
10174
10175 if (is_pae_paging(vcpu)) {
10176 for (i = 0 ; i < 4 ; i++)
10177 sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i);
10178 sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID;
10179 }
10180}
10181
Ken Hofsass01643c52018-01-31 16:03:36 -080010182int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
10183 struct kvm_sregs *sregs)
10184{
10185 vcpu_load(vcpu);
10186 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +010010187 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010188 return 0;
10189}
10190
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -030010191int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
10192 struct kvm_mp_state *mp_state)
10193{
Jim Mattson4fe09bc2021-06-04 10:26:04 -070010194 int r;
10195
Christoffer Dallfd232562017-12-04 21:35:30 +010010196 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -080010197 if (kvm_mpx_supported())
10198 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +010010199
Jim Mattson4fe09bc2021-06-04 10:26:04 -070010200 r = kvm_apic_accept_events(vcpu);
10201 if (r < 0)
10202 goto out;
10203 r = 0;
10204
Tom Lendacky647daca2021-01-04 14:20:01 -060010205 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED ||
10206 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) &&
10207 vcpu->arch.pv.pv_unhalted)
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +053010208 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
10209 else
10210 mp_state->mp_state = vcpu->arch.mp_state;
10211
Jim Mattson4fe09bc2021-06-04 10:26:04 -070010212out:
Sean Christophersonf958bd22019-12-09 12:19:31 -080010213 if (kvm_mpx_supported())
10214 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +010010215 vcpu_put(vcpu);
Jim Mattson4fe09bc2021-06-04 10:26:04 -070010216 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -030010217}
10218
10219int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
10220 struct kvm_mp_state *mp_state)
10221{
Christoffer Dalle83dff52017-12-04 21:35:31 +010010222 int ret = -EINVAL;
10223
10224 vcpu_load(vcpu);
10225
Paolo Bonzinibce87cc2016-01-08 13:48:51 +010010226 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +010010227 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +010010228 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +010010229
Liran Alon27cbe7d2019-11-11 11:16:40 +020010230 /*
10231 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
10232 * INIT state; latched init should be reported using
10233 * KVM_SET_VCPU_EVENTS, so reject it here.
10234 */
10235 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +010010236 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
10237 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +010010238 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +010010239
Jan Kiszka66450a22013-03-13 12:42:34 +010010240 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
10241 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
10242 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
10243 } else
10244 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +030010245 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +010010246
10247 ret = 0;
10248out:
10249 vcpu_put(vcpu);
10250 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -030010251}
10252
Kevin Wolf7f3d35f2012-02-08 14:34:38 +010010253int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
10254 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010255{
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010256 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +030010257 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010258
Mohammed Gamal8ec47222010-08-16 00:47:01 +030010259 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +020010260
Kevin Wolf7f3d35f2012-02-08 14:34:38 +010010261 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +090010262 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -070010263 if (ret) {
10264 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10265 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
10266 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -070010267 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -070010268 }
Gleb Natapovc6975182010-02-18 12:15:01 +020010269
Takuya Yoshikawa9d741912011-05-29 21:53:48 +090010270 kvm_rip_write(vcpu, ctxt->eip);
10271 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -070010272 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +020010273}
10274EXPORT_SYMBOL_GPL(kvm_task_switch);
10275
Sean Christophersonee69c922020-10-06 18:44:16 -070010276static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -050010277{
Tianyu Lan37b95952018-01-16 17:34:07 +080010278 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -050010279 /*
10280 * When EFER.LME and CR0.PG are set, the processor is in
10281 * 64-bit mode (though maybe in a 32-bit code segment).
10282 * CR4.PAE and EFER.LMA must be set.
10283 */
Sean Christophersonee69c922020-10-06 18:44:16 -070010284 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA))
10285 return false;
Sean Christophersonca29e142021-02-03 16:01:12 -080010286 if (kvm_vcpu_is_illegal_gpa(vcpu, sregs->cr3))
Paolo Bonzinic1c35cf2020-11-13 08:30:38 -050010287 return false;
Lan Tianyuf2981032017-12-14 03:01:52 -050010288 } else {
10289 /*
10290 * Not in 64-bit mode: EFER.LMA is clear and the code
10291 * segment cannot be 64-bit.
10292 */
10293 if (sregs->efer & EFER_LMA || sregs->cs.l)
Sean Christophersonee69c922020-10-06 18:44:16 -070010294 return false;
Lan Tianyuf2981032017-12-14 03:01:52 -050010295 }
10296
Sean Christophersonee69c922020-10-06 18:44:16 -070010297 return kvm_is_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -050010298}
10299
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010300static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,
10301 int *mmu_reset_needed, bool update_pdptrs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010302{
Jan Kiszka58cb6282014-01-24 16:48:44 +010010303 struct msr_data apic_base_msr;
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010304 int idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +020010305 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +010010306
Sean Christophersonee69c922020-10-06 18:44:16 -070010307 if (!kvm_is_valid_sregs(vcpu, sregs))
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010308 return -EINVAL;
Lan Tianyuf2981032017-12-14 03:01:52 -050010309
Jim Mattsond3802282017-08-10 10:14:13 -070010310 apic_base_msr.data = sregs->apic_base;
10311 apic_base_msr.host_initiated = true;
10312 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010313 return -EINVAL;
Petr Matousek6d1068b2012-11-06 19:24:07 +010010314
Tom Lendacky52657132020-12-10 11:09:59 -060010315 if (vcpu->arch.guest_state_protected)
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010316 return 0;
Tom Lendacky52657132020-12-10 11:09:59 -060010317
Gleb Natapov89a27f42010-02-16 10:51:48 +020010318 dt.size = sregs->idt.limit;
10319 dt.address = sregs->idt.base;
Jason Baronb36464772021-01-14 22:27:56 -050010320 static_call(kvm_x86_set_idt)(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +020010321 dt.size = sregs->gdt.limit;
10322 dt.address = sregs->gdt.base;
Jason Baronb36464772021-01-14 22:27:56 -050010323 static_call(kvm_x86_set_gdt)(vcpu, &dt);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010324
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010325 vcpu->arch.cr2 = sregs->cr2;
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010326 *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +020010327 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -070010328 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010329
Avi Kivity2d3ad1f2008-02-24 11:20:43 +020010330 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010331
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010332 *mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Jason Baronb36464772021-01-14 22:27:56 -050010333 static_call(kvm_x86_set_efer)(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010334
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010335 *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Jason Baronb36464772021-01-14 22:27:56 -050010336 static_call(kvm_x86_set_cr0)(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +000010337 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010338
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010339 *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Jason Baronb36464772021-01-14 22:27:56 -050010340 static_call(kvm_x86_set_cr4)(vcpu, sregs->cr4);
Xiao Guangrong63f42e02011-01-12 15:39:18 +080010341
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010342 if (update_pdptrs) {
10343 idx = srcu_read_lock(&vcpu->kvm->srcu);
10344 if (is_pae_paging(vcpu)) {
10345 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
10346 *mmu_reset_needed = 1;
10347 }
10348 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -020010349 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010350
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +020010351 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
10352 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
10353 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
10354 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
10355 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
10356 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010357
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +020010358 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
10359 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010360
Mikhail Ershov5f0269f2009-08-03 14:58:25 +030010361 update_cr8_intercept(vcpu);
10362
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -030010363 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +030010364 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -030010365 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +020010366 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -030010367 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
10368
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010369 return 0;
10370}
10371
10372static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
10373{
10374 int pending_vec, max_bits;
10375 int mmu_reset_needed = 0;
10376 int ret = __set_sregs_common(vcpu, sregs, &mmu_reset_needed, true);
10377
10378 if (ret)
10379 return ret;
10380
10381 if (mmu_reset_needed)
10382 kvm_mmu_reset_context(vcpu);
10383
Tom Lendacky52657132020-12-10 11:09:59 -060010384 max_bits = KVM_NR_INTERRUPTS;
10385 pending_vec = find_first_bit(
10386 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010387
Tom Lendacky52657132020-12-10 11:09:59 -060010388 if (pending_vec < max_bits) {
10389 kvm_queue_interrupt(vcpu, pending_vec, false);
10390 pr_debug("Set back pending irq %d\n", pending_vec);
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010391 kvm_make_request(KVM_REQ_EVENT, vcpu);
Tom Lendacky52657132020-12-10 11:09:59 -060010392 }
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010393 return 0;
10394}
Tom Lendacky52657132020-12-10 11:09:59 -060010395
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010396static int __set_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2)
10397{
10398 int mmu_reset_needed = 0;
10399 bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID;
10400 bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) &&
10401 !(sregs2->efer & EFER_LMA);
10402 int i, ret;
Avi Kivity3842d132010-07-27 12:30:24 +030010403
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010404 if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID)
10405 return -EINVAL;
10406
10407 if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected))
10408 return -EINVAL;
10409
10410 ret = __set_sregs_common(vcpu, (struct kvm_sregs *)sregs2,
10411 &mmu_reset_needed, !valid_pdptrs);
10412 if (ret)
10413 return ret;
10414
10415 if (valid_pdptrs) {
10416 for (i = 0; i < 4 ; i++)
10417 kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]);
10418
10419 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
10420 mmu_reset_needed = 1;
Maxim Levitsky158a48e2021-06-07 12:02:03 +030010421 vcpu->arch.pdptrs_from_userspace = true;
Maxim Levitsky6dba9402021-06-07 12:02:02 +030010422 }
10423 if (mmu_reset_needed)
10424 kvm_mmu_reset_context(vcpu);
10425 return 0;
Ken Hofsass01643c52018-01-31 16:03:36 -080010426}
10427
10428int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
10429 struct kvm_sregs *sregs)
10430{
10431 int ret;
10432
10433 vcpu_load(vcpu);
10434 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +010010435 vcpu_put(vcpu);
10436 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010437}
10438
Jan Kiszkad0bfb942008-12-15 13:52:10 +010010439int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
10440 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010441{
Jan Kiszka355be0b2009-10-03 00:31:21 +020010442 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010443 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010444
Tom Lendacky8d4846b2020-12-10 11:09:43 -060010445 if (vcpu->arch.guest_state_protected)
10446 return -EINVAL;
10447
Christoffer Dall66b56562017-12-04 21:35:33 +010010448 vcpu_load(vcpu);
10449
Jan Kiszka4f926bf22009-10-30 12:46:59 +010010450 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
10451 r = -EBUSY;
10452 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +030010453 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +010010454 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
10455 kvm_queue_exception(vcpu, DB_VECTOR);
10456 else
10457 kvm_queue_exception(vcpu, BP_VECTOR);
10458 }
10459
Jan Kiszka91586a32009-10-05 13:07:21 +020010460 /*
10461 * Read rflags as long as potentially injected trace flags are still
10462 * filtered out.
10463 */
10464 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +020010465
10466 vcpu->guest_debug = dbg->control;
10467 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
10468 vcpu->guest_debug = 0;
10469
10470 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010471 for (i = 0; i < KVM_NR_DB_REGS; ++i)
10472 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +020010473 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010474 } else {
10475 for (i = 0; i < KVM_NR_DB_REGS; i++)
10476 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010477 }
Jan Kiszkac8639012012-09-21 05:42:55 +020010478 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010479
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010480 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Yuan Yaoe87e46d2021-05-26 14:38:28 +080010481 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010482
Jan Kiszka91586a32009-10-05 13:07:21 +020010483 /*
10484 * Trigger an rflags update that will inject or remove the trace
10485 * flags.
10486 */
10487 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +010010488
Jason Baronb36464772021-01-14 22:27:56 -050010489 static_call(kvm_x86_update_exception_bitmap)(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +020010490
Jan Kiszka4f926bf22009-10-30 12:46:59 +010010491 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010492
Avi Kivity2122ff52010-05-13 11:25:04 +030010493out:
Christoffer Dall66b56562017-12-04 21:35:33 +010010494 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010495 return r;
10496}
10497
10498/*
Zhang Xiantao8b006792007-11-16 13:05:55 +080010499 * Translate a guest virtual address to a guest physical address.
10500 */
10501int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
10502 struct kvm_translation *tr)
10503{
10504 unsigned long vaddr = tr->linear_address;
10505 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -020010506 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +080010507
Christoffer Dall1da5b612017-12-04 21:35:32 +010010508 vcpu_load(vcpu);
10509
Marcelo Tosattif656ce02009-12-23 14:35:25 -020010510 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +020010511 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -020010512 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +080010513 tr->physical_address = gpa;
10514 tr->valid = gpa != UNMAPPED_GVA;
10515 tr->writeable = 1;
10516 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +080010517
Christoffer Dall1da5b612017-12-04 21:35:32 +010010518 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +080010519 return 0;
10520}
10521
Hollis Blanchardd0752062007-10-31 17:24:25 -050010522int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
10523{
Christoffer Dall13931232017-12-04 21:35:34 +010010524 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010525
Tom Lendackyed02b212020-12-10 11:10:01 -060010526 if (!vcpu->arch.guest_fpu)
10527 return 0;
10528
Christoffer Dall13931232017-12-04 21:35:34 +010010529 vcpu_load(vcpu);
10530
Marc Orrb666a4b2018-11-06 14:53:56 -080010531 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010532 memcpy(fpu->fpr, fxsave->st_space, 128);
10533 fpu->fcw = fxsave->cwd;
10534 fpu->fsw = fxsave->swd;
10535 fpu->ftwx = fxsave->twd;
10536 fpu->last_opcode = fxsave->fop;
10537 fpu->last_ip = fxsave->rip;
10538 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +000010539 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -050010540
Christoffer Dall13931232017-12-04 21:35:34 +010010541 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -050010542 return 0;
10543}
10544
10545int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
10546{
Christoffer Dall6a96bc72017-12-04 21:35:35 +010010547 struct fxregs_state *fxsave;
10548
Tom Lendackyed02b212020-12-10 11:10:01 -060010549 if (!vcpu->arch.guest_fpu)
10550 return 0;
10551
Christoffer Dall6a96bc72017-12-04 21:35:35 +010010552 vcpu_load(vcpu);
10553
Marc Orrb666a4b2018-11-06 14:53:56 -080010554 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010555
Hollis Blanchardd0752062007-10-31 17:24:25 -050010556 memcpy(fxsave->st_space, fpu->fpr, 128);
10557 fxsave->cwd = fpu->fcw;
10558 fxsave->swd = fpu->fsw;
10559 fxsave->twd = fpu->ftwx;
10560 fxsave->fop = fpu->last_opcode;
10561 fxsave->rip = fpu->last_ip;
10562 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +000010563 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -050010564
Christoffer Dall6a96bc72017-12-04 21:35:35 +010010565 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -050010566 return 0;
10567}
10568
Ken Hofsass01643c52018-01-31 16:03:36 -080010569static void store_regs(struct kvm_vcpu *vcpu)
10570{
10571 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
10572
10573 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
10574 __get_regs(vcpu, &vcpu->run->s.regs.regs);
10575
10576 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
10577 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
10578
10579 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
10580 kvm_vcpu_ioctl_x86_get_vcpu_events(
10581 vcpu, &vcpu->run->s.regs.events);
10582}
10583
10584static int sync_regs(struct kvm_vcpu *vcpu)
10585{
Ken Hofsass01643c52018-01-31 16:03:36 -080010586 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
10587 __set_regs(vcpu, &vcpu->run->s.regs.regs);
10588 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
10589 }
10590 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
10591 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
10592 return -EINVAL;
10593 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
10594 }
10595 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
10596 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
10597 vcpu, &vcpu->run->s.regs.events))
10598 return -EINVAL;
10599 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
10600 }
10601
10602 return 0;
10603}
10604
Ingo Molnar0ee6a512015-04-27 06:58:22 +020010605static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -050010606{
Tom Lendackyed02b212020-12-10 11:10:01 -060010607 if (!vcpu->arch.guest_fpu)
10608 return;
10609
Marc Orrb666a4b2018-11-06 14:53:56 -080010610 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +020010611 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -080010612 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +010010613 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010614
Dexuan Cui2acf9232010-06-10 11:27:12 +080010615 /*
10616 * Ensure guest xcr0 is valid for loading
10617 */
Dave Hansend91cab72015-09-02 16:31:26 -070010618 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +080010619
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010620 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010621}
Hollis Blanchardd0752062007-10-31 17:24:25 -050010622
Tom Lendackyed02b212020-12-10 11:10:01 -060010623void kvm_free_guest_fpu(struct kvm_vcpu *vcpu)
10624{
10625 if (vcpu->arch.guest_fpu) {
10626 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
10627 vcpu->arch.guest_fpu = NULL;
10628 }
10629}
10630EXPORT_SYMBOL_GPL(kvm_free_guest_fpu);
10631
Sean Christopherson897cc382019-12-18 13:55:09 -080010632int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010633{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010010634 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -080010635 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
10636 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +080010637
Sean Christopherson897cc382019-12-18 13:55:09 -080010638 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +020010639}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010640
Sean Christophersone529ef62019-12-18 13:55:15 -080010641int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +020010642{
Sean Christopherson95a0d012019-12-18 13:55:23 -080010643 struct page *page;
10644 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010645
Sean Christopherson63f5a192021-06-22 10:56:52 -070010646 vcpu->arch.last_vmentry_cpu = -1;
10647
Sean Christopherson95a0d012019-12-18 13:55:23 -080010648 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
10649 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
10650 else
10651 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
10652
Sean Christopherson95a0d012019-12-18 13:55:23 -080010653 r = kvm_mmu_create(vcpu);
10654 if (r < 0)
10655 return r;
10656
10657 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -080010658 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
10659 if (r < 0)
10660 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -060010661 if (kvm_apicv_activated(vcpu->kvm))
10662 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010663 } else
Cun Li6e4e3b42021-01-11 23:24:35 +080010664 static_branch_inc(&kvm_has_noapic_vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010665
10666 r = -ENOMEM;
10667
Shakeel Butt93bb59c2020-12-18 14:01:38 -080010668 page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010669 if (!page)
10670 goto fail_free_lapic;
10671 vcpu->arch.pio_data = page_address(page);
10672
10673 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
10674 GFP_KERNEL_ACCOUNT);
10675 if (!vcpu->arch.mce_banks)
10676 goto fail_free_pio_data;
10677 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
10678
10679 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
10680 GFP_KERNEL_ACCOUNT))
10681 goto fail_free_mce_banks;
10682
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010683 if (!alloc_emulate_ctxt(vcpu))
10684 goto free_wbinvd_dirty_mask;
10685
Sean Christopherson95a0d012019-12-18 13:55:23 -080010686 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
10687 GFP_KERNEL_ACCOUNT);
10688 if (!vcpu->arch.user_fpu) {
10689 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010690 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010691 }
10692
10693 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
10694 GFP_KERNEL_ACCOUNT);
10695 if (!vcpu->arch.guest_fpu) {
10696 pr_err("kvm: failed to allocate vcpu's fpu\n");
10697 goto free_user_fpu;
10698 }
10699 fx_init(vcpu);
10700
Sean Christopherson95a0d012019-12-18 13:55:23 -080010701 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Sean Christophersona8ac8642021-02-03 16:01:15 -080010702 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010703
10704 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
10705
10706 kvm_async_pf_hash_reset(vcpu);
10707 kvm_pmu_init(vcpu);
10708
10709 vcpu->arch.pending_external_vector = -1;
10710 vcpu->arch.preempted_in_kernel = false;
10711
Vineeth Pillai3c86c0d2021-06-03 15:14:36 +000010712#if IS_ENABLED(CONFIG_HYPERV)
10713 vcpu->arch.hv_root_tdp = INVALID_PAGE;
10714#endif
10715
Jason Baronb36464772021-01-14 22:27:56 -050010716 r = static_call(kvm_x86_vcpu_create)(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010717 if (r)
10718 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -080010719
Sean Christopherson0cf91352019-03-07 15:43:02 -080010720 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -070010721 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +080010722 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +010010723 vcpu_load(vcpu);
Ilias Stamatis1ab92872021-06-07 11:54:38 +010010724 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010725 kvm_vcpu_reset(vcpu, false);
Sean Christophersonc9060662021-06-09 16:42:33 -070010726 kvm_init_mmu(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010727 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +010010728 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010729
10730free_guest_fpu:
Tom Lendackyed02b212020-12-10 11:10:01 -060010731 kvm_free_guest_fpu(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010732free_user_fpu:
10733 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010734free_emulate_ctxt:
10735 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010736free_wbinvd_dirty_mask:
10737 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
10738fail_free_mce_banks:
10739 kfree(vcpu->arch.mce_banks);
10740fail_free_pio_data:
10741 free_page((unsigned long)vcpu->arch.pio_data);
10742fail_free_lapic:
10743 kvm_free_lapic(vcpu);
10744fail_mmu_destroy:
10745 kvm_mmu_destroy(vcpu);
10746 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010747}
10748
Dominik Dingel31928aa2014-12-04 15:47:07 +010010749void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010750{
Andrew Jones332967a2014-02-28 12:52:55 +010010751 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010752
Christoffer Dallec7660c2017-12-04 21:35:23 +010010753 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +010010754 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +010010755 vcpu_load(vcpu);
Paolo Bonzini0c899c22020-09-24 14:45:27 +020010756 kvm_synchronize_tsc(vcpu, 0);
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010757 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010758
10759 /* poll control enabled by default */
10760 vcpu->arch.msr_kvm_poll_control = 1;
10761
Christoffer Dallec7660c2017-12-04 21:35:23 +010010762 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010763
Wanpeng Lib34de572020-02-28 11:18:41 +080010764 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
10765 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
10766 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010767}
10768
Hollis Blanchardd40ccc62007-11-19 14:04:43 -060010769void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010770{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +010010771 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010772 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +020010773
Paolo Bonzini4cbc4182020-01-30 18:47:38 +010010774 kvm_release_pfn(cache->pfn, cache->dirty, cache);
10775
Sean Christopherson50b143e2019-12-18 13:55:07 -080010776 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010777
Jason Baronb36464772021-01-14 22:27:56 -050010778 static_call(kvm_x86_vcpu_free)(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -080010779
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010780 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -080010781 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
10782 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Tom Lendackyed02b212020-12-10 11:10:01 -060010783 kvm_free_guest_fpu(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010784
10785 kvm_hv_vcpu_uninit(vcpu);
10786 kvm_pmu_destroy(vcpu);
10787 kfree(vcpu->arch.mce_banks);
10788 kvm_free_lapic(vcpu);
10789 idx = srcu_read_lock(&vcpu->kvm->srcu);
10790 kvm_mmu_destroy(vcpu);
10791 srcu_read_unlock(&vcpu->kvm->srcu, idx);
10792 free_page((unsigned long)vcpu->arch.pio_data);
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +020010793 kvfree(vcpu->arch.cpuid_entries);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010794 if (!lapic_in_kernel(vcpu))
Cun Li6e4e3b42021-01-11 23:24:35 +080010795 static_branch_dec(&kvm_has_noapic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010796}
10797
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010798void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010799{
Sean Christopherson0aa18372021-06-22 10:56:48 -070010800 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Sean Christopherson4c72ab52021-07-13 09:33:24 -070010801 unsigned long new_cr0;
Sean Christopherson49d86652021-07-13 09:32:57 -070010802 u32 eax, dummy;
Sean Christopherson0aa18372021-06-22 10:56:48 -070010803
Radim Krčmářb7e31be2018-03-01 15:24:25 +010010804 kvm_lapic_reset(vcpu, init_event);
10805
Paolo Bonzinie69fab52015-06-04 10:44:44 +020010806 vcpu->arch.hflags = 0;
10807
Paolo Bonzinic43203c2016-06-01 22:26:00 +020010808 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +020010809 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +030010810 atomic_set(&vcpu->arch.nmi_queued, 0);
10811 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +020010812 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +030010813 kvm_clear_interrupt_queue(vcpu);
10814 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +020010815
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010816 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +030010817 kvm_update_dr0123(vcpu);
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +080010818 vcpu->arch.dr6 = DR6_ACTIVE_LOW;
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010819 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +020010820 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010821
Nadav Amit11190222015-04-02 03:10:38 +030010822 vcpu->arch.cr2 = 0;
10823
Avi Kivity3842d132010-07-27 12:30:24 +030010824 kvm_make_request(KVM_REQ_EVENT, vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010825 vcpu->arch.apf.msr_en_val = 0;
10826 vcpu->arch.apf.msr_int_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -040010827 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +030010828
Glauber Costa12f9a482011-02-01 14:16:40 -050010829 kvmclock_reset(vcpu);
10830
Gleb Natapovaf585b92010-10-14 11:22:46 +020010831 kvm_clear_async_pf_completion_queue(vcpu);
10832 kvm_async_pf_hash_reset(vcpu);
10833 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010834
Tom Lendackyed02b212020-12-10 11:10:01 -060010835 if (vcpu->arch.guest_fpu && kvm_mpx_supported()) {
Wanpeng Lia554d202017-10-11 05:10:19 -070010836 void *mpx_state_buffer;
10837
10838 /*
10839 * To avoid have the INIT path from kvm_apic_has_events() that be
10840 * called with loaded FPU and does not let userspace fix the state.
10841 */
Rik van Rielf775b132017-11-14 16:54:23 -050010842 if (init_event)
10843 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -080010844 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +020010845 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -070010846 if (mpx_state_buffer)
10847 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -080010848 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +020010849 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -070010850 if (mpx_state_buffer)
10851 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -050010852 if (init_event)
10853 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -070010854 }
10855
Paolo Bonzini64d60672015-05-07 11:36:11 +020010856 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010857 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +020010858 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -070010859
Kyle Hueydb2336a2017-03-20 01:16:28 -070010860 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -070010861
10862 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +020010863 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020010864
Julian Stecklina66f7b722012-12-05 15:26:19 +010010865 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
10866 vcpu->arch.regs_avail = ~0;
10867 vcpu->arch.regs_dirty = ~0;
10868
Sean Christopherson49d86652021-07-13 09:32:57 -070010869 /*
10870 * Fall back to KVM's default Family/Model/Stepping of 0x600 (P6/Athlon)
10871 * if no CPUID match is found. Note, it's impossible to get a match at
10872 * RESET since KVM emulates RESET before exposing the vCPU to userspace,
10873 * i.e. it'simpossible for kvm_cpuid() to find a valid entry on RESET.
10874 * But, go through the motions in case that's ever remedied.
10875 */
10876 eax = 1;
10877 if (!kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true))
10878 eax = 0x600;
10879 kvm_rdx_write(vcpu, eax);
10880
Wanpeng Lia554d202017-10-11 05:10:19 -070010881 vcpu->arch.ia32_xss = 0;
10882
Jason Baronb36464772021-01-14 22:27:56 -050010883 static_call(kvm_x86_vcpu_reset)(vcpu, init_event);
Sean Christopherson0aa18372021-06-22 10:56:48 -070010884
Sean Christophersonf39e8052021-07-13 09:33:14 -070010885 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
10886 kvm_rip_write(vcpu, 0xfff0);
10887
Sean Christopherson4c72ab52021-07-13 09:33:24 -070010888 /*
10889 * CR0.CD/NW are set on RESET, preserved on INIT. Note, some versions
10890 * of Intel's SDM list CD/NW as being set on INIT, but they contradict
10891 * (or qualify) that with a footnote stating that CD/NW are preserved.
10892 */
10893 new_cr0 = X86_CR0_ET;
10894 if (init_event)
10895 new_cr0 |= (old_cr0 & (X86_CR0_NW | X86_CR0_CD));
10896 else
10897 new_cr0 |= X86_CR0_NW | X86_CR0_CD;
10898
10899 static_call(kvm_x86_set_cr0)(vcpu, new_cr0);
Sean Christophersonf39e8052021-07-13 09:33:14 -070010900 static_call(kvm_x86_set_cr4)(vcpu, 0);
10901 static_call(kvm_x86_set_efer)(vcpu, 0);
10902 static_call(kvm_x86_update_exception_bitmap)(vcpu);
10903
Sean Christopherson0aa18372021-06-22 10:56:48 -070010904 /*
10905 * Reset the MMU context if paging was enabled prior to INIT (which is
10906 * implied if CR0.PG=1 as CR0 will be '0' prior to RESET). Unlike the
10907 * standard CR0/CR4/EFER modification paths, only CR0.PG needs to be
10908 * checked because it is unconditionally cleared on INIT and all other
10909 * paging related bits are ignored if paging is disabled, i.e. CR0.WP,
10910 * CR4, and EFER changes are all irrelevant if CR0.PG was '0'.
10911 */
10912 if (old_cr0 & X86_CR0_PG)
10913 kvm_mmu_reset_context(vcpu);
Sean Christophersondf37ed32021-07-13 09:32:39 -070010914
10915 /*
10916 * Intel's SDM states that all TLB entries are flushed on INIT. AMD's
10917 * APM states the TLBs are untouched by INIT, but it also states that
10918 * the TLBs are flushed on "External initialization of the processor."
10919 * Flush the guest TLB regardless of vendor, there is no meaningful
10920 * benefit in relying on the guest to flush the TLB immediately after
10921 * INIT. A spurious TLB flush is benign and likely negligible from a
10922 * performance perspective.
10923 */
10924 if (init_event)
10925 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010926}
Sean Christopherson265e4352021-07-13 09:33:22 -070010927EXPORT_SYMBOL_GPL(kvm_vcpu_reset);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010928
Paolo Bonzini2b4a2732014-11-24 14:35:24 +010010929void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +010010930{
10931 struct kvm_segment cs;
10932
10933 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
10934 cs.selector = vector << 8;
10935 cs.base = vector << 12;
10936 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
10937 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010938}
Tom Lendacky647daca2021-01-04 14:20:01 -060010939EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010940
Radim Krčmář13a34e02014-08-28 15:13:03 +020010941int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010942{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -100010943 struct kvm *kvm;
10944 struct kvm_vcpu *vcpu;
10945 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010946 int ret;
10947 u64 local_tsc;
10948 u64 max_tsc = 0;
10949 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +030010950
Sean Christopherson7e34fbd2020-09-23 11:03:55 -070010951 kvm_user_return_msr_cpu_online();
Jason Baronb36464772021-01-14 22:27:56 -050010952 ret = static_call(kvm_x86_hardware_enable)();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010953 if (ret != 0)
10954 return ret;
10955
Andy Lutomirski4ea16362015-06-25 18:44:07 +020010956 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010010957 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010958 list_for_each_entry(kvm, &vm_list, vm_list) {
10959 kvm_for_each_vcpu(i, vcpu, kvm) {
10960 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +080010961 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010962 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
10963 backwards_tsc = true;
10964 if (vcpu->arch.last_host_tsc > max_tsc)
10965 max_tsc = vcpu->arch.last_host_tsc;
10966 }
10967 }
10968 }
10969
10970 /*
10971 * Sometimes, even reliable TSCs go backwards. This happens on
10972 * platforms that reset TSC during suspend or hibernate actions, but
10973 * maintain synchronization. We must compensate. Fortunately, we can
10974 * detect that condition here, which happens early in CPU bringup,
10975 * before any KVM threads can be running. Unfortunately, we can't
10976 * bring the TSCs fully up to date with real time, as we aren't yet far
10977 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020010978 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010979 * variables that haven't been updated yet.
10980 *
10981 * So we simply find the maximum observed TSC above, then record the
10982 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
10983 * the adjustment will be applied. Note that we accumulate
10984 * adjustments, in case multiple suspend cycles happen before some VCPU
10985 * gets a chance to run again. In the event that no KVM threads get a
10986 * chance to run, we will miss the entire elapsed period, as we'll have
10987 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
10988 * loose cycle time. This isn't too big a deal, since the loss will be
10989 * uniform across all VCPUs (not to mention the scenario is extremely
10990 * unlikely). It is possible that a second hibernate recovery happens
10991 * much faster than a first, causing the observed TSC here to be
10992 * smaller; this would require additional padding adjustment, which is
10993 * why we set last_host_tsc to the local tsc observed here.
10994 *
10995 * N.B. - this code below runs only on platforms with reliable TSC,
10996 * as that is the only way backwards_tsc is set above. Also note
10997 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
10998 * have the same delta_cyc adjustment applied if backwards_tsc
10999 * is detected. Note further, this adjustment is only done once,
11000 * as we reset last_host_tsc on all VCPUs to stop this from being
11001 * called multiple times (one for each physical CPU bringup).
11002 *
Guo Chao4a969982012-06-28 15:17:27 +080011003 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020011004 * will be compensated by the logic in vcpu_load, which sets the TSC to
11005 * catchup mode. This will catchup all VCPUs to real time, but cannot
11006 * guarantee that they stay in perfect synchronization.
11007 */
11008 if (backwards_tsc) {
11009 u64 delta_cyc = max_tsc - local_tsc;
11010 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +020011011 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020011012 kvm_for_each_vcpu(i, vcpu, kvm) {
11013 vcpu->arch.tsc_offset_adjustment += delta_cyc;
11014 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +080011015 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020011016 }
11017
11018 /*
11019 * We have to disable TSC offset matching.. if you were
11020 * booting a VM while issuing an S4 host suspend....
11021 * you may have some problem. Solving this issue is
11022 * left as an exercise to the reader.
11023 */
11024 kvm->arch.last_tsc_nsec = 0;
11025 kvm->arch.last_tsc_write = 0;
11026 }
11027
11028 }
11029 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080011030}
11031
Radim Krčmář13a34e02014-08-28 15:13:03 +020011032void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080011033{
Jason Baronb36464772021-01-14 22:27:56 -050011034 static_call(kvm_x86_hardware_disable)();
Radim Krčmář13a34e02014-08-28 15:13:03 +020011035 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080011036}
11037
Sean Christophersonb9904082020-03-21 13:25:55 -070011038int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080011039{
Sean Christophersond008dfd2020-03-21 13:25:56 -070011040 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030011041 int r;
11042
Sean Christopherson91661982020-03-02 15:57:06 -080011043 rdmsrl_safe(MSR_EFER, &host_efer);
11044
Paolo Bonzini408e9a32020-03-05 16:11:56 +010011045 if (boot_cpu_has(X86_FEATURE_XSAVES))
11046 rdmsrl(MSR_IA32_XSS, host_xss);
11047
Sean Christophersond008dfd2020-03-21 13:25:56 -070011048 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030011049 if (r != 0)
11050 return r;
11051
Sean Christophersonafaf0b22020-03-21 13:26:00 -070011052 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
Jason Baronb36464772021-01-14 22:27:56 -050011053 kvm_ops_static_call_update();
Sean Christopherson69c6f692020-03-21 13:25:59 -070011054
Paolo Bonzini408e9a32020-03-05 16:11:56 +010011055 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
11056 supported_xss = 0;
11057
Paolo Bonzini139f7422020-05-05 09:40:46 -040011058#define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f)
11059 cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_);
11060#undef __kvm_cpu_cap_has
Sean Christophersonb11306b2019-12-10 14:44:13 -080011061
Haozhong Zhang35181e82015-10-20 15:39:03 +080011062 if (kvm_has_tsc_control) {
11063 /*
11064 * Make sure the user can only configure tsc_khz values that
11065 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -030011066 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +080011067 * be 1 on all machines.
11068 */
11069 u64 max = min(0x7fffffffULL,
11070 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
11071 kvm_max_guest_tsc_khz = max;
11072
Haozhong Zhangad7218832015-10-20 15:39:02 +080011073 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +080011074 }
Haozhong Zhangad7218832015-10-20 15:39:02 +080011075
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030011076 kvm_init_msr_list();
11077 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080011078}
11079
11080void kvm_arch_hardware_unsetup(void)
11081{
Jason Baronb36464772021-01-14 22:27:56 -050011082 static_call(kvm_x86_hardware_unsetup)();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080011083}
11084
Sean Christophersonb9904082020-03-21 13:25:55 -070011085int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080011086{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080011087 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -070011088 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080011089
11090 WARN_ON(!irqs_disabled());
11091
Paolo Bonzini139f7422020-05-05 09:40:46 -040011092 if (__cr4_reserved_bits(cpu_has, c) !=
11093 __cr4_reserved_bits(cpu_has, &boot_cpu_data))
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080011094 return -EIO;
11095
Sean Christophersond008dfd2020-03-21 13:25:56 -070011096 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080011097}
11098
Paolo Bonzinid71ba782015-07-29 11:56:48 +020011099bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
11100{
11101 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
11102}
11103EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
11104
11105bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
11106{
11107 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
11108}
11109
Cun Li6e4e3b42021-01-11 23:24:35 +080011110__read_mostly DEFINE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu);
11111EXPORT_SYMBOL_GPL(kvm_has_noapic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +030011112
Radim Krčmáře790d9e2014-08-21 18:08:05 +020011113void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
11114{
Like Xub35e5542019-10-27 18:52:43 +080011115 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
11116
Paolo Bonzinic595cee2018-07-02 13:07:14 +020011117 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +080011118 if (pmu->version && unlikely(pmu->event_count)) {
11119 pmu->need_cleanup = true;
11120 kvm_make_request(KVM_REQ_PMU, vcpu);
11121 }
Jason Baronb36464772021-01-14 22:27:56 -050011122 static_call(kvm_x86_sched_in)(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +020011123}
11124
Sean Christopherson562b6b02020-01-26 16:41:13 -080011125void kvm_arch_free_vm(struct kvm *kvm)
11126{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +010011127 kfree(to_kvm_hv(kvm)->hv_pa_pg);
Sean Christopherson562b6b02020-01-26 16:41:13 -080011128 vfree(kvm);
11129}
11130
11131
Carsten Ottee08b9632012-01-04 10:25:20 +010011132int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011133{
Carsten Ottee08b9632012-01-04 10:25:20 +010011134 if (type)
11135 return -EINVAL;
11136
Paolo Bonzini6ef768f2014-11-20 13:45:31 +010011137 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +080011138 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -070011139 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +010011140 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030011141 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060011142 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011143
Sheng Yang5550af42008-10-15 20:15:06 +080011144 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
11145 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -060011146 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
11147 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
11148 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +080011149
Jan Kiszka038f8c12011-02-04 10:49:11 +010011150 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +030011151 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -020011152 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
11153
Paolo Bonzini8171cd62020-01-22 14:36:09 +010011154 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -020011155 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +010011156
Drew Schmitt6fbbde92018-08-20 10:32:15 -070011157 kvm->arch.guest_can_read_msr_platform_info = true;
11158
Vineeth Pillai3c86c0d2021-06-03 15:14:36 +000011159#if IS_ENABLED(CONFIG_HYPERV)
11160 spin_lock_init(&kvm->arch.hv_root_tdp_lock);
11161 kvm->arch.hv_root_tdp = INVALID_PAGE;
11162#endif
11163
Andrew Jones7e44e442014-02-28 12:52:54 +010011164 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +010011165 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +010011166
Vitaly Kuznetsov4651fc52021-06-09 17:09:09 +020011167 kvm_apicv_init(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +030011168 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +080011169 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +080011170 kvm_mmu_init_vm(kvm);
Paolo Bonzini319afe62021-08-04 12:48:41 -040011171 kvm_xen_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +080011172
Jason Baronb36464772021-01-14 22:27:56 -050011173 return static_call(kvm_x86_vm_init)(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011174}
11175
Junaid Shahid1aa9b952019-11-04 20:26:00 +010011176int kvm_arch_post_init_vm(struct kvm *kvm)
11177{
11178 return kvm_mmu_post_init_vm(kvm);
11179}
11180
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011181static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
11182{
Christoffer Dallec7660c2017-12-04 21:35:23 +010011183 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011184 kvm_mmu_unload(vcpu);
11185 vcpu_put(vcpu);
11186}
11187
11188static void kvm_free_vcpus(struct kvm *kvm)
11189{
11190 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +030011191 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011192
11193 /*
11194 * Unpin any mmu pages first.
11195 */
Gleb Natapovaf585b92010-10-14 11:22:46 +020011196 kvm_for_each_vcpu(i, vcpu, kvm) {
11197 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +030011198 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011199 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +030011200 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -080011201 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011202
Gleb Natapov988a2ca2009-06-09 15:56:29 +030011203 mutex_lock(&kvm->lock);
11204 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
11205 kvm->vcpus[i] = NULL;
11206
11207 atomic_set(&kvm->online_vcpus, 0);
11208 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011209}
11210
Sheng Yangad8ba2c2009-01-06 10:03:02 +080011211void kvm_arch_sync_events(struct kvm *kvm)
11212{
Andrew Jones332967a2014-02-28 12:52:55 +010011213 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +010011214 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +080011215 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +080011216}
11217
Peter Xuff5a9832020-09-30 21:20:33 -040011218#define ERR_PTR_USR(e) ((void __user *)ERR_PTR(e))
11219
11220/**
11221 * __x86_set_memory_region: Setup KVM internal memory slot
11222 *
11223 * @kvm: the kvm pointer to the VM.
11224 * @id: the slot ID to setup.
11225 * @gpa: the GPA to install the slot (unused when @size == 0).
11226 * @size: the size of the slot. Set to zero to uninstall a slot.
11227 *
11228 * This function helps to setup a KVM internal memory slot. Specify
11229 * @size > 0 to install a new slot, while @size == 0 to uninstall a
11230 * slot. The return code can be one of the following:
11231 *
11232 * HVA: on success (uninstall will return a bogus HVA)
11233 * -errno: on error
11234 *
11235 * The caller should always use IS_ERR() to check the return value
11236 * before use. Note, the KVM internal memory slots are guaranteed to
11237 * remain valid and unchanged until the VM is destroyed, i.e., the
11238 * GPA->HVA translation will not change. However, the HVA is a user
11239 * address, i.e. its accessibility is not guaranteed, and must be
11240 * accessed via __copy_{to,from}_user().
11241 */
11242void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
11243 u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011244{
11245 int i, r;
Kees Cook3f649ab2020-06-03 13:09:38 -070011246 unsigned long hva, old_npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011247 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -080011248 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011249
11250 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020011251 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
Peter Xuff5a9832020-09-30 21:20:33 -040011252 return ERR_PTR_USR(-EINVAL);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011253
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011254 slot = id_to_memslot(slots, id);
11255 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -080011256 if (slot && slot->npages)
Peter Xuff5a9832020-09-30 21:20:33 -040011257 return ERR_PTR_USR(-EEXIST);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011258
11259 /*
11260 * MAP_SHARED to prevent internal slot pages from being moved
11261 * by fork()/COW.
11262 */
11263 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
11264 MAP_SHARED | MAP_ANONYMOUS, 0);
11265 if (IS_ERR((void *)hva))
Peter Xuff5a9832020-09-30 21:20:33 -040011266 return (void __user *)hva;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011267 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -080011268 if (!slot || !slot->npages)
Muhammad Usama Anjum46914532021-03-05 23:08:16 +050011269 return NULL;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011270
Sean Christopherson0577d1a2020-02-18 13:07:31 -080011271 old_npages = slot->npages;
Zheng Zhan Liangb66f9ba2021-02-01 13:53:10 +080011272 hva = slot->userspace_addr;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011273 }
11274
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011275 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020011276 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011277
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020011278 m.slot = id | (i << 16);
11279 m.flags = 0;
11280 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011281 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020011282 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011283 r = __kvm_set_memory_region(kvm, &m);
11284 if (r < 0)
Peter Xuff5a9832020-09-30 21:20:33 -040011285 return ERR_PTR_USR(r);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011286 }
11287
Eric Biggers103c7632018-01-31 17:30:21 -080011288 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -080011289 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011290
Peter Xuff5a9832020-09-30 21:20:33 -040011291 return (void __user *)hva;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011292}
11293EXPORT_SYMBOL_GPL(__x86_set_memory_region);
11294
Junaid Shahid1aa9b952019-11-04 20:26:00 +010011295void kvm_arch_pre_destroy_vm(struct kvm *kvm)
11296{
11297 kvm_mmu_pre_destroy_vm(kvm);
11298}
11299
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011300void kvm_arch_destroy_vm(struct kvm *kvm)
11301{
Andrew Honig27469d22013-04-18 09:38:14 -070011302 if (current->mm == kvm->mm) {
11303 /*
11304 * Free memory regions allocated on behalf of userspace,
11305 * unless the the memory map has changed due to process exit
11306 * or fd copying.
11307 */
Peter Xu6a3c6232020-01-09 09:57:16 -050011308 mutex_lock(&kvm->slots_lock);
11309 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
11310 0, 0);
11311 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
11312 0, 0);
11313 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
11314 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -070011315 }
Jason Baronb36464772021-01-14 22:27:56 -050011316 static_call_cond(kvm_x86_vm_destroy)(kvm);
Sean Christophersonb318e8d2021-03-16 11:44:33 -070011317 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1));
Peter Xuc7611592017-03-15 16:01:19 +080011318 kvm_pic_destroy(kvm);
11319 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011320 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +020011321 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -070011322 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +080011323 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +020011324 kvm_page_track_cleanup(kvm);
David Woodhouse7d6bbeb2021-02-02 15:48:05 +000011325 kvm_xen_destroy_vm(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +030011326 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011327}
Zhang Xiantao0de10342007-11-20 16:25:04 +080011328
Ben Gardonc9b929b2021-05-18 10:34:08 -070011329static void memslot_rmap_free(struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011330{
11331 int i;
11332
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011333 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -080011334 kvfree(slot->arch.rmap[i]);
11335 slot->arch.rmap[i] = NULL;
Ben Gardonc9b929b2021-05-18 10:34:08 -070011336 }
11337}
Sean Christophersone96c81e2020-02-18 13:07:27 -080011338
Ben Gardonc9b929b2021-05-18 10:34:08 -070011339void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
11340{
11341 int i;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011342
Ben Gardonc9b929b2021-05-18 10:34:08 -070011343 memslot_rmap_free(slot);
11344
11345 for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -080011346 kvfree(slot->arch.lpage_info[i - 1]);
11347 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011348 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080011349
Sean Christophersone96c81e2020-02-18 13:07:27 -080011350 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011351}
11352
Ben Gardon56dd1012021-05-18 10:34:09 -070011353static int memslot_rmap_alloc(struct kvm_memory_slot *slot,
11354 unsigned long npages)
11355{
11356 const int sz = sizeof(*slot->arch.rmap[0]);
11357 int i;
11358
11359 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
11360 int level = i + 1;
Peter Xu4139b192021-07-30 18:04:51 -040011361 int lpages = __kvm_mmu_slot_lpages(slot, npages, level);
Ben Gardon56dd1012021-05-18 10:34:09 -070011362
Ben Gardond501f742021-05-18 10:34:14 -070011363 WARN_ON(slot->arch.rmap[i]);
11364
Ben Gardon56dd1012021-05-18 10:34:09 -070011365 slot->arch.rmap[i] = kvcalloc(lpages, sz, GFP_KERNEL_ACCOUNT);
11366 if (!slot->arch.rmap[i]) {
11367 memslot_rmap_free(slot);
11368 return -ENOMEM;
11369 }
11370 }
11371
11372 return 0;
11373}
11374
Ben Gardond501f742021-05-18 10:34:14 -070011375int alloc_all_memslots_rmaps(struct kvm *kvm)
11376{
11377 struct kvm_memslots *slots;
11378 struct kvm_memory_slot *slot;
11379 int r, i;
11380
11381 /*
11382 * Check if memslots alreday have rmaps early before acquiring
11383 * the slots_arch_lock below.
11384 */
11385 if (kvm_memslots_have_rmaps(kvm))
11386 return 0;
11387
11388 mutex_lock(&kvm->slots_arch_lock);
11389
11390 /*
11391 * Read memslots_have_rmaps again, under the slots arch lock,
11392 * before allocating the rmaps
11393 */
11394 if (kvm_memslots_have_rmaps(kvm)) {
11395 mutex_unlock(&kvm->slots_arch_lock);
11396 return 0;
11397 }
11398
11399 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
11400 slots = __kvm_memslots(kvm, i);
11401 kvm_for_each_memslot(slot, slots) {
11402 r = memslot_rmap_alloc(slot, slot->npages);
11403 if (r) {
11404 mutex_unlock(&kvm->slots_arch_lock);
11405 return r;
11406 }
11407 }
11408 }
11409
11410 /*
11411 * Ensure that memslots_have_rmaps becomes true strictly after
11412 * all the rmap pointers are set.
11413 */
11414 smp_store_release(&kvm->arch.memslots_have_rmaps, true);
11415 mutex_unlock(&kvm->slots_arch_lock);
11416 return 0;
11417}
11418
Ben Gardona2557402021-05-18 10:34:12 -070011419static int kvm_alloc_memslot_metadata(struct kvm *kvm,
11420 struct kvm_memory_slot *slot,
Sean Christopherson0dab98b2020-02-18 13:07:19 -080011421 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011422{
Ben Gardon56dd1012021-05-18 10:34:09 -070011423 int i, r;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011424
Sean Christophersonedd4fa32020-02-18 13:07:15 -080011425 /*
11426 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
11427 * old arrays will be freed by __kvm_set_memory_region() if installing
11428 * the new memslot is successful.
11429 */
11430 memset(&slot->arch, 0, sizeof(slot->arch));
11431
Ben Gardone2209712021-05-18 10:34:13 -070011432 if (kvm_memslots_have_rmaps(kvm)) {
Ben Gardona2557402021-05-18 10:34:12 -070011433 r = memslot_rmap_alloc(slot, npages);
11434 if (r)
11435 return r;
11436 }
Ben Gardon56dd1012021-05-18 10:34:09 -070011437
11438 for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011439 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011440 unsigned long ugfn;
11441 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011442 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011443
Peter Xu4139b192021-07-30 18:04:51 -040011444 lpages = __kvm_mmu_slot_lpages(slot, npages, level);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011445
Ben Gardon254272c2019-02-11 11:02:50 -080011446 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011447 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011448 goto out_free;
11449
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011450 slot->arch.lpage_info[i - 1] = linfo;
11451
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011452 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011453 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011454 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011455 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011456 ugfn = slot->userspace_addr >> PAGE_SHIFT;
11457 /*
11458 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -080011459 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011460 */
Sean Christopherson600087b2020-03-02 15:57:05 -080011461 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011462 unsigned long j;
11463
11464 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011465 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011466 }
11467 }
11468
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080011469 if (kvm_page_track_create_memslot(slot, npages))
11470 goto out_free;
11471
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011472 return 0;
11473
11474out_free:
Ben Gardonc9b929b2021-05-18 10:34:08 -070011475 memslot_rmap_free(slot);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011476
Ben Gardonc9b929b2021-05-18 10:34:08 -070011477 for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +010011478 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011479 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011480 }
11481 return -ENOMEM;
11482}
11483
Sean Christopherson15248252019-02-05 12:54:17 -080011484void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090011485{
Boris Ostrovsky91724812019-12-05 01:30:51 +000011486 struct kvm_vcpu *vcpu;
11487 int i;
11488
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +090011489 /*
11490 * memslots->generation has been incremented.
11491 * mmio generation may have reached its maximum value.
11492 */
Sean Christopherson15248252019-02-05 12:54:17 -080011493 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +000011494
11495 /* Force re-initialization of steal_time cache */
11496 kvm_for_each_vcpu(i, vcpu, kvm)
11497 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090011498}
11499
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011500int kvm_arch_prepare_memory_region(struct kvm *kvm,
11501 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +020011502 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +090011503 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +080011504{
Sean Christopherson0dab98b2020-02-18 13:07:19 -080011505 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
Ben Gardona2557402021-05-18 10:34:12 -070011506 return kvm_alloc_memslot_metadata(kvm, memslot,
Sean Christopherson0dab98b2020-02-18 13:07:19 -080011507 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011508 return 0;
11509}
11510
Makarand Sonarea85863c2021-02-12 16:50:12 -080011511
11512static void kvm_mmu_update_cpu_dirty_logging(struct kvm *kvm, bool enable)
11513{
11514 struct kvm_arch *ka = &kvm->arch;
11515
11516 if (!kvm_x86_ops.cpu_dirty_log_size)
11517 return;
11518
11519 if ((enable && ++ka->cpu_dirty_logging_count == 1) ||
11520 (!enable && --ka->cpu_dirty_logging_count == 0))
11521 kvm_make_all_cpus_request(kvm, KVM_REQ_UPDATE_CPU_DIRTY_LOGGING);
11522
11523 WARN_ON_ONCE(ka->cpu_dirty_logging_count < 0);
11524}
11525
Kai Huang88178fd2015-01-28 10:54:27 +080011526static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
Anthony Yznaga37416792020-06-02 13:07:30 -070011527 struct kvm_memory_slot *old,
Hamza Mahfooz269e9552021-07-12 22:33:38 -040011528 const struct kvm_memory_slot *new,
Anthony Yznaga37416792020-06-02 13:07:30 -070011529 enum kvm_mr_change change)
Kai Huang88178fd2015-01-28 10:54:27 +080011530{
Makarand Sonarea85863c2021-02-12 16:50:12 -080011531 bool log_dirty_pages = new->flags & KVM_MEM_LOG_DIRTY_PAGES;
11532
Anthony Yznaga37416792020-06-02 13:07:30 -070011533 /*
Makarand Sonarea85863c2021-02-12 16:50:12 -080011534 * Update CPU dirty logging if dirty logging is being toggled. This
11535 * applies to all operations.
11536 */
11537 if ((old->flags ^ new->flags) & KVM_MEM_LOG_DIRTY_PAGES)
11538 kvm_mmu_update_cpu_dirty_logging(kvm, log_dirty_pages);
11539
11540 /*
11541 * Nothing more to do for RO slots (which can't be dirtied and can't be
Sean Christophersonb6e16ae2021-02-12 16:50:13 -080011542 * made writable) or CREATE/MOVE/DELETE of a slot.
11543 *
11544 * For a memslot with dirty logging disabled:
11545 * CREATE: No dirty mappings will already exist.
11546 * MOVE/DELETE: The old mappings will already have been cleaned up by
11547 * kvm_arch_flush_shadow_memslot()
11548 *
11549 * For a memslot with dirty logging enabled:
11550 * CREATE: No shadow pages exist, thus nothing to write-protect
11551 * and no dirty bits to clear.
11552 * MOVE/DELETE: The old mappings will already have been cleaned up by
11553 * kvm_arch_flush_shadow_memslot().
Anthony Yznaga37416792020-06-02 13:07:30 -070011554 */
11555 if ((change != KVM_MR_FLAGS_ONLY) || (new->flags & KVM_MEM_READONLY))
Kai Huang88178fd2015-01-28 10:54:27 +080011556 return;
Kai Huang88178fd2015-01-28 10:54:27 +080011557
11558 /*
Sean Christopherson52f460792021-02-12 16:50:11 -080011559 * READONLY and non-flags changes were filtered out above, and the only
11560 * other flag is LOG_DIRTY_PAGES, i.e. something is wrong if dirty
11561 * logging isn't being toggled on or off.
Anthony Yznaga37416792020-06-02 13:07:30 -070011562 */
Sean Christopherson52f460792021-02-12 16:50:11 -080011563 if (WARN_ON_ONCE(!((old->flags ^ new->flags) & KVM_MEM_LOG_DIRTY_PAGES)))
11564 return;
11565
Sean Christophersonb6e16ae2021-02-12 16:50:13 -080011566 if (!log_dirty_pages) {
11567 /*
11568 * Dirty logging tracks sptes in 4k granularity, meaning that
11569 * large sptes have to be split. If live migration succeeds,
11570 * the guest in the source machine will be destroyed and large
11571 * sptes will be created in the destination. However, if the
11572 * guest continues to run in the source machine (for example if
11573 * live migration fails), small sptes will remain around and
11574 * cause bad performance.
11575 *
11576 * Scan sptes if dirty logging has been stopped, dropping those
11577 * which can be collapsed into a single large-page spte. Later
11578 * page faults will create the large-page sptes.
11579 */
Anthony Yznaga37416792020-06-02 13:07:30 -070011580 kvm_mmu_zap_collapsible_sptes(kvm, new);
Sean Christophersonb6e16ae2021-02-12 16:50:13 -080011581 } else {
Keqian Zhu89212912021-04-29 11:41:15 +080011582 /*
11583 * Initially-all-set does not require write protecting any page,
11584 * because they're all assumed to be dirty.
11585 */
11586 if (kvm_dirty_log_manual_protect_and_init_set(kvm))
11587 return;
Anthony Yznaga37416792020-06-02 13:07:30 -070011588
Sean Christophersona018eba2021-02-12 16:50:10 -080011589 if (kvm_x86_ops.cpu_dirty_log_size) {
Keqian Zhu89212912021-04-29 11:41:15 +080011590 kvm_mmu_slot_leaf_clear_dirty(kvm, new);
11591 kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_2M);
11592 } else {
11593 kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_4K);
Jay Zhou3c9bd402020-02-27 09:32:27 +080011594 }
Kai Huang88178fd2015-01-28 10:54:27 +080011595 }
11596}
11597
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011598void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020011599 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080011600 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020011601 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090011602 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011603{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080011604 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080011605 kvm_mmu_change_mmu_pages(kvm,
11606 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080011607
Hamza Mahfooz269e9552021-07-12 22:33:38 -040011608 kvm_mmu_slot_apply_flags(kvm, old, new, change);
Sean Christopherson21198842020-02-18 13:07:25 -080011609
11610 /* Free the arrays associated with the old memslot. */
11611 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080011612 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080011613}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080011614
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030011615void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030011616{
Sean Christopherson7390de12019-02-05 13:01:31 -080011617 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030011618}
11619
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030011620void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
11621 struct kvm_memory_slot *slot)
11622{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080011623 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030011624}
11625
Liran Alone6c67d82018-09-04 10:56:52 +030011626static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
11627{
11628 return (is_guest_mode(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070011629 kvm_x86_ops.guest_apic_has_interrupt &&
Jason Baronb36464772021-01-14 22:27:56 -050011630 static_call(kvm_x86_guest_apic_has_interrupt)(vcpu));
Liran Alone6c67d82018-09-04 10:56:52 +030011631}
11632
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011633static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
11634{
11635 if (!list_empty_careful(&vcpu->async_pf.done))
11636 return true;
11637
11638 if (kvm_apic_has_events(vcpu))
11639 return true;
11640
11641 if (vcpu->arch.pv.pv_unhalted)
11642 return true;
11643
Wanpeng Lia5f01f82017-09-13 04:04:01 -070011644 if (vcpu->arch.exception.pending)
11645 return true;
11646
ZhuangYanying47a66ee2017-05-26 13:16:48 +080011647 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
11648 (vcpu->arch.nmi_pending &&
Jason Baronb36464772021-01-14 22:27:56 -050011649 static_call(kvm_x86_nmi_allowed)(vcpu, false)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011650 return true;
11651
ZhuangYanying47a66ee2017-05-26 13:16:48 +080011652 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -040011653 (vcpu->arch.smi_pending &&
Jason Baronb36464772021-01-14 22:27:56 -050011654 static_call(kvm_x86_smi_allowed)(vcpu, false)))
Paolo Bonzini73917732015-10-13 10:19:35 +020011655 return true;
11656
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011657 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030011658 (kvm_cpu_has_interrupt(vcpu) ||
11659 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011660 return true;
11661
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030011662 if (kvm_hv_has_stimer_pending(vcpu))
11663 return true;
11664
Sean Christophersond2060bd2020-04-22 19:25:39 -070011665 if (is_guest_mode(vcpu) &&
11666 kvm_x86_ops.nested_ops->hv_timer_pending &&
11667 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
11668 return true;
11669
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011670 return false;
11671}
11672
Zhang Xiantao1d737c82007-12-14 09:35:10 +080011673int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
11674{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011675 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080011676}
Zhang Xiantao57361992007-12-17 14:21:40 +080011677
Haiwei Li10dbdf92021-04-21 11:25:13 +080011678bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu)
11679{
11680 if (vcpu->arch.apicv_active && static_call(kvm_x86_dy_apicv_has_pending_interrupt)(vcpu))
11681 return true;
11682
11683 return false;
11684}
11685
Wanpeng Li17e433b2019-08-05 10:03:19 +080011686bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
11687{
11688 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
11689 return true;
11690
11691 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
11692 kvm_test_request(KVM_REQ_SMI, vcpu) ||
11693 kvm_test_request(KVM_REQ_EVENT, vcpu))
11694 return true;
11695
Haiwei Li10dbdf92021-04-21 11:25:13 +080011696 return kvm_arch_dy_has_pending_interrupt(vcpu);
Wanpeng Li17e433b2019-08-05 10:03:19 +080011697}
11698
Longpeng(Mike)199b5762017-08-08 12:05:32 +080011699bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
11700{
Wanpeng Lib86bb112021-04-22 16:34:19 +080011701 if (vcpu->arch.guest_state_protected)
11702 return true;
11703
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080011704 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080011705}
11706
Christoffer Dallb6d33832012-03-08 16:44:24 -050011707int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080011708{
Christoffer Dallb6d33832012-03-08 16:44:24 -050011709 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080011710}
Gleb Natapov78646122009-03-23 12:12:11 +020011711
11712int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
11713{
Jason Baronb36464772021-01-14 22:27:56 -050011714 return static_call(kvm_x86_interrupt_allowed)(vcpu, false);
Gleb Natapov78646122009-03-23 12:12:11 +020011715}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011716
Nadav Amit82b32772014-11-02 11:54:45 +020011717unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
11718{
Tom Lendacky7ed9abf2020-12-10 11:09:54 -060011719 /* Can't read the RIP when guest state is protected, just return 0 */
11720 if (vcpu->arch.guest_state_protected)
11721 return 0;
11722
Nadav Amit82b32772014-11-02 11:54:45 +020011723 if (is_64_bit_mode(vcpu))
11724 return kvm_rip_read(vcpu);
11725 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
11726 kvm_rip_read(vcpu));
11727}
11728EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
11729
Jan Kiszkaf92653e2010-02-23 17:47:55 +010011730bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
11731{
Nadav Amit82b32772014-11-02 11:54:45 +020011732 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010011733}
11734EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
11735
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011736unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
11737{
11738 unsigned long rflags;
11739
Jason Baronb36464772021-01-14 22:27:56 -050011740 rflags = static_call(kvm_x86_get_rflags)(vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011741 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010011742 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011743 return rflags;
11744}
11745EXPORT_SYMBOL_GPL(kvm_get_rflags);
11746
Paolo Bonzini6addfc42014-03-27 11:29:28 +010011747static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011748{
11749 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010011750 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010011751 rflags |= X86_EFLAGS_TF;
Jason Baronb36464772021-01-14 22:27:56 -050011752 static_call(kvm_x86_set_rflags)(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010011753}
11754
11755void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
11756{
11757 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030011758 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011759}
11760EXPORT_SYMBOL_GPL(kvm_set_rflags);
11761
Gleb Natapov56028d02010-10-17 18:13:42 +020011762void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
11763{
11764 int r;
11765
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011766 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080011767 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020011768 return;
11769
11770 r = kvm_mmu_reload(vcpu);
11771 if (unlikely(r))
11772 return;
11773
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011774 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080011775 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080011776 return;
11777
Sean Christopherson7a026742020-02-06 14:14:34 -080011778 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020011779}
11780
Gleb Natapovaf585b92010-10-14 11:22:46 +020011781static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
11782{
Peter Xudd03bca2020-04-16 11:58:59 -040011783 BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
11784
Gleb Natapovaf585b92010-10-14 11:22:46 +020011785 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
11786}
11787
11788static inline u32 kvm_async_pf_next_probe(u32 key)
11789{
Peter Xudd03bca2020-04-16 11:58:59 -040011790 return (key + 1) & (ASYNC_PF_PER_VCPU - 1);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011791}
11792
11793static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
11794{
11795 u32 key = kvm_async_pf_hash_fn(gfn);
11796
11797 while (vcpu->arch.apf.gfns[key] != ~0)
11798 key = kvm_async_pf_next_probe(key);
11799
11800 vcpu->arch.apf.gfns[key] = gfn;
11801}
11802
11803static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
11804{
11805 int i;
11806 u32 key = kvm_async_pf_hash_fn(gfn);
11807
Peter Xudd03bca2020-04-16 11:58:59 -040011808 for (i = 0; i < ASYNC_PF_PER_VCPU &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080011809 (vcpu->arch.apf.gfns[key] != gfn &&
11810 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020011811 key = kvm_async_pf_next_probe(key);
11812
11813 return key;
11814}
11815
11816bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
11817{
11818 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
11819}
11820
11821static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
11822{
11823 u32 i, j, k;
11824
11825 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
Peter Xu0fd46042020-04-16 11:59:10 -040011826
11827 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn))
11828 return;
11829
Gleb Natapovaf585b92010-10-14 11:22:46 +020011830 while (true) {
11831 vcpu->arch.apf.gfns[i] = ~0;
11832 do {
11833 j = kvm_async_pf_next_probe(j);
11834 if (vcpu->arch.apf.gfns[j] == ~0)
11835 return;
11836 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
11837 /*
11838 * k lies cyclically in ]i,j]
11839 * | i.k.j |
11840 * |....j i.k.| or |.k..j i...|
11841 */
11842 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
11843 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
11844 i = j;
11845 }
11846}
11847
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020011848static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020011849{
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020011850 u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT;
11851
11852 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason,
11853 sizeof(reason));
11854}
11855
11856static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token)
11857{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011858 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
Paolo Bonzini4e335d92017-05-02 16:20:18 +020011859
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011860 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
11861 &token, offset, sizeof(token));
11862}
11863
11864static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu)
11865{
11866 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
11867 u32 val;
11868
11869 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
11870 &val, offset, sizeof(val)))
11871 return false;
11872
11873 return !val;
Gleb Natapov7c907052010-10-14 11:22:53 +020011874}
11875
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011876static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
11877{
11878 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
11879 return false;
11880
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011881 if (!kvm_pv_async_pf_enabled(vcpu) ||
Jason Baronb36464772021-01-14 22:27:56 -050011882 (vcpu->arch.apf.send_user_only && static_call(kvm_x86_get_cpl)(vcpu) == 0))
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011883 return false;
11884
11885 return true;
11886}
11887
11888bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
11889{
11890 if (unlikely(!lapic_in_kernel(vcpu) ||
11891 kvm_event_needs_reinjection(vcpu) ||
11892 vcpu->arch.exception.pending))
11893 return false;
11894
11895 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
11896 return false;
11897
11898 /*
11899 * If interrupts are off we cannot even use an artificial
11900 * halt state.
11901 */
Paolo Bonzinic300ab92020-04-23 14:08:58 -040011902 return kvm_arch_interrupt_allowed(vcpu);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011903}
11904
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011905bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
Gleb Natapovaf585b92010-10-14 11:22:46 +020011906 struct kvm_async_pf *work)
11907{
Avi Kivity6389ee92010-11-29 16:12:30 +020011908 struct x86_exception fault;
11909
Sean Christopherson736c2912019-12-06 15:57:14 -080011910 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011911 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020011912
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011913 if (kvm_can_deliver_async_pf(vcpu) &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020011914 !apf_put_user_notpresent(vcpu)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020011915 fault.vector = PF_VECTOR;
11916 fault.error_code_valid = true;
11917 fault.error_code = 0;
11918 fault.nested_page_fault = false;
11919 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070011920 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020011921 kvm_inject_page_fault(vcpu, &fault);
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011922 return true;
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011923 } else {
11924 /*
11925 * It is not possible to deliver a paravirtualized asynchronous
11926 * page fault, but putting the guest in an artificial halt state
11927 * can be beneficial nevertheless: if an interrupt arrives, we
11928 * can deliver it timely and perhaps the guest will schedule
11929 * another process. When the instruction that triggered a page
11930 * fault is retried, hopefully the page will be ready in the host.
11931 */
11932 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011933 return false;
Gleb Natapov7c907052010-10-14 11:22:53 +020011934 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020011935}
11936
11937void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
11938 struct kvm_async_pf *work)
11939{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011940 struct kvm_lapic_irq irq = {
11941 .delivery_mode = APIC_DM_FIXED,
11942 .vector = vcpu->arch.apf.vec
11943 };
Avi Kivity6389ee92010-11-29 16:12:30 +020011944
chai wenf2e10662013-10-14 22:22:33 +080011945 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020011946 work->arch.token = ~0; /* broadcast wakeup */
11947 else
11948 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080011949 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020011950
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011951 if ((work->wakeup_all || work->notpresent_injected) &&
11952 kvm_pv_async_pf_enabled(vcpu) &&
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011953 !apf_put_user_ready(vcpu, work->arch.token)) {
11954 vcpu->arch.apf.pageready_pending = true;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011955 kvm_apic_set_irq(vcpu, &irq, NULL);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011956 }
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011957
Xiao Guangronge6d53e32010-11-01 17:01:28 +080011958 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030011959 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020011960}
11961
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011962void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu)
11963{
11964 kvm_make_request(KVM_REQ_APF_READY, vcpu);
11965 if (!vcpu->arch.apf.pageready_pending)
11966 kvm_vcpu_kick(vcpu);
11967}
11968
Vitaly Kuznetsov7c0ade62020-05-25 16:41:18 +020011969bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020011970{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011971 if (!kvm_pv_async_pf_enabled(vcpu))
Gleb Natapov7c907052010-10-14 11:22:53 +020011972 return true;
11973 else
Vitaly Kuznetsov2f15d022021-04-22 11:29:48 +020011974 return kvm_lapic_enabled(vcpu) && apf_pageready_slot_free(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011975}
11976
Paolo Bonzini5544eb92015-07-07 15:41:58 +020011977void kvm_arch_start_assignment(struct kvm *kvm)
11978{
Marcelo Tosatti57ab8792021-05-25 10:41:16 -030011979 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1)
11980 static_call_cond(kvm_x86_start_assignment)(kvm);
Paolo Bonzini5544eb92015-07-07 15:41:58 +020011981}
11982EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
11983
11984void kvm_arch_end_assignment(struct kvm *kvm)
11985{
11986 atomic_dec(&kvm->arch.assigned_device_count);
11987}
11988EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
11989
11990bool kvm_arch_has_assigned_device(struct kvm *kvm)
11991{
11992 return atomic_read(&kvm->arch.assigned_device_count);
11993}
11994EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
11995
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060011996void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
11997{
11998 atomic_inc(&kvm->arch.noncoherent_dma_count);
11999}
12000EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
12001
12002void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
12003{
12004 atomic_dec(&kvm->arch.noncoherent_dma_count);
12005}
12006EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
12007
12008bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
12009{
12010 return atomic_read(&kvm->arch.noncoherent_dma_count);
12011}
12012EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
12013
Alex Williamson14717e22016-05-05 11:58:35 -060012014bool kvm_arch_has_irq_bypass(void)
12015{
Sean Christopherson92735b12019-08-02 15:06:17 -070012016 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060012017}
12018
Feng Wu87276882015-09-18 22:29:40 +080012019int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
12020 struct irq_bypass_producer *prod)
12021{
12022 struct kvm_kernel_irqfd *irqfd =
12023 container_of(cons, struct kvm_kernel_irqfd, consumer);
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080012024 int ret;
Feng Wu87276882015-09-18 22:29:40 +080012025
Alex Williamson14717e22016-05-05 11:58:35 -060012026 irqfd->producer = prod;
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080012027 kvm_arch_start_assignment(irqfd->kvm);
Jason Baronb36464772021-01-14 22:27:56 -050012028 ret = static_call(kvm_x86_update_pi_irte)(irqfd->kvm,
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080012029 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080012030
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080012031 if (ret)
12032 kvm_arch_end_assignment(irqfd->kvm);
12033
12034 return ret;
Feng Wu87276882015-09-18 22:29:40 +080012035}
12036
12037void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
12038 struct irq_bypass_producer *prod)
12039{
12040 int ret;
12041 struct kvm_kernel_irqfd *irqfd =
12042 container_of(cons, struct kvm_kernel_irqfd, consumer);
12043
Feng Wu87276882015-09-18 22:29:40 +080012044 WARN_ON(irqfd->producer != prod);
12045 irqfd->producer = NULL;
12046
12047 /*
12048 * When producer of consumer is unregistered, we change back to
12049 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020012050 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080012051 * int this case doesn't want to receive the interrupts.
12052 */
Jason Baronb36464772021-01-14 22:27:56 -050012053 ret = static_call(kvm_x86_update_pi_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 0);
Feng Wu87276882015-09-18 22:29:40 +080012054 if (ret)
12055 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
12056 " fails: %d\n", irqfd->consumer.token, ret);
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080012057
12058 kvm_arch_end_assignment(irqfd->kvm);
Feng Wu87276882015-09-18 22:29:40 +080012059}
12060
12061int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
12062 uint32_t guest_irq, bool set)
12063{
Jason Baronb36464772021-01-14 22:27:56 -050012064 return static_call(kvm_x86_update_pi_irte)(kvm, host_irq, guest_irq, set);
Feng Wu87276882015-09-18 22:29:40 +080012065}
12066
Feng Wu520040142016-01-25 16:53:33 +080012067bool kvm_vector_hashing_enabled(void)
12068{
12069 return vector_hashing;
12070}
Feng Wu520040142016-01-25 16:53:33 +080012071
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030012072bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
12073{
12074 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
12075}
12076EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
12077
Maxim Levitsky841c2be2020-07-08 14:57:31 +030012078
12079int kvm_spec_ctrl_test_value(u64 value)
Paolo Bonzini6441fa62020-01-20 16:33:06 +010012080{
Maxim Levitsky841c2be2020-07-08 14:57:31 +030012081 /*
12082 * test that setting IA32_SPEC_CTRL to given value
12083 * is allowed by the host processor
12084 */
Paolo Bonzini6441fa62020-01-20 16:33:06 +010012085
Maxim Levitsky841c2be2020-07-08 14:57:31 +030012086 u64 saved_value;
12087 unsigned long flags;
12088 int ret = 0;
Paolo Bonzini6441fa62020-01-20 16:33:06 +010012089
Maxim Levitsky841c2be2020-07-08 14:57:31 +030012090 local_irq_save(flags);
Paolo Bonzini6441fa62020-01-20 16:33:06 +010012091
Maxim Levitsky841c2be2020-07-08 14:57:31 +030012092 if (rdmsrl_safe(MSR_IA32_SPEC_CTRL, &saved_value))
12093 ret = 1;
12094 else if (wrmsrl_safe(MSR_IA32_SPEC_CTRL, value))
12095 ret = 1;
12096 else
12097 wrmsrl(MSR_IA32_SPEC_CTRL, saved_value);
12098
12099 local_irq_restore(flags);
12100
12101 return ret;
Paolo Bonzini6441fa62020-01-20 16:33:06 +010012102}
Maxim Levitsky841c2be2020-07-08 14:57:31 +030012103EXPORT_SYMBOL_GPL(kvm_spec_ctrl_test_value);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030012104
Mohammed Gamal89786142020-07-10 17:48:03 +020012105void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code)
12106{
12107 struct x86_exception fault;
Paolo Bonzini19cf4b72020-08-17 13:53:04 -040012108 u32 access = error_code &
12109 (PFERR_WRITE_MASK | PFERR_FETCH_MASK | PFERR_USER_MASK);
Mohammed Gamal89786142020-07-10 17:48:03 +020012110
12111 if (!(error_code & PFERR_PRESENT_MASK) ||
Paolo Bonzini19cf4b72020-08-17 13:53:04 -040012112 vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, &fault) != UNMAPPED_GVA) {
Mohammed Gamal89786142020-07-10 17:48:03 +020012113 /*
12114 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page
12115 * tables probably do not match the TLB. Just proceed
12116 * with the error code that the processor gave.
12117 */
12118 fault.vector = PF_VECTOR;
12119 fault.error_code_valid = true;
12120 fault.error_code = error_code;
12121 fault.nested_page_fault = false;
12122 fault.address = gva;
12123 }
12124 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault);
12125}
12126EXPORT_SYMBOL_GPL(kvm_fixup_and_inject_pf_error);
Carsten Otte043405e2007-10-10 17:16:19 +020012127
Babu Moger3f3393b2020-09-11 14:29:05 -050012128/*
12129 * Handles kvm_read/write_guest_virt*() result and either injects #PF or returns
12130 * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value
12131 * indicates whether exit to userspace is needed.
12132 */
12133int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
12134 struct x86_exception *e)
12135{
12136 if (r == X86EMUL_PROPAGATE_FAULT) {
12137 kvm_inject_emulated_page_fault(vcpu, e);
12138 return 1;
12139 }
12140
12141 /*
12142 * In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED
12143 * while handling a VMX instruction KVM could've handled the request
12144 * correctly by exiting to userspace and performing I/O but there
12145 * doesn't seem to be a real use-case behind such requests, just return
12146 * KVM_EXIT_INTERNAL_ERROR for now.
12147 */
12148 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
12149 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
12150 vcpu->run->internal.ndata = 0;
12151
12152 return 0;
12153}
12154EXPORT_SYMBOL_GPL(kvm_handle_memory_failure);
12155
Babu Moger97150922020-09-11 14:29:12 -050012156int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva)
12157{
12158 bool pcid_enabled;
12159 struct x86_exception e;
Babu Moger97150922020-09-11 14:29:12 -050012160 struct {
12161 u64 pcid;
12162 u64 gla;
12163 } operand;
12164 int r;
12165
12166 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
12167 if (r != X86EMUL_CONTINUE)
12168 return kvm_handle_memory_failure(vcpu, r, &e);
12169
12170 if (operand.pcid >> 12 != 0) {
12171 kvm_inject_gp(vcpu, 0);
12172 return 1;
12173 }
12174
12175 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
12176
12177 switch (type) {
12178 case INVPCID_TYPE_INDIV_ADDR:
12179 if ((!pcid_enabled && (operand.pcid != 0)) ||
12180 is_noncanonical_address(operand.gla, vcpu)) {
12181 kvm_inject_gp(vcpu, 0);
12182 return 1;
12183 }
12184 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
12185 return kvm_skip_emulated_instruction(vcpu);
12186
12187 case INVPCID_TYPE_SINGLE_CTXT:
12188 if (!pcid_enabled && (operand.pcid != 0)) {
12189 kvm_inject_gp(vcpu, 0);
12190 return 1;
12191 }
12192
Sean Christopherson21823fb2021-06-09 16:42:24 -070012193 kvm_invalidate_pcid(vcpu, operand.pcid);
Babu Moger97150922020-09-11 14:29:12 -050012194 return kvm_skip_emulated_instruction(vcpu);
12195
12196 case INVPCID_TYPE_ALL_NON_GLOBAL:
12197 /*
12198 * Currently, KVM doesn't mark global entries in the shadow
12199 * page tables, so a non-global flush just degenerates to a
12200 * global flush. If needed, we could optimize this later by
12201 * keeping track of global entries in shadow page tables.
12202 */
12203
12204 fallthrough;
12205 case INVPCID_TYPE_ALL_INCL_GLOBAL:
Sean Christopherson28f28d42021-06-09 16:42:30 -070012206 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
Babu Moger97150922020-09-11 14:29:12 -050012207 return kvm_skip_emulated_instruction(vcpu);
12208
12209 default:
12210 BUG(); /* We have already checked above that type <= 3 */
12211 }
12212}
12213EXPORT_SYMBOL_GPL(kvm_handle_invpcid);
12214
Tom Lendacky8f423a82020-12-10 11:09:53 -060012215static int complete_sev_es_emulated_mmio(struct kvm_vcpu *vcpu)
12216{
12217 struct kvm_run *run = vcpu->run;
12218 struct kvm_mmio_fragment *frag;
12219 unsigned int len;
12220
12221 BUG_ON(!vcpu->mmio_needed);
12222
12223 /* Complete previous fragment */
12224 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
12225 len = min(8u, frag->len);
12226 if (!vcpu->mmio_is_write)
12227 memcpy(frag->data, run->mmio.data, len);
12228
12229 if (frag->len <= 8) {
12230 /* Switch to the next fragment. */
12231 frag++;
12232 vcpu->mmio_cur_fragment++;
12233 } else {
12234 /* Go forward to the next mmio piece. */
12235 frag->data += len;
12236 frag->gpa += len;
12237 frag->len -= len;
12238 }
12239
12240 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
12241 vcpu->mmio_needed = 0;
12242
12243 // VMG change, at this point, we're always done
12244 // RIP has already been advanced
12245 return 1;
12246 }
12247
12248 // More MMIO is needed
12249 run->mmio.phys_addr = frag->gpa;
12250 run->mmio.len = min(8u, frag->len);
12251 run->mmio.is_write = vcpu->mmio_is_write;
12252 if (run->mmio.is_write)
12253 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
12254 run->exit_reason = KVM_EXIT_MMIO;
12255
12256 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio;
12257
12258 return 0;
12259}
12260
12261int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes,
12262 void *data)
12263{
12264 int handled;
12265 struct kvm_mmio_fragment *frag;
12266
12267 if (!data)
12268 return -EINVAL;
12269
12270 handled = write_emultor.read_write_mmio(vcpu, gpa, bytes, data);
12271 if (handled == bytes)
12272 return 1;
12273
12274 bytes -= handled;
12275 gpa += handled;
12276 data += handled;
12277
12278 /*TODO: Check if need to increment number of frags */
12279 frag = vcpu->mmio_fragments;
12280 vcpu->mmio_nr_fragments = 1;
12281 frag->len = bytes;
12282 frag->gpa = gpa;
12283 frag->data = data;
12284
12285 vcpu->mmio_needed = 1;
12286 vcpu->mmio_cur_fragment = 0;
12287
12288 vcpu->run->mmio.phys_addr = gpa;
12289 vcpu->run->mmio.len = min(8u, frag->len);
12290 vcpu->run->mmio.is_write = 1;
12291 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
12292 vcpu->run->exit_reason = KVM_EXIT_MMIO;
12293
12294 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio;
12295
12296 return 0;
12297}
12298EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_write);
12299
12300int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes,
12301 void *data)
12302{
12303 int handled;
12304 struct kvm_mmio_fragment *frag;
12305
12306 if (!data)
12307 return -EINVAL;
12308
12309 handled = read_emultor.read_write_mmio(vcpu, gpa, bytes, data);
12310 if (handled == bytes)
12311 return 1;
12312
12313 bytes -= handled;
12314 gpa += handled;
12315 data += handled;
12316
12317 /*TODO: Check if need to increment number of frags */
12318 frag = vcpu->mmio_fragments;
12319 vcpu->mmio_nr_fragments = 1;
12320 frag->len = bytes;
12321 frag->gpa = gpa;
12322 frag->data = data;
12323
12324 vcpu->mmio_needed = 1;
12325 vcpu->mmio_cur_fragment = 0;
12326
12327 vcpu->run->mmio.phys_addr = gpa;
12328 vcpu->run->mmio.len = min(8u, frag->len);
12329 vcpu->run->mmio.is_write = 0;
12330 vcpu->run->exit_reason = KVM_EXIT_MMIO;
12331
12332 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio;
12333
12334 return 0;
12335}
12336EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_read);
12337
Tom Lendacky7ed9abf2020-12-10 11:09:54 -060012338static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu)
12339{
12340 memcpy(vcpu->arch.guest_ins_data, vcpu->arch.pio_data,
12341 vcpu->arch.pio.count * vcpu->arch.pio.size);
12342 vcpu->arch.pio.count = 0;
12343
12344 return 1;
12345}
12346
12347static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size,
12348 unsigned int port, void *data, unsigned int count)
12349{
12350 int ret;
12351
12352 ret = emulator_pio_out_emulated(vcpu->arch.emulate_ctxt, size, port,
12353 data, count);
12354 if (ret)
12355 return ret;
12356
12357 vcpu->arch.pio.count = 0;
12358
12359 return 0;
12360}
12361
12362static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size,
12363 unsigned int port, void *data, unsigned int count)
12364{
12365 int ret;
12366
12367 ret = emulator_pio_in_emulated(vcpu->arch.emulate_ctxt, size, port,
12368 data, count);
12369 if (ret) {
12370 vcpu->arch.pio.count = 0;
12371 } else {
12372 vcpu->arch.guest_ins_data = data;
12373 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;
12374 }
12375
12376 return 0;
12377}
12378
12379int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
12380 unsigned int port, void *data, unsigned int count,
12381 int in)
12382{
12383 return in ? kvm_sev_es_ins(vcpu, size, port, data, count)
12384 : kvm_sev_es_outs(vcpu, size, port, data, count);
12385}
12386EXPORT_SYMBOL_GPL(kvm_sev_es_string_io);
12387
Lorenzo Bresciad95df952020-12-23 14:45:07 +000012388EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry);
Carsten Otte043405e2007-10-10 17:16:19 +020012389EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
12390EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
12391EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
12392EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
12393EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
12394EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
12395EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
12396EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
12397EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
12398EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070012399EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Carsten Otte043405e2007-10-10 17:16:19 +020012400EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
12401EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
12402EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
12403EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080012404EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Carsten Otte043405e2007-10-10 17:16:19 +020012405EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
12406EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
12407EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
12408EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050012409EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060012410EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);
Tom Lendackyd523ab6b2020-12-10 11:09:48 -060012411EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter);
12412EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit);
Tom Lendacky59e38b52020-12-10 11:09:52 -060012413EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter);
12414EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit);