blob: 1017d398e72dea08ae6d7dac943c0489a5ca5660 [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>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020069#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020070#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100071#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030072#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080073#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010074#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010075#include <asm/hypervisor.h>
Babu Moger97150922020-09-11 14:29:12 -050076#include <asm/tlbflush.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080077#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090078#include <asm/emulate_prefix.h>
Sean Christophersonfe7e9482021-04-12 16:21:43 +120079#include <asm/sgx.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000080#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020081
Dave Hansend1898b72016-06-01 10:42:20 -070082#define CREATE_TRACE_POINTS
83#include "trace.h"
84
Carsten Otte313a3dc2007-10-11 19:16:52 +020085#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080086#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080087u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
88EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080089
Avi Kivity0f65dd72011-04-20 13:37:53 +030090#define emul_to_vcpu(ctxt) \
Sean Christophersonc9b8b072020-02-18 15:29:48 -080091 ((struct kvm_vcpu *)(ctxt)->vcpu)
Avi Kivity0f65dd72011-04-20 13:37:53 +030092
Joerg Roedel50a37eb2008-01-31 14:57:38 +010093/* EFER defaults:
94 * - enable syscall per default because its emulated by KVM
95 * - enable LME and LMA per default on 64 bit KVM
96 */
97#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080098static
99u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100100#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +0800101static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100102#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +0200103
Sean Christophersonb11306b2019-12-10 14:44:13 -0800104static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
105
Radim Krčmářc5192652016-07-12 22:09:28 +0200106#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
107 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200108
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300109static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300110static void process_nmi(struct kvm_vcpu *vcpu);
Jay Zhou1f7becf2021-01-18 16:47:20 +0800111static void process_smi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200112static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100113static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800114static void store_regs(struct kvm_vcpu *vcpu);
115static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200116
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700117struct kvm_x86_ops kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300118EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800119
Jason Baron9af54712021-01-14 22:27:55 -0500120#define KVM_X86_OP(func) \
121 DEFINE_STATIC_CALL_NULL(kvm_x86_##func, \
122 *(((struct kvm_x86_ops *)0)->func));
123#define KVM_X86_OP_NULL KVM_X86_OP
124#include <asm/kvm-x86-ops.h>
125EXPORT_STATIC_CALL_GPL(kvm_x86_get_cs_db_l_bits);
126EXPORT_STATIC_CALL_GPL(kvm_x86_cache_reg);
127EXPORT_STATIC_CALL_GPL(kvm_x86_tlb_flush_current);
128
Paolo Bonzini893590c2015-11-06 11:46:24 +0100129static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030130module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200131
Like Xud8550662021-01-08 09:36:55 +0800132bool __read_mostly report_ignored_msrs = true;
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200133module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
Like Xud8550662021-01-08 09:36:55 +0800134EXPORT_SYMBOL_GPL(report_ignored_msrs);
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200135
Wanpeng Li4c276252018-05-05 04:02:32 -0700136unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200137module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
138
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300139static bool __read_mostly kvmclock_periodic_sync = true;
140module_param(kvmclock_periodic_sync, bool, S_IRUGO);
141
Paolo Bonzini893590c2015-11-06 11:46:24 +0100142bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100143EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100144u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100145EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800146u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
147EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
148u64 __read_mostly kvm_max_tsc_scaling_ratio;
149EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700150u64 __read_mostly kvm_default_tsc_scaling_ratio;
151EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +0800152bool __read_mostly kvm_has_bus_lock_exit;
153EXPORT_SYMBOL_GPL(kvm_has_bus_lock_exit);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100154
Zachary Amsdencc578282012-02-03 15:43:50 -0200155/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100156static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200157module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
158
Sean Christophersonc3941d92019-04-17 10:15:33 -0700159/*
160 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
Ingo Molnard9f6e122021-03-18 15:28:01 +0100161 * adaptive tuning starting from default advancement of 1000ns. '0' disables
Sean Christophersonc3941d92019-04-17 10:15:33 -0700162 * advancement entirely. Any other value is used as-is and disables adaptive
Ingo Molnard9f6e122021-03-18 15:28:01 +0100163 * tuning, i.e. allows privileged userspace to set an exact advancement time.
Sean Christophersonc3941d92019-04-17 10:15:33 -0700164 */
165static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800166module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500167
Feng Wu520040142016-01-25 16:53:33 +0800168static bool __read_mostly vector_hashing = true;
169module_param(vector_hashing, bool, S_IRUGO);
170
Liran Alonc4ae60e2018-03-12 13:12:47 +0200171bool __read_mostly enable_vmware_backdoor = false;
172module_param(enable_vmware_backdoor, bool, S_IRUGO);
173EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
174
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700175static bool __read_mostly force_emulation_prefix = false;
176module_param(force_emulation_prefix, bool, S_IRUGO);
177
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800178int __read_mostly pi_inject_timer = -1;
179module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
180
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700181/*
182 * Restoring the host value for MSRs that are only consumed when running in
183 * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
184 * returns to userspace, i.e. the kernel can run with the guest's value.
185 */
186#define KVM_MAX_NR_USER_RETURN_MSRS 16
Avi Kivity18863bd2009-09-07 11:12:18 +0300187
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700188struct kvm_user_return_msrs {
Avi Kivity18863bd2009-09-07 11:12:18 +0300189 struct user_return_notifier urn;
190 bool registered;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700191 struct kvm_user_return_msr_values {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800192 u64 host;
193 u64 curr;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700194 } values[KVM_MAX_NR_USER_RETURN_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300195};
196
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700197u32 __read_mostly kvm_nr_uret_msrs;
198EXPORT_SYMBOL_GPL(kvm_nr_uret_msrs);
199static u32 __read_mostly kvm_uret_msrs_list[KVM_MAX_NR_USER_RETURN_MSRS];
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700200static struct kvm_user_return_msrs __percpu *user_return_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300201
Sean Christophersoncfc48182020-03-02 15:56:23 -0800202#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
203 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
204 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
205 | XFEATURE_MASK_PKRU)
206
Sean Christopherson91661982020-03-02 15:57:06 -0800207u64 __read_mostly host_efer;
208EXPORT_SYMBOL_GPL(host_efer);
209
Mohammed Gamalb96e6502020-09-03 16:11:22 +0200210bool __read_mostly allow_smaller_maxphyaddr = 0;
Mohammed Gamal3edd6832020-07-10 17:48:11 +0200211EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr);
212
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +0200213bool __read_mostly enable_apicv = true;
214EXPORT_SYMBOL_GPL(enable_apicv);
215
Tom Lendacky86137772020-12-10 11:10:07 -0600216u64 __read_mostly host_xss;
217EXPORT_SYMBOL_GPL(host_xss);
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100218u64 __read_mostly supported_xss;
219EXPORT_SYMBOL_GPL(supported_xss);
Aaron Lewis139a12c2019-10-21 16:30:25 -0700220
Hollis Blanchard417bc302007-10-31 17:24:23 -0500221struct kvm_stats_debugfs_item debugfs_entries[] = {
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200222 VCPU_STAT("pf_fixed", pf_fixed),
223 VCPU_STAT("pf_guest", pf_guest),
224 VCPU_STAT("tlb_flush", tlb_flush),
225 VCPU_STAT("invlpg", invlpg),
226 VCPU_STAT("exits", exits),
227 VCPU_STAT("io_exits", io_exits),
228 VCPU_STAT("mmio_exits", mmio_exits),
229 VCPU_STAT("signal_exits", signal_exits),
230 VCPU_STAT("irq_window", irq_window_exits),
231 VCPU_STAT("nmi_window", nmi_window_exits),
232 VCPU_STAT("halt_exits", halt_exits),
233 VCPU_STAT("halt_successful_poll", halt_successful_poll),
234 VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
235 VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
236 VCPU_STAT("halt_wakeup", halt_wakeup),
237 VCPU_STAT("hypercalls", hypercalls),
238 VCPU_STAT("request_irq", request_irq_exits),
239 VCPU_STAT("irq_exits", irq_exits),
240 VCPU_STAT("host_state_reload", host_state_reload),
241 VCPU_STAT("fpu_reload", fpu_reload),
242 VCPU_STAT("insn_emulation", insn_emulation),
243 VCPU_STAT("insn_emulation_fail", insn_emulation_fail),
244 VCPU_STAT("irq_injections", irq_injections),
245 VCPU_STAT("nmi_injections", nmi_injections),
246 VCPU_STAT("req_event", req_event),
247 VCPU_STAT("l1d_flush", l1d_flush),
David Matlackcb953122020-05-08 11:22:40 -0700248 VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
249 VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
Dongli Zhang43c11d92021-03-05 14:57:47 -0800250 VCPU_STAT("nested_run", nested_run),
Wanpeng Li4a7132e2021-04-09 12:18:30 +0800251 VCPU_STAT("directed_yield_attempted", directed_yield_attempted),
252 VCPU_STAT("directed_yield_successful", directed_yield_successful),
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200253 VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped),
254 VM_STAT("mmu_pte_write", mmu_pte_write),
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200255 VM_STAT("mmu_pde_zapped", mmu_pde_zapped),
256 VM_STAT("mmu_flooded", mmu_flooded),
257 VM_STAT("mmu_recycled", mmu_recycled),
258 VM_STAT("mmu_cache_miss", mmu_cache_miss),
259 VM_STAT("mmu_unsync", mmu_unsync),
260 VM_STAT("remote_tlb_flush", remote_tlb_flush),
261 VM_STAT("largepages", lpages, .mode = 0444),
262 VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444),
263 VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions),
Hollis Blanchard417bc302007-10-31 17:24:23 -0500264 { NULL }
265};
266
Dexuan Cui2acf9232010-06-10 11:27:12 +0800267u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800268u64 __read_mostly supported_xcr0;
269EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800270
Sean Christopherson80fbd282020-06-08 11:02:18 -0700271static struct kmem_cache *x86_fpu_cache;
Marc Orrb666a4b2018-11-06 14:53:56 -0800272
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800273static struct kmem_cache *x86_emulator_cache;
274
Peter Xu6abe9c12020-06-22 18:04:41 -0400275/*
276 * When called, it means the previous get/set msr reached an invalid msr.
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200277 * Return true if we want to ignore/silent this failed msr access.
Peter Xu6abe9c12020-06-22 18:04:41 -0400278 */
Haiwei Lid6328262021-03-13 13:10:32 +0800279static bool kvm_msr_ignored_check(u32 msr, u64 data, bool write)
Peter Xu6abe9c12020-06-22 18:04:41 -0400280{
281 const char *op = write ? "wrmsr" : "rdmsr";
282
283 if (ignore_msrs) {
284 if (report_ignored_msrs)
Takashi Iwaid383b312020-10-30 16:14:14 +0100285 kvm_pr_unimpl("ignored %s: 0x%x data 0x%llx\n",
286 op, msr, data);
Peter Xu6abe9c12020-06-22 18:04:41 -0400287 /* Mask the error */
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200288 return true;
Peter Xu6abe9c12020-06-22 18:04:41 -0400289 } else {
Takashi Iwaid383b312020-10-30 16:14:14 +0100290 kvm_debug_ratelimited("unhandled %s: 0x%x data 0x%llx\n",
291 op, msr, data);
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200292 return false;
Peter Xu6abe9c12020-06-22 18:04:41 -0400293 }
294}
295
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800296static struct kmem_cache *kvm_alloc_emulator_cache(void)
297{
Sean Christopherson06add252020-02-18 15:29:50 -0800298 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
299 unsigned int size = sizeof(struct x86_emulate_ctxt);
300
301 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800302 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800303 SLAB_ACCOUNT, useroffset,
304 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800305}
306
Jan Kiszkab6785de2012-09-20 07:43:17 +0200307static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300308
Gleb Natapovaf585b92010-10-14 11:22:46 +0200309static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
310{
311 int i;
Peter Xudd03bca2020-04-16 11:58:59 -0400312 for (i = 0; i < ASYNC_PF_PER_VCPU; i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +0200313 vcpu->arch.apf.gfns[i] = ~0;
314}
315
Avi Kivity18863bd2009-09-07 11:12:18 +0300316static void kvm_on_user_return(struct user_return_notifier *urn)
317{
318 unsigned slot;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700319 struct kvm_user_return_msrs *msrs
320 = container_of(urn, struct kvm_user_return_msrs, urn);
321 struct kvm_user_return_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700322 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300323
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700324 /*
325 * Disabling irqs at this point since the following code could be
326 * interrupted and executed through kvm_arch_hardware_disable()
327 */
328 local_irq_save(flags);
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700329 if (msrs->registered) {
330 msrs->registered = false;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700331 user_return_notifier_unregister(urn);
332 }
333 local_irq_restore(flags);
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700334 for (slot = 0; slot < kvm_nr_uret_msrs; ++slot) {
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700335 values = &msrs->values[slot];
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800336 if (values->host != values->curr) {
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700337 wrmsrl(kvm_uret_msrs_list[slot], values->host);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800338 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300339 }
340 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300341}
342
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700343static int kvm_probe_user_return_msr(u32 msr)
Sean Christopherson5104d7f2021-05-04 10:17:24 -0700344{
345 u64 val;
346 int ret;
347
348 preempt_disable();
349 ret = rdmsrl_safe(msr, &val);
350 if (ret)
351 goto out;
352 ret = wrmsrl_safe(msr, val);
353out:
354 preempt_enable();
355 return ret;
356}
Sean Christopherson5104d7f2021-05-04 10:17:24 -0700357
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700358int kvm_add_user_return_msr(u32 msr)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800359{
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700360 BUG_ON(kvm_nr_uret_msrs >= KVM_MAX_NR_USER_RETURN_MSRS);
361
362 if (kvm_probe_user_return_msr(msr))
363 return -1;
364
365 kvm_uret_msrs_list[kvm_nr_uret_msrs] = msr;
366 return kvm_nr_uret_msrs++;
Avi Kivity18863bd2009-09-07 11:12:18 +0300367}
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700368EXPORT_SYMBOL_GPL(kvm_add_user_return_msr);
Avi Kivity18863bd2009-09-07 11:12:18 +0300369
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -0700370int kvm_find_user_return_msr(u32 msr)
371{
372 int i;
373
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700374 for (i = 0; i < kvm_nr_uret_msrs; ++i) {
375 if (kvm_uret_msrs_list[i] == msr)
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -0700376 return i;
377 }
378 return -1;
379}
380EXPORT_SYMBOL_GPL(kvm_find_user_return_msr);
381
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700382static void kvm_user_return_msr_cpu_online(void)
Avi Kivity18863bd2009-09-07 11:12:18 +0300383{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800384 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700385 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Sean Christopherson05c19c22019-11-22 12:04:50 -0800386 u64 value;
387 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300388
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700389 for (i = 0; i < kvm_nr_uret_msrs; ++i) {
390 rdmsrl_safe(kvm_uret_msrs_list[i], &value);
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700391 msrs->values[i].host = value;
392 msrs->values[i].curr = value;
Sean Christopherson05c19c22019-11-22 12:04:50 -0800393 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300394}
395
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700396int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300397{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200398 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700399 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700400 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300401
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700402 value = (value & mask) | (msrs->values[slot].host & ~mask);
403 if (value == msrs->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700404 return 0;
Sean Christopherson9cc39a52021-05-04 10:17:31 -0700405 err = wrmsrl_safe(kvm_uret_msrs_list[slot], value);
Andy Honig8b3c3102014-08-27 11:16:44 -0700406 if (err)
407 return 1;
408
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700409 msrs->values[slot].curr = value;
410 if (!msrs->registered) {
411 msrs->urn.on_user_return = kvm_on_user_return;
412 user_return_notifier_register(&msrs->urn);
413 msrs->registered = true;
Avi Kivity18863bd2009-09-07 11:12:18 +0300414 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700415 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300416}
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700417EXPORT_SYMBOL_GPL(kvm_set_user_return_msr);
Avi Kivity18863bd2009-09-07 11:12:18 +0300418
Radim Krčmář13a34e02014-08-28 15:13:03 +0200419static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200420{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200421 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700422 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200423
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700424 if (msrs->registered)
425 kvm_on_user_return(&msrs->urn);
Avi Kivity3548bab2009-11-28 14:18:47 +0200426}
427
Carsten Otte6866b832007-10-29 16:09:10 +0100428u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
429{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300430 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100431}
432EXPORT_SYMBOL_GPL(kvm_get_apic_base);
433
Jim Mattson58871642018-05-09 16:56:04 -0400434enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
435{
436 return kvm_apic_mode(kvm_get_apic_base(vcpu));
437}
438EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
439
Jan Kiszka58cb6282014-01-24 16:48:44 +0100440int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100441{
Jim Mattson58871642018-05-09 16:56:04 -0400442 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
443 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Sean Christophersona8ac8642021-02-03 16:01:15 -0800444 u64 reserved_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu) | 0x2ff |
Radim Krčmářd6321d42017-08-05 00:12:49 +0200445 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100446
Jim Mattson58871642018-05-09 16:56:04 -0400447 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700448 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400449 if (!msr_info->host_initiated) {
450 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
451 return 1;
452 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
453 return 1;
454 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100455
456 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800457 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100458 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100459}
460EXPORT_SYMBOL_GPL(kvm_set_apic_base);
461
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +0200462asmlinkage __visible noinstr void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000463{
464 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200465 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000466}
467EXPORT_SYMBOL_GPL(kvm_spurious_fault);
468
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200469#define EXCPT_BENIGN 0
470#define EXCPT_CONTRIBUTORY 1
471#define EXCPT_PF 2
472
473static int exception_class(int vector)
474{
475 switch (vector) {
476 case PF_VECTOR:
477 return EXCPT_PF;
478 case DE_VECTOR:
479 case TS_VECTOR:
480 case NP_VECTOR:
481 case SS_VECTOR:
482 case GP_VECTOR:
483 return EXCPT_CONTRIBUTORY;
484 default:
485 break;
486 }
487 return EXCPT_BENIGN;
488}
489
Nadav Amitd6e8c852014-07-24 14:51:24 +0300490#define EXCPT_FAULT 0
491#define EXCPT_TRAP 1
492#define EXCPT_ABORT 2
493#define EXCPT_INTERRUPT 3
494
495static int exception_type(int vector)
496{
497 unsigned int mask;
498
499 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
500 return EXCPT_INTERRUPT;
501
502 mask = 1 << vector;
503
504 /* #DB is trap, as instruction watchpoints are handled elsewhere */
505 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
506 return EXCPT_TRAP;
507
508 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
509 return EXCPT_ABORT;
510
511 /* Reserved exceptions will result in fault */
512 return EXCPT_FAULT;
513}
514
Jim Mattsonda998b42018-10-16 14:29:22 -0700515void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
516{
517 unsigned nr = vcpu->arch.exception.nr;
518 bool has_payload = vcpu->arch.exception.has_payload;
519 unsigned long payload = vcpu->arch.exception.payload;
520
521 if (!has_payload)
522 return;
523
524 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700525 case DB_VECTOR:
526 /*
527 * "Certain debug exceptions may clear bit 0-3. The
528 * remaining contents of the DR6 register are never
529 * cleared by the processor".
530 */
531 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
532 /*
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800533 * In order to reflect the #DB exception payload in guest
534 * dr6, three components need to be considered: active low
535 * bit, FIXED_1 bits and active high bits (e.g. DR6_BD,
536 * DR6_BS and DR6_BT)
537 * DR6_ACTIVE_LOW contains the FIXED_1 and active low bits.
538 * In the target guest dr6:
539 * FIXED_1 bits should always be set.
540 * Active low bits should be cleared if 1-setting in payload.
541 * Active high bits should be set if 1-setting in payload.
542 *
543 * Note, the payload is compatible with the pending debug
544 * exceptions/exit qualification under VMX, that active_low bits
545 * are active high in payload.
546 * So they need to be flipped for DR6.
Jim Mattsonf10c7292018-10-16 14:29:23 -0700547 */
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800548 vcpu->arch.dr6 |= DR6_ACTIVE_LOW;
Jim Mattsonf10c7292018-10-16 14:29:23 -0700549 vcpu->arch.dr6 |= payload;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800550 vcpu->arch.dr6 ^= payload & DR6_ACTIVE_LOW;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800551
552 /*
553 * The #DB payload is defined as compatible with the 'pending
554 * debug exceptions' field under VMX, not DR6. While bit 12 is
555 * defined in the 'pending debug exceptions' field (enabled
556 * breakpoint), it is reserved and must be zero in DR6.
557 */
558 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700559 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700560 case PF_VECTOR:
561 vcpu->arch.cr2 = payload;
562 break;
563 }
564
565 vcpu->arch.exception.has_payload = false;
566 vcpu->arch.exception.payload = 0;
567}
568EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
569
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200570static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200571 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700572 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200573{
574 u32 prev_nr;
575 int class1, class2;
576
Avi Kivity3842d132010-07-27 12:30:24 +0300577 kvm_make_request(KVM_REQ_EVENT, vcpu);
578
Wanpeng Li664f8e22017-08-24 03:35:09 -0700579 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200580 queue:
Wanpeng Li664f8e22017-08-24 03:35:09 -0700581 if (reinject) {
582 /*
583 * On vmentry, vcpu->arch.exception.pending is only
584 * true if an event injection was blocked by
585 * nested_run_pending. In that case, however,
586 * vcpu_enter_guest requests an immediate exit,
587 * and the guest shouldn't proceed far enough to
588 * need reinjection.
589 */
590 WARN_ON_ONCE(vcpu->arch.exception.pending);
591 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700592 if (WARN_ON_ONCE(has_payload)) {
593 /*
594 * A reinjected event has already
595 * delivered its payload.
596 */
597 has_payload = false;
598 payload = 0;
599 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700600 } else {
601 vcpu->arch.exception.pending = true;
602 vcpu->arch.exception.injected = false;
603 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200604 vcpu->arch.exception.has_error_code = has_error;
605 vcpu->arch.exception.nr = nr;
606 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700607 vcpu->arch.exception.has_payload = has_payload;
608 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800609 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700610 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200611 return;
612 }
613
614 /* to check exception */
615 prev_nr = vcpu->arch.exception.nr;
616 if (prev_nr == DF_VECTOR) {
617 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300618 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200619 return;
620 }
621 class1 = exception_class(prev_nr);
622 class2 = exception_class(nr);
623 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
624 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700625 /*
626 * Generate double fault per SDM Table 5-5. Set
627 * exception.pending = true so that the double fault
628 * can trigger a nested vmexit.
629 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200630 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700631 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200632 vcpu->arch.exception.has_error_code = true;
633 vcpu->arch.exception.nr = DF_VECTOR;
634 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700635 vcpu->arch.exception.has_payload = false;
636 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200637 } else
638 /* replace previous exception with a new one in a hope
639 that instruction re-execution will regenerate lost
640 exception */
641 goto queue;
642}
643
Avi Kivity298101d2007-11-25 13:41:11 +0200644void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
645{
Jim Mattson91e86d22018-10-16 14:29:21 -0700646 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200647}
648EXPORT_SYMBOL_GPL(kvm_queue_exception);
649
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200650void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
651{
Jim Mattson91e86d22018-10-16 14:29:21 -0700652 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200653}
654EXPORT_SYMBOL_GPL(kvm_requeue_exception);
655
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400656void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
657 unsigned long payload)
Jim Mattsonf10c7292018-10-16 14:29:23 -0700658{
659 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
660}
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400661EXPORT_SYMBOL_GPL(kvm_queue_exception_p);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700662
Jim Mattsonda998b42018-10-16 14:29:22 -0700663static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
664 u32 error_code, unsigned long payload)
665{
666 kvm_multiple_exception(vcpu, nr, true, error_code,
667 true, payload, false);
668}
669
Kyle Huey6affcbe2016-11-29 12:40:40 -0800670int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200671{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100672 if (err)
673 kvm_inject_gp(vcpu, 0);
674 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800675 return kvm_skip_emulated_instruction(vcpu);
676
677 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100678}
679EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200680
Avi Kivity6389ee92010-11-29 16:12:30 +0200681void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200682{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200683 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700684 vcpu->arch.exception.nested_apf =
685 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700686 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700687 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700688 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
689 } else {
690 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
691 fault->address);
692 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200693}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300694EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200695
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700696bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
697 struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200698{
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400699 struct kvm_mmu *fault_mmu;
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700700 WARN_ON_ONCE(fault->vector != PF_VECTOR);
701
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400702 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu :
703 vcpu->arch.walk_mmu;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200704
Junaid Shahidee1fa202020-03-20 14:28:03 -0700705 /*
706 * Invalidate the TLB entry for the faulting address, if it exists,
707 * else the access will fault indefinitely (and to emulate hardware).
708 */
709 if ((fault->error_code & PFERR_PRESENT_MASK) &&
710 !(fault->error_code & PFERR_RSVD_MASK))
711 kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address,
712 fault_mmu->root_hpa);
713
714 fault_mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200715 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200716}
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700717EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200718
Sheng Yang3419ffc2008-05-15 09:52:48 +0800719void kvm_inject_nmi(struct kvm_vcpu *vcpu)
720{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300721 atomic_inc(&vcpu->arch.nmi_queued);
722 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800723}
724EXPORT_SYMBOL_GPL(kvm_inject_nmi);
725
Avi Kivity298101d2007-11-25 13:41:11 +0200726void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
727{
Jim Mattson91e86d22018-10-16 14:29:21 -0700728 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200729}
730EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
731
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200732void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
733{
Jim Mattson91e86d22018-10-16 14:29:21 -0700734 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200735}
736EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
737
Carsten Ottea03490e2007-10-29 16:09:35 +0100738/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300739 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
740 * a #GP and return false.
741 */
742bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200743{
Jason Baronb36464772021-01-14 22:27:56 -0500744 if (static_call(kvm_x86_get_cpl)(vcpu) <= required_cpl)
Avi Kivity0a79b002009-09-01 12:03:25 +0300745 return true;
746 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
747 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100748}
Avi Kivity0a79b002009-09-01 12:03:25 +0300749EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100750
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300751bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
752{
753 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
754 return true;
755
756 kvm_queue_exception(vcpu, UD_VECTOR);
757 return false;
758}
759EXPORT_SYMBOL_GPL(kvm_require_dr);
760
Carsten Ottea03490e2007-10-29 16:09:35 +0100761/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200762 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200763 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200764 * can read from guest physical or from the guest's guest physical memory.
765 */
766int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
767 gfn_t ngfn, void *data, int offset, int len,
768 u32 access)
769{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200770 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200771 gfn_t real_gfn;
772 gpa_t ngpa;
773
774 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200775 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200776 if (real_gfn == UNMAPPED_GVA)
777 return -EFAULT;
778
779 real_gfn = gpa_to_gfn(real_gfn);
780
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200781 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200782}
783EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
784
Fengguang Wu69b00492015-01-19 22:33:39 +0800785static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200786 void *data, int offset, int len, u32 access)
787{
788 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
789 data, offset, len, access);
790}
791
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700792static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
793{
Sean Christopherson5b7f5752021-02-03 16:01:13 -0800794 return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2);
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700795}
796
Joerg Roedelec92fe42010-09-10 17:30:51 +0200797/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700798 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100799 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200800int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100801{
802 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
803 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
804 int i;
805 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200806 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100807
Joerg Roedelff03a072010-09-10 17:30:57 +0200808 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
809 offset * sizeof(u64), sizeof(pdpte),
810 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100811 if (ret < 0) {
812 ret = 0;
813 goto out;
814 }
815 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400816 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700817 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100818 ret = 0;
819 goto out;
820 }
821 }
822 ret = 1;
823
Joerg Roedelff03a072010-09-10 17:30:57 +0200824 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700825 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
826
Carsten Ottea03490e2007-10-29 16:09:35 +0100827out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100828
829 return ret;
830}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100831EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100832
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100833bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200834{
Joerg Roedelff03a072010-09-10 17:30:57 +0200835 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200836 int offset;
837 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200838 int r;
839
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200840 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200841 return false;
842
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700843 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300844 return true;
845
Paolo Bonzinia5121772017-07-24 18:54:38 +0200846 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
847 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200848 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
849 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200850 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800851 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200852
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800853 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200854}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100855EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200856
Tom Lendackyf27ad382020-12-10 11:09:56 -0600857void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned long cr0)
858{
859 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
860
861 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
862 kvm_clear_async_pf_completion_queue(vcpu);
863 kvm_async_pf_hash_reset(vcpu);
864 }
865
866 if ((cr0 ^ old_cr0) & update_bits)
867 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)
949 __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)
963 __write_pkru(vcpu->arch.host_pkru);
964 }
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{
1045 unsigned long mmu_role_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
1046 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
1047
1048 if (((cr4 ^ old_cr4) & mmu_role_bits) ||
1049 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
1050 kvm_mmu_reset_context(vcpu);
1051}
1052EXPORT_SYMBOL_GPL(kvm_post_set_cr4);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001053
Avi Kivitya83b29c2010-06-10 17:02:15 +03001054int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +01001055{
Avi Kivityfc78f512009-12-07 12:16:48 +02001056 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +08001057 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Jim Mattsoncb957ad2020-08-17 11:16:54 -07001058 X86_CR4_SMEP;
Xiao Guangrong0be02262015-05-11 22:55:21 +08001059
Sean Christophersonee69c922020-10-06 18:44:16 -07001060 if (!kvm_is_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02001061 return 1;
1062
Carsten Ottea03490e2007-10-29 16:09:35 +01001063 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +03001064 if (!(cr4 & X86_CR4_PAE))
1065 return 1;
Sean Christophersond74fcfc2020-07-02 19:17:14 -07001066 if ((cr4 ^ old_cr4) & X86_CR4_LA57)
1067 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +03001068 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
1069 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +02001070 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
1071 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +03001072 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001073
Mao, Junjiead756a12012-07-02 01:18:48 +00001074 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +02001075 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +00001076 return 1;
1077
1078 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
1079 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
1080 return 1;
1081 }
1082
Jason Baronb36464772021-01-14 22:27:56 -05001083 static_call(kvm_x86_set_cr4)(vcpu, cr4);
Gleb Natapov0f122442010-04-28 19:15:31 +03001084
Tom Lendacky5b51cb12020-12-10 11:09:57 -06001085 kvm_post_set_cr4(vcpu, old_cr4, cr4);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001086
Gleb Natapov0f122442010-04-28 19:15:31 +03001087 return 0;
1088}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001089EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +01001090
Avi Kivity23902182010-06-10 17:02:16 +03001091int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +01001092{
Junaid Shahidade61e22018-06-27 14:59:15 -07001093 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +01001094#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -07001095 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
1096
Junaid Shahidade61e22018-06-27 14:59:15 -07001097 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001098 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1099 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001100 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001101#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001102
Avi Kivity9f8fe502010-12-05 17:30:00 +02001103 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001104 if (!skip_tlb_flush) {
1105 kvm_mmu_sync_roots(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001106 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001107 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001108 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001109 }
1110
Sean Christopherson886bbcc2021-04-21 19:21:21 -07001111 /*
1112 * Do not condition the GPA check on long mode, this helper is used to
1113 * stuff CR3, e.g. for RSM emulation, and there is no guarantee that
1114 * the current vCPU mode is accurate.
1115 */
1116 if (kvm_vcpu_is_illegal_gpa(vcpu, cr3))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001117 return 1;
Sean Christopherson886bbcc2021-04-21 19:21:21 -07001118
1119 if (is_pae_paging(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001120 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001121
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07001122 kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001123 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001124 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001125
Gleb Natapov0f122442010-04-28 19:15:31 +03001126 return 0;
1127}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001128EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001129
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001130int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001131{
Gleb Natapov0f122442010-04-28 19:15:31 +03001132 if (cr8 & CR8_RESERVED_BITS)
1133 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001134 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001135 kvm_lapic_set_tpr(vcpu, cr8);
1136 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001137 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001138 return 0;
1139}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001140EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001141
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001142unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001143{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001144 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001145 return kvm_lapic_get_cr8(vcpu);
1146 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001147 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001148}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001149EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001150
Nadav Amitae561ed2015-04-02 03:10:37 +03001151static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1152{
1153 int i;
1154
1155 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1156 for (i = 0; i < KVM_NR_DB_REGS; i++)
1157 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1158 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1159 }
1160}
1161
Paolo Bonzini7c866632020-05-16 08:42:28 -04001162void kvm_update_dr7(struct kvm_vcpu *vcpu)
Jan Kiszkac8639012012-09-21 05:42:55 +02001163{
1164 unsigned long dr7;
1165
1166 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1167 dr7 = vcpu->arch.guest_debug_dr7;
1168 else
1169 dr7 = vcpu->arch.dr7;
Jason Baronb36464772021-01-14 22:27:56 -05001170 static_call(kvm_x86_set_dr7)(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001171 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1172 if (dr7 & DR7_BP_EN_MASK)
1173 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001174}
Paolo Bonzini7c866632020-05-16 08:42:28 -04001175EXPORT_SYMBOL_GPL(kvm_update_dr7);
Jan Kiszkac8639012012-09-21 05:42:55 +02001176
Nadav Amit6f43ed02014-07-15 17:37:46 +03001177static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1178{
1179 u64 fixed = DR6_FIXED_1;
1180
Radim Krčmářd6321d42017-08-05 00:12:49 +02001181 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001182 fixed |= DR6_RTM;
Chenyi Qiange8ea85f2021-02-02 17:04:32 +08001183
1184 if (!guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))
1185 fixed |= DR6_BUS_LOCK;
Nadav Amit6f43ed02014-07-15 17:37:46 +03001186 return fixed;
1187}
1188
Paolo Bonzini996ff542020-12-14 07:49:54 -05001189int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001190{
Marios Pomonisea740052019-12-11 12:47:52 -08001191 size_t size = ARRAY_SIZE(vcpu->arch.db);
1192
Gleb Natapov020df072010-04-13 10:05:23 +03001193 switch (dr) {
1194 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001195 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001196 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1197 vcpu->arch.eff_db[dr] = val;
1198 break;
1199 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001200 case 6:
Krish Sadhukhanf5f61452020-05-22 18:19:51 -04001201 if (!kvm_dr6_valid(val))
Paolo Bonzini996ff542020-12-14 07:49:54 -05001202 return 1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001203 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001204 break;
1205 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001206 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001207 if (!kvm_dr7_valid(val))
Paolo Bonzini996ff542020-12-14 07:49:54 -05001208 return 1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001209 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001210 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001211 break;
1212 }
1213
1214 return 0;
1215}
1216EXPORT_SYMBOL_GPL(kvm_set_dr);
1217
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05001218void kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001219{
Marios Pomonisea740052019-12-11 12:47:52 -08001220 size_t size = ARRAY_SIZE(vcpu->arch.db);
1221
Gleb Natapov020df072010-04-13 10:05:23 +03001222 switch (dr) {
1223 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001224 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001225 break;
1226 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001227 case 6:
Paolo Bonzini5679b802020-05-04 11:28:25 -04001228 *val = vcpu->arch.dr6;
Gleb Natapov020df072010-04-13 10:05:23 +03001229 break;
1230 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001231 default: /* 7 */
1232 *val = vcpu->arch.dr7;
1233 break;
1234 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001235}
Gleb Natapov020df072010-04-13 10:05:23 +03001236EXPORT_SYMBOL_GPL(kvm_get_dr);
1237
Sean Christophersonc483c452021-02-04 16:57:48 -08001238int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu)
Avi Kivity022cd0e2011-11-10 14:57:23 +02001239{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001240 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001241 u64 data;
Avi Kivity022cd0e2011-11-10 14:57:23 +02001242
Sean Christophersonc483c452021-02-04 16:57:48 -08001243 if (kvm_pmu_rdpmc(vcpu, ecx, &data)) {
1244 kvm_inject_gp(vcpu, 0);
1245 return 1;
1246 }
1247
Sean Christophersonde3cd112019-04-30 10:36:17 -07001248 kvm_rax_write(vcpu, (u32)data);
1249 kvm_rdx_write(vcpu, data >> 32);
Sean Christophersonc483c452021-02-04 16:57:48 -08001250 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001251}
Sean Christophersonc483c452021-02-04 16:57:48 -08001252EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001253
Carsten Otte043405e2007-10-10 17:16:19 +02001254/*
1255 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1256 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1257 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001258 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1259 * extract the supported MSRs from the related const lists.
1260 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001261 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001262 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001263 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001264 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001265
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001266static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001267 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001268 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001269#ifdef CONFIG_X86_64
1270 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1271#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001272 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001273 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001274 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001275 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1276 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1277 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1278 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1279 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1280 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001281 MSR_IA32_UMWAIT_CONTROL,
1282
Jim Mattsone2ada662019-08-21 11:20:04 -07001283 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1284 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1285 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1286 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1287 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1288 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1289 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1290 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1291 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1292 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1293 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1294 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1295 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001296 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1297 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1298 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1299 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1300 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1301 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1302 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1303 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1304 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001305};
1306
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001307static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001308static unsigned num_msrs_to_save;
1309
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001310static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001311 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1312 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1313 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1314 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001315 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001316 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1317 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001318 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001319 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001320 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001321 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001322 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001323 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001324 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1325 HV_X64_MSR_TSC_EMULATION_STATUS,
Jon Doronf97f5a52020-05-29 16:45:40 +03001326 HV_X64_MSR_SYNDBG_OPTIONS,
1327 HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS,
1328 HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER,
1329 HV_X64_MSR_SYNDBG_PENDING_BUFFER,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001330
1331 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02001332 MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001333
Will Auldba904632012-11-29 12:42:50 -08001334 MSR_IA32_TSC_ADJUST,
Dave Hansen09141ec2020-03-05 09:47:06 -08001335 MSR_IA32_TSC_DEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001336 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001337 MSR_IA32_PERF_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001338 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001339 MSR_IA32_MCG_STATUS,
1340 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001341 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001342 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001343 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001344 MSR_PLATFORM_INFO,
1345 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001346 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001347 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001348 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001349
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001350 /*
1351 * The following list leaves out MSRs whose values are determined
1352 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1353 * We always support the "true" VMX control MSRs, even if the host
1354 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001355 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001356 */
1357 MSR_IA32_VMX_BASIC,
1358 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1359 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1360 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1361 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1362 MSR_IA32_VMX_MISC,
1363 MSR_IA32_VMX_CR0_FIXED0,
1364 MSR_IA32_VMX_CR4_FIXED0,
1365 MSR_IA32_VMX_VMCS_ENUM,
1366 MSR_IA32_VMX_PROCBASED_CTLS2,
1367 MSR_IA32_VMX_EPT_VPID_CAP,
1368 MSR_IA32_VMX_VMFUNC,
1369
Borislav Petkov191c8132019-04-18 18:32:50 +02001370 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001371 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001372};
1373
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001374static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001375static unsigned num_emulated_msrs;
1376
Tom Lendacky801e4592018-02-21 13:39:51 -06001377/*
1378 * List of msr numbers which are used to expose MSR-based features that
1379 * can be used by a hypervisor to validate requested CPU features.
1380 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001381static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001382 MSR_IA32_VMX_BASIC,
1383 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1384 MSR_IA32_VMX_PINBASED_CTLS,
1385 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1386 MSR_IA32_VMX_PROCBASED_CTLS,
1387 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1388 MSR_IA32_VMX_EXIT_CTLS,
1389 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1390 MSR_IA32_VMX_ENTRY_CTLS,
1391 MSR_IA32_VMX_MISC,
1392 MSR_IA32_VMX_CR0_FIXED0,
1393 MSR_IA32_VMX_CR0_FIXED1,
1394 MSR_IA32_VMX_CR4_FIXED0,
1395 MSR_IA32_VMX_CR4_FIXED1,
1396 MSR_IA32_VMX_VMCS_ENUM,
1397 MSR_IA32_VMX_PROCBASED_CTLS2,
1398 MSR_IA32_VMX_EPT_VPID_CAP,
1399 MSR_IA32_VMX_VMFUNC,
1400
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001401 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001402 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001403 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001404 MSR_IA32_PERF_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001405};
1406
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001407static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001408static unsigned int num_msr_based_features;
1409
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001410static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001411{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001412 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001413
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001414 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1415 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001416
1417 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001418 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1419 * the nested hypervisor runs with NX huge pages. If it is not,
Ingo Molnard9f6e122021-03-18 15:28:01 +01001420 * L1 is anyway vulnerable to ITLB_MULTIHIT exploits from other
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001421 * L1 guests, so it need not worry about its own (L2) guests.
1422 */
1423 data |= ARCH_CAP_PSCHANGE_MC_NO;
1424
1425 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001426 * If we're doing cache flushes (either "always" or "cond")
1427 * we will do one whenever the guest does a vmlaunch/vmresume.
1428 * If an outer hypervisor is doing the cache flush for us
1429 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1430 * capability to the guest too, and if EPT is disabled we're not
1431 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1432 * require a nested hypervisor to do a flush of its own.
1433 */
1434 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1435 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1436
Paolo Bonzini0c549142019-08-19 17:24:07 +02001437 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1438 data |= ARCH_CAP_RDCL_NO;
1439 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1440 data |= ARCH_CAP_SSB_NO;
1441 if (!boot_cpu_has_bug(X86_BUG_MDS))
1442 data |= ARCH_CAP_MDS_NO;
1443
Paolo Bonzini71316362021-01-28 11:45:00 -05001444 if (!boot_cpu_has(X86_FEATURE_RTM)) {
1445 /*
1446 * If RTM=0 because the kernel has disabled TSX, the host might
1447 * have TAA_NO or TSX_CTRL. Clear TAA_NO (the guest sees RTM=0
1448 * and therefore knows that there cannot be TAA) but keep
1449 * TSX_CTRL: some buggy userspaces leave it set on tsx=on hosts,
1450 * and we want to allow migrating those guests to tsx=off hosts.
1451 */
1452 data &= ~ARCH_CAP_TAA_NO;
1453 } else if (!boot_cpu_has_bug(X86_BUG_TAA)) {
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001454 data |= ARCH_CAP_TAA_NO;
Paolo Bonzini71316362021-01-28 11:45:00 -05001455 } else {
1456 /*
1457 * Nothing to do here; we emulate TSX_CTRL if present on the
1458 * host so the guest can choose between disabling TSX or
1459 * using VERW to clear CPU buffers.
1460 */
1461 }
Pawan Guptae1d38b62019-10-23 12:23:33 +02001462
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001463 return data;
1464}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001465
Wanpeng Li66421c12018-02-28 14:03:30 +08001466static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1467{
1468 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001469 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001470 msr->data = kvm_get_arch_capabilities();
1471 break;
1472 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001473 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001474 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001475 default:
Jason Baronb36464772021-01-14 22:27:56 -05001476 return static_call(kvm_x86_get_msr_feature)(msr);
Wanpeng Li66421c12018-02-28 14:03:30 +08001477 }
1478 return 0;
1479}
1480
Tom Lendacky801e4592018-02-21 13:39:51 -06001481static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1482{
1483 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001484 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001485
1486 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001487 r = kvm_get_msr_feature(&msr);
Peter Xu12bc2132020-06-22 18:04:42 -04001488
1489 if (r == KVM_MSR_RET_INVALID) {
1490 /* Unconditionally clear the output for simplicity */
1491 *data = 0;
Haiwei Lid6328262021-03-13 13:10:32 +08001492 if (kvm_msr_ignored_check(index, 0, false))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001493 r = 0;
Peter Xu12bc2132020-06-22 18:04:42 -04001494 }
1495
Wanpeng Li66421c12018-02-28 14:03:30 +08001496 if (r)
1497 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001498
1499 *data = msr.data;
1500
1501 return 0;
1502}
1503
Sean Christopherson11988492019-04-02 08:19:15 -07001504static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1505{
1506 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1507 return false;
1508
1509 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1510 return false;
1511
Sean Christopherson0a629562019-04-02 08:19:16 -07001512 if (efer & (EFER_LME | EFER_LMA) &&
1513 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1514 return false;
1515
1516 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1517 return false;
1518
Sean Christopherson11988492019-04-02 08:19:15 -07001519 return true;
1520
1521}
Jan Kiszka384bb782013-04-20 10:52:36 +02001522bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001523{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001524 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001525 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001526
Sean Christopherson11988492019-04-02 08:19:15 -07001527 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001528}
1529EXPORT_SYMBOL_GPL(kvm_valid_efer);
1530
Sean Christopherson11988492019-04-02 08:19:15 -07001531static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001532{
1533 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001534 u64 efer = msr_info->data;
Maxim Levitsky72f211e2020-10-01 14:29:53 +03001535 int r;
Jan Kiszka384bb782013-04-20 10:52:36 +02001536
Sean Christopherson11988492019-04-02 08:19:15 -07001537 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001538 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001539
Sean Christopherson11988492019-04-02 08:19:15 -07001540 if (!msr_info->host_initiated) {
1541 if (!__kvm_valid_efer(vcpu, efer))
1542 return 1;
1543
1544 if (is_paging(vcpu) &&
1545 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1546 return 1;
1547 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001548
Carsten Otte15c4a642007-10-30 18:44:17 +01001549 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001550 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001551
Jason Baronb36464772021-01-14 22:27:56 -05001552 r = static_call(kvm_x86_set_efer)(vcpu, efer);
Maxim Levitsky72f211e2020-10-01 14:29:53 +03001553 if (r) {
1554 WARN_ON(r > 0);
1555 return r;
1556 }
Sheng Yanga3d204e2010-05-12 16:40:40 +08001557
Sheng Yangaad82702010-05-12 16:40:42 +08001558 /* Update reserved bits */
1559 if ((efer ^ old_efer) & EFER_NX)
1560 kvm_mmu_reset_context(vcpu);
1561
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001562 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001563}
1564
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001565void kvm_enable_efer_bits(u64 mask)
1566{
1567 efer_reserved_bits &= ~mask;
1568}
1569EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1570
Alexander Graf51de8152020-09-25 16:34:17 +02001571bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type)
1572{
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001573 struct kvm_x86_msr_filter *msr_filter;
1574 struct msr_bitmap_range *ranges;
Alexander Graf1a1552542020-09-25 16:34:21 +02001575 struct kvm *kvm = vcpu->kvm;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001576 bool allowed;
Alexander Graf1a1552542020-09-25 16:34:21 +02001577 int idx;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001578 u32 i;
Alexander Graf1a1552542020-09-25 16:34:21 +02001579
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001580 /* x2APIC MSRs do not support filtering. */
1581 if (index >= 0x800 && index <= 0x8ff)
Alexander Graf1a1552542020-09-25 16:34:21 +02001582 return true;
1583
Alexander Graf1a1552542020-09-25 16:34:21 +02001584 idx = srcu_read_lock(&kvm->srcu);
1585
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001586 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu);
1587 if (!msr_filter) {
1588 allowed = true;
1589 goto out;
1590 }
1591
1592 allowed = msr_filter->default_allow;
1593 ranges = msr_filter->ranges;
1594
1595 for (i = 0; i < msr_filter->count; i++) {
Alexander Graf1a1552542020-09-25 16:34:21 +02001596 u32 start = ranges[i].base;
1597 u32 end = start + ranges[i].nmsrs;
1598 u32 flags = ranges[i].flags;
1599 unsigned long *bitmap = ranges[i].bitmap;
1600
1601 if ((index >= start) && (index < end) && (flags & type)) {
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001602 allowed = !!test_bit(index - start, bitmap);
Alexander Graf1a1552542020-09-25 16:34:21 +02001603 break;
1604 }
1605 }
1606
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001607out:
Alexander Graf1a1552542020-09-25 16:34:21 +02001608 srcu_read_unlock(&kvm->srcu, idx);
1609
Sean Christophersonb318e8d2021-03-16 11:44:33 -07001610 return allowed;
Alexander Graf51de8152020-09-25 16:34:17 +02001611}
1612EXPORT_SYMBOL_GPL(kvm_msr_allowed);
1613
Carsten Otte15c4a642007-10-30 18:44:17 +01001614/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001615 * Write @data into the MSR specified by @index. Select MSR specific fault
1616 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001617 * Returns 0 on success, non-0 otherwise.
1618 * Assumes vcpu_load() was already called.
1619 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001620static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1621 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001622{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001623 struct msr_data msr;
1624
Alexander Graf1a1552542020-09-25 16:34:21 +02001625 if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_WRITE))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001626 return KVM_MSR_RET_FILTERED;
Alexander Graf1a1552542020-09-25 16:34:21 +02001627
Sean Christophersonf20935d2019-09-05 14:22:54 -07001628 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001629 case MSR_FS_BASE:
1630 case MSR_GS_BASE:
1631 case MSR_KERNEL_GS_BASE:
1632 case MSR_CSTAR:
1633 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001634 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001635 return 1;
1636 break;
1637 case MSR_IA32_SYSENTER_EIP:
1638 case MSR_IA32_SYSENTER_ESP:
1639 /*
1640 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1641 * non-canonical address is written on Intel but not on
1642 * AMD (which ignores the top 32-bits, because it does
1643 * not implement 64-bit SYSENTER).
1644 *
1645 * 64-bit code should hence be able to write a non-canonical
1646 * value on AMD. Making the address canonical ensures that
1647 * vmentry does not fail on Intel after writing a non-canonical
1648 * value, and that something deterministic happens if the guest
1649 * invokes 64-bit SYSENTER.
1650 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001651 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Sean Christopherson61a05d42021-05-04 10:17:33 -07001652 break;
1653 case MSR_TSC_AUX:
1654 if (!kvm_is_supported_user_return_msr(MSR_TSC_AUX))
1655 return 1;
1656
1657 if (!host_initiated &&
1658 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) &&
1659 !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))
1660 return 1;
1661
1662 /*
1663 * Per Intel's SDM, bits 63:32 are reserved, but AMD's APM has
1664 * incomplete and conflicting architectural behavior. Current
1665 * AMD CPUs completely ignore bits 63:32, i.e. they aren't
1666 * reserved and always read as zeros. Enforce Intel's reserved
1667 * bits check if and only if the guest CPU is Intel, and clear
1668 * the bits in all other cases. This ensures cross-vendor
1669 * migration will provide consistent behavior for the guest.
1670 */
1671 if (guest_cpuid_is_intel(vcpu) && (data >> 32) != 0)
1672 return 1;
1673
1674 data = (u32)data;
1675 break;
Nadav Amit854e8bb2014-09-16 03:24:05 +03001676 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001677
1678 msr.data = data;
1679 msr.index = index;
1680 msr.host_initiated = host_initiated;
1681
Jason Baronb36464772021-01-14 22:27:56 -05001682 return static_call(kvm_x86_set_msr)(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001683}
1684
Peter Xu6abe9c12020-06-22 18:04:41 -04001685static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu,
1686 u32 index, u64 data, bool host_initiated)
1687{
1688 int ret = __kvm_set_msr(vcpu, index, data, host_initiated);
1689
1690 if (ret == KVM_MSR_RET_INVALID)
Haiwei Lid6328262021-03-13 13:10:32 +08001691 if (kvm_msr_ignored_check(index, data, true))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001692 ret = 0;
Peter Xu6abe9c12020-06-22 18:04:41 -04001693
1694 return ret;
1695}
1696
Sean Christophersonf20935d2019-09-05 14:22:54 -07001697/*
1698 * Read the MSR specified by @index into @data. Select MSR specific fault
1699 * checks are bypassed if @host_initiated is %true.
1700 * Returns 0 on success, non-0 otherwise.
1701 * Assumes vcpu_load() was already called.
1702 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001703int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1704 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001705{
1706 struct msr_data msr;
1707 int ret;
1708
Alexander Graf1a1552542020-09-25 16:34:21 +02001709 if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_READ))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001710 return KVM_MSR_RET_FILTERED;
Alexander Graf1a1552542020-09-25 16:34:21 +02001711
Sean Christopherson61a05d42021-05-04 10:17:33 -07001712 switch (index) {
1713 case MSR_TSC_AUX:
1714 if (!kvm_is_supported_user_return_msr(MSR_TSC_AUX))
1715 return 1;
1716
1717 if (!host_initiated &&
1718 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) &&
1719 !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))
1720 return 1;
1721 break;
1722 }
1723
Sean Christophersonf20935d2019-09-05 14:22:54 -07001724 msr.index = index;
1725 msr.host_initiated = host_initiated;
1726
Jason Baronb36464772021-01-14 22:27:56 -05001727 ret = static_call(kvm_x86_get_msr)(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001728 if (!ret)
1729 *data = msr.data;
1730 return ret;
1731}
1732
Peter Xu6abe9c12020-06-22 18:04:41 -04001733static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu,
1734 u32 index, u64 *data, bool host_initiated)
1735{
1736 int ret = __kvm_get_msr(vcpu, index, data, host_initiated);
1737
1738 if (ret == KVM_MSR_RET_INVALID) {
1739 /* Unconditionally clear *data for simplicity */
1740 *data = 0;
Haiwei Lid6328262021-03-13 13:10:32 +08001741 if (kvm_msr_ignored_check(index, 0, false))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001742 ret = 0;
Peter Xu6abe9c12020-06-22 18:04:41 -04001743 }
1744
1745 return ret;
1746}
1747
Sean Christophersonf20935d2019-09-05 14:22:54 -07001748int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1749{
Peter Xu6abe9c12020-06-22 18:04:41 -04001750 return kvm_get_msr_ignored_check(vcpu, index, data, false);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001751}
1752EXPORT_SYMBOL_GPL(kvm_get_msr);
1753
1754int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1755{
Peter Xu6abe9c12020-06-22 18:04:41 -04001756 return kvm_set_msr_ignored_check(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001757}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001758EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001759
Paolo Bonzini8b474422020-12-14 07:44:46 -05001760static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu)
Alexander Graf1ae09952020-09-25 16:34:16 +02001761{
Paolo Bonzini8b474422020-12-14 07:44:46 -05001762 int err = vcpu->run->msr.error;
1763 if (!err) {
Alexander Graf1ae09952020-09-25 16:34:16 +02001764 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data);
1765 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32);
1766 }
1767
Jason Baronb36464772021-01-14 22:27:56 -05001768 return static_call(kvm_x86_complete_emulated_msr)(vcpu, err);
Alexander Graf1ae09952020-09-25 16:34:16 +02001769}
1770
1771static int complete_emulated_wrmsr(struct kvm_vcpu *vcpu)
1772{
Jason Baronb36464772021-01-14 22:27:56 -05001773 return static_call(kvm_x86_complete_emulated_msr)(vcpu, vcpu->run->msr.error);
Alexander Graf1ae09952020-09-25 16:34:16 +02001774}
1775
1776static u64 kvm_msr_reason(int r)
1777{
1778 switch (r) {
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001779 case KVM_MSR_RET_INVALID:
Alexander Graf1ae09952020-09-25 16:34:16 +02001780 return KVM_MSR_EXIT_REASON_UNKNOWN;
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001781 case KVM_MSR_RET_FILTERED:
Alexander Graf1a1552542020-09-25 16:34:21 +02001782 return KVM_MSR_EXIT_REASON_FILTER;
Alexander Graf1ae09952020-09-25 16:34:16 +02001783 default:
1784 return KVM_MSR_EXIT_REASON_INVAL;
1785 }
1786}
1787
1788static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index,
1789 u32 exit_reason, u64 data,
1790 int (*completion)(struct kvm_vcpu *vcpu),
1791 int r)
1792{
1793 u64 msr_reason = kvm_msr_reason(r);
1794
1795 /* Check if the user wanted to know about this MSR fault */
1796 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason))
1797 return 0;
1798
1799 vcpu->run->exit_reason = exit_reason;
1800 vcpu->run->msr.error = 0;
1801 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad));
1802 vcpu->run->msr.reason = msr_reason;
1803 vcpu->run->msr.index = index;
1804 vcpu->run->msr.data = data;
1805 vcpu->arch.complete_userspace_io = completion;
1806
1807 return 1;
1808}
1809
1810static int kvm_get_msr_user_space(struct kvm_vcpu *vcpu, u32 index, int r)
1811{
1812 return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_RDMSR, 0,
1813 complete_emulated_rdmsr, r);
1814}
1815
1816static int kvm_set_msr_user_space(struct kvm_vcpu *vcpu, u32 index, u64 data, int r)
1817{
1818 return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_WRMSR, data,
1819 complete_emulated_wrmsr, r);
1820}
1821
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001822int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1823{
1824 u32 ecx = kvm_rcx_read(vcpu);
1825 u64 data;
Alexander Graf1ae09952020-09-25 16:34:16 +02001826 int r;
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001827
Alexander Graf1ae09952020-09-25 16:34:16 +02001828 r = kvm_get_msr(vcpu, ecx, &data);
1829
1830 /* MSR read failed? See if we should ask user space */
1831 if (r && kvm_get_msr_user_space(vcpu, ecx, r)) {
1832 /* Bounce to user space */
1833 return 0;
1834 }
1835
Paolo Bonzini8b474422020-12-14 07:44:46 -05001836 if (!r) {
1837 trace_kvm_msr_read(ecx, data);
1838
1839 kvm_rax_write(vcpu, data & -1u);
1840 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1841 } else {
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001842 trace_kvm_msr_read_ex(ecx);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001843 }
1844
Jason Baronb36464772021-01-14 22:27:56 -05001845 return static_call(kvm_x86_complete_emulated_msr)(vcpu, r);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001846}
1847EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1848
1849int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1850{
1851 u32 ecx = kvm_rcx_read(vcpu);
1852 u64 data = kvm_read_edx_eax(vcpu);
Alexander Graf1ae09952020-09-25 16:34:16 +02001853 int r;
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001854
Alexander Graf1ae09952020-09-25 16:34:16 +02001855 r = kvm_set_msr(vcpu, ecx, data);
1856
1857 /* MSR write failed? See if we should ask user space */
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03001858 if (r && kvm_set_msr_user_space(vcpu, ecx, data, r))
Alexander Graf1ae09952020-09-25 16:34:16 +02001859 /* Bounce to user space */
1860 return 0;
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03001861
1862 /* Signal all other negative errors to userspace */
1863 if (r < 0)
1864 return r;
Alexander Graf1ae09952020-09-25 16:34:16 +02001865
Paolo Bonzini8b474422020-12-14 07:44:46 -05001866 if (!r)
1867 trace_kvm_msr_write(ecx, data);
1868 else
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001869 trace_kvm_msr_write_ex(ecx, data);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001870
Jason Baronb36464772021-01-14 22:27:56 -05001871 return static_call(kvm_x86_complete_emulated_msr)(vcpu, r);
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001872}
1873EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1874
Sean Christopherson5ff3a352021-02-04 16:57:47 -08001875int kvm_emulate_as_nop(struct kvm_vcpu *vcpu)
1876{
1877 return kvm_skip_emulated_instruction(vcpu);
1878}
1879EXPORT_SYMBOL_GPL(kvm_emulate_as_nop);
1880
1881int kvm_emulate_invd(struct kvm_vcpu *vcpu)
1882{
1883 /* Treat an INVD instruction as a NOP and just skip it. */
1884 return kvm_emulate_as_nop(vcpu);
1885}
1886EXPORT_SYMBOL_GPL(kvm_emulate_invd);
1887
1888int kvm_emulate_mwait(struct kvm_vcpu *vcpu)
1889{
1890 pr_warn_once("kvm: MWAIT instruction emulated as NOP!\n");
1891 return kvm_emulate_as_nop(vcpu);
1892}
1893EXPORT_SYMBOL_GPL(kvm_emulate_mwait);
1894
1895int kvm_handle_invalid_op(struct kvm_vcpu *vcpu)
1896{
1897 kvm_queue_exception(vcpu, UD_VECTOR);
1898 return 1;
1899}
1900EXPORT_SYMBOL_GPL(kvm_handle_invalid_op);
1901
1902int kvm_emulate_monitor(struct kvm_vcpu *vcpu)
1903{
1904 pr_warn_once("kvm: MONITOR instruction emulated as NOP!\n");
1905 return kvm_emulate_as_nop(vcpu);
1906}
1907EXPORT_SYMBOL_GPL(kvm_emulate_monitor);
1908
Paolo Bonzinid89d04a2021-02-02 10:44:23 -05001909static inline bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001910{
Frederic Weisbecker4ae7dc92021-02-01 00:05:48 +01001911 xfer_to_guest_mode_prepare();
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001912 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02001913 xfer_to_guest_mode_work_pending();
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001914}
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001915
Carsten Otte313a3dc2007-10-11 19:16:52 +02001916/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001917 * The fast path for frequent and performance sensitive wrmsr emulation,
1918 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1919 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1920 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1921 * other cases which must be called after interrupts are enabled on the host.
1922 */
1923static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1924{
Wanpeng Lie1be9ac2020-03-26 10:20:01 +08001925 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1926 return 1;
1927
1928 if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001929 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
Wanpeng Li4064a4c2020-04-02 16:20:26 +08001930 ((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
1931 ((u32)(data >> 32) != X2APIC_BROADCAST)) {
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001932
Wanpeng Lid5361672020-03-26 10:20:02 +08001933 data &= ~(1 << 12);
1934 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001935 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001936 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1937 trace_kvm_apic_write(APIC_ICR, (u32)data);
1938 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001939 }
1940
1941 return 1;
1942}
1943
Wanpeng Liae95f562020-04-28 14:23:28 +08001944static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data)
1945{
1946 if (!kvm_can_use_hv_timer(vcpu))
1947 return 1;
1948
1949 kvm_set_lapic_tscdeadline_msr(vcpu, data);
1950 return 0;
1951}
1952
Wanpeng Li404d5d72020-04-28 14:23:25 +08001953fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001954{
1955 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001956 u64 data;
Wanpeng Li404d5d72020-04-28 14:23:25 +08001957 fastpath_t ret = EXIT_FASTPATH_NONE;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001958
1959 switch (msr) {
1960 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001961 data = kvm_read_edx_eax(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08001962 if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) {
1963 kvm_skip_emulated_instruction(vcpu);
1964 ret = EXIT_FASTPATH_EXIT_HANDLED;
Haiwei Li80bc97f2020-05-18 09:31:38 +08001965 }
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001966 break;
Dave Hansen09141ec2020-03-05 09:47:06 -08001967 case MSR_IA32_TSC_DEADLINE:
Wanpeng Liae95f562020-04-28 14:23:28 +08001968 data = kvm_read_edx_eax(vcpu);
1969 if (!handle_fastpath_set_tscdeadline(vcpu, data)) {
1970 kvm_skip_emulated_instruction(vcpu);
1971 ret = EXIT_FASTPATH_REENTER_GUEST;
1972 }
1973 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001974 default:
Wanpeng Li404d5d72020-04-28 14:23:25 +08001975 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001976 }
1977
Wanpeng Li404d5d72020-04-28 14:23:25 +08001978 if (ret != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001979 trace_kvm_msr_write(msr, data);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001980
Wanpeng Li404d5d72020-04-28 14:23:25 +08001981 return ret;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001982}
1983EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1984
1985/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001986 * Adapt set_msr() to msr_io()'s calling convention
1987 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001988static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1989{
Peter Xu6abe9c12020-06-22 18:04:41 -04001990 return kvm_get_msr_ignored_check(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001991}
1992
Carsten Otte313a3dc2007-10-11 19:16:52 +02001993static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1994{
Peter Xu6abe9c12020-06-22 18:04:41 -04001995 return kvm_set_msr_ignored_check(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001996}
1997
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001998#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001999struct pvclock_clock {
2000 int vclock_mode;
2001 u64 cycle_last;
2002 u64 mask;
2003 u32 mult;
2004 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002005 u64 base_cycles;
2006 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002007};
2008
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002009struct pvclock_gtod_data {
2010 seqcount_t seq;
2011
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002012 struct pvclock_clock clock; /* extract of a clocksource struct */
2013 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002014
Paolo Bonzini917f9472020-01-22 14:32:20 +01002015 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002016 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002017};
2018
2019static struct pvclock_gtod_data pvclock_gtod_data;
2020
2021static void update_pvclock_gtod(struct timekeeper *tk)
2022{
2023 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
2024
2025 write_seqcount_begin(&vdata->seq);
2026
2027 /* copy pvclock gtod data */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002028 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Peter Zijlstra876e7882015-03-19 10:09:06 +01002029 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
2030 vdata->clock.mask = tk->tkr_mono.mask;
2031 vdata->clock.mult = tk->tkr_mono.mult;
2032 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002033 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
2034 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002035
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002036 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002037 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
2038 vdata->raw_clock.mask = tk->tkr_raw.mask;
2039 vdata->raw_clock.mult = tk->tkr_raw.mult;
2040 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002041 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
2042 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002043
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002044 vdata->wall_time_sec = tk->xtime_sec;
2045
Paolo Bonzini917f9472020-01-22 14:32:20 +01002046 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002047
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002048 write_seqcount_end(&vdata->seq);
2049}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002050
2051static s64 get_kvmclock_base_ns(void)
2052{
2053 /* Count up from boot time, but with the frequency of the raw clock. */
2054 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
2055}
2056#else
2057static s64 get_kvmclock_base_ns(void)
2058{
2059 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
2060 return ktime_get_boottime_ns();
2061}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002062#endif
2063
Joao Martins629b5342018-06-28 15:06:43 -04002064void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock, int sec_hi_ofs)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002065{
Avi Kivity9ed3c442010-05-04 15:00:37 +03002066 int version;
2067 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002068 struct pvclock_wall_clock wc;
Joao Martins629b5342018-06-28 15:06:43 -04002069 u32 wc_sec_hi;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002070 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002071
2072 if (!wall_clock)
2073 return;
2074
Avi Kivity9ed3c442010-05-04 15:00:37 +03002075 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
2076 if (r)
2077 return;
2078
2079 if (version & 1)
2080 ++version; /* first time write, random junk */
2081
2082 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002083
Nicholas Krause1dab1342015-12-30 13:08:46 -05002084 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
2085 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002086
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002087 /*
2088 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10002089 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002090 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002091 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002092 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002093
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002094 wc.nsec = do_div(wall_nsec, 1000000000);
2095 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002096 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002097
2098 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
2099
Joao Martins629b5342018-06-28 15:06:43 -04002100 if (sec_hi_ofs) {
2101 wc_sec_hi = wall_nsec >> 32;
2102 kvm_write_guest(kvm, wall_clock + sec_hi_ofs,
2103 &wc_sec_hi, sizeof(wc_sec_hi));
2104 }
2105
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002106 version++;
2107 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002108}
2109
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00002110static void kvm_write_system_time(struct kvm_vcpu *vcpu, gpa_t system_time,
2111 bool old_msr, bool host_initiated)
2112{
2113 struct kvm_arch *ka = &vcpu->kvm->arch;
2114
2115 if (vcpu->vcpu_id == 0 && !host_initiated) {
Oliver Upton1e293d12020-10-27 16:10:43 -07002116 if (ka->boot_vcpu_runs_old_kvmclock != old_msr)
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00002117 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
2118
2119 ka->boot_vcpu_runs_old_kvmclock = old_msr;
2120 }
2121
2122 vcpu->arch.time = system_time;
2123 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
2124
2125 /* we verify if the enable bit is set... */
2126 vcpu->arch.pv_time_enabled = false;
2127 if (!(system_time & 1))
2128 return;
2129
2130 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
2131 &vcpu->arch.pv_time, system_time & ~1ULL,
2132 sizeof(struct pvclock_vcpu_time_info)))
2133 vcpu->arch.pv_time_enabled = true;
2134
2135 return;
2136}
2137
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002138static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
2139{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01002140 do_shl32_div32(dividend, divisor);
2141 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002142}
2143
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002144static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002145 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002146{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002147 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002148 int32_t shift = 0;
2149 uint64_t tps64;
2150 uint32_t tps32;
2151
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002152 tps64 = base_hz;
2153 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02002154 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002155 tps64 >>= 1;
2156 shift--;
2157 }
2158
2159 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02002160 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
2161 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002162 scaled64 >>= 1;
2163 else
2164 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002165 shift++;
2166 }
2167
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002168 *pshift = shift;
2169 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002170}
2171
Marcelo Tosattid8281992012-11-27 23:29:01 -02002172#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002173static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002174#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002175
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002176static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08002177static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002178
Zachary Amsdencc578282012-02-03 15:43:50 -02002179static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10002180{
Zachary Amsdencc578282012-02-03 15:43:50 -02002181 u64 v = (u64)khz * (1000000 + ppm);
2182 do_div(v, 1000000);
2183 return v;
2184}
2185
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002186static void kvm_vcpu_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 l1_multiplier);
2187
Haozhong Zhang381d5852015-10-20 15:39:04 +08002188static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
2189{
2190 u64 ratio;
2191
2192 /* Guest TSC same frequency as host TSC? */
2193 if (!scale) {
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002194 kvm_vcpu_write_tsc_multiplier(vcpu, kvm_default_tsc_scaling_ratio);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002195 return 0;
2196 }
2197
2198 /* TSC scaling supported? */
2199 if (!kvm_has_tsc_control) {
2200 if (user_tsc_khz > tsc_khz) {
2201 vcpu->arch.tsc_catchup = 1;
2202 vcpu->arch.tsc_always_catchup = 1;
2203 return 0;
2204 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02002205 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08002206 return -1;
2207 }
2208 }
2209
2210 /* TSC scaling required - calculate ratio */
2211 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
2212 user_tsc_khz, tsc_khz);
2213
2214 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02002215 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
2216 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002217 return -1;
2218 }
2219
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002220 kvm_vcpu_write_tsc_multiplier(vcpu, ratio);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002221 return 0;
2222}
2223
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002224static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02002225{
2226 u32 thresh_lo, thresh_hi;
2227 int use_scaling = 0;
2228
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002229 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002230 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08002231 /* set tsc_scaling_ratio to a safe value */
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002232 kvm_vcpu_write_tsc_multiplier(vcpu, kvm_default_tsc_scaling_ratio);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002233 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08002234 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002235
Zachary Amsdenc2855452010-09-18 14:38:15 -10002236 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002237 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02002238 &vcpu->arch.virtual_tsc_shift,
2239 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002240 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02002241
2242 /*
2243 * Compute the variation in TSC rate which is acceptable
2244 * within the range of tolerance and decide if the
2245 * rate being applied is within that bounds of the hardware
2246 * rate. If so, no scaling or compensation need be done.
2247 */
2248 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
2249 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002250 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
2251 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 -02002252 use_scaling = 1;
2253 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002254 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002255}
2256
2257static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
2258{
Zachary Amsdene26101b2012-02-03 15:43:57 -02002259 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02002260 vcpu->arch.virtual_tsc_mult,
2261 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002262 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002263 return tsc;
2264}
2265
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002266static inline int gtod_is_based_on_tsc(int mode)
2267{
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002268 return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002269}
2270
Fengguang Wu69b00492015-01-19 22:33:39 +08002271static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002272{
2273#ifdef CONFIG_X86_64
2274 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002275 struct kvm_arch *ka = &vcpu->kvm->arch;
2276 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2277
2278 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2279 atomic_read(&vcpu->kvm->online_vcpus));
2280
Marcelo Tosatti7f187922014-11-04 21:30:44 -02002281 /*
2282 * Once the masterclock is enabled, always perform request in
2283 * order to update it.
2284 *
2285 * In order to enable masterclock, the host clocksource must be TSC
2286 * and the vcpus need to have matched TSCs. When that happens,
2287 * perform request to enable masterclock.
2288 */
2289 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002290 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002291 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
2292
2293 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
2294 atomic_read(&vcpu->kvm->online_vcpus),
2295 ka->use_master_clock, gtod->clock.vclock_mode);
2296#endif
2297}
2298
Haozhong Zhang35181e82015-10-20 15:39:03 +08002299/*
2300 * Multiply tsc by a fixed point number represented by ratio.
2301 *
2302 * The most significant 64-N bits (mult) of ratio represent the
2303 * integral part of the fixed point number; the remaining N bits
2304 * (frac) represent the fractional part, ie. ratio represents a fixed
2305 * point number (mult + frac * 2^(-N)).
2306 *
2307 * N equals to kvm_tsc_scaling_ratio_frac_bits.
2308 */
2309static inline u64 __scale_tsc(u64 ratio, u64 tsc)
2310{
2311 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
2312}
2313
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002314u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc, u64 ratio)
Haozhong Zhang35181e82015-10-20 15:39:03 +08002315{
2316 u64 _tsc = tsc;
Haozhong Zhang35181e82015-10-20 15:39:03 +08002317
2318 if (ratio != kvm_default_tsc_scaling_ratio)
2319 _tsc = __scale_tsc(ratio, tsc);
2320
2321 return _tsc;
2322}
2323EXPORT_SYMBOL_GPL(kvm_scale_tsc);
2324
Ilias Stamatis9b399df2021-05-26 19:44:10 +01002325static u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
Haozhong Zhang07c14192015-10-20 15:39:05 +08002326{
2327 u64 tsc;
2328
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002329 tsc = kvm_scale_tsc(vcpu, rdtsc(), vcpu->arch.l1_tsc_scaling_ratio);
Haozhong Zhang07c14192015-10-20 15:39:05 +08002330
2331 return target_tsc - tsc;
2332}
2333
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002334u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
2335{
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002336 return vcpu->arch.l1_tsc_offset +
2337 kvm_scale_tsc(vcpu, host_tsc, vcpu->arch.l1_tsc_scaling_ratio);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002338}
2339EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
2340
Ilias Stamatis83150f22021-05-26 19:44:14 +01002341u64 kvm_calc_nested_tsc_offset(u64 l1_offset, u64 l2_offset, u64 l2_multiplier)
2342{
2343 u64 nested_offset;
2344
2345 if (l2_multiplier == kvm_default_tsc_scaling_ratio)
2346 nested_offset = l1_offset;
2347 else
2348 nested_offset = mul_s64_u64_shr((s64) l1_offset, l2_multiplier,
2349 kvm_tsc_scaling_ratio_frac_bits);
2350
2351 nested_offset += l2_offset;
2352 return nested_offset;
2353}
2354EXPORT_SYMBOL_GPL(kvm_calc_nested_tsc_offset);
2355
2356u64 kvm_calc_nested_tsc_multiplier(u64 l1_multiplier, u64 l2_multiplier)
2357{
2358 if (l2_multiplier != kvm_default_tsc_scaling_ratio)
2359 return mul_u64_u64_shr(l1_multiplier, l2_multiplier,
2360 kvm_tsc_scaling_ratio_frac_bits);
2361
2362 return l1_multiplier;
2363}
2364EXPORT_SYMBOL_GPL(kvm_calc_nested_tsc_multiplier);
2365
Ilias Stamatisedcfe542021-05-26 19:44:15 +01002366static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 l1_offset)
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002367{
Ilias Stamatisedcfe542021-05-26 19:44:15 +01002368 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2369 vcpu->arch.l1_tsc_offset,
2370 l1_offset);
2371
2372 vcpu->arch.l1_tsc_offset = l1_offset;
2373
2374 /*
2375 * If we are here because L1 chose not to trap WRMSR to TSC then
2376 * according to the spec this should set L1's TSC (as opposed to
2377 * setting L1's offset for L2).
2378 */
2379 if (is_guest_mode(vcpu))
2380 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
2381 l1_offset,
2382 static_call(kvm_x86_get_l2_tsc_offset)(vcpu),
2383 static_call(kvm_x86_get_l2_tsc_multiplier)(vcpu));
2384 else
2385 vcpu->arch.tsc_offset = l1_offset;
2386
2387 static_call(kvm_x86_write_tsc_offset)(vcpu, vcpu->arch.tsc_offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002388}
2389
Ilias Stamatis1ab92872021-06-07 11:54:38 +01002390static void kvm_vcpu_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 l1_multiplier)
2391{
2392 vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier;
2393
2394 /* Userspace is changing the multiplier while L2 is active */
2395 if (is_guest_mode(vcpu))
2396 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier(
2397 l1_multiplier,
2398 static_call(kvm_x86_get_l2_tsc_multiplier)(vcpu));
2399 else
2400 vcpu->arch.tsc_scaling_ratio = l1_multiplier;
2401
2402 if (kvm_has_tsc_control)
2403 static_call(kvm_x86_write_tsc_multiplier)(
2404 vcpu, vcpu->arch.tsc_scaling_ratio);
2405}
2406
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002407static inline bool kvm_check_tsc_unstable(void)
2408{
2409#ifdef CONFIG_X86_64
2410 /*
2411 * TSC is marked unstable when we're running on Hyper-V,
2412 * 'TSC page' clocksource is good.
2413 */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002414 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002415 return false;
2416#endif
2417 return check_tsc_unstable();
2418}
2419
Paolo Bonzini0c899c22020-09-24 14:45:27 +02002420static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data)
Zachary Amsden99e3e302010-08-19 22:07:17 -10002421{
2422 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002423 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002424 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002425 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002426 bool already_matched;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002427 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002428
Jan Kiszka038f8c12011-02-04 10:49:11 +01002429 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Ilias Stamatis9b399df2021-05-26 19:44:10 +01002430 offset = kvm_compute_l1_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002431 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002432 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002433
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002434 if (vcpu->arch.virtual_tsc_khz) {
Paolo Bonzini0c899c22020-09-24 14:45:27 +02002435 if (data == 0) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03002436 /*
2437 * detection of vcpu initialization -- need to sync
2438 * with other vCPUs. This particularly helps to keep
2439 * kvm_clock stable after CPU hotplug
2440 */
2441 synchronizing = true;
2442 } else {
2443 u64 tsc_exp = kvm->arch.last_tsc_write +
2444 nsec_to_cycles(vcpu, elapsed);
2445 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
2446 /*
2447 * Special case: TSC write with a small delta (1 second)
2448 * of virtual cycle time against real time is
2449 * interpreted as an attempt to synchronize the CPU.
2450 */
2451 synchronizing = data < tsc_exp + tsc_hz &&
2452 data + tsc_hz > tsc_exp;
2453 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002454 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002455
2456 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002457 * For a reliable TSC, we can match TSC offsets, and for an unstable
2458 * TSC, we add elapsed time in this computation. We could let the
2459 * compensation code attempt to catch up if we fall behind, but
2460 * it's better to try to match offsets from the beginning.
2461 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002462 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002463 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002464 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002465 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002466 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002467 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002468 data += delta;
Ilias Stamatis9b399df2021-05-26 19:44:10 +01002469 offset = kvm_compute_l1_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002470 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002471 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002472 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002473 } else {
2474 /*
2475 * We split periods of matched TSC writes into generations.
2476 * For each generation, we track the original measured
2477 * nanosecond time, offset, and write, so if TSCs are in
2478 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002479 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002480 *
2481 * These values are tracked in kvm->arch.cur_xxx variables.
2482 */
2483 kvm->arch.cur_tsc_generation++;
2484 kvm->arch.cur_tsc_nsec = ns;
2485 kvm->arch.cur_tsc_write = data;
2486 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002487 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002488 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002489
2490 /*
2491 * We also track th most recent recorded KHZ, write and time to
2492 * allow the matching interval to be extended at each write.
2493 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002494 kvm->arch.last_tsc_nsec = ns;
2495 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002496 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002497
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002498 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002499
2500 /* Keep track of which generation this VCPU has synchronized to */
2501 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2502 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2503 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2504
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002505 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002506 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002507
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002508 spin_lock_irqsave(&kvm->arch.pvclock_gtod_sync_lock, flags);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002509 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002510 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002511 } else if (!already_matched) {
2512 kvm->arch.nr_vcpus_matched_tsc++;
2513 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002514
2515 kvm_track_tsc_matching(vcpu);
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002516 spin_unlock_irqrestore(&kvm->arch.pvclock_gtod_sync_lock, flags);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002517}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002518
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002519static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2520 s64 adjustment)
2521{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002522 u64 tsc_offset = vcpu->arch.l1_tsc_offset;
Leonid Shatz326e7422018-11-06 12:14:25 +02002523 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002524}
2525
2526static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2527{
Ilias Stamatis805d7052021-05-26 19:44:09 +01002528 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002529 WARN_ON(adjustment < 0);
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002530 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment,
2531 vcpu->arch.l1_tsc_scaling_ratio);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002532 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002533}
2534
Marcelo Tosattid8281992012-11-27 23:29:01 -02002535#ifdef CONFIG_X86_64
2536
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002537static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002538{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002539 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002540 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002541
2542 if (likely(ret >= last))
2543 return ret;
2544
2545 /*
2546 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002547 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002548 * very likely) and there's a data dependence, so force GCC
2549 * to generate a branch instead. I don't barrier() because
2550 * we don't actually need a barrier, and if this function
2551 * ever gets inlined it will generate worse code.
2552 */
2553 asm volatile ("");
2554 return last;
2555}
2556
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002557static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2558 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002559{
2560 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002561 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002562
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002563 switch (clock->vclock_mode) {
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002564 case VDSO_CLOCKMODE_HVCLOCK:
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002565 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2566 tsc_timestamp);
2567 if (tsc_pg_val != U64_MAX) {
2568 /* TSC page valid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002569 *mode = VDSO_CLOCKMODE_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002570 v = (tsc_pg_val - clock->cycle_last) &
2571 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002572 } else {
2573 /* TSC page invalid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002574 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002575 }
2576 break;
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002577 case VDSO_CLOCKMODE_TSC:
2578 *mode = VDSO_CLOCKMODE_TSC;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002579 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002580 v = (*tsc_timestamp - clock->cycle_last) &
2581 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002582 break;
2583 default:
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002584 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002585 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002586
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002587 if (*mode == VDSO_CLOCKMODE_NONE)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002588 *tsc_timestamp = v = 0;
2589
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002590 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002591}
2592
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002593static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002594{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002595 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002596 unsigned long seq;
2597 int mode;
2598 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002599
Marcelo Tosattid8281992012-11-27 23:29:01 -02002600 do {
2601 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002602 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002603 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002604 ns >>= gtod->raw_clock.shift;
2605 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002606 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002607 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002608
2609 return mode;
2610}
2611
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002612static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002613{
2614 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2615 unsigned long seq;
2616 int mode;
2617 u64 ns;
2618
2619 do {
2620 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002621 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002622 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002623 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002624 ns >>= gtod->clock.shift;
2625 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2626
2627 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2628 ts->tv_nsec = ns;
2629
2630 return mode;
2631}
2632
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002633/* returns true if host is using TSC based clocksource */
2634static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002635{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002636 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002637 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002638 return false;
2639
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002640 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002641 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002642}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002643
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002644/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002645static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002646 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002647{
2648 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002649 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002650 return false;
2651
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002652 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002653}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002654#endif
2655
2656/*
2657 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002658 * Assuming a stable TSC across physical CPUS, and a stable TSC
2659 * across virtual CPUs, the following condition is possible.
2660 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002661 * CPUs at the next numbered event.
2662 *
2663 * "timespecX" represents host monotonic time. "tscX" represents
2664 * RDTSC value.
2665 *
2666 * VCPU0 on CPU0 | VCPU1 on CPU1
2667 *
2668 * 1. read timespec0,tsc0
2669 * 2. | timespec1 = timespec0 + N
2670 * | tsc1 = tsc0 + M
2671 * 3. transition to guest | transition to guest
2672 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2673 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2674 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2675 *
2676 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2677 *
2678 * - ret0 < ret1
2679 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2680 * ...
2681 * - 0 < N - M => M < N
2682 *
2683 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2684 * always the case (the difference between two distinct xtime instances
2685 * might be smaller then the difference between corresponding TSC reads,
2686 * when updating guest vcpus pvclock areas).
2687 *
2688 * To avoid that problem, do not allow visibility of distinct
2689 * system_timestamp/tsc_timestamp values simultaneously: use a master
2690 * copy of host monotonic time values. Update that master copy
2691 * in lockstep.
2692 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002693 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002694 *
2695 */
2696
2697static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2698{
2699#ifdef CONFIG_X86_64
2700 struct kvm_arch *ka = &kvm->arch;
2701 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002702 bool host_tsc_clocksource, vcpus_matched;
2703
2704 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2705 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002706
2707 /*
2708 * If the host uses TSC clock, then passthrough TSC as stable
2709 * to the guest.
2710 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002711 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002712 &ka->master_kernel_ns,
2713 &ka->master_cycle_now);
2714
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002715 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002716 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002717 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002718
Marcelo Tosattid8281992012-11-27 23:29:01 -02002719 if (ka->use_master_clock)
2720 atomic_set(&kvm_guest_has_master_clock, 1);
2721
2722 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002723 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2724 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002725#endif
2726}
2727
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002728void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2729{
2730 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2731}
2732
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002733static void kvm_gen_update_masterclock(struct kvm *kvm)
2734{
2735#ifdef CONFIG_X86_64
2736 int i;
2737 struct kvm_vcpu *vcpu;
2738 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002739 unsigned long flags;
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002740
Vitaly Kuznetsove880c6e2021-03-16 15:37:34 +01002741 kvm_hv_invalidate_tsc_page(kvm);
2742
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002743 kvm_make_mclock_inprogress_request(kvm);
Paolo Bonzinic2c647f2021-03-25 14:05:11 -04002744
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002745 /* no guest entries from this point */
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002746 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002747 pvclock_update_vm_gtod_copy(kvm);
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002748 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002749
2750 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002751 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002752
2753 /* guest entries allowed */
2754 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002755 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002756#endif
2757}
2758
Marcelo Tosattie891a322017-04-17 12:51:37 -03002759u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002760{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002761 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002762 struct pvclock_vcpu_time_info hv_clock;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002763 unsigned long flags;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002764 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002765
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002766 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Paolo Bonzini8b953442016-11-16 18:31:30 +01002767 if (!ka->use_master_clock) {
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002768 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002769 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002770 }
2771
Paolo Bonzini8b953442016-11-16 18:31:30 +01002772 hv_clock.tsc_timestamp = ka->master_cycle_now;
2773 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002774 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Paolo Bonzini8b953442016-11-16 18:31:30 +01002775
Wanpeng Lie2c22062017-05-11 18:12:05 -07002776 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2777 get_cpu();
2778
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002779 if (__this_cpu_read(cpu_tsc_khz)) {
2780 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2781 &hv_clock.tsc_shift,
2782 &hv_clock.tsc_to_system_mul);
2783 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2784 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002785 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002786
2787 put_cpu();
2788
2789 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002790}
2791
Joao Martinsaa096aa2019-02-01 13:01:45 -05002792static void kvm_setup_pvclock_page(struct kvm_vcpu *v,
2793 struct gfn_to_hva_cache *cache,
2794 unsigned int offset)
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002795{
2796 struct kvm_vcpu_arch *vcpu = &v->arch;
2797 struct pvclock_vcpu_time_info guest_hv_clock;
2798
Joao Martinsaa096aa2019-02-01 13:01:45 -05002799 if (unlikely(kvm_read_guest_offset_cached(v->kvm, cache,
2800 &guest_hv_clock, offset, sizeof(guest_hv_clock))))
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002801 return;
2802
2803 /* This VCPU is paused, but it's legal for a guest to read another
2804 * VCPU's kvmclock, so we really have to follow the specification where
2805 * it says that version is odd if data is being modified, and even after
2806 * it is consistent.
2807 *
2808 * Version field updates must be kept separate. This is because
2809 * kvm_write_guest_cached might use a "rep movs" instruction, and
2810 * writes within a string instruction are weakly ordered. So there
2811 * are three writes overall.
2812 *
2813 * As a small optimization, only write the version field in the first
2814 * and third write. The vcpu->pv_time cache is still valid, because the
2815 * version field is the first in the struct.
2816 */
2817 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2818
Liran Alon51c4b8b2017-11-05 16:11:30 +02002819 if (guest_hv_clock.version & 1)
2820 ++guest_hv_clock.version; /* first time write, random junk */
2821
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002822 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Joao Martinsaa096aa2019-02-01 13:01:45 -05002823 kvm_write_guest_offset_cached(v->kvm, cache,
2824 &vcpu->hv_clock, offset,
2825 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002826
2827 smp_wmb();
2828
2829 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2830 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2831
2832 if (vcpu->pvclock_set_guest_stopped_request) {
2833 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2834 vcpu->pvclock_set_guest_stopped_request = false;
2835 }
2836
2837 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2838
Joao Martinsaa096aa2019-02-01 13:01:45 -05002839 kvm_write_guest_offset_cached(v->kvm, cache,
2840 &vcpu->hv_clock, offset,
2841 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002842
2843 smp_wmb();
2844
2845 vcpu->hv_clock.version++;
Joao Martinsaa096aa2019-02-01 13:01:45 -05002846 kvm_write_guest_offset_cached(v->kvm, cache,
2847 &vcpu->hv_clock, offset,
2848 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002849}
2850
Zachary Amsden34c238a2010-09-18 14:38:14 -10002851static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002852{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002853 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002854 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002855 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002856 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002857 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002858 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002859 bool use_master_clock;
2860
2861 kernel_ns = 0;
2862 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002863
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002864 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002865 * If the host uses TSC clock, then passthrough TSC as stable
2866 * to the guest.
2867 */
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002868 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002869 use_master_clock = ka->use_master_clock;
2870 if (use_master_clock) {
2871 host_tsc = ka->master_cycle_now;
2872 kernel_ns = ka->master_kernel_ns;
2873 }
Paolo Bonzinia83829f2021-03-25 14:11:14 -04002874 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002875
2876 /* Keep irq disabled to prevent changes to the clock */
2877 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002878 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2879 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002880 local_irq_restore(flags);
2881 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2882 return 1;
2883 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002884 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002885 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002886 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002887 }
2888
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002889 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002890
2891 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002892 * We may have to catch up the TSC to match elapsed wall clock
2893 * time for two reasons, even if kvmclock is used.
2894 * 1) CPU could have been running below the maximum TSC rate
2895 * 2) Broken TSC compensation resets the base at each VCPU
2896 * entry to avoid unknown leaps of TSC even when running
2897 * again on the same CPU. This may cause apparent elapsed
2898 * time to disappear, and the guest to stand still or run
2899 * very slowly.
2900 */
2901 if (vcpu->tsc_catchup) {
2902 u64 tsc = compute_guest_tsc(v, kernel_ns);
2903 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002904 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002905 tsc_timestamp = tsc;
2906 }
2907 }
2908
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002909 local_irq_restore(flags);
2910
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002911 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002912
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002913 if (kvm_has_tsc_control)
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01002914 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz,
2915 v->arch.l1_tsc_scaling_ratio);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002916
2917 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002918 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002919 &vcpu->hv_clock.tsc_shift,
2920 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002921 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002922 }
2923
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002924 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002925 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002926 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002927
Marcelo Tosattid8281992012-11-27 23:29:01 -02002928 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002929 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002930 if (use_master_clock)
2931 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2932
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002933 vcpu->hv_clock.flags = pvclock_flags;
2934
Paolo Bonzini095cf552016-02-08 12:54:12 +01002935 if (vcpu->pv_time_enabled)
Joao Martinsaa096aa2019-02-01 13:01:45 -05002936 kvm_setup_pvclock_page(v, &vcpu->pv_time, 0);
2937 if (vcpu->xen.vcpu_info_set)
2938 kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_info_cache,
2939 offsetof(struct compat_vcpu_info, time));
Joao Martinsf2340cd2018-07-23 11:20:57 -04002940 if (vcpu->xen.vcpu_time_info_set)
2941 kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0);
Paolo Bonzini095cf552016-02-08 12:54:12 +01002942 if (v == kvm_get_vcpu(v->kvm, 0))
2943 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002944 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002945}
2946
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002947/*
2948 * kvmclock updates which are isolated to a given vcpu, such as
2949 * vcpu->cpu migration, should not allow system_timestamp from
2950 * the rest of the vcpus to remain static. Otherwise ntp frequency
2951 * correction applies to one vcpu's system_timestamp but not
2952 * the others.
2953 *
2954 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002955 * We need to rate-limit these requests though, as they can
2956 * considerably slow guests that have a large number of vcpus.
2957 * The time for a remote vcpu to update its kvmclock is bound
2958 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002959 */
2960
Andrew Jones7e44e442014-02-28 12:52:54 +01002961#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2962
2963static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002964{
2965 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002966 struct delayed_work *dwork = to_delayed_work(work);
2967 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2968 kvmclock_update_work);
2969 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002970 struct kvm_vcpu *vcpu;
2971
2972 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002973 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002974 kvm_vcpu_kick(vcpu);
2975 }
2976}
2977
Andrew Jones7e44e442014-02-28 12:52:54 +01002978static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2979{
2980 struct kvm *kvm = v->kvm;
2981
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002982 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002983 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2984 KVMCLOCK_UPDATE_DELAY);
2985}
2986
Andrew Jones332967a2014-02-28 12:52:55 +01002987#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2988
2989static void kvmclock_sync_fn(struct work_struct *work)
2990{
2991 struct delayed_work *dwork = to_delayed_work(work);
2992 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2993 kvmclock_sync_work);
2994 struct kvm *kvm = container_of(ka, struct kvm, arch);
2995
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002996 if (!kvmclock_periodic_sync)
2997 return;
2998
Andrew Jones332967a2014-02-28 12:52:55 +01002999 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
3000 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
3001 KVMCLOCK_SYNC_PERIOD);
3002}
3003
Borislav Petkov191c8132019-04-18 18:32:50 +02003004/*
3005 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
3006 */
3007static bool can_set_mci_status(struct kvm_vcpu *vcpu)
3008{
3009 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08003010 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02003011 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
3012
3013 return false;
3014}
3015
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003016static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08003017{
3018 u64 mcg_cap = vcpu->arch.mcg_cap;
3019 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003020 u32 msr = msr_info->index;
3021 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08003022
3023 switch (msr) {
3024 case MSR_IA32_MCG_STATUS:
3025 vcpu->arch.mcg_status = data;
3026 break;
3027 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003028 if (!(mcg_cap & MCG_CTL_P) &&
3029 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08003030 return 1;
3031 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02003032 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08003033 vcpu->arch.mcg_ctl = data;
3034 break;
3035 default:
3036 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003037 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003038 u32 offset = array_index_nospec(
3039 msr - MSR_IA32_MC0_CTL,
3040 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3041
Andre Przywara114be422010-03-24 17:46:42 +01003042 /* only 0 or all 1s can be written to IA32_MCi_CTL
3043 * some Linux kernels though clear bit 10 in bank 4 to
3044 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
3045 * this to avoid an uncatched #GP in the guest
3046 */
Huang Ying890ca9a2009-05-11 16:48:15 +08003047 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01003048 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08003049 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02003050
3051 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003052 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02003053 (offset & 0x3) == 1 && data != 0) {
3054 if (!can_set_mci_status(vcpu))
3055 return -1;
3056 }
3057
Huang Ying890ca9a2009-05-11 16:48:15 +08003058 vcpu->arch.mce_banks[offset] = data;
3059 break;
3060 }
3061 return 1;
3062 }
3063 return 0;
3064}
3065
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003066static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu)
3067{
3068 u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
3069
3070 return (vcpu->arch.apf.msr_en_val & mask) == mask;
3071}
3072
Gleb Natapov344d9582010-10-14 11:22:50 +02003073static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
3074{
3075 gpa_t gpa = data & ~0x3f;
3076
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003077 /* Bits 4:5 are reserved, Should be zero */
3078 if (data & 0x30)
Gleb Natapov344d9582010-10-14 11:22:50 +02003079 return 1;
3080
Oliver Upton66570e92020-08-18 15:24:28 +00003081 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_VMEXIT) &&
3082 (data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT))
3083 return 1;
3084
3085 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT) &&
3086 (data & KVM_ASYNC_PF_DELIVERY_AS_INT))
3087 return 1;
3088
Wanpeng Li9d3c4472020-06-29 18:26:31 +08003089 if (!lapic_in_kernel(vcpu))
Vitaly Kuznetsovd831de12020-09-11 11:31:47 +02003090 return data ? 1 : 0;
Wanpeng Li9d3c4472020-06-29 18:26:31 +08003091
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003092 vcpu->arch.apf.msr_en_val = data;
Gleb Natapov344d9582010-10-14 11:22:50 +02003093
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003094 if (!kvm_pv_async_pf_enabled(vcpu)) {
Gleb Natapov344d9582010-10-14 11:22:50 +02003095 kvm_clear_async_pf_completion_queue(vcpu);
3096 kvm_async_pf_hash_reset(vcpu);
3097 return 0;
3098 }
3099
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003100 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003101 sizeof(u64)))
Gleb Natapov344d9582010-10-14 11:22:50 +02003102 return 1;
3103
Gleb Natapov6adba522010-10-14 11:22:55 +02003104 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07003105 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003106
Gleb Natapov344d9582010-10-14 11:22:50 +02003107 kvm_async_pf_wakeup_all(vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003108
3109 return 0;
3110}
3111
3112static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data)
3113{
3114 /* Bits 8-63 are reserved */
3115 if (data >> 8)
3116 return 1;
3117
3118 if (!lapic_in_kernel(vcpu))
3119 return 1;
3120
3121 vcpu->arch.apf.msr_int_val = data;
3122
3123 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK;
3124
Gleb Natapov344d9582010-10-14 11:22:50 +02003125 return 0;
3126}
3127
Glauber Costa12f9a482011-02-01 14:16:40 -05003128static void kvmclock_reset(struct kvm_vcpu *vcpu)
3129{
Andy Honig0b794592013-02-20 14:48:10 -08003130 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02003131 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05003132}
3133
Sean Christopherson77809382020-03-20 14:28:18 -07003134static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08003135{
3136 ++vcpu->stat.tlb_flush;
Jason Baronb36464772021-01-14 22:27:56 -05003137 static_call(kvm_x86_tlb_flush_all)(vcpu);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08003138}
3139
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04003140static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
3141{
3142 ++vcpu->stat.tlb_flush;
Lai Jiangshanb53e84e2021-06-01 01:22:56 +08003143
3144 if (!tdp_enabled) {
3145 /*
3146 * A TLB flush on behalf of the guest is equivalent to
3147 * INVPCID(all), toggling CR4.PGE, etc., which requires
3148 * a forced sync of the shadow page tables. Unload the
3149 * entire MMU here and the subsequent load will sync the
3150 * shadow page tables, and also flush the TLB.
3151 */
3152 kvm_mmu_unload(vcpu);
3153 return;
3154 }
3155
Jason Baronb36464772021-01-14 22:27:56 -05003156 static_call(kvm_x86_tlb_flush_guest)(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04003157}
3158
Glauber Costac9aaa892011-07-11 15:28:14 -04003159static void record_steal_time(struct kvm_vcpu *vcpu)
3160{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003161 struct kvm_host_map map;
3162 struct kvm_steal_time *st;
3163
David Woodhouse30b5c852021-03-01 12:53:09 +00003164 if (kvm_xen_msr_enabled(vcpu->kvm)) {
3165 kvm_xen_runstate_set_running(vcpu);
3166 return;
3167 }
3168
Glauber Costac9aaa892011-07-11 15:28:14 -04003169 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3170 return;
3171
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003172 /* -EAGAIN is returned in atomic context so we can just return. */
3173 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
3174 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04003175 return;
3176
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003177 st = map.hva +
3178 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3179
Wanpeng Lif38a7b72017-12-12 17:33:04 -08003180 /*
3181 * Doing a TLB flush here, on the guest's behalf, can avoid
3182 * expensive IPIs.
3183 */
Oliver Upton66570e92020-08-18 15:24:28 +00003184 if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) {
Lai Jiangshanaf3511f2021-06-01 01:46:28 +08003185 u8 st_preempted = xchg(&st->preempted, 0);
3186
Oliver Upton66570e92020-08-18 15:24:28 +00003187 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Lai Jiangshanaf3511f2021-06-01 01:46:28 +08003188 st_preempted & KVM_VCPU_FLUSH_TLB);
3189 if (st_preempted & KVM_VCPU_FLUSH_TLB)
Oliver Upton66570e92020-08-18 15:24:28 +00003190 kvm_vcpu_flush_tlb_guest(vcpu);
Wanpeng Li1eff0ad2021-05-18 05:00:33 -07003191 } else {
3192 st->preempted = 0;
Oliver Upton66570e92020-08-18 15:24:28 +00003193 }
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003194
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003195 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003196
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003197 if (st->version & 1)
3198 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003199
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003200 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003201
3202 smp_wmb();
3203
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003204 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08003205 vcpu->arch.st.last_steal;
3206 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003207
Wanpeng Li35f3fae12016-05-03 11:43:10 +08003208 smp_wmb();
3209
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003210 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04003211
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003212 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04003213}
3214
Will Auld8fe8ab42012-11-29 12:42:12 -08003215int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003216{
Gleb Natapov57537852012-01-15 14:17:22 +02003217 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08003218 u32 msr = msr_info->index;
3219 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02003220
Joao Martins1232f8e2018-06-13 06:10:37 -04003221 if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr)
Joao Martins23200b72018-06-13 09:55:44 -04003222 return kvm_xen_write_hypercall_page(vcpu, data);
Joao Martins1232f8e2018-06-13 06:10:37 -04003223
Carsten Otte15c4a642007-10-30 18:44:17 +01003224 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01003225 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003226 case MSR_IA32_UCODE_WRITE:
3227 case MSR_VM_HSAVE_PA:
3228 case MSR_AMD64_PATCH_LOADER:
3229 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02003230 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003231 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003232 break;
3233
Wanpeng Li518e7b92018-02-28 14:03:31 +08003234 case MSR_IA32_UCODE_REV:
3235 if (msr_info->host_initiated)
3236 vcpu->arch.microcode_version = data;
3237 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003238 case MSR_IA32_ARCH_CAPABILITIES:
3239 if (!msr_info->host_initiated)
3240 return 1;
3241 vcpu->arch.arch_capabilities = data;
3242 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02003243 case MSR_IA32_PERF_CAPABILITIES: {
3244 struct kvm_msr_entry msr_ent = {.index = msr, .data = 0};
3245
3246 if (!msr_info->host_initiated)
3247 return 1;
3248 if (guest_cpuid_has(vcpu, X86_FEATURE_PDCM) && kvm_get_msr_feature(&msr_ent))
3249 return 1;
3250 if (data & ~msr_ent.data)
3251 return 1;
3252
3253 vcpu->arch.perf_capabilities = data;
3254
3255 return 0;
3256 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003257 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07003258 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02003259 case MSR_K7_HWCR:
3260 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01003261 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08003262 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02003263
3264 /* Handle McStatusWrEn */
3265 if (data == BIT_ULL(18)) {
3266 vcpu->arch.msr_hwcr = data;
3267 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003268 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
3269 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02003270 return 1;
3271 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003272 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003273 case MSR_FAM10H_MMIO_CONF_BASE:
3274 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003275 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
3276 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003277 return 1;
3278 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003279 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003280 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003281 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003282 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01003283 return kvm_set_apic_base(vcpu, msr_info);
Xiaoyao Libf10bd02020-06-16 15:33:07 +08003284 case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003285 return kvm_x2apic_msr_write(vcpu, msr, data);
Dave Hansen09141ec2020-03-05 09:47:06 -08003286 case MSR_IA32_TSC_DEADLINE:
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003287 kvm_set_lapic_tscdeadline_msr(vcpu, data);
3288 break;
Will Auldba904632012-11-29 12:42:50 -08003289 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003290 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08003291 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06003292 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08003293 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08003294 }
3295 vcpu->arch.ia32_tsc_adjust_msr = data;
3296 }
3297 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003298 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08003299 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
3300 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
3301 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
3302 return 1;
3303 vcpu->arch.ia32_misc_enable_msr = data;
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08003304 kvm_update_cpuid_runtime(vcpu);
Wanpeng Li511a85562019-05-21 14:06:54 +08003305 } else {
3306 vcpu->arch.ia32_misc_enable_msr = data;
3307 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003308 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003309 case MSR_IA32_SMBASE:
3310 if (!msr_info->host_initiated)
3311 return 1;
3312 vcpu->arch.smbase = data;
3313 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003314 case MSR_IA32_POWER_CTL:
3315 vcpu->arch.msr_ia32_power_ctl = data;
3316 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003317 case MSR_IA32_TSC:
Paolo Bonzini0c899c22020-09-24 14:45:27 +02003318 if (msr_info->host_initiated) {
3319 kvm_synchronize_tsc(vcpu, data);
3320 } else {
Ilias Stamatis9b399df2021-05-26 19:44:10 +01003321 u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset;
Paolo Bonzini0c899c22020-09-24 14:45:27 +02003322 adjust_tsc_offset_guest(vcpu, adj);
3323 vcpu->arch.ia32_tsc_adjust_msr += adj;
3324 }
Paolo Bonzinidd259932018-04-13 11:38:35 +02003325 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003326 case MSR_IA32_XSS:
3327 if (!msr_info->host_initiated &&
3328 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3329 return 1;
3330 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08003331 * KVM supports exposing PT to the guest, but does not support
3332 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
3333 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003334 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01003335 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003336 return 1;
3337 vcpu->arch.ia32_xss = data;
3338 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003339 case MSR_SMI_COUNT:
3340 if (!msr_info->host_initiated)
3341 return 1;
3342 vcpu->arch.smi_count = data;
3343 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003344 case MSR_KVM_WALL_CLOCK_NEW:
Oliver Upton66570e92020-08-18 15:24:28 +00003345 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3346 return 1;
3347
Joao Martins629b5342018-06-28 15:06:43 -04003348 vcpu->kvm->arch.wall_clock = data;
3349 kvm_write_wall_clock(vcpu->kvm, data, 0);
Oliver Upton66570e92020-08-18 15:24:28 +00003350 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003351 case MSR_KVM_WALL_CLOCK:
Oliver Upton66570e92020-08-18 15:24:28 +00003352 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3353 return 1;
3354
Joao Martins629b5342018-06-28 15:06:43 -04003355 vcpu->kvm->arch.wall_clock = data;
3356 kvm_write_wall_clock(vcpu->kvm, data, 0);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003357 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003358 case MSR_KVM_SYSTEM_TIME_NEW:
Oliver Upton66570e92020-08-18 15:24:28 +00003359 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3360 return 1;
3361
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003362 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003363 break;
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003364 case MSR_KVM_SYSTEM_TIME:
Oliver Upton66570e92020-08-18 15:24:28 +00003365 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3366 return 1;
3367
3368 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated);
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003369 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003370 case MSR_KVM_ASYNC_PF_EN:
Oliver Upton66570e92020-08-18 15:24:28 +00003371 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3372 return 1;
3373
Gleb Natapov344d9582010-10-14 11:22:50 +02003374 if (kvm_pv_enable_async_pf(vcpu, data))
3375 return 1;
3376 break;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003377 case MSR_KVM_ASYNC_PF_INT:
Oliver Upton66570e92020-08-18 15:24:28 +00003378 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
3379 return 1;
3380
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003381 if (kvm_pv_enable_async_pf_int(vcpu, data))
3382 return 1;
3383 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003384 case MSR_KVM_ASYNC_PF_ACK:
Oliver Upton66570e92020-08-18 15:24:28 +00003385 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3386 return 1;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003387 if (data & 0x1) {
3388 vcpu->arch.apf.pageready_pending = false;
3389 kvm_check_async_pf_completion(vcpu);
3390 }
3391 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003392 case MSR_KVM_STEAL_TIME:
Oliver Upton66570e92020-08-18 15:24:28 +00003393 if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME))
3394 return 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04003395
3396 if (unlikely(!sched_info_on()))
3397 return 1;
3398
3399 if (data & KVM_STEAL_RESERVED_MASK)
3400 return 1;
3401
Glauber Costac9aaa892011-07-11 15:28:14 -04003402 vcpu->arch.st.msr_val = data;
3403
3404 if (!(data & KVM_MSR_ENABLED))
3405 break;
3406
Glauber Costac9aaa892011-07-11 15:28:14 -04003407 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
3408
3409 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003410 case MSR_KVM_PV_EOI_EN:
Oliver Upton66570e92020-08-18 15:24:28 +00003411 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI))
3412 return 1;
3413
Ladi Prosek72bbf932018-10-16 18:49:59 +02003414 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003415 return 1;
3416 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003417
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003418 case MSR_KVM_POLL_CONTROL:
Oliver Upton66570e92020-08-18 15:24:28 +00003419 if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL))
3420 return 1;
3421
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003422 /* only enable bit supported */
3423 if (data & (-1ULL << 1))
3424 return 1;
3425
3426 vcpu->arch.msr_kvm_poll_control = data;
3427 break;
3428
Huang Ying890ca9a2009-05-11 16:48:15 +08003429 case MSR_IA32_MCG_CTL:
3430 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003431 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003432 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02003433
Wei Huang6912ac32015-06-12 01:34:56 -04003434 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3435 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003436 pr = true;
3437 fallthrough;
Wei Huang6912ac32015-06-12 01:34:56 -04003438 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3439 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003440 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003441 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02003442
3443 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03003444 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
3445 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02003446 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003447 case MSR_K7_CLK_CTL:
3448 /*
3449 * Ignore all writes to this no longer documented MSR.
3450 * Writes are only relevant for old K7 processors,
3451 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08003452 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003453 * affected processor models on the command line, hence
3454 * the need to ignore the workaround.
3455 */
3456 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003457 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003458 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3459 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003460 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3461 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003462 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003463 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3464 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3465 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003466 return kvm_hv_set_msr_common(vcpu, msr, data,
3467 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003468 case MSR_IA32_BBL_CR_CTL3:
3469 /* Drop writes to this legacy MSR -- see rdmsr
3470 * counterpart for further detail.
3471 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003472 if (report_ignored_msrs)
3473 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
3474 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05003475 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003476 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003477 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003478 return 1;
3479 vcpu->arch.osvw.length = data;
3480 break;
3481 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003482 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003483 return 1;
3484 vcpu->arch.osvw.status = data;
3485 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003486 case MSR_PLATFORM_INFO:
3487 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07003488 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
3489 cpuid_fault_enabled(vcpu)))
3490 return 1;
3491 vcpu->arch.msr_platform_info = data;
3492 break;
3493 case MSR_MISC_FEATURES_ENABLES:
3494 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
3495 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3496 !supports_cpuid_fault(vcpu)))
3497 return 1;
3498 vcpu->arch.msr_misc_features_enables = data;
3499 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003500 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003501 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003502 return kvm_pmu_set_msr(vcpu, msr_info);
Peter Xu6abe9c12020-06-22 18:04:41 -04003503 return KVM_MSR_RET_INVALID;
Carsten Otte15c4a642007-10-30 18:44:17 +01003504 }
3505 return 0;
3506}
3507EXPORT_SYMBOL_GPL(kvm_set_msr_common);
3508
Paolo Bonzini44883f02018-07-26 13:01:52 +02003509static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003510{
3511 u64 data;
3512 u64 mcg_cap = vcpu->arch.mcg_cap;
3513 unsigned bank_num = mcg_cap & 0xff;
3514
3515 switch (msr) {
3516 case MSR_IA32_P5_MC_ADDR:
3517 case MSR_IA32_P5_MC_TYPE:
3518 data = 0;
3519 break;
3520 case MSR_IA32_MCG_CAP:
3521 data = vcpu->arch.mcg_cap;
3522 break;
3523 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003524 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003525 return 1;
3526 data = vcpu->arch.mcg_ctl;
3527 break;
3528 case MSR_IA32_MCG_STATUS:
3529 data = vcpu->arch.mcg_status;
3530 break;
3531 default:
3532 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003533 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003534 u32 offset = array_index_nospec(
3535 msr - MSR_IA32_MC0_CTL,
3536 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3537
Huang Ying890ca9a2009-05-11 16:48:15 +08003538 data = vcpu->arch.mce_banks[offset];
3539 break;
3540 }
3541 return 1;
3542 }
3543 *pdata = data;
3544 return 0;
3545}
3546
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003547int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003548{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003549 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003550 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003551 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003552 case MSR_IA32_LASTBRANCHFROMIP:
3553 case MSR_IA32_LASTBRANCHTOIP:
3554 case MSR_IA32_LASTINTFROMIP:
3555 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303556 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003557 case MSR_K8_TSEG_ADDR:
3558 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003559 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003560 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003561 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003562 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003563 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003564 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003565 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003566 case MSR_F15H_EX_CFG:
Venkatesh Srinivas2ca1a062020-04-16 11:42:54 -07003567 /*
3568 * Intel Sandy Bridge CPUs must support the RAPL (running average power
3569 * limit) MSRs. Just return 0, as we do not want to expose the host
3570 * data here. Do not conditionalize this on CPUID, as KVM does not do
3571 * so for existing CPU-specific MSRs.
3572 */
3573 case MSR_RAPL_POWER_UNIT:
3574 case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */
3575 case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */
3576 case MSR_PKG_ENERGY_STATUS: /* Total package */
3577 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003578 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003579 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003580 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Vitaly Kuznetsovc28fa562021-03-29 14:48:04 +02003581 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
3582 return kvm_pmu_get_msr(vcpu, msr_info);
3583 if (!msr_info->host_initiated)
3584 return 1;
3585 msr_info->data = 0;
3586 break;
Wei Huang6912ac32015-06-12 01:34:56 -04003587 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3588 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3589 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3590 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003591 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003592 return kvm_pmu_get_msr(vcpu, msr_info);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003593 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003594 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003595 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003596 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003597 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003598 case MSR_IA32_ARCH_CAPABILITIES:
3599 if (!msr_info->host_initiated &&
3600 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3601 return 1;
3602 msr_info->data = vcpu->arch.arch_capabilities;
3603 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02003604 case MSR_IA32_PERF_CAPABILITIES:
3605 if (!msr_info->host_initiated &&
3606 !guest_cpuid_has(vcpu, X86_FEATURE_PDCM))
3607 return 1;
3608 msr_info->data = vcpu->arch.perf_capabilities;
3609 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003610 case MSR_IA32_POWER_CTL:
3611 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3612 break;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003613 case MSR_IA32_TSC: {
3614 /*
3615 * Intel SDM states that MSR_IA32_TSC read adds the TSC offset
3616 * even when not intercepted. AMD manual doesn't explicitly
3617 * state this but appears to behave the same.
3618 *
Maxim Levitskyee6fa052020-09-21 13:38:05 +03003619 * On userspace reads and writes, however, we unconditionally
Paolo Bonzinic0623f52020-10-21 18:05:58 -04003620 * return L1's TSC value to ensure backwards-compatible
Maxim Levitskyee6fa052020-09-21 13:38:05 +03003621 * behavior for migration.
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003622 */
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01003623 u64 offset, ratio;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003624
Ilias Stamatisfe3eb502021-05-26 19:44:11 +01003625 if (msr_info->host_initiated) {
3626 offset = vcpu->arch.l1_tsc_offset;
3627 ratio = vcpu->arch.l1_tsc_scaling_ratio;
3628 } else {
3629 offset = vcpu->arch.tsc_offset;
3630 ratio = vcpu->arch.tsc_scaling_ratio;
3631 }
3632
3633 msr_info->data = kvm_scale_tsc(vcpu, rdtsc(), ratio) + offset;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003634 break;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003635 }
Avi Kivity9ba075a2008-05-26 20:06:35 +03003636 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003637 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003638 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003639 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003640 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003641 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003642 /*
3643 * MSR_EBC_FREQUENCY_ID
3644 * Conservative value valid for even the basic CPU models.
3645 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3646 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3647 * and 266MHz for model 3, or 4. Set Core Clock
3648 * Frequency to System Bus Frequency Ratio to 1 (bits
3649 * 31:24) even though these are only valid for CPU
3650 * models > 2, however guests may end up dividing or
3651 * multiplying by zero otherwise.
3652 */
3653 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003654 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003655 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003656 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003657 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003658 break;
Xiaoyao Libf10bd02020-06-16 15:33:07 +08003659 case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003660 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Dave Hansen09141ec2020-03-05 09:47:06 -08003661 case MSR_IA32_TSC_DEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003662 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003663 break;
Will Auldba904632012-11-29 12:42:50 -08003664 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003665 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003666 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003667 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003668 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003669 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003670 case MSR_IA32_SMBASE:
3671 if (!msr_info->host_initiated)
3672 return 1;
3673 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003674 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003675 case MSR_SMI_COUNT:
3676 msr_info->data = vcpu->arch.smi_count;
3677 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003678 case MSR_IA32_PERF_STATUS:
3679 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003680 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003681 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003682 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003683 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003684 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003685 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003686 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003687 case MSR_KVM_WALL_CLOCK:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003688 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3689 return 1;
3690
3691 msr_info->data = vcpu->kvm->arch.wall_clock;
3692 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003693 case MSR_KVM_WALL_CLOCK_NEW:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003694 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3695 return 1;
3696
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003697 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003698 break;
3699 case MSR_KVM_SYSTEM_TIME:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003700 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3701 return 1;
3702
3703 msr_info->data = vcpu->arch.time;
3704 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003705 case MSR_KVM_SYSTEM_TIME_NEW:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003706 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3707 return 1;
3708
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003709 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003710 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003711 case MSR_KVM_ASYNC_PF_EN:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003712 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3713 return 1;
3714
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003715 msr_info->data = vcpu->arch.apf.msr_en_val;
3716 break;
3717 case MSR_KVM_ASYNC_PF_INT:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003718 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
3719 return 1;
3720
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003721 msr_info->data = vcpu->arch.apf.msr_int_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003722 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003723 case MSR_KVM_ASYNC_PF_ACK:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003724 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3725 return 1;
3726
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003727 msr_info->data = 0;
3728 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003729 case MSR_KVM_STEAL_TIME:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003730 if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME))
3731 return 1;
3732
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003733 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003734 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003735 case MSR_KVM_PV_EOI_EN:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003736 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI))
3737 return 1;
3738
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003739 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003740 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003741 case MSR_KVM_POLL_CONTROL:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003742 if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL))
3743 return 1;
3744
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003745 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3746 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003747 case MSR_IA32_P5_MC_ADDR:
3748 case MSR_IA32_P5_MC_TYPE:
3749 case MSR_IA32_MCG_CAP:
3750 case MSR_IA32_MCG_CTL:
3751 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003752 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003753 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3754 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003755 case MSR_IA32_XSS:
3756 if (!msr_info->host_initiated &&
3757 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3758 return 1;
3759 msr_info->data = vcpu->arch.ia32_xss;
3760 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003761 case MSR_K7_CLK_CTL:
3762 /*
3763 * Provide expected ramp-up count for K7. All other
3764 * are set to zero, indicating minimum divisors for
3765 * every field.
3766 *
3767 * This prevents guest kernels on AMD host with CPU
3768 * type 6, model 8 and higher from exploding due to
3769 * the rdmsr failing.
3770 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003771 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003772 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003773 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003774 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3775 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003776 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3777 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003778 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003779 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3780 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3781 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003782 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003783 msr_info->index, &msr_info->data,
3784 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003785 case MSR_IA32_BBL_CR_CTL3:
3786 /* This legacy MSR exists but isn't fully documented in current
3787 * silicon. It is however accessed by winxp in very narrow
3788 * scenarios where it sets bit #19, itself documented as
3789 * a "reserved" bit. Best effort attempt to source coherent
3790 * read data here should the balance of the register be
3791 * interpreted by the guest:
3792 *
3793 * L2 cache control register 3: 64GB range, 256KB size,
3794 * enabled, latency 0x1, configured
3795 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003796 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003797 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003798 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003799 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003800 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003801 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003802 break;
3803 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003804 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003805 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003806 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003807 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003808 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003809 if (!msr_info->host_initiated &&
3810 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3811 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003812 msr_info->data = vcpu->arch.msr_platform_info;
3813 break;
3814 case MSR_MISC_FEATURES_ENABLES:
3815 msr_info->data = vcpu->arch.msr_misc_features_enables;
3816 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003817 case MSR_K7_HWCR:
3818 msr_info->data = vcpu->arch.msr_hwcr;
3819 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003820 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003821 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003822 return kvm_pmu_get_msr(vcpu, msr_info);
Peter Xu6abe9c12020-06-22 18:04:41 -04003823 return KVM_MSR_RET_INVALID;
Carsten Otte15c4a642007-10-30 18:44:17 +01003824 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003825 return 0;
3826}
3827EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3828
Carsten Otte313a3dc2007-10-11 19:16:52 +02003829/*
3830 * Read or write a bunch of msrs. All parameters are kernel addresses.
3831 *
3832 * @return number of msrs set successfully.
3833 */
3834static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3835 struct kvm_msr_entry *entries,
3836 int (*do_msr)(struct kvm_vcpu *vcpu,
3837 unsigned index, u64 *data))
3838{
Tom Lendacky801e4592018-02-21 13:39:51 -06003839 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003840
Carsten Otte313a3dc2007-10-11 19:16:52 +02003841 for (i = 0; i < msrs->nmsrs; ++i)
3842 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3843 break;
3844
Carsten Otte313a3dc2007-10-11 19:16:52 +02003845 return i;
3846}
3847
3848/*
3849 * Read or write a bunch of msrs. Parameters are user addresses.
3850 *
3851 * @return number of msrs set successfully.
3852 */
3853static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3854 int (*do_msr)(struct kvm_vcpu *vcpu,
3855 unsigned index, u64 *data),
3856 int writeback)
3857{
3858 struct kvm_msrs msrs;
3859 struct kvm_msr_entry *entries;
3860 int r, n;
3861 unsigned size;
3862
3863 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003864 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003865 goto out;
3866
3867 r = -E2BIG;
3868 if (msrs.nmsrs >= MAX_IO_MSRS)
3869 goto out;
3870
Carsten Otte313a3dc2007-10-11 19:16:52 +02003871 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003872 entries = memdup_user(user_msrs->entries, size);
3873 if (IS_ERR(entries)) {
3874 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003875 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003876 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003877
3878 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3879 if (r < 0)
3880 goto out_free;
3881
3882 r = -EFAULT;
3883 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3884 goto out_free;
3885
3886 r = n;
3887
3888out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003889 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003890out:
3891 return r;
3892}
3893
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003894static inline bool kvm_can_mwait_in_guest(void)
3895{
3896 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003897 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3898 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003899}
3900
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02003901static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu,
3902 struct kvm_cpuid2 __user *cpuid_arg)
3903{
3904 struct kvm_cpuid2 cpuid;
3905 int r;
3906
3907 r = -EFAULT;
3908 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
3909 return r;
3910
3911 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries);
3912 if (r)
3913 return r;
3914
3915 r = -EFAULT;
3916 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
3917 return r;
3918
3919 return 0;
3920}
3921
Alexander Graf784aa3d2014-07-14 18:27:35 +02003922int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003923{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003924 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003925
3926 switch (ext) {
3927 case KVM_CAP_IRQCHIP:
3928 case KVM_CAP_HLT:
3929 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003930 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003931 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003932 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003933 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003934 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003935 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003936 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003937 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003938 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003939 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003940 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003941 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003942 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003943 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003944 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003945 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003946 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003947 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003948 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003949 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003950 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003951 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003952 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003953 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003954 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003955 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003956 case KVM_CAP_HYPERV_CPUID:
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02003957 case KVM_CAP_SYS_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003958 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003959 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003960 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003961 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003962 case KVM_CAP_ASYNC_PF:
Vitaly Kuznetsov72de5fa4c2020-05-25 16:41:22 +02003963 case KVM_CAP_ASYNC_PF_INT:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003964 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003965 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003966 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003967 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003968 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003969 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003970 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003971 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003972 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003973 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003974 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003975 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003976 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003977 case KVM_CAP_EXCEPTION_PAYLOAD:
Peter Xub9b27822020-05-05 11:47:50 -04003978 case KVM_CAP_SET_GUEST_DEBUG:
Jim Mattson1aa561b2020-06-03 16:56:21 -07003979 case KVM_CAP_LAST_CPU:
Alexander Graf1ae09952020-09-25 16:34:16 +02003980 case KVM_CAP_X86_USER_SPACE_MSR:
Alexander Graf1a1552542020-09-25 16:34:21 +02003981 case KVM_CAP_X86_MSR_FILTER:
Oliver Upton66570e92020-08-18 15:24:28 +00003982 case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
Sean Christophersonfe7e9482021-04-12 16:21:43 +12003983#ifdef CONFIG_X86_SGX_KVM
3984 case KVM_CAP_SGX_ATTRIBUTE:
3985#endif
Nathan Tempelman54526d12021-04-08 22:32:14 +00003986 case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003987 r = 1;
3988 break;
Maxim Levitsky7e582cc2021-04-01 16:54:45 +03003989 case KVM_CAP_SET_GUEST_DEBUG2:
3990 return KVM_GUESTDBG_VALID_MASK;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05003991#ifdef CONFIG_KVM_XEN
Joao Martins23200b72018-06-13 09:55:44 -04003992 case KVM_CAP_XEN_HVM:
3993 r = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR |
David Woodhouse8d4e7e82020-12-04 01:02:04 +00003994 KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL |
3995 KVM_XEN_HVM_CONFIG_SHARED_INFO;
David Woodhouse30b5c852021-03-01 12:53:09 +00003996 if (sched_info_on())
3997 r |= KVM_XEN_HVM_CONFIG_RUNSTATE;
Joao Martins23200b72018-06-13 09:55:44 -04003998 break;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05003999#endif
Ken Hofsass01643c52018-01-31 16:03:36 -08004000 case KVM_CAP_SYNC_REGS:
4001 r = KVM_SYNC_X86_VALID_FIELDS;
4002 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01004003 case KVM_CAP_ADJUST_CLOCK:
4004 r = KVM_CLOCK_TSC_STABLE;
4005 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004006 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08004007 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
4008 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004009 if(kvm_can_mwait_in_guest())
4010 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02004011 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004012 case KVM_CAP_X86_SMM:
4013 /* SMBASE is usually relocated above 1M on modern chipsets,
4014 * and SMM handlers might indeed rely on 4G segment limits,
4015 * so do not report SMM to be available if real mode is
4016 * emulated via vm86 mode. Still, do not go to great lengths
4017 * to avoid userspace's usage of the feature, because it is a
4018 * fringe case that is not enabled except via specific settings
4019 * of the module parameters.
4020 */
Jason Baronb36464772021-01-14 22:27:56 -05004021 r = static_call(kvm_x86_has_emulated_msr)(kvm, MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004022 break;
Avi Kivity774ead32007-12-26 13:57:04 +02004023 case KVM_CAP_VAPIC:
Jason Baronb36464772021-01-14 22:27:56 -05004024 r = !static_call(kvm_x86_cpu_has_accelerated_tpr)();
Avi Kivity774ead32007-12-26 13:57:04 +02004025 break;
Avi Kivityf7252302008-02-20 11:53:16 +02004026 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03004027 r = KVM_SOFT_MAX_VCPUS;
4028 break;
4029 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02004030 r = KVM_MAX_VCPUS;
4031 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02004032 case KVM_CAP_MAX_VCPU_ID:
4033 r = KVM_MAX_VCPU_ID;
4034 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03004035 case KVM_CAP_PV_MMU: /* obsolete */
4036 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004037 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08004038 case KVM_CAP_MCE:
4039 r = KVM_MAX_MCE_BANKS;
4040 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004041 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02004042 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004043 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004044 case KVM_CAP_TSC_CONTROL:
4045 r = kvm_has_tsc_control;
4046 break;
Radim Krčmář371313132016-07-12 22:09:27 +02004047 case KVM_CAP_X2APIC_API:
4048 r = KVM_X2APIC_API_VALID_FLAGS;
4049 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004050 case KVM_CAP_NESTED_STATE:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004051 r = kvm_x86_ops.nested_ops->get_state ?
4052 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004053 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004054 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004055 r = kvm_x86_ops.enable_direct_tlbflush != NULL;
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02004056 break;
4057 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004058 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004059 break;
Mohammed Gamal3edd6832020-07-10 17:48:11 +02004060 case KVM_CAP_SMALLER_MAXPHYADDR:
4061 r = (int) allow_smaller_maxphyaddr;
4062 break;
Andrew Jones004a0122020-08-04 19:06:04 +02004063 case KVM_CAP_STEAL_TIME:
4064 r = sched_info_on();
4065 break;
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08004066 case KVM_CAP_X86_BUS_LOCK_EXIT:
4067 if (kvm_has_bus_lock_exit)
4068 r = KVM_BUS_LOCK_DETECTION_OFF |
4069 KVM_BUS_LOCK_DETECTION_EXIT;
4070 else
4071 r = 0;
4072 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08004073 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08004074 break;
4075 }
4076 return r;
4077
4078}
4079
Carsten Otte043405e2007-10-10 17:16:19 +02004080long kvm_arch_dev_ioctl(struct file *filp,
4081 unsigned int ioctl, unsigned long arg)
4082{
4083 void __user *argp = (void __user *)arg;
4084 long r;
4085
4086 switch (ioctl) {
4087 case KVM_GET_MSR_INDEX_LIST: {
4088 struct kvm_msr_list __user *user_msr_list = argp;
4089 struct kvm_msr_list msr_list;
4090 unsigned n;
4091
4092 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004093 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02004094 goto out;
4095 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004096 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004097 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02004098 goto out;
4099 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02004100 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02004101 goto out;
4102 r = -EFAULT;
4103 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
4104 num_msrs_to_save * sizeof(u32)))
4105 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02004106 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02004107 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004108 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02004109 goto out;
4110 r = 0;
4111 break;
4112 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02004113 case KVM_GET_SUPPORTED_CPUID:
4114 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02004115 struct kvm_cpuid2 __user *cpuid_arg = argp;
4116 struct kvm_cpuid2 cpuid;
4117
4118 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004119 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02004120 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02004121
4122 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
4123 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02004124 if (r)
4125 goto out;
4126
4127 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004128 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02004129 goto out;
4130 r = 0;
4131 break;
4132 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08004133 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08004134 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004135 if (copy_to_user(argp, &kvm_mce_cap_supported,
4136 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004137 goto out;
4138 r = 0;
4139 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004140 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
4141 struct kvm_msr_list __user *user_msr_list = argp;
4142 struct kvm_msr_list msr_list;
4143 unsigned int n;
4144
4145 r = -EFAULT;
4146 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
4147 goto out;
4148 n = msr_list.nmsrs;
4149 msr_list.nmsrs = num_msr_based_features;
4150 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
4151 goto out;
4152 r = -E2BIG;
4153 if (n < msr_list.nmsrs)
4154 goto out;
4155 r = -EFAULT;
4156 if (copy_to_user(user_msr_list->indices, &msr_based_features,
4157 num_msr_based_features * sizeof(u32)))
4158 goto out;
4159 r = 0;
4160 break;
4161 }
4162 case KVM_GET_MSRS:
4163 r = msr_io(NULL, argp, do_get_msr_feature, 1);
4164 break;
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02004165 case KVM_GET_SUPPORTED_HV_CPUID:
4166 r = kvm_ioctl_get_supported_hv_cpuid(NULL, argp);
4167 break;
Carsten Otte043405e2007-10-10 17:16:19 +02004168 default:
4169 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08004170 break;
Carsten Otte043405e2007-10-10 17:16:19 +02004171 }
4172out:
4173 return r;
4174}
4175
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004176static void wbinvd_ipi(void *garbage)
4177{
4178 wbinvd();
4179}
4180
4181static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
4182{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06004183 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004184}
4185
Carsten Otte313a3dc2007-10-11 19:16:52 +02004186void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
4187{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004188 /* Address WBINVD may be executed by guest */
4189 if (need_emulate_wbinvd(vcpu)) {
Jason Baronb36464772021-01-14 22:27:56 -05004190 if (static_call(kvm_x86_has_wbinvd_exit)())
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004191 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
4192 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
4193 smp_call_function_single(vcpu->cpu,
4194 wbinvd_ipi, NULL, 1);
4195 }
4196
Jason Baronb36464772021-01-14 22:27:56 -05004197 static_call(kvm_x86_vcpu_load)(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02004198
Babu Moger37486132020-05-12 18:59:06 -05004199 /* Save host pkru register if supported */
4200 vcpu->arch.host_pkru = read_pkru();
4201
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02004202 /* Apply any externally detected TSC adjustments (due to suspend) */
4203 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
4204 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
4205 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08004206 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02004207 }
4208
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01004209 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02004210 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02004211 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10004212 if (tsc_delta < 0)
4213 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07004214
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01004215 if (kvm_check_tsc_unstable()) {
Ilias Stamatis9b399df2021-05-26 19:44:10 +01004216 u64 offset = kvm_compute_l1_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02004217 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04004218 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10004219 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10004220 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02004221
4222 if (kvm_lapic_hv_timer_in_use(vcpu))
4223 kvm_lapic_restart_hv_timer(vcpu);
4224
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02004225 /*
4226 * On a host with synchronized TSC, there is no need to update
4227 * kvmclock on vcpu->cpu migration
4228 */
4229 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03004230 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10004231 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02004232 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10004233 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10004234 }
Glauber Costac9aaa892011-07-11 15:28:14 -04004235
Glauber Costac9aaa892011-07-11 15:28:14 -04004236 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004237}
4238
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004239static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
4240{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004241 struct kvm_host_map map;
4242 struct kvm_steal_time *st;
4243
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004244 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
4245 return;
4246
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00004247 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00004248 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004249
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004250 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
4251 &vcpu->arch.st.cache, true))
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004252 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004253
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004254 st = map.hva +
4255 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
4256
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00004257 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004258
4259 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004260}
4261
Carsten Otte313a3dc2007-10-11 19:16:52 +02004262void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
4263{
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004264 int idx;
4265
Tom Lendackyf1c63662020-12-14 10:29:50 -05004266 if (vcpu->preempted && !vcpu->arch.guest_state_protected)
Jason Baronb36464772021-01-14 22:27:56 -05004267 vcpu->arch.preempted_in_kernel = !static_call(kvm_x86_get_cpl)(vcpu);
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004268
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004269 /*
4270 * Take the srcu lock as memslots will be accessed to check the gfn
4271 * cache generation against the memslots generation.
4272 */
4273 idx = srcu_read_lock(&vcpu->kvm->srcu);
David Woodhouse30b5c852021-03-01 12:53:09 +00004274 if (kvm_xen_msr_enabled(vcpu->kvm))
4275 kvm_xen_runstate_set_preempted(vcpu);
4276 else
4277 kvm_steal_time_set_preempted(vcpu);
Wanpeng Li9c1a0742021-04-23 16:23:20 +08004278 srcu_read_unlock(&vcpu->kvm->srcu, idx);
David Woodhouse30b5c852021-03-01 12:53:09 +00004279
Jason Baronb36464772021-01-14 22:27:56 -05004280 static_call(kvm_x86_vcpu_put)(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02004281 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01004282 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02004283 * If userspace has set any breakpoints or watchpoints, dr6 is restored
4284 * on every vmexit, but if not, we might have a stale dr6 from the
4285 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01004286 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02004287 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004288}
4289
Carsten Otte313a3dc2007-10-11 19:16:52 +02004290static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
4291 struct kvm_lapic_state *s)
4292{
Liran Alonfa59cc02017-12-24 18:12:53 +02004293 if (vcpu->arch.apicv_active)
Jason Baronb36464772021-01-14 22:27:56 -05004294 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004295
Radim Krčmářa92e2542016-07-12 22:09:22 +02004296 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004297}
4298
4299static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
4300 struct kvm_lapic_state *s)
4301{
Radim Krčmářa92e2542016-07-12 22:09:22 +02004302 int r;
4303
4304 r = kvm_apic_set_state(vcpu, s);
4305 if (r)
4306 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03004307 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004308
4309 return 0;
4310}
4311
Matt Gingell127a4572015-11-17 17:32:05 +01004312static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
4313{
Paolo Bonzini71cc8492020-11-27 09:18:20 +01004314 /*
4315 * We can accept userspace's request for interrupt injection
4316 * as long as we have a place to store the interrupt number.
4317 * The actual injection will happen when the CPU is able to
4318 * deliver the interrupt.
4319 */
4320 if (kvm_cpu_has_extint(vcpu))
4321 return false;
4322
4323 /* Acknowledging ExtINT does not happen if LINT0 is masked. */
Matt Gingell127a4572015-11-17 17:32:05 +01004324 return (!lapic_in_kernel(vcpu) ||
4325 kvm_apic_accept_pic_intr(vcpu));
4326}
4327
Matt Gingell782d4222015-11-16 15:26:00 -08004328static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
4329{
4330 return kvm_arch_interrupt_allowed(vcpu) &&
Matt Gingell782d4222015-11-16 15:26:00 -08004331 kvm_cpu_accept_dm_intr(vcpu);
4332}
4333
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004334static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
4335 struct kvm_interrupt *irq)
4336{
Chen Gang02cdb502013-02-27 11:33:25 +08004337 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004338 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004339
4340 if (!irqchip_in_kernel(vcpu->kvm)) {
4341 kvm_queue_interrupt(vcpu, irq->irq, false);
4342 kvm_make_request(KVM_REQ_EVENT, vcpu);
4343 return 0;
4344 }
4345
4346 /*
4347 * With in-kernel LAPIC, we only use this to inject EXTINT, so
4348 * fail for in-kernel 8259.
4349 */
4350 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004351 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004352
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004353 if (vcpu->arch.pending_external_vector != -1)
4354 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004355
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004356 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08004357 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004358 return 0;
4359}
4360
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004361static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
4362{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004363 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004364
4365 return 0;
4366}
4367
Paolo Bonzinif0778252015-04-01 15:06:40 +02004368static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
4369{
Paolo Bonzini64d60672015-05-07 11:36:11 +02004370 kvm_make_request(KVM_REQ_SMI, vcpu);
4371
Paolo Bonzinif0778252015-04-01 15:06:40 +02004372 return 0;
4373}
4374
Avi Kivityb209749f2007-10-22 16:50:39 +02004375static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
4376 struct kvm_tpr_access_ctl *tac)
4377{
4378 if (tac->flags)
4379 return -EINVAL;
4380 vcpu->arch.tpr_access_reporting = !!tac->enabled;
4381 return 0;
4382}
4383
Huang Ying890ca9a2009-05-11 16:48:15 +08004384static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
4385 u64 mcg_cap)
4386{
4387 int r;
4388 unsigned bank_num = mcg_cap & 0xff, bank;
4389
4390 r = -EINVAL;
Jim Mattsonc4e0e4ab2020-05-11 15:56:16 -07004391 if (!bank_num || bank_num > KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08004392 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004393 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08004394 goto out;
4395 r = 0;
4396 vcpu->arch.mcg_cap = mcg_cap;
4397 /* Init IA32_MCG_CTL to all 1s */
4398 if (mcg_cap & MCG_CTL_P)
4399 vcpu->arch.mcg_ctl = ~(u64)0;
4400 /* Init IA32_MCi_CTL to all 1s */
4401 for (bank = 0; bank < bank_num; bank++)
4402 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004403
Jason Baronb36464772021-01-14 22:27:56 -05004404 static_call(kvm_x86_setup_mce)(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08004405out:
4406 return r;
4407}
4408
4409static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
4410 struct kvm_x86_mce *mce)
4411{
4412 u64 mcg_cap = vcpu->arch.mcg_cap;
4413 unsigned bank_num = mcg_cap & 0xff;
4414 u64 *banks = vcpu->arch.mce_banks;
4415
4416 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
4417 return -EINVAL;
4418 /*
4419 * if IA32_MCG_CTL is not all 1s, the uncorrected error
4420 * reporting is disabled
4421 */
4422 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
4423 vcpu->arch.mcg_ctl != ~(u64)0)
4424 return 0;
4425 banks += 4 * mce->bank;
4426 /*
4427 * if IA32_MCi_CTL is not all 1s, the uncorrected error
4428 * reporting is disabled for the bank
4429 */
4430 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
4431 return 0;
4432 if (mce->status & MCI_STATUS_UC) {
4433 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02004434 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03004435 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08004436 return 0;
4437 }
4438 if (banks[1] & MCI_STATUS_VAL)
4439 mce->status |= MCI_STATUS_OVER;
4440 banks[2] = mce->addr;
4441 banks[3] = mce->misc;
4442 vcpu->arch.mcg_status = mce->mcg_status;
4443 banks[1] = mce->status;
4444 kvm_queue_exception(vcpu, MC_VECTOR);
4445 } else if (!(banks[1] & MCI_STATUS_VAL)
4446 || !(banks[1] & MCI_STATUS_UC)) {
4447 if (banks[1] & MCI_STATUS_VAL)
4448 mce->status |= MCI_STATUS_OVER;
4449 banks[2] = mce->addr;
4450 banks[3] = mce->misc;
4451 banks[1] = mce->status;
4452 } else
4453 banks[1] |= MCI_STATUS_OVER;
4454 return 0;
4455}
4456
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004457static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
4458 struct kvm_vcpu_events *events)
4459{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004460 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07004461
Jay Zhou1f7becf2021-01-18 16:47:20 +08004462 if (kvm_check_request(KVM_REQ_SMI, vcpu))
4463 process_smi(vcpu);
4464
Wanpeng Li664f8e22017-08-24 03:35:09 -07004465 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08004466 * In guest mode, payload delivery should be deferred,
4467 * so that the L1 hypervisor can intercept #PF before
4468 * CR2 is modified (or intercept #DB before DR6 is
4469 * modified under nVMX). Unless the per-VM capability,
4470 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
4471 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
4472 * opportunistically defer the exception payload, deliver it if the
4473 * capability hasn't been requested before processing a
4474 * KVM_GET_VCPU_EVENTS.
4475 */
4476 if (!vcpu->kvm->arch.exception_payload_enabled &&
4477 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
4478 kvm_deliver_exception_payload(vcpu);
4479
4480 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07004481 * The API doesn't provide the instruction length for software
4482 * exceptions, so don't report them. As long as the guest RIP
4483 * isn't advanced, we should expect to encounter the exception
4484 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07004485 */
Jim Mattson59073aa2018-10-16 14:29:20 -07004486 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
4487 events->exception.injected = 0;
4488 events->exception.pending = 0;
4489 } else {
4490 events->exception.injected = vcpu->arch.exception.injected;
4491 events->exception.pending = vcpu->arch.exception.pending;
4492 /*
4493 * For ABI compatibility, deliberately conflate
4494 * pending and injected exceptions when
4495 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
4496 */
4497 if (!vcpu->kvm->arch.exception_payload_enabled)
4498 events->exception.injected |=
4499 vcpu->arch.exception.pending;
4500 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004501 events->exception.nr = vcpu->arch.exception.nr;
4502 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
4503 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07004504 events->exception_has_payload = vcpu->arch.exception.has_payload;
4505 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004506
Jan Kiszka03b82a32010-02-15 10:45:41 +01004507 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03004508 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004509 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01004510 events->interrupt.soft = 0;
Jason Baronb36464772021-01-14 22:27:56 -05004511 events->interrupt.shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004512
4513 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004514 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jason Baronb36464772021-01-14 22:27:56 -05004515 events->nmi.masked = static_call(kvm_x86_get_nmi_mask)(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004516 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004517
Jan Kiszka66450a22013-03-13 12:42:34 +01004518 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004519
Paolo Bonzinif0778252015-04-01 15:06:40 +02004520 events->smi.smm = is_smm(vcpu);
4521 events->smi.pending = vcpu->arch.smi_pending;
4522 events->smi.smm_inside_nmi =
4523 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
4524 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
4525
Jan Kiszkadab4b912009-12-06 18:24:15 +01004526 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02004527 | KVM_VCPUEVENT_VALID_SHADOW
4528 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07004529 if (vcpu->kvm->arch.exception_payload_enabled)
4530 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
4531
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004532 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004533}
4534
Sean Christophersondc872752021-06-09 11:56:15 -07004535static void kvm_smm_changed(struct kvm_vcpu *vcpu, bool entering_smm);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004536
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004537static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
4538 struct kvm_vcpu_events *events)
4539{
Jan Kiszkadab4b912009-12-06 18:24:15 +01004540 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01004541 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02004542 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07004543 | KVM_VCPUEVENT_VALID_SMM
4544 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004545 return -EINVAL;
4546
Jim Mattson59073aa2018-10-16 14:29:20 -07004547 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
4548 if (!vcpu->kvm->arch.exception_payload_enabled)
4549 return -EINVAL;
4550 if (events->exception.pending)
4551 events->exception.injected = 0;
4552 else
4553 events->exception_has_payload = 0;
4554 } else {
4555 events->exception.pending = 0;
4556 events->exception_has_payload = 0;
4557 }
4558
4559 if ((events->exception.injected || events->exception.pending) &&
4560 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02004561 return -EINVAL;
4562
David Hildenbrand28bf2882017-03-23 11:46:03 +01004563 /* INITs are latched while in SMM */
4564 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
4565 (events->smi.smm || events->smi.pending) &&
4566 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4567 return -EINVAL;
4568
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004569 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07004570 vcpu->arch.exception.injected = events->exception.injected;
4571 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004572 vcpu->arch.exception.nr = events->exception.nr;
4573 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
4574 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07004575 vcpu->arch.exception.has_payload = events->exception_has_payload;
4576 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004577
Liran Alon04140b42018-03-23 03:01:31 +03004578 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004579 vcpu->arch.interrupt.nr = events->interrupt.nr;
4580 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01004581 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
Jason Baronb36464772021-01-14 22:27:56 -05004582 static_call(kvm_x86_set_interrupt_shadow)(vcpu,
4583 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004584
4585 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01004586 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
4587 vcpu->arch.nmi_pending = events->nmi.pending;
Jason Baronb36464772021-01-14 22:27:56 -05004588 static_call(kvm_x86_set_nmi_mask)(vcpu, events->nmi.masked);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004589
Jan Kiszka66450a22013-03-13 12:42:34 +01004590 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004591 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01004592 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004593
Paolo Bonzinif0778252015-04-01 15:06:40 +02004594 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersondc872752021-06-09 11:56:15 -07004595 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm)
4596 kvm_smm_changed(vcpu, events->smi.smm);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004597
Paolo Bonzinif0778252015-04-01 15:06:40 +02004598 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004599
4600 if (events->smi.smm) {
4601 if (events->smi.smm_inside_nmi)
4602 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02004603 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004604 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02004605 }
4606
4607 if (lapic_in_kernel(vcpu)) {
4608 if (events->smi.latched_init)
4609 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
4610 else
4611 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02004612 }
4613 }
4614
Avi Kivity3842d132010-07-27 12:30:24 +03004615 kvm_make_request(KVM_REQ_EVENT, vcpu);
4616
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004617 return 0;
4618}
4619
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004620static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
4621 struct kvm_debugregs *dbgregs)
4622{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004623 unsigned long val;
4624
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004625 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004626 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004627 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004628 dbgregs->dr7 = vcpu->arch.dr7;
4629 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004630 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004631}
4632
4633static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
4634 struct kvm_debugregs *dbgregs)
4635{
4636 if (dbgregs->flags)
4637 return -EINVAL;
4638
Paolo Bonzinifd238002020-11-13 08:31:09 -05004639 if (!kvm_dr6_valid(dbgregs->dr6))
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004640 return -EINVAL;
Paolo Bonzinifd238002020-11-13 08:31:09 -05004641 if (!kvm_dr7_valid(dbgregs->dr7))
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004642 return -EINVAL;
4643
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004644 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004645 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004646 vcpu->arch.dr6 = dbgregs->dr6;
4647 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004648 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004649
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004650 return 0;
4651}
4652
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004653#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4654
4655static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4656{
Marc Orrb666a4b2018-11-06 14:53:56 -08004657 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004658 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004659 u64 valid;
4660
4661 /*
4662 * Copy legacy XSAVE area, to avoid complications with CPUID
4663 * leaves 0 and 1 in the loop below.
4664 */
4665 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4666
4667 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004668 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004669 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4670
4671 /*
4672 * Copy each region from the possibly compacted offset to the
4673 * non-compacted offset.
4674 */
Dave Hansend91cab72015-09-02 16:31:26 -07004675 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004676 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004677 u64 xfeature_mask = valid & -valid;
4678 int xfeature_nr = fls64(xfeature_mask) - 1;
4679 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004680
4681 if (src) {
4682 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004683 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004684 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004685 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004686 memcpy(dest + offset, &vcpu->arch.pkru,
4687 sizeof(vcpu->arch.pkru));
4688 else
4689 memcpy(dest + offset, src, size);
4690
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004691 }
4692
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004693 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004694 }
4695}
4696
4697static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4698{
Marc Orrb666a4b2018-11-06 14:53:56 -08004699 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004700 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4701 u64 valid;
4702
4703 /*
4704 * Copy legacy XSAVE area, to avoid complications with CPUID
4705 * leaves 0 and 1 in the loop below.
4706 */
4707 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4708
4709 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004710 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004711 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004712 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004713
4714 /*
4715 * Copy each region from the non-compacted offset to the
4716 * possibly compacted offset.
4717 */
Dave Hansend91cab72015-09-02 16:31:26 -07004718 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004719 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004720 u64 xfeature_mask = valid & -valid;
4721 int xfeature_nr = fls64(xfeature_mask) - 1;
4722 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004723
4724 if (dest) {
4725 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004726 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004727 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004728 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004729 memcpy(&vcpu->arch.pkru, src + offset,
4730 sizeof(vcpu->arch.pkru));
4731 else
4732 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004733 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004734
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004735 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004736 }
4737}
4738
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004739static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4740 struct kvm_xsave *guest_xsave)
4741{
Tom Lendackyed02b212020-12-10 11:10:01 -06004742 if (!vcpu->arch.guest_fpu)
4743 return;
4744
Borislav Petkovd366bf72016-04-04 22:25:02 +02004745 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004746 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4747 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004748 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004749 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004750 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004751 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004752 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004753 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004754 }
4755}
4756
Wanpeng Lia5758132017-05-11 02:58:55 -07004757#define XSAVE_MXCSR_OFFSET 24
4758
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004759static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4760 struct kvm_xsave *guest_xsave)
4761{
Tom Lendackyed02b212020-12-10 11:10:01 -06004762 u64 xstate_bv;
4763 u32 mxcsr;
4764
4765 if (!vcpu->arch.guest_fpu)
4766 return 0;
4767
4768 xstate_bv = *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
4769 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004770
Borislav Petkovd366bf72016-04-04 22:25:02 +02004771 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004772 /*
4773 * Here we allow setting states that are not present in
4774 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4775 * with old userspace.
4776 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004777 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004778 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004779 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004780 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004781 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4782 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004783 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004784 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004785 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004786 }
4787 return 0;
4788}
4789
4790static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4791 struct kvm_xcrs *guest_xcrs)
4792{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004793 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004794 guest_xcrs->nr_xcrs = 0;
4795 return;
4796 }
4797
4798 guest_xcrs->nr_xcrs = 1;
4799 guest_xcrs->flags = 0;
4800 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4801 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4802}
4803
4804static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4805 struct kvm_xcrs *guest_xcrs)
4806{
4807 int i, r = 0;
4808
Borislav Petkovd366bf72016-04-04 22:25:02 +02004809 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004810 return -EINVAL;
4811
4812 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4813 return -EINVAL;
4814
4815 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4816 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004817 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004818 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004819 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004820 break;
4821 }
4822 if (r)
4823 r = -EINVAL;
4824 return r;
4825}
4826
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004827/*
4828 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4829 * stopped by the hypervisor. This function will be called from the host only.
4830 * EINVAL is returned when the host attempts to set the flag for a guest that
4831 * does not support pv clocks.
4832 */
4833static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4834{
Andy Honig0b794592013-02-20 14:48:10 -08004835 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004836 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004837 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004838 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4839 return 0;
4840}
4841
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004842static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4843 struct kvm_enable_cap *cap)
4844{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004845 int r;
4846 uint16_t vmcs_version;
4847 void __user *user_ptr;
4848
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004849 if (cap->flags)
4850 return -EINVAL;
4851
4852 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004853 case KVM_CAP_HYPERV_SYNIC2:
4854 if (cap->args[0])
4855 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004856 fallthrough;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004857
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004858 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004859 if (!irqchip_in_kernel(vcpu->kvm))
4860 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004861 return kvm_hv_activate_synic(vcpu, cap->cap ==
4862 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004863 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004864 if (!kvm_x86_ops.nested_ops->enable_evmcs)
Sean Christopherson51589172018-12-03 13:53:10 -08004865 return -ENOTTY;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004866 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004867 if (!r) {
4868 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4869 if (copy_to_user(user_ptr, &vmcs_version,
4870 sizeof(vmcs_version)))
4871 r = -EFAULT;
4872 }
4873 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004874 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004875 if (!kvm_x86_ops.enable_direct_tlbflush)
Tianyu Lan344c6c82019-08-22 22:30:20 +08004876 return -ENOTTY;
4877
Jason Baronb36464772021-01-14 22:27:56 -05004878 return static_call(kvm_x86_enable_direct_tlbflush)(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004879
Oliver Upton66570e92020-08-18 15:24:28 +00004880 case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
4881 vcpu->arch.pv_cpuid.enforce = cap->args[0];
Oliver Upton01b4f512020-10-27 16:10:42 -07004882 if (vcpu->arch.pv_cpuid.enforce)
4883 kvm_update_pv_runtime(vcpu);
Oliver Upton66570e92020-08-18 15:24:28 +00004884
4885 return 0;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004886 default:
4887 return -EINVAL;
4888 }
4889}
4890
Carsten Otte313a3dc2007-10-11 19:16:52 +02004891long kvm_arch_vcpu_ioctl(struct file *filp,
4892 unsigned int ioctl, unsigned long arg)
4893{
4894 struct kvm_vcpu *vcpu = filp->private_data;
4895 void __user *argp = (void __user *)arg;
4896 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004897 union {
4898 struct kvm_lapic_state *lapic;
4899 struct kvm_xsave *xsave;
4900 struct kvm_xcrs *xcrs;
4901 void *buffer;
4902 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004903
Christoffer Dall9b0624712017-12-04 21:35:36 +01004904 vcpu_load(vcpu);
4905
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004906 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004907 switch (ioctl) {
4908 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004909 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004910 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004911 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004912 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4913 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004914
Dave Hansenb772ff32008-08-11 10:01:47 -07004915 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004916 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004917 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004918 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004919 if (r)
4920 goto out;
4921 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004922 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004923 goto out;
4924 r = 0;
4925 break;
4926 }
4927 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004928 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004929 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004930 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004931 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004932 if (IS_ERR(u.lapic)) {
4933 r = PTR_ERR(u.lapic);
4934 goto out_nofree;
4935 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004936
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004937 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004938 break;
4939 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004940 case KVM_INTERRUPT: {
4941 struct kvm_interrupt irq;
4942
4943 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004944 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004945 goto out;
4946 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004947 break;
4948 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004949 case KVM_NMI: {
4950 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004951 break;
4952 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004953 case KVM_SMI: {
4954 r = kvm_vcpu_ioctl_smi(vcpu);
4955 break;
4956 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004957 case KVM_SET_CPUID: {
4958 struct kvm_cpuid __user *cpuid_arg = argp;
4959 struct kvm_cpuid cpuid;
4960
4961 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004962 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004963 goto out;
4964 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004965 break;
4966 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004967 case KVM_SET_CPUID2: {
4968 struct kvm_cpuid2 __user *cpuid_arg = argp;
4969 struct kvm_cpuid2 cpuid;
4970
4971 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004972 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004973 goto out;
4974 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004975 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004976 break;
4977 }
4978 case KVM_GET_CPUID2: {
4979 struct kvm_cpuid2 __user *cpuid_arg = argp;
4980 struct kvm_cpuid2 cpuid;
4981
4982 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004983 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004984 goto out;
4985 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004986 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004987 if (r)
4988 goto out;
4989 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004990 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004991 goto out;
4992 r = 0;
4993 break;
4994 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004995 case KVM_GET_MSRS: {
4996 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004997 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004998 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004999 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06005000 }
5001 case KVM_SET_MSRS: {
5002 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005003 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06005004 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005005 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06005006 }
Avi Kivityb209749f2007-10-22 16:50:39 +02005007 case KVM_TPR_ACCESS_REPORTING: {
5008 struct kvm_tpr_access_ctl tac;
5009
5010 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005011 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02005012 goto out;
5013 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
5014 if (r)
5015 goto out;
5016 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005017 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02005018 goto out;
5019 r = 0;
5020 break;
5021 };
Avi Kivityb93463a2007-10-25 16:52:32 +02005022 case KVM_SET_VAPIC_ADDR: {
5023 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01005024 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02005025
5026 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005027 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02005028 goto out;
5029 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005030 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02005031 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01005032 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08005033 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01005034 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02005035 break;
5036 }
Huang Ying890ca9a2009-05-11 16:48:15 +08005037 case KVM_X86_SETUP_MCE: {
5038 u64 mcg_cap;
5039
5040 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005041 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08005042 goto out;
5043 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
5044 break;
5045 }
5046 case KVM_X86_SET_MCE: {
5047 struct kvm_x86_mce mce;
5048
5049 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005050 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08005051 goto out;
5052 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
5053 break;
5054 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005055 case KVM_GET_VCPU_EVENTS: {
5056 struct kvm_vcpu_events events;
5057
5058 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
5059
5060 r = -EFAULT;
5061 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
5062 break;
5063 r = 0;
5064 break;
5065 }
5066 case KVM_SET_VCPU_EVENTS: {
5067 struct kvm_vcpu_events events;
5068
5069 r = -EFAULT;
5070 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
5071 break;
5072
5073 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
5074 break;
5075 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01005076 case KVM_GET_DEBUGREGS: {
5077 struct kvm_debugregs dbgregs;
5078
5079 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
5080
5081 r = -EFAULT;
5082 if (copy_to_user(argp, &dbgregs,
5083 sizeof(struct kvm_debugregs)))
5084 break;
5085 r = 0;
5086 break;
5087 }
5088 case KVM_SET_DEBUGREGS: {
5089 struct kvm_debugregs dbgregs;
5090
5091 r = -EFAULT;
5092 if (copy_from_user(&dbgregs, argp,
5093 sizeof(struct kvm_debugregs)))
5094 break;
5095
5096 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
5097 break;
5098 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005099 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08005100 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005101 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005102 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005103 break;
5104
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005105 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005106
5107 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005108 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005109 break;
5110 r = 0;
5111 break;
5112 }
5113 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02005114 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01005115 if (IS_ERR(u.xsave)) {
5116 r = PTR_ERR(u.xsave);
5117 goto out_nofree;
5118 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005119
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005120 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005121 break;
5122 }
5123 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08005124 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005125 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005126 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005127 break;
5128
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005129 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005130
5131 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005132 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005133 sizeof(struct kvm_xcrs)))
5134 break;
5135 r = 0;
5136 break;
5137 }
5138 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02005139 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01005140 if (IS_ERR(u.xcrs)) {
5141 r = PTR_ERR(u.xcrs);
5142 goto out_nofree;
5143 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005144
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005145 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08005146 break;
5147 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01005148 case KVM_SET_TSC_KHZ: {
5149 u32 user_tsc_khz;
5150
5151 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01005152 user_tsc_khz = (u32)arg;
5153
Marcelo Tosatti26769f92020-06-16 08:47:41 -03005154 if (kvm_has_tsc_control &&
5155 user_tsc_khz >= kvm_max_guest_tsc_khz)
Joerg Roedel92a1f122011-03-25 09:44:51 +01005156 goto out;
5157
Zachary Amsdencc578282012-02-03 15:43:50 -02005158 if (user_tsc_khz == 0)
5159 user_tsc_khz = tsc_khz;
5160
Haozhong Zhang381d5852015-10-20 15:39:04 +08005161 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
5162 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01005163
Joerg Roedel92a1f122011-03-25 09:44:51 +01005164 goto out;
5165 }
5166 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02005167 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01005168 goto out;
5169 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05005170 case KVM_KVMCLOCK_CTRL: {
5171 r = kvm_set_guest_paused(vcpu);
5172 goto out;
5173 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03005174 case KVM_ENABLE_CAP: {
5175 struct kvm_enable_cap cap;
5176
5177 r = -EFAULT;
5178 if (copy_from_user(&cap, argp, sizeof(cap)))
5179 goto out;
5180 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
5181 break;
5182 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005183 case KVM_GET_NESTED_STATE: {
5184 struct kvm_nested_state __user *user_kvm_nested_state = argp;
5185 u32 user_data_size;
5186
5187 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04005188 if (!kvm_x86_ops.nested_ops->get_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005189 break;
5190
5191 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03005192 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005193 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03005194 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005195
Paolo Bonzini33b22172020-04-17 10:24:18 -04005196 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state,
5197 user_data_size);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005198 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03005199 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005200
5201 if (r > user_data_size) {
5202 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03005203 r = -EFAULT;
5204 else
5205 r = -E2BIG;
5206 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005207 }
Liran Alon26b471c2018-09-16 14:28:20 +03005208
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005209 r = 0;
5210 break;
5211 }
5212 case KVM_SET_NESTED_STATE: {
5213 struct kvm_nested_state __user *user_kvm_nested_state = argp;
5214 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08005215 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005216
5217 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04005218 if (!kvm_x86_ops.nested_ops->set_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005219 break;
5220
Liran Alon26b471c2018-09-16 14:28:20 +03005221 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005222 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03005223 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005224
Liran Alon26b471c2018-09-16 14:28:20 +03005225 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005226 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03005227 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005228
5229 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02005230 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04005231 | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING
5232 | KVM_STATE_NESTED_GIF_SET))
Liran Alon26b471c2018-09-16 14:28:20 +03005233 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005234
5235 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02005236 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
5237 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03005238 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005239
Sean Christophersonad5996d2019-11-22 08:58:18 -08005240 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini33b22172020-04-17 10:24:18 -04005241 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08005242 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005243 break;
5244 }
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02005245 case KVM_GET_SUPPORTED_HV_CPUID:
5246 r = kvm_ioctl_get_supported_hv_cpuid(vcpu, argp);
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01005247 break;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005248#ifdef CONFIG_KVM_XEN
David Woodhouse3e324612021-02-02 16:53:25 +00005249 case KVM_XEN_VCPU_GET_ATTR: {
5250 struct kvm_xen_vcpu_attr xva;
5251
5252 r = -EFAULT;
5253 if (copy_from_user(&xva, argp, sizeof(xva)))
5254 goto out;
5255 r = kvm_xen_vcpu_get_attr(vcpu, &xva);
5256 if (!r && copy_to_user(argp, &xva, sizeof(xva)))
5257 r = -EFAULT;
5258 break;
5259 }
5260 case KVM_XEN_VCPU_SET_ATTR: {
5261 struct kvm_xen_vcpu_attr xva;
5262
5263 r = -EFAULT;
5264 if (copy_from_user(&xva, argp, sizeof(xva)))
5265 goto out;
5266 r = kvm_xen_vcpu_set_attr(vcpu, &xva);
5267 break;
5268 }
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005269#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +02005270 default:
5271 r = -EINVAL;
5272 }
5273out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005274 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01005275out_nofree:
5276 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005277 return r;
5278}
5279
Souptick Joarder1499fa82018-04-19 00:49:58 +05305280vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01005281{
5282 return VM_FAULT_SIGBUS;
5283}
5284
Carsten Otte1fe779f2007-10-29 16:08:35 +01005285static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
5286{
5287 int ret;
5288
5289 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08005290 return -EINVAL;
Jason Baronb36464772021-01-14 22:27:56 -05005291 ret = static_call(kvm_x86_set_tss_addr)(kvm, addr);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005292 return ret;
5293}
5294
Sheng Yangb927a3c2009-07-21 10:42:48 +08005295static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
5296 u64 ident_addr)
5297{
Jason Baronb36464772021-01-14 22:27:56 -05005298 return static_call(kvm_x86_set_identity_map_addr)(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08005299}
5300
Carsten Otte1fe779f2007-10-29 16:08:35 +01005301static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005302 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01005303{
5304 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
5305 return -EINVAL;
5306
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005307 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005308
5309 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005310 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005311
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005312 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005313 return 0;
5314}
5315
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005316static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01005317{
Dave Hansen39de71e2010-08-19 18:11:14 -07005318 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005319}
5320
Carsten Otte1fe779f2007-10-29 16:08:35 +01005321static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
5322{
David Hildenbrand90bca052017-04-07 10:50:23 +02005323 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005324 int r;
5325
5326 r = 0;
5327 switch (chip->chip_id) {
5328 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02005329 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01005330 sizeof(struct kvm_pic_state));
5331 break;
5332 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02005333 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01005334 sizeof(struct kvm_pic_state));
5335 break;
5336 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02005337 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005338 break;
5339 default:
5340 r = -EINVAL;
5341 break;
5342 }
5343 return r;
5344}
5345
5346static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
5347{
David Hildenbrand90bca052017-04-07 10:50:23 +02005348 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005349 int r;
5350
5351 r = 0;
5352 switch (chip->chip_id) {
5353 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02005354 spin_lock(&pic->lock);
5355 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01005356 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02005357 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005358 break;
5359 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02005360 spin_lock(&pic->lock);
5361 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01005362 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02005363 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005364 break;
5365 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02005366 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005367 break;
5368 default:
5369 r = -EINVAL;
5370 break;
5371 }
David Hildenbrand90bca052017-04-07 10:50:23 +02005372 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005373 return r;
5374}
5375
Sheng Yange0f63cb2008-03-04 00:50:59 +08005376static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
5377{
Radim Krčmář34f39412016-03-02 22:56:50 +01005378 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
5379
5380 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
5381
5382 mutex_lock(&kps->lock);
5383 memcpy(ps, &kps->channels, sizeof(*ps));
5384 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305385 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08005386}
5387
5388static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
5389{
Andrew Honig01856042015-11-18 14:50:23 -08005390 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01005391 struct kvm_pit *pit = kvm->arch.vpit;
5392
5393 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01005394 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08005395 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01005396 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
5397 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305398 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04005399}
5400
5401static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
5402{
Beth Kone9f42752009-07-07 11:50:38 -04005403 mutex_lock(&kvm->arch.vpit->pit_state.lock);
5404 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
5405 sizeof(ps->channels));
5406 ps->flags = kvm->arch.vpit->pit_state.flags;
5407 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005408 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305409 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04005410}
5411
5412static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
5413{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305414 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08005415 int i;
Beth Kone9f42752009-07-07 11:50:38 -04005416 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01005417 struct kvm_pit *pit = kvm->arch.vpit;
5418
5419 mutex_lock(&pit->pit_state.lock);
5420 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04005421 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
5422 if (!prev_legacy && cur_legacy)
5423 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01005424 memcpy(&pit->pit_state.channels, &ps->channels,
5425 sizeof(pit->pit_state.channels));
5426 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08005427 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01005428 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01005429 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01005430 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305431 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08005432}
5433
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005434static int kvm_vm_ioctl_reinject(struct kvm *kvm,
5435 struct kvm_reinject_control *control)
5436{
Radim Krčmář71474e22016-03-02 22:56:45 +01005437 struct kvm_pit *pit = kvm->arch.vpit;
5438
Radim Krčmář71474e22016-03-02 22:56:45 +01005439 /* pit->pit_state.lock was overloaded to prevent userspace from getting
5440 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
5441 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
5442 */
5443 mutex_lock(&pit->pit_state.lock);
5444 kvm_pit_set_reinject(pit, control->pit_reinject);
5445 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01005446
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005447 return 0;
5448}
5449
Sean Christopherson0dff0842020-02-18 13:07:29 -08005450void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08005451{
Sean Christophersona018eba2021-02-12 16:50:10 -08005452
Kai Huang88178fd2015-01-28 10:54:27 +08005453 /*
Sean Christophersona018eba2021-02-12 16:50:10 -08005454 * Flush all CPUs' dirty log buffers to the dirty_bitmap. Called
5455 * before reporting dirty_bitmap to userspace. KVM flushes the buffers
5456 * on all VM-Exits, thus we only need to kick running vCPUs to force a
5457 * VM-Exit.
Kai Huang88178fd2015-01-28 10:54:27 +08005458 */
Sean Christophersona018eba2021-02-12 16:50:10 -08005459 struct kvm_vcpu *vcpu;
5460 int i;
5461
5462 kvm_for_each_vcpu(i, vcpu, kvm)
5463 kvm_vcpu_kick(vcpu);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08005464}
5465
Yang Zhangaa2fbe62013-04-11 19:21:40 +08005466int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
5467 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04005468{
5469 if (!irqchip_in_kernel(kvm))
5470 return -ENXIO;
5471
5472 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08005473 irq_event->irq, irq_event->level,
5474 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04005475 return 0;
5476}
5477
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01005478int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
5479 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03005480{
5481 int r;
5482
5483 if (cap->flags)
5484 return -EINVAL;
5485
5486 switch (cap->cap) {
5487 case KVM_CAP_DISABLE_QUIRKS:
5488 kvm->arch.disabled_quirks = cap->args[0];
5489 r = 0;
5490 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07005491 case KVM_CAP_SPLIT_IRQCHIP: {
5492 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07005493 r = -EINVAL;
5494 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
5495 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07005496 r = -EEXIST;
5497 if (irqchip_in_kernel(kvm))
5498 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02005499 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07005500 goto split_irqchip_unlock;
5501 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02005502 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07005503 goto split_irqchip_unlock;
5504 /* Pairs with irqchip_in_kernel. */
5505 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005506 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07005507 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07005508 r = 0;
5509split_irqchip_unlock:
5510 mutex_unlock(&kvm->lock);
5511 break;
5512 }
Radim Krčmář371313132016-07-12 22:09:27 +02005513 case KVM_CAP_X2APIC_API:
5514 r = -EINVAL;
5515 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
5516 break;
5517
5518 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
5519 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02005520 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
5521 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02005522
5523 r = 0;
5524 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005525 case KVM_CAP_X86_DISABLE_EXITS:
5526 r = -EINVAL;
5527 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
5528 break;
5529
5530 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
5531 kvm_can_mwait_in_guest())
5532 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03005533 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07005534 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005535 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
5536 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08005537 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
5538 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005539 r = 0;
5540 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07005541 case KVM_CAP_MSR_PLATFORM_INFO:
5542 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
5543 r = 0;
5544 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07005545 case KVM_CAP_EXCEPTION_PAYLOAD:
5546 kvm->arch.exception_payload_enabled = cap->args[0];
5547 r = 0;
5548 break;
Alexander Graf1ae09952020-09-25 16:34:16 +02005549 case KVM_CAP_X86_USER_SPACE_MSR:
5550 kvm->arch.user_space_msr_mask = cap->args[0];
5551 r = 0;
5552 break;
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005553 case KVM_CAP_X86_BUS_LOCK_EXIT:
5554 r = -EINVAL;
5555 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE)
5556 break;
5557
5558 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) &&
5559 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT))
5560 break;
5561
5562 if (kvm_has_bus_lock_exit &&
5563 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)
5564 kvm->arch.bus_lock_detection_enabled = true;
5565 r = 0;
5566 break;
Sean Christophersonfe7e9482021-04-12 16:21:43 +12005567#ifdef CONFIG_X86_SGX_KVM
5568 case KVM_CAP_SGX_ATTRIBUTE: {
5569 unsigned long allowed_attributes = 0;
5570
5571 r = sgx_set_attribute(&allowed_attributes, cap->args[0]);
5572 if (r)
5573 break;
5574
5575 /* KVM only supports the PROVISIONKEY privileged attribute. */
5576 if ((allowed_attributes & SGX_ATTR_PROVISIONKEY) &&
5577 !(allowed_attributes & ~SGX_ATTR_PROVISIONKEY))
5578 kvm->arch.sgx_provisioning_allowed = true;
5579 else
5580 r = -EINVAL;
5581 break;
5582 }
5583#endif
Nathan Tempelman54526d12021-04-08 22:32:14 +00005584 case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
5585 r = -EINVAL;
5586 if (kvm_x86_ops.vm_copy_enc_context_from)
5587 r = kvm_x86_ops.vm_copy_enc_context_from(kvm, cap->args[0]);
5588 return r;
Nadav Amit90de4a12015-04-13 01:53:41 +03005589 default:
5590 r = -EINVAL;
5591 break;
5592 }
5593 return r;
5594}
5595
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005596static struct kvm_x86_msr_filter *kvm_alloc_msr_filter(bool default_allow)
Alexander Graf1a1552542020-09-25 16:34:21 +02005597{
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005598 struct kvm_x86_msr_filter *msr_filter;
Alexander Graf1a1552542020-09-25 16:34:21 +02005599
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005600 msr_filter = kzalloc(sizeof(*msr_filter), GFP_KERNEL_ACCOUNT);
5601 if (!msr_filter)
5602 return NULL;
Alexander Graf1a1552542020-09-25 16:34:21 +02005603
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005604 msr_filter->default_allow = default_allow;
5605 return msr_filter;
Alexander Graf1a1552542020-09-25 16:34:21 +02005606}
5607
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005608static void kvm_free_msr_filter(struct kvm_x86_msr_filter *msr_filter)
Alexander Graf1a1552542020-09-25 16:34:21 +02005609{
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005610 u32 i;
5611
5612 if (!msr_filter)
5613 return;
5614
5615 for (i = 0; i < msr_filter->count; i++)
5616 kfree(msr_filter->ranges[i].bitmap);
5617
5618 kfree(msr_filter);
5619}
5620
5621static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,
5622 struct kvm_msr_filter_range *user_range)
5623{
Alexander Graf1a1552542020-09-25 16:34:21 +02005624 unsigned long *bitmap = NULL;
5625 size_t bitmap_size;
Alexander Graf1a1552542020-09-25 16:34:21 +02005626
5627 if (!user_range->nmsrs)
5628 return 0;
5629
Siddharth Chandrasekaranaca35282021-05-03 14:21:11 +02005630 if (user_range->flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE))
5631 return -EINVAL;
5632
5633 if (!user_range->flags)
5634 return -EINVAL;
5635
Alexander Graf1a1552542020-09-25 16:34:21 +02005636 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long);
5637 if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE)
5638 return -EINVAL;
5639
5640 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size);
5641 if (IS_ERR(bitmap))
5642 return PTR_ERR(bitmap);
5643
Siddharth Chandrasekaranaca35282021-05-03 14:21:11 +02005644 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) {
Alexander Graf1a1552542020-09-25 16:34:21 +02005645 .flags = user_range->flags,
5646 .base = user_range->base,
5647 .nmsrs = user_range->nmsrs,
5648 .bitmap = bitmap,
5649 };
5650
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005651 msr_filter->count++;
Alexander Graf1a1552542020-09-25 16:34:21 +02005652 return 0;
Alexander Graf1a1552542020-09-25 16:34:21 +02005653}
5654
5655static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp)
5656{
5657 struct kvm_msr_filter __user *user_msr_filter = argp;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005658 struct kvm_x86_msr_filter *new_filter, *old_filter;
Alexander Graf1a1552542020-09-25 16:34:21 +02005659 struct kvm_msr_filter filter;
5660 bool default_allow;
Paolo Bonzini043248b2020-10-20 10:57:01 -04005661 bool empty = true;
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005662 int r = 0;
Alexander Graf1a1552542020-09-25 16:34:21 +02005663 u32 i;
5664
5665 if (copy_from_user(&filter, user_msr_filter, sizeof(filter)))
5666 return -EFAULT;
5667
Paolo Bonzini043248b2020-10-20 10:57:01 -04005668 for (i = 0; i < ARRAY_SIZE(filter.ranges); i++)
5669 empty &= !filter.ranges[i].nmsrs;
Alexander Graf1a1552542020-09-25 16:34:21 +02005670
5671 default_allow = !(filter.flags & KVM_MSR_FILTER_DEFAULT_DENY);
Paolo Bonzini043248b2020-10-20 10:57:01 -04005672 if (empty && !default_allow)
5673 return -EINVAL;
5674
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005675 new_filter = kvm_alloc_msr_filter(default_allow);
5676 if (!new_filter)
5677 return -ENOMEM;
Paolo Bonzini043248b2020-10-20 10:57:01 -04005678
Alexander Graf1a1552542020-09-25 16:34:21 +02005679 for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) {
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005680 r = kvm_add_msr_filter(new_filter, &filter.ranges[i]);
5681 if (r) {
5682 kvm_free_msr_filter(new_filter);
5683 return r;
5684 }
Alexander Graf1a1552542020-09-25 16:34:21 +02005685 }
5686
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005687 mutex_lock(&kvm->lock);
5688
5689 /* The per-VM filter is protected by kvm->lock... */
5690 old_filter = srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1);
5691
5692 rcu_assign_pointer(kvm->arch.msr_filter, new_filter);
5693 synchronize_srcu(&kvm->srcu);
5694
5695 kvm_free_msr_filter(old_filter);
5696
Alexander Graf1a1552542020-09-25 16:34:21 +02005697 kvm_make_all_cpus_request(kvm, KVM_REQ_MSR_FILTER_CHANGED);
5698 mutex_unlock(&kvm->lock);
5699
Sean Christophersonb318e8d2021-03-16 11:44:33 -07005700 return 0;
Alexander Graf1a1552542020-09-25 16:34:21 +02005701}
5702
Sergey Senozhatsky7d628742021-06-06 11:10:45 +09005703#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER
5704static int kvm_arch_suspend_notifier(struct kvm *kvm)
5705{
5706 struct kvm_vcpu *vcpu;
5707 int i, ret = 0;
5708
5709 mutex_lock(&kvm->lock);
5710 kvm_for_each_vcpu(i, vcpu, kvm) {
5711 if (!vcpu->arch.pv_time_enabled)
5712 continue;
5713
5714 ret = kvm_set_guest_paused(vcpu);
5715 if (ret) {
5716 kvm_err("Failed to pause guest VCPU%d: %d\n",
5717 vcpu->vcpu_id, ret);
5718 break;
5719 }
5720 }
5721 mutex_unlock(&kvm->lock);
5722
5723 return ret ? NOTIFY_BAD : NOTIFY_DONE;
5724}
5725
5726int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state)
5727{
5728 switch (state) {
5729 case PM_HIBERNATION_PREPARE:
5730 case PM_SUSPEND_PREPARE:
5731 return kvm_arch_suspend_notifier(kvm);
5732 }
5733
5734 return NOTIFY_DONE;
5735}
5736#endif /* CONFIG_HAVE_KVM_PM_NOTIFIER */
5737
Carsten Otte1fe779f2007-10-29 16:08:35 +01005738long kvm_arch_vm_ioctl(struct file *filp,
5739 unsigned int ioctl, unsigned long arg)
5740{
5741 struct kvm *kvm = filp->private_data;
5742 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03005743 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07005744 /*
5745 * This union makes it completely explicit to gcc-3.x
5746 * that these two variables' stack usage should be
5747 * combined, not added together.
5748 */
5749 union {
5750 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04005751 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005752 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07005753 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005754
5755 switch (ioctl) {
5756 case KVM_SET_TSS_ADDR:
5757 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005758 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005759 case KVM_SET_IDENTITY_MAP_ADDR: {
5760 u64 ident_addr;
5761
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005762 mutex_lock(&kvm->lock);
5763 r = -EINVAL;
5764 if (kvm->created_vcpus)
5765 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005766 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005767 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005768 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005769 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005770set_identity_unlock:
5771 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08005772 break;
5773 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01005774 case KVM_SET_NR_MMU_PAGES:
5775 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005776 break;
5777 case KVM_GET_NR_MMU_PAGES:
5778 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
5779 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005780 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005781 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01005782
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005783 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01005784 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005785 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005786
Avi Kivity3e515702012-03-05 14:23:29 +02005787 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02005788 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02005789 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005790
5791 r = kvm_pic_init(kvm);
5792 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005793 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005794
5795 r = kvm_ioapic_init(kvm);
5796 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01005797 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005798 goto create_irqchip_unlock;
5799 }
5800
Avi Kivity399ec802008-11-19 13:58:46 +02005801 r = kvm_setup_default_irq_routing(kvm);
5802 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08005803 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005804 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005805 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02005806 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01005807 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005808 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005809 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005810 create_irqchip_unlock:
5811 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005812 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005813 }
Sheng Yang78376992008-01-28 05:10:22 +08005814 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005815 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
5816 goto create_pit;
5817 case KVM_CREATE_PIT2:
5818 r = -EFAULT;
5819 if (copy_from_user(&u.pit_config, argp,
5820 sizeof(struct kvm_pit_config)))
5821 goto out;
5822 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005823 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02005824 r = -EEXIST;
5825 if (kvm->arch.vpit)
5826 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08005827 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005828 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08005829 if (kvm->arch.vpit)
5830 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02005831 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005832 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08005833 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005834 case KVM_GET_IRQCHIP: {
5835 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005836 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005837
Sasha Levinff5c2c02011-12-04 19:36:29 +02005838 chip = memdup_user(argp, sizeof(*chip));
5839 if (IS_ERR(chip)) {
5840 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005841 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005842 }
5843
Carsten Otte1fe779f2007-10-29 16:08:35 +01005844 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005845 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005846 goto get_irqchip_out;
5847 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5848 if (r)
5849 goto get_irqchip_out;
5850 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005851 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005852 goto get_irqchip_out;
5853 r = 0;
5854 get_irqchip_out:
5855 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005856 break;
5857 }
5858 case KVM_SET_IRQCHIP: {
5859 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005860 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005861
Sasha Levinff5c2c02011-12-04 19:36:29 +02005862 chip = memdup_user(argp, sizeof(*chip));
5863 if (IS_ERR(chip)) {
5864 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005865 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005866 }
5867
Carsten Otte1fe779f2007-10-29 16:08:35 +01005868 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005869 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005870 goto set_irqchip_out;
5871 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005872 set_irqchip_out:
5873 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005874 break;
5875 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005876 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005877 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005878 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005879 goto out;
5880 r = -ENXIO;
5881 if (!kvm->arch.vpit)
5882 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005883 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005884 if (r)
5885 goto out;
5886 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005887 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005888 goto out;
5889 r = 0;
5890 break;
5891 }
5892 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005893 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005894 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005895 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005896 mutex_lock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005897 r = -ENXIO;
5898 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005899 goto set_pit_out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005900 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005901set_pit_out:
5902 mutex_unlock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005903 break;
5904 }
Beth Kone9f42752009-07-07 11:50:38 -04005905 case KVM_GET_PIT2: {
5906 r = -ENXIO;
5907 if (!kvm->arch.vpit)
5908 goto out;
5909 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5910 if (r)
5911 goto out;
5912 r = -EFAULT;
5913 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5914 goto out;
5915 r = 0;
5916 break;
5917 }
5918 case KVM_SET_PIT2: {
5919 r = -EFAULT;
5920 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5921 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005922 mutex_lock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005923 r = -ENXIO;
5924 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005925 goto set_pit2_out;
Beth Kone9f42752009-07-07 11:50:38 -04005926 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005927set_pit2_out:
5928 mutex_unlock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005929 break;
5930 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005931 case KVM_REINJECT_CONTROL: {
5932 struct kvm_reinject_control control;
5933 r = -EFAULT;
5934 if (copy_from_user(&control, argp, sizeof(control)))
5935 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005936 r = -ENXIO;
5937 if (!kvm->arch.vpit)
5938 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005939 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005940 break;
5941 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005942 case KVM_SET_BOOT_CPU_ID:
5943 r = 0;
5944 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005945 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005946 r = -EBUSY;
5947 else
5948 kvm->arch.bsp_vcpu_id = arg;
5949 mutex_unlock(&kvm->lock);
5950 break;
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005951#ifdef CONFIG_KVM_XEN
Ed Swierkffde22a2009-10-15 15:21:43 -07005952 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005953 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005954 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005955 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005956 goto out;
David Woodhouse78e98782021-02-02 13:19:35 +00005957 r = kvm_xen_hvm_config(kvm, &xhc);
Ed Swierkffde22a2009-10-15 15:21:43 -07005958 break;
5959 }
Joao Martinsa76b9642020-12-03 15:52:25 +00005960 case KVM_XEN_HVM_GET_ATTR: {
5961 struct kvm_xen_hvm_attr xha;
5962
5963 r = -EFAULT;
5964 if (copy_from_user(&xha, argp, sizeof(xha)))
Hannes Edercded19f2009-02-21 02:19:13 +01005965 goto out;
Joao Martinsa76b9642020-12-03 15:52:25 +00005966 r = kvm_xen_hvm_get_attr(kvm, &xha);
5967 if (!r && copy_to_user(argp, &xha, sizeof(xha)))
5968 r = -EFAULT;
5969 break;
5970 }
5971 case KVM_XEN_HVM_SET_ATTR: {
5972 struct kvm_xen_hvm_attr xha;
5973
5974 r = -EFAULT;
5975 if (copy_from_user(&xha, argp, sizeof(xha)))
5976 goto out;
5977 r = kvm_xen_hvm_set_attr(kvm, &xha);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005978 break;
5979 }
Paolo Bonzinib59b1532021-02-26 04:54:45 -05005980#endif
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005981 case KVM_SET_CLOCK: {
Vitaly Kuznetsov77fcbe82021-03-31 14:41:29 +02005982 struct kvm_arch *ka = &kvm->arch;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005983 struct kvm_clock_data user_ns;
5984 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005985
5986 r = -EFAULT;
5987 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5988 goto out;
5989
5990 r = -EINVAL;
5991 if (user_ns.flags)
5992 goto out;
5993
5994 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005995 /*
5996 * TODO: userspace has to take care of races with VCPU_RUN, so
5997 * kvm_gen_update_masterclock() can be cut down to locked
5998 * pvclock_update_vm_gtod_copy().
5999 */
6000 kvm_gen_update_masterclock(kvm);
Vitaly Kuznetsov77fcbe82021-03-31 14:41:29 +02006001
6002 /*
6003 * This pairs with kvm_guest_time_update(): when masterclock is
6004 * in use, we use master_kernel_ns + kvmclock_offset to set
6005 * unsigned 'system_time' so if we use get_kvmclock_ns() (which
6006 * is slightly ahead) here we risk going negative on unsigned
6007 * 'system_time' when 'user_ns.clock' is very small.
6008 */
6009 spin_lock_irq(&ka->pvclock_gtod_sync_lock);
6010 if (kvm->arch.use_master_clock)
6011 now_ns = ka->master_kernel_ns;
6012 else
6013 now_ns = get_kvmclock_base_ns();
6014 ka->kvmclock_offset = user_ns.clock - now_ns;
6015 spin_unlock_irq(&ka->pvclock_gtod_sync_lock);
6016
Radim Krčmář0bc48be2017-05-16 22:50:00 +02006017 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006018 break;
6019 }
6020 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006021 struct kvm_clock_data user_ns;
6022 u64 now_ns;
6023
Marcelo Tosattie891a322017-04-17 12:51:37 -03006024 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02006025 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01006026 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04006027 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006028
6029 r = -EFAULT;
6030 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
6031 goto out;
6032 r = 0;
6033 break;
6034 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06006035 case KVM_MEMORY_ENCRYPT_OP: {
6036 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006037 if (kvm_x86_ops.mem_enc_op)
Jason Baronb36464772021-01-14 22:27:56 -05006038 r = static_call(kvm_x86_mem_enc_op)(kvm, argp);
Brijesh Singh5acc5c02017-12-04 10:57:26 -06006039 break;
6040 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06006041 case KVM_MEMORY_ENCRYPT_REG_REGION: {
6042 struct kvm_enc_region region;
6043
6044 r = -EFAULT;
6045 if (copy_from_user(&region, argp, sizeof(region)))
6046 goto out;
6047
6048 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006049 if (kvm_x86_ops.mem_enc_reg_region)
Jason Baronb36464772021-01-14 22:27:56 -05006050 r = static_call(kvm_x86_mem_enc_reg_region)(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06006051 break;
6052 }
6053 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
6054 struct kvm_enc_region region;
6055
6056 r = -EFAULT;
6057 if (copy_from_user(&region, argp, sizeof(region)))
6058 goto out;
6059
6060 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006061 if (kvm_x86_ops.mem_enc_unreg_region)
Jason Baronb36464772021-01-14 22:27:56 -05006062 r = static_call(kvm_x86_mem_enc_unreg_region)(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06006063 break;
6064 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03006065 case KVM_HYPERV_EVENTFD: {
6066 struct kvm_hyperv_eventfd hvevfd;
6067
6068 r = -EFAULT;
6069 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
6070 goto out;
6071 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
6072 break;
6073 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07006074 case KVM_SET_PMU_EVENT_FILTER:
6075 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
6076 break;
Alexander Graf1a1552542020-09-25 16:34:21 +02006077 case KVM_X86_SET_MSR_FILTER:
6078 r = kvm_vm_ioctl_set_msr_filter(kvm, argp);
6079 break;
Carsten Otte043405e2007-10-10 17:16:19 +02006080 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02006081 r = -ENOTTY;
Carsten Otte043405e2007-10-10 17:16:19 +02006082 }
6083out:
6084 return r;
6085}
6086
6087static void kvm_init_msr_list(void)
6088{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006089 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02006090 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006091 unsigned i;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006092
Jim Mattsone2ada662019-08-21 11:20:04 -07006093 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006094 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006095
6096 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07006097
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08006098 num_msrs_to_save = 0;
6099 num_emulated_msrs = 0;
6100 num_msr_based_features = 0;
6101
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006102 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
6103 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006104 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006105
6106 /*
6107 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01006108 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006109 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006110 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006111 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03006112 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006113 continue;
6114 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01006115 case MSR_TSC_AUX:
Sean Christopherson36fa06f2021-05-04 10:17:26 -07006116 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP) &&
6117 !kvm_cpu_cap_has(X86_FEATURE_RDPID))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01006118 continue;
6119 break;
Maxim Levitskyf4cfcd22020-05-23 19:14:55 +03006120 case MSR_IA32_UMWAIT_CONTROL:
6121 if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG))
6122 continue;
6123 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08006124 case MSR_IA32_RTIT_CTL:
6125 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006126 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08006127 continue;
6128 break;
6129 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006130 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08006131 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
6132 continue;
6133 break;
6134 case MSR_IA32_RTIT_OUTPUT_BASE:
6135 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006136 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08006137 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
6138 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
6139 continue;
6140 break;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08006141 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christopherson7b874c22020-03-02 15:56:59 -08006142 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006143 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08006144 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
6145 continue;
6146 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02006147 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006148 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006149 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
6150 continue;
6151 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02006152 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006153 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02006154 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
6155 continue;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08006156 break;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006157 default:
6158 break;
6159 }
6160
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006161 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Ottebbd9b642007-10-30 18:44:21 +01006162 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02006163
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006164 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
Jason Baronb36464772021-01-14 22:27:56 -05006165 if (!static_call(kvm_x86_has_emulated_msr)(NULL, emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02006166 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02006167
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006168 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02006169 }
Tom Lendacky801e4592018-02-21 13:39:51 -06006170
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006171 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06006172 struct kvm_msr_entry msr;
6173
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006174 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08006175 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06006176 continue;
6177
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08006178 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06006179 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01006180}
6181
6182static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
6183 const void *v)
6184{
Avi Kivity70252a12010-01-19 12:51:22 +02006185 int handled = 0;
6186 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006187
Avi Kivity70252a12010-01-19 12:51:22 +02006188 do {
6189 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01006190 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006191 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
6192 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02006193 break;
6194 handled += n;
6195 addr += n;
6196 len -= n;
6197 v += n;
6198 } while (len);
6199
6200 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006201}
6202
6203static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
6204{
Avi Kivity70252a12010-01-19 12:51:22 +02006205 int handled = 0;
6206 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006207
Avi Kivity70252a12010-01-19 12:51:22 +02006208 do {
6209 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01006210 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006211 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
6212 addr, n, v))
6213 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02006214 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006215 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02006216 handled += n;
6217 addr += n;
6218 len -= n;
6219 v += n;
6220 } while (len);
6221
6222 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006223}
6224
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006225static void kvm_set_segment(struct kvm_vcpu *vcpu,
6226 struct kvm_segment *var, int seg)
6227{
Jason Baronb36464772021-01-14 22:27:56 -05006228 static_call(kvm_x86_set_segment)(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006229}
6230
6231void kvm_get_segment(struct kvm_vcpu *vcpu,
6232 struct kvm_segment *var, int seg)
6233{
Jason Baronb36464772021-01-14 22:27:56 -05006234 static_call(kvm_x86_get_segment)(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006235}
6236
Paolo Bonzini54987b72014-09-02 13:23:06 +02006237gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
6238 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02006239{
6240 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02006241
6242 BUG_ON(!mmu_is_nested(vcpu));
6243
6244 /* NPT walks are always user-walks */
6245 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006246 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02006247
6248 return t_gpa;
6249}
6250
Avi Kivityab9ae312010-11-22 17:53:26 +02006251gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
6252 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006253{
Jason Baronb36464772021-01-14 22:27:56 -05006254 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02006255 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006256}
Sean Christopherson54f958c2021-04-12 16:21:33 +12006257EXPORT_SYMBOL_GPL(kvm_mmu_gva_to_gpa_read);
Gleb Natapov1871c602010-02-10 14:21:32 +02006258
Avi Kivityab9ae312010-11-22 17:53:26 +02006259 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
6260 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006261{
Jason Baronb36464772021-01-14 22:27:56 -05006262 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02006263 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02006264 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006265}
6266
Avi Kivityab9ae312010-11-22 17:53:26 +02006267gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
6268 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006269{
Jason Baronb36464772021-01-14 22:27:56 -05006270 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02006271 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02006272 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006273}
Sean Christopherson54f958c2021-04-12 16:21:33 +12006274EXPORT_SYMBOL_GPL(kvm_mmu_gva_to_gpa_write);
Gleb Natapov1871c602010-02-10 14:21:32 +02006275
6276/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02006277gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
6278 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006279{
Avi Kivityab9ae312010-11-22 17:53:26 +02006280 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006281}
6282
6283static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
6284 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006285 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006286{
6287 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006288 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006289
6290 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02006291 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02006292 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006293 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02006294 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006295 int ret;
6296
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006297 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02006298 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006299 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
6300 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006301 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006302 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006303 goto out;
6304 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01006305
Izik Eidus77c20022008-12-29 01:42:19 +02006306 bytes -= toread;
6307 data += toread;
6308 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006309 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006310out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006311 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006312}
Izik Eidus77c20022008-12-29 01:42:19 +02006313
Gleb Natapov1871c602010-02-10 14:21:32 +02006314/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03006315static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
6316 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006317 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02006318{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006319 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Jason Baronb36464772021-01-14 22:27:56 -05006320 u32 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02006321 unsigned offset;
6322 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006323
Paolo Bonzini44583cb2014-05-13 14:02:13 +02006324 /* Inline kvm_read_guest_virt_helper for speed. */
6325 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
6326 exception);
6327 if (unlikely(gpa == UNMAPPED_GVA))
6328 return X86EMUL_PROPAGATE_FAULT;
6329
6330 offset = addr & (PAGE_SIZE-1);
6331 if (WARN_ON(offset + bytes > PAGE_SIZE))
6332 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006333 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
6334 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02006335 if (unlikely(ret < 0))
6336 return X86EMUL_IO_NEEDED;
6337
6338 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02006339}
6340
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006341int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03006342 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006343 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;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006346
Paolo Bonzini353c0952019-01-29 18:41:16 +01006347 /*
6348 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
6349 * is returned, but our callers are not ready for that and they blindly
6350 * call kvm_inject_page_fault. Ensure that they at least do not leak
6351 * uninitialized kernel stack memory into cr2 and error code.
6352 */
6353 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02006354 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006355 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006356}
Nadav Har'El064aea72011-05-25 23:04:56 +03006357EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02006358
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006359static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
6360 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006361 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02006362{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006363 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006364 u32 access = 0;
6365
Jason Baronb36464772021-01-14 22:27:56 -05006366 if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006367 access |= PFERR_USER_MASK;
6368
6369 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02006370}
6371
Radim Krčmář7a036a62015-10-30 16:36:24 +01006372static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
6373 unsigned long addr, void *val, unsigned int bytes)
6374{
6375 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6376 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
6377
6378 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
6379}
6380
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006381static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
6382 struct kvm_vcpu *vcpu, u32 access,
6383 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02006384{
6385 void *data = val;
6386 int r = X86EMUL_CONTINUE;
6387
6388 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02006389 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006390 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02006391 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02006392 unsigned offset = addr & (PAGE_SIZE-1);
6393 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
6394 int ret;
6395
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006396 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02006397 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006398 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02006399 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006400 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02006401 goto out;
6402 }
6403
6404 bytes -= towrite;
6405 data += towrite;
6406 addr += towrite;
6407 }
6408out:
6409 return r;
6410}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006411
6412static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006413 unsigned int bytes, struct x86_exception *exception,
6414 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006415{
6416 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006417 u32 access = PFERR_WRITE_MASK;
6418
Jason Baronb36464772021-01-14 22:27:56 -05006419 if (!system && static_call(kvm_x86_get_cpl)(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006420 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006421
6422 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006423 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006424}
6425
6426int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
6427 unsigned int bytes, struct x86_exception *exception)
6428{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006429 /* kvm_write_guest_virt_system can pull in tons of pages. */
6430 vcpu->arch.l1tf_flush_l1d = true;
6431
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006432 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
6433 PFERR_WRITE_MASK, exception);
6434}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006435EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02006436
Wanpeng Li082d06e2018-04-03 16:28:48 -07006437int handle_ud(struct kvm_vcpu *vcpu)
6438{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09006439 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006440 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006441 char sig[5]; /* ud2; .ascii "kvm" */
6442 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07006443
Jason Baronb36464772021-01-14 22:27:56 -05006444 if (unlikely(!static_call(kvm_x86_can_emulate_instruction)(vcpu, NULL, 0)))
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07006445 return 1;
6446
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006447 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006448 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
6449 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09006450 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006451 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07006452 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006453 }
6454
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006455 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07006456}
6457EXPORT_SYMBOL_GPL(handle_ud);
6458
Tom Lendacky0f89b202016-12-14 14:59:23 -05006459static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
6460 gpa_t gpa, bool write)
6461{
6462 /* For APIC access vmexit */
6463 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
6464 return 1;
6465
6466 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
6467 trace_vcpu_match_mmio(gva, gpa, write, true);
6468 return 1;
6469 }
6470
6471 return 0;
6472}
6473
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006474static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
6475 gpa_t *gpa, struct x86_exception *exception,
6476 bool write)
6477{
Jason Baronb36464772021-01-14 22:27:56 -05006478 u32 access = ((static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0)
Avi Kivity97d64b72012-09-12 14:52:00 +03006479 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006480
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08006481 /*
6482 * currently PKRU is only applied to ept enabled guest so
6483 * there is no pkey in EPT page table for L1 guest or EPT
6484 * shadow page table for L2 guest.
6485 */
Avi Kivity97d64b72012-09-12 14:52:00 +03006486 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08006487 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07006488 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08006489 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
6490 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08006491 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08006492 return 1;
6493 }
6494
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006495 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
6496
6497 if (*gpa == UNMAPPED_GVA)
6498 return -1;
6499
Tom Lendacky0f89b202016-12-14 14:59:23 -05006500 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006501}
6502
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006503int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006504 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02006505{
6506 int ret;
6507
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006508 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02006509 if (ret < 0)
6510 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08006511 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02006512 return 1;
6513}
6514
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006515struct read_write_emulator_ops {
6516 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
6517 int bytes);
6518 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
6519 void *val, int bytes);
6520 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
6521 int bytes, void *val);
6522 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
6523 void *val, int bytes);
6524 bool write;
6525};
6526
6527static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
6528{
6529 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006530 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006531 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006532 vcpu->mmio_read_completed = 0;
6533 return 1;
6534 }
6535
6536 return 0;
6537}
6538
6539static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
6540 void *val, int bytes)
6541{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006542 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006543}
6544
6545static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
6546 void *val, int bytes)
6547{
6548 return emulator_write_phys(vcpu, gpa, val, bytes);
6549}
6550
6551static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
6552{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006553 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006554 return vcpu_mmio_write(vcpu, gpa, bytes, val);
6555}
6556
6557static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
6558 void *val, int bytes)
6559{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006560 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006561 return X86EMUL_IO_NEEDED;
6562}
6563
6564static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
6565 void *val, int bytes)
6566{
Avi Kivityf78146b2012-04-18 19:22:47 +03006567 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
6568
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006569 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006570 return X86EMUL_CONTINUE;
6571}
6572
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006573static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006574 .read_write_prepare = read_prepare,
6575 .read_write_emulate = read_emulate,
6576 .read_write_mmio = vcpu_mmio_read,
6577 .read_write_exit_mmio = read_exit_mmio,
6578};
6579
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006580static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006581 .read_write_emulate = write_emulate,
6582 .read_write_mmio = write_mmio,
6583 .read_write_exit_mmio = write_exit_mmio,
6584 .write = true,
6585};
6586
Xiao Guangrong22388a32011-07-13 14:32:31 +08006587static int emulator_read_write_onepage(unsigned long addr, void *val,
6588 unsigned int bytes,
6589 struct x86_exception *exception,
6590 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006591 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006592{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006593 gpa_t gpa;
6594 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08006595 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03006596 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006597 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05006598
6599 /*
6600 * If the exit was due to a NPF we may already have a GPA.
6601 * If the GPA is present, use it to avoid the GVA to GPA table walk.
6602 * Note, this cannot be used on string operations since string
6603 * operation using rep will only have the initial GPA from the NPF
6604 * occurred.
6605 */
Sean Christopherson744e6992020-02-18 15:03:09 -08006606 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
6607 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
6608 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02006609 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
6610 } else {
6611 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
6612 if (ret < 0)
6613 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05006614 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08006615
Brijesh Singh618232e2017-08-17 18:36:57 +02006616 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01006617 return X86EMUL_CONTINUE;
6618
Carsten Ottebbd9b642007-10-30 18:44:21 +01006619 /*
6620 * Is this MMIO handled locally?
6621 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08006622 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02006623 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006624 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006625
Avi Kivity70252a12010-01-19 12:51:22 +02006626 gpa += handled;
6627 bytes -= handled;
6628 val += handled;
6629
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006630 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
6631 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
6632 frag->gpa = gpa;
6633 frag->data = val;
6634 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03006635 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006636}
6637
Xiubo Li52eb5a62015-03-13 17:39:45 +08006638static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
6639 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08006640 void *val, unsigned int bytes,
6641 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006642 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006643{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006644 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03006645 gpa_t gpa;
6646 int rc;
6647
6648 if (ops->read_write_prepare &&
6649 ops->read_write_prepare(vcpu, val, bytes))
6650 return X86EMUL_CONTINUE;
6651
6652 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006653
Carsten Ottebbd9b642007-10-30 18:44:21 +01006654 /* Crossing a page boundary? */
6655 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03006656 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006657
6658 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08006659 rc = emulator_read_write_onepage(addr, val, now, exception,
6660 vcpu, ops);
6661
Carsten Ottebbd9b642007-10-30 18:44:21 +01006662 if (rc != X86EMUL_CONTINUE)
6663 return rc;
6664 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02006665 if (ctxt->mode != X86EMUL_MODE_PROT64)
6666 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006667 val += now;
6668 bytes -= now;
6669 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08006670
Avi Kivityf78146b2012-04-18 19:22:47 +03006671 rc = emulator_read_write_onepage(addr, val, bytes, exception,
6672 vcpu, ops);
6673 if (rc != X86EMUL_CONTINUE)
6674 return rc;
6675
6676 if (!vcpu->mmio_nr_fragments)
6677 return rc;
6678
6679 gpa = vcpu->mmio_fragments[0].gpa;
6680
6681 vcpu->mmio_needed = 1;
6682 vcpu->mmio_cur_fragment = 0;
6683
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006684 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03006685 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
6686 vcpu->run->exit_reason = KVM_EXIT_MMIO;
6687 vcpu->run->mmio.phys_addr = gpa;
6688
6689 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08006690}
6691
6692static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
6693 unsigned long addr,
6694 void *val,
6695 unsigned int bytes,
6696 struct x86_exception *exception)
6697{
6698 return emulator_read_write(ctxt, addr, val, bytes,
6699 exception, &read_emultor);
6700}
6701
Xiubo Li52eb5a62015-03-13 17:39:45 +08006702static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08006703 unsigned long addr,
6704 const void *val,
6705 unsigned int bytes,
6706 struct x86_exception *exception)
6707{
6708 return emulator_read_write(ctxt, addr, (void *)val, bytes,
6709 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006710}
Carsten Ottebbd9b642007-10-30 18:44:21 +01006711
Avi Kivitydaea3e72010-03-15 13:59:54 +02006712#define CMPXCHG_TYPE(t, ptr, old, new) \
6713 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
6714
6715#ifdef CONFIG_X86_64
6716# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
6717#else
6718# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01006719 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02006720#endif
6721
Avi Kivity0f65dd72011-04-20 13:37:53 +03006722static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
6723 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006724 const void *old,
6725 const void *new,
6726 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03006727 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006728{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006729 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006730 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02006731 u64 page_line_mask;
Avi Kivitydaea3e72010-03-15 13:59:54 +02006732 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02006733 char *kaddr;
6734 bool exchanged;
6735
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006736 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02006737 if (bytes > 8 || (bytes & (bytes - 1)))
6738 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006739
Avi Kivitydaea3e72010-03-15 13:59:54 +02006740 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006741
Avi Kivitydaea3e72010-03-15 13:59:54 +02006742 if (gpa == UNMAPPED_GVA ||
6743 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
6744 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006745
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02006746 /*
6747 * Emulate the atomic as a straight write to avoid #AC if SLD is
6748 * enabled in the host and the access splits a cache line.
6749 */
6750 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
6751 page_line_mask = ~(cache_line_size() - 1);
6752 else
6753 page_line_mask = PAGE_MASK;
6754
6755 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask))
Avi Kivitydaea3e72010-03-15 13:59:54 +02006756 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006757
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006758 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08006759 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02006760
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006761 kaddr = map.hva + offset_in_page(gpa);
6762
Avi Kivitydaea3e72010-03-15 13:59:54 +02006763 switch (bytes) {
6764 case 1:
6765 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
6766 break;
6767 case 2:
6768 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
6769 break;
6770 case 4:
6771 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
6772 break;
6773 case 8:
6774 exchanged = CMPXCHG64(kaddr, old, new);
6775 break;
6776 default:
6777 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006778 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006779
6780 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02006781
6782 if (!exchanged)
6783 return X86EMUL_CMPXCHG_FAILED;
6784
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08006785 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03006786
6787 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02006788
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006789emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02006790 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006791
Avi Kivity0f65dd72011-04-20 13:37:53 +03006792 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006793}
6794
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006795static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
6796{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006797 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006798
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006799 for (i = 0; i < vcpu->arch.pio.count; i++) {
6800 if (vcpu->arch.pio.in)
6801 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
6802 vcpu->arch.pio.size, pd);
6803 else
6804 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
6805 vcpu->arch.pio.port, vcpu->arch.pio.size,
6806 pd);
6807 if (r)
6808 break;
6809 pd += vcpu->arch.pio.size;
6810 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006811 return r;
6812}
6813
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006814static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
6815 unsigned short port, void *val,
6816 unsigned int count, bool in)
6817{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006818 vcpu->arch.pio.port = port;
6819 vcpu->arch.pio.in = in;
6820 vcpu->arch.pio.count = count;
6821 vcpu->arch.pio.size = size;
6822
6823 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
6824 vcpu->arch.pio.count = 0;
6825 return 1;
6826 }
6827
6828 vcpu->run->exit_reason = KVM_EXIT_IO;
6829 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
6830 vcpu->run->io.size = size;
6831 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
6832 vcpu->run->io.count = count;
6833 vcpu->run->io.port = port;
6834
6835 return 0;
6836}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006837
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006838static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6839 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006840{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006841 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03006842
Gleb Natapov79729952010-03-18 15:20:24 +02006843 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006844 goto data_avail;
6845
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006846 memset(vcpu->arch.pio_data, 0, size * count);
6847
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006848 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
6849 if (ret) {
6850data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006851 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006852 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02006853 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006854 return 1;
6855 }
6856
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006857 return 0;
6858}
6859
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006860static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
6861 int size, unsigned short port, void *val,
6862 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006863{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006864 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006865
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006866}
6867
6868static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
6869 unsigned short port, const void *val,
6870 unsigned int count)
6871{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006872 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006873 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006874 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006875}
6876
Carsten Ottebbd9b642007-10-30 18:44:21 +01006877static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
6878 int size, unsigned short port,
6879 const void *val, unsigned int count)
6880{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006881 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006882}
6883
6884static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
6885{
Jason Baronb36464772021-01-14 22:27:56 -05006886 return static_call(kvm_x86_get_segment_base)(vcpu, seg);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006887}
6888
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006889static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006890{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006891 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006892}
6893
Jiang Biaoae6a2372016-11-07 08:54:51 +08006894static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006895{
6896 if (!need_emulate_wbinvd(vcpu))
6897 return X86EMUL_CONTINUE;
6898
Jason Baronb36464772021-01-14 22:27:56 -05006899 if (static_call(kvm_x86_has_wbinvd_exit)()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01006900 int cpu = get_cpu();
6901
6902 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Wanpeng Lic2162e12021-03-12 10:45:51 +08006903 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006904 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006905 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006906 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006907 } else
6908 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006909 return X86EMUL_CONTINUE;
6910}
Joel Schopp5cb56052015-03-02 13:43:31 -06006911
6912int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6913{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006914 kvm_emulate_wbinvd_noskip(vcpu);
6915 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006916}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006917EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6918
Joel Schopp5cb56052015-03-02 13:43:31 -06006919
6920
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006921static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6922{
Joel Schopp5cb56052015-03-02 13:43:31 -06006923 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006924}
6925
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05006926static void emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6927 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006928{
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05006929 kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006930}
6931
Xiubo Li52eb5a62015-03-13 17:39:45 +08006932static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6933 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006934{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006935
Paolo Bonzini996ff542020-12-14 07:49:54 -05006936 return kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006937}
6938
Gleb Natapov52a46612010-03-18 15:20:03 +02006939static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6940{
6941 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6942}
6943
Avi Kivity717746e2011-04-20 13:37:53 +03006944static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006945{
Avi Kivity717746e2011-04-20 13:37:53 +03006946 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006947 unsigned long value;
6948
6949 switch (cr) {
6950 case 0:
6951 value = kvm_read_cr0(vcpu);
6952 break;
6953 case 2:
6954 value = vcpu->arch.cr2;
6955 break;
6956 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006957 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006958 break;
6959 case 4:
6960 value = kvm_read_cr4(vcpu);
6961 break;
6962 case 8:
6963 value = kvm_get_cr8(vcpu);
6964 break;
6965 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006966 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006967 return 0;
6968 }
6969
6970 return value;
6971}
6972
Avi Kivity717746e2011-04-20 13:37:53 +03006973static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006974{
Avi Kivity717746e2011-04-20 13:37:53 +03006975 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006976 int res = 0;
6977
Gleb Natapov52a46612010-03-18 15:20:03 +02006978 switch (cr) {
6979 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006980 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006981 break;
6982 case 2:
6983 vcpu->arch.cr2 = val;
6984 break;
6985 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006986 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006987 break;
6988 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006989 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006990 break;
6991 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006992 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006993 break;
6994 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006995 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006996 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006997 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006998
6999 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02007000}
7001
Avi Kivity717746e2011-04-20 13:37:53 +03007002static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02007003{
Jason Baronb36464772021-01-14 22:27:56 -05007004 return static_call(kvm_x86_get_cpl)(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02007005}
7006
Avi Kivity4bff1e862011-04-20 13:37:53 +03007007static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007008{
Jason Baronb36464772021-01-14 22:27:56 -05007009 static_call(kvm_x86_get_gdt)(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007010}
7011
Avi Kivity4bff1e862011-04-20 13:37:53 +03007012static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03007013{
Jason Baronb36464772021-01-14 22:27:56 -05007014 static_call(kvm_x86_get_idt)(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03007015}
7016
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03007017static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
7018{
Jason Baronb36464772021-01-14 22:27:56 -05007019 static_call(kvm_x86_set_gdt)(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03007020}
7021
7022static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
7023{
Jason Baronb36464772021-01-14 22:27:56 -05007024 static_call(kvm_x86_set_idt)(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03007025}
7026
Avi Kivity4bff1e862011-04-20 13:37:53 +03007027static unsigned long emulator_get_cached_segment_base(
7028 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03007029{
Avi Kivity4bff1e862011-04-20 13:37:53 +03007030 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03007031}
7032
Avi Kivity1aa36612011-04-27 13:20:30 +03007033static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
7034 struct desc_struct *desc, u32 *base3,
7035 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007036{
7037 struct kvm_segment var;
7038
Avi Kivity4bff1e862011-04-20 13:37:53 +03007039 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03007040 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007041
Gleb Natapov378a8b02013-01-21 15:36:48 +02007042 if (var.unusable) {
7043 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02007044 if (base3)
7045 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007046 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02007047 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007048
7049 if (var.g)
7050 var.limit >>= 12;
7051 set_desc_limit(desc, var.limit);
7052 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02007053#ifdef CONFIG_X86_64
7054 if (base3)
7055 *base3 = var.base >> 32;
7056#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007057 desc->type = var.type;
7058 desc->s = var.s;
7059 desc->dpl = var.dpl;
7060 desc->p = var.present;
7061 desc->avl = var.avl;
7062 desc->l = var.l;
7063 desc->d = var.db;
7064 desc->g = var.g;
7065
7066 return true;
7067}
7068
Avi Kivity1aa36612011-04-27 13:20:30 +03007069static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
7070 struct desc_struct *desc, u32 base3,
7071 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007072{
Avi Kivity4bff1e862011-04-20 13:37:53 +03007073 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007074 struct kvm_segment var;
7075
Avi Kivity1aa36612011-04-27 13:20:30 +03007076 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007077 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02007078#ifdef CONFIG_X86_64
7079 var.base |= ((u64)base3) << 32;
7080#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007081 var.limit = get_desc_limit(desc);
7082 if (desc->g)
7083 var.limit = (var.limit << 12) | 0xfff;
7084 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007085 var.dpl = desc->dpl;
7086 var.db = desc->d;
7087 var.s = desc->s;
7088 var.l = desc->l;
7089 var.g = desc->g;
7090 var.avl = desc->avl;
7091 var.present = desc->p;
7092 var.unusable = !var.present;
7093 var.padding = 0;
7094
7095 kvm_set_segment(vcpu, &var, seg);
7096 return;
7097}
7098
Avi Kivity717746e2011-04-20 13:37:53 +03007099static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
7100 u32 msr_index, u64 *pdata)
7101{
Alexander Graf1ae09952020-09-25 16:34:16 +02007102 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7103 int r;
7104
7105 r = kvm_get_msr(vcpu, msr_index, pdata);
7106
7107 if (r && kvm_get_msr_user_space(vcpu, msr_index, r)) {
7108 /* Bounce to user space */
7109 return X86EMUL_IO_NEEDED;
7110 }
7111
7112 return r;
Avi Kivity717746e2011-04-20 13:37:53 +03007113}
7114
7115static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
7116 u32 msr_index, u64 data)
7117{
Alexander Graf1ae09952020-09-25 16:34:16 +02007118 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7119 int r;
7120
7121 r = kvm_set_msr(vcpu, msr_index, data);
7122
7123 if (r && kvm_set_msr_user_space(vcpu, msr_index, data, r)) {
7124 /* Bounce to user space */
7125 return X86EMUL_IO_NEEDED;
7126 }
7127
7128 return r;
Avi Kivity717746e2011-04-20 13:37:53 +03007129}
7130
Paolo Bonzini64d60672015-05-07 11:36:11 +02007131static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
7132{
7133 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7134
7135 return vcpu->arch.smbase;
7136}
7137
7138static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
7139{
7140 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7141
7142 vcpu->arch.smbase = smbase;
7143}
7144
Nadav Amit67f4d422014-06-02 18:34:09 +03007145static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
7146 u32 pmc)
7147{
Like Xu98ff80f2019-10-27 18:52:40 +08007148 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03007149}
7150
Avi Kivity222d21a2011-11-10 14:57:30 +02007151static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
7152 u32 pmc, u64 *pdata)
7153{
Wei Huangc6702c92015-06-19 13:44:45 +02007154 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02007155}
7156
Avi Kivity6c3287f2011-04-20 15:43:05 +03007157static void emulator_halt(struct x86_emulate_ctxt *ctxt)
7158{
7159 emul_to_vcpu(ctxt)->arch.halt_request = 1;
7160}
7161
Avi Kivity29535382011-04-20 13:37:53 +03007162static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007163 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02007164 enum x86_intercept_stage stage)
7165{
Jason Baronb36464772021-01-14 22:27:56 -05007166 return static_call(kvm_x86_check_intercept)(emul_to_vcpu(ctxt), info, stage,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007167 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02007168}
7169
Yu Zhange911eb32017-08-24 20:27:52 +08007170static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08007171 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
7172 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01007173{
Sean Christophersonf91af512020-03-04 17:34:37 -08007174 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01007175}
7176
Sean Christopherson5ae78e92019-12-17 13:32:38 -08007177static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
7178{
7179 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
7180}
7181
7182static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
7183{
7184 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
7185}
7186
7187static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
7188{
7189 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
7190}
7191
Avi Kivitydd856ef2012-08-27 23:46:17 +03007192static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
7193{
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07007194 return kvm_register_read_raw(emul_to_vcpu(ctxt), reg);
Avi Kivitydd856ef2012-08-27 23:46:17 +03007195}
7196
7197static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
7198{
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07007199 kvm_register_write_raw(emul_to_vcpu(ctxt), reg, val);
Avi Kivitydd856ef2012-08-27 23:46:17 +03007200}
7201
Nadav Amit801806d2015-01-26 09:32:23 +02007202static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
7203{
Jason Baronb36464772021-01-14 22:27:56 -05007204 static_call(kvm_x86_set_nmi_mask)(emul_to_vcpu(ctxt), masked);
Nadav Amit801806d2015-01-26 09:32:23 +02007205}
7206
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007207static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
7208{
7209 return emul_to_vcpu(ctxt)->arch.hflags;
7210}
7211
Sean Christophersonedce4652021-06-09 11:56:13 -07007212static void emulator_exiting_smm(struct x86_emulate_ctxt *ctxt)
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007213{
Sean Christopherson78fcb2c2021-06-09 11:56:11 -07007214 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
7215
Sean Christophersondc872752021-06-09 11:56:15 -07007216 kvm_smm_changed(vcpu, false);
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007217}
7218
Sean Christophersoned193212019-04-02 08:03:09 -07007219static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
7220 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007221{
Jason Baronb36464772021-01-14 22:27:56 -05007222 return static_call(kvm_x86_pre_leave_smm)(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007223}
7224
Sean Christophersonc5833c72019-04-02 08:03:10 -07007225static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
7226{
Sean Christophersonfa75e082021-06-09 11:56:14 -07007227 kvm_mmu_reset_context(emul_to_vcpu(ctxt));
Sean Christophersonc5833c72019-04-02 08:03:10 -07007228}
7229
Sean Christopherson25b17222021-06-09 11:56:12 -07007230static void emulator_triple_fault(struct x86_emulate_ctxt *ctxt)
7231{
7232 kvm_make_request(KVM_REQ_TRIPLE_FAULT, emul_to_vcpu(ctxt));
7233}
7234
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02007235static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
7236{
7237 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
7238}
7239
Mathias Krause0225fb52012-08-30 01:30:16 +02007240static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03007241 .read_gpr = emulator_read_gpr,
7242 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007243 .read_std = emulator_read_std,
7244 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01007245 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02007246 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01007247 .read_emulated = emulator_read_emulated,
7248 .write_emulated = emulator_write_emulated,
7249 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03007250 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007251 .pio_in_emulated = emulator_pio_in_emulated,
7252 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03007253 .get_segment = emulator_get_segment,
7254 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03007255 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02007256 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03007257 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03007258 .set_gdt = emulator_set_gdt,
7259 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02007260 .get_cr = emulator_get_cr,
7261 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02007262 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03007263 .get_dr = emulator_get_dr,
7264 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02007265 .get_smbase = emulator_get_smbase,
7266 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03007267 .set_msr = emulator_set_msr,
7268 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03007269 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02007270 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03007271 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03007272 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03007273 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02007274 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01007275 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08007276 .guest_has_long_mode = emulator_guest_has_long_mode,
7277 .guest_has_movbe = emulator_guest_has_movbe,
7278 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02007279 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02007280 .get_hflags = emulator_get_hflags,
Sean Christophersonedce4652021-06-09 11:56:13 -07007281 .exiting_smm = emulator_exiting_smm,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007282 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07007283 .post_leave_smm = emulator_post_leave_smm,
Sean Christopherson25b17222021-06-09 11:56:12 -07007284 .triple_fault = emulator_triple_fault,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02007285 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01007286};
7287
Gleb Natapov95cb2292010-04-28 19:15:43 +03007288static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
7289{
Jason Baronb36464772021-01-14 22:27:56 -05007290 u32 int_shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03007291 /*
7292 * an sti; sti; sequence only disable interrupts for the first
7293 * instruction. So, if the last instruction, be it emulated or
7294 * not, left the system with the INT_STI flag enabled, it
7295 * means that the last instruction is an sti. We should not
7296 * leave the flag on in this case. The same goes for mov ss
7297 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02007298 if (int_shadow & mask)
7299 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007300 if (unlikely(int_shadow || mask)) {
Jason Baronb36464772021-01-14 22:27:56 -05007301 static_call(kvm_x86_set_interrupt_shadow)(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007302 if (!mask)
7303 kvm_make_request(KVM_REQ_EVENT, vcpu);
7304 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03007305}
7306
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007307static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03007308{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007309 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02007310 if (ctxt->exception.vector == PF_VECTOR)
Sean Christopherson53b3d8e2020-03-20 14:28:01 -07007311 return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007312
7313 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02007314 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
7315 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03007316 else
Avi Kivityda9cb572010-11-22 17:53:21 +02007317 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007318 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03007319}
7320
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007321static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
7322{
7323 struct x86_emulate_ctxt *ctxt;
7324
7325 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
7326 if (!ctxt) {
7327 pr_err("kvm: failed to allocate vcpu's emulator\n");
7328 return NULL;
7329 }
7330
7331 ctxt->vcpu = vcpu;
7332 ctxt->ops = &emulate_ops;
7333 vcpu->arch.emulate_ctxt = ctxt;
7334
7335 return ctxt;
7336}
7337
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007338static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
7339{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007340 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007341 int cs_db, cs_l;
7342
Jason Baronb36464772021-01-14 22:27:56 -05007343 static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007344
Sean Christopherson744e6992020-02-18 15:03:09 -08007345 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007346 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007347 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
7348
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007349 ctxt->eip = kvm_rip_read(vcpu);
7350 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
7351 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03007352 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007353 cs_db ? X86EMUL_MODE_PROT32 :
7354 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02007355 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007356 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
7357 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09007358
Wanpeng Lida6393c2021-05-27 17:01:36 -07007359 ctxt->interruptibility = 0;
7360 ctxt->have_exception = false;
7361 ctxt->exception.vector = -1;
7362 ctxt->perm_ok = false;
7363
Avi Kivitydd856ef2012-08-27 23:46:17 +03007364 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007365 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007366}
7367
Sean Christopherson9497e1f2019-08-27 14:40:36 -07007368void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02007369{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007370 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02007371 int ret;
7372
7373 init_emulate_ctxt(vcpu);
7374
Avi Kivity9dac77f2011-06-01 15:34:25 +03007375 ctxt->op_bytes = 2;
7376 ctxt->ad_bytes = 2;
7377 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007378 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02007379
Sean Christopherson9497e1f2019-08-27 14:40:36 -07007380 if (ret != X86EMUL_CONTINUE) {
7381 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7382 } else {
7383 ctxt->eip = ctxt->_eip;
7384 kvm_rip_write(vcpu, ctxt->eip);
7385 kvm_set_rflags(vcpu, ctxt->eflags);
7386 }
Mohammed Gamal63995652010-09-19 14:34:06 +02007387}
7388EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
7389
Liran Alone2366172018-03-12 13:12:49 +02007390static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007391{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007392 ++vcpu->stat.insn_emulation_fail;
7393 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02007394
Sean Christopherson42cbf062019-08-27 14:40:31 -07007395 if (emulation_type & EMULTYPE_VMWARE_GP) {
7396 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007397 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07007398 }
Liran Alone2366172018-03-12 13:12:49 +02007399
Sean Christopherson738fece2019-08-27 14:40:34 -07007400 if (emulation_type & EMULTYPE_SKIP) {
7401 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7402 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7403 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007404 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07007405 }
7406
Sean Christopherson22da61c2019-08-27 14:40:28 -07007407 kvm_queue_exception(vcpu, UD_VECTOR);
7408
Jason Baronb36464772021-01-14 22:27:56 -05007409 if (!is_guest_mode(vcpu) && static_call(kvm_x86_get_cpl)(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01007410 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7411 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7412 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007413 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01007414 }
Liran Alone2366172018-03-12 13:12:49 +02007415
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007416 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007417}
7418
Sean Christopherson736c2912019-12-06 15:57:14 -08007419static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03007420 bool write_fault_to_shadow_pgtable,
7421 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03007422{
Sean Christopherson736c2912019-12-06 15:57:14 -08007423 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08007424 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03007425
Sean Christopherson92daa482020-02-18 15:03:08 -08007426 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03007427 return false;
7428
Sean Christopherson92daa482020-02-18 15:03:08 -08007429 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
7430 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07007431 return false;
7432
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007433 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007434 /*
7435 * Write permission should be allowed since only
7436 * write access need to be emulated.
7437 */
Sean Christopherson736c2912019-12-06 15:57:14 -08007438 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03007439
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007440 /*
7441 * If the mapping is invalid in guest, let cpu retry
7442 * it to generate fault.
7443 */
7444 if (gpa == UNMAPPED_GVA)
7445 return true;
7446 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03007447
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08007448 /*
7449 * Do not retry the unhandleable instruction if it faults on the
7450 * readonly host memory, otherwise it will goto a infinite loop:
7451 * retry instruction -> write #PF -> emulation fail -> retry
7452 * instruction -> ...
7453 */
7454 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007455
7456 /*
7457 * If the instruction failed on the error pfn, it can not be fixed,
7458 * report the error to userspace.
7459 */
7460 if (is_error_noslot_pfn(pfn))
7461 return false;
7462
7463 kvm_release_pfn_clean(pfn);
7464
7465 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007466 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007467 unsigned int indirect_shadow_pages;
7468
Ben Gardon531810c2021-02-02 10:57:24 -08007469 write_lock(&vcpu->kvm->mmu_lock);
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007470 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
Ben Gardon531810c2021-02-02 10:57:24 -08007471 write_unlock(&vcpu->kvm->mmu_lock);
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007472
7473 if (indirect_shadow_pages)
7474 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
7475
Gleb Natapova6f177e2010-07-08 12:41:12 +03007476 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08007477 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03007478
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007479 /*
7480 * if emulation was due to access to shadowed page table
7481 * and it failed try to unshadow page and re-enter the
7482 * guest to let CPU execute the instruction.
7483 */
7484 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007485
7486 /*
7487 * If the access faults on its page table, it can not
7488 * be fixed by unprotecting shadow page and it should
7489 * be reported to userspace.
7490 */
7491 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03007492}
7493
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007494static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08007495 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007496{
7497 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08007498 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007499
7500 last_retry_eip = vcpu->arch.last_retry_eip;
7501 last_retry_addr = vcpu->arch.last_retry_addr;
7502
7503 /*
7504 * If the emulation is caused by #PF and it is non-page_table
7505 * writing instruction, it means the VM-EXIT is caused by shadow
7506 * page protected, we can zap the shadow page and retry this
7507 * instruction directly.
7508 *
7509 * Note: if the guest uses a non-page-table modifying instruction
7510 * on the PDE that points to the instruction, then we will unmap
7511 * the instruction and go to an infinite loop. So, we cache the
7512 * last retried eip and the last fault address, if we meet the eip
7513 * and the address again, we can break out of the potential infinite
7514 * loop.
7515 */
7516 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
7517
Sean Christopherson92daa482020-02-18 15:03:08 -08007518 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007519 return false;
7520
Sean Christopherson92daa482020-02-18 15:03:08 -08007521 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
7522 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07007523 return false;
7524
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007525 if (x86_page_table_writing_insn(ctxt))
7526 return false;
7527
Sean Christopherson736c2912019-12-06 15:57:14 -08007528 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007529 return false;
7530
7531 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08007532 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007533
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007534 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08007535 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007536
Xiao Guangrong22368022013-01-13 23:44:12 +08007537 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007538
7539 return true;
7540}
7541
Gleb Natapov716d51a2012-09-03 15:24:26 +03007542static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
7543static int complete_emulated_pio(struct kvm_vcpu *vcpu);
7544
Sean Christophersondc872752021-06-09 11:56:15 -07007545static void kvm_smm_changed(struct kvm_vcpu *vcpu, bool entering_smm)
Paolo Bonzinia5845392015-04-01 18:18:53 +02007546{
Sean Christopherson1270e642021-06-09 11:56:17 -07007547 trace_kvm_smm_transition(vcpu->vcpu_id, vcpu->arch.smbase, entering_smm);
Sean Christopherson0d7ee6f2021-06-09 11:56:16 -07007548
Sean Christophersondc872752021-06-09 11:56:15 -07007549 if (entering_smm) {
7550 vcpu->arch.hflags |= HF_SMM_MASK;
7551 } else {
7552 vcpu->arch.hflags &= ~(HF_SMM_MASK | HF_SMM_INSIDE_NMI_MASK);
7553
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007554 /* Process a latched INIT or SMI, if any. */
7555 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007556 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02007557
7558 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007559}
7560
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007561static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
7562 unsigned long *db)
7563{
7564 u32 dr6 = 0;
7565 int i;
7566 u32 enable, rwlen;
7567
7568 enable = dr7;
7569 rwlen = dr7 >> 16;
7570 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
7571 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
7572 dr6 |= (1 << i);
7573 return dr6;
7574}
7575
Sean Christopherson120c2c42019-08-27 14:40:29 -07007576static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007577{
7578 struct kvm_run *kvm_run = vcpu->run;
7579
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007580 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08007581 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW;
Peter Xud5d260c2020-05-05 16:49:59 -04007582 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007583 kvm_run->debug.arch.exception = DB_VECTOR;
7584 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007585 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007586 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07007587 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007588 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007589}
7590
Kyle Huey6affcbe2016-11-29 12:40:40 -08007591int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
7592{
Jason Baronb36464772021-01-14 22:27:56 -05007593 unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02007594 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007595
Jason Baronb36464772021-01-14 22:27:56 -05007596 r = static_call(kvm_x86_skip_emulated_instruction)(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007597 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02007598 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007599
7600 /*
7601 * rflags is the old, "raw" value of the flags. The new value has
7602 * not been saved yet.
7603 *
7604 * This is correct even for TF set by the guest, because "the
7605 * processor will not generate this exception after the instruction
7606 * that sets the TF flag".
7607 */
7608 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07007609 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007610 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007611}
7612EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
7613
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007614static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
7615{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007616 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
7617 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02007618 struct kvm_run *kvm_run = vcpu->run;
7619 unsigned long eip = kvm_get_linear_rip(vcpu);
7620 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007621 vcpu->arch.guest_debug_dr7,
7622 vcpu->arch.eff_db);
7623
7624 if (dr6 != 0) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08007625 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW;
Nadav Amit82b32772014-11-02 11:54:45 +02007626 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007627 kvm_run->debug.arch.exception = DB_VECTOR;
7628 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007629 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007630 return true;
7631 }
7632 }
7633
Nadav Amit4161a562014-07-17 01:19:31 +03007634 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
7635 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02007636 unsigned long eip = kvm_get_linear_rip(vcpu);
7637 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007638 vcpu->arch.dr7,
7639 vcpu->arch.db);
7640
7641 if (dr6 != 0) {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04007642 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007643 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007644 return true;
7645 }
7646 }
7647
7648 return false;
7649}
7650
Liran Alon04789b62018-03-12 13:12:50 +02007651static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
7652{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02007653 switch (ctxt->opcode_len) {
7654 case 1:
7655 switch (ctxt->b) {
7656 case 0xe4: /* IN */
7657 case 0xe5:
7658 case 0xec:
7659 case 0xed:
7660 case 0xe6: /* OUT */
7661 case 0xe7:
7662 case 0xee:
7663 case 0xef:
7664 case 0x6c: /* INS */
7665 case 0x6d:
7666 case 0x6e: /* OUTS */
7667 case 0x6f:
7668 return true;
7669 }
7670 break;
7671 case 2:
7672 switch (ctxt->b) {
7673 case 0x33: /* RDPMC */
7674 return true;
7675 }
7676 break;
Liran Alon04789b62018-03-12 13:12:50 +02007677 }
7678
7679 return false;
7680}
7681
Wei Huang4aa26912021-01-26 03:18:28 -05007682/*
7683 * Decode to be emulated instruction. Return EMULATION_OK if success.
7684 */
7685int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
7686 void *insn, int insn_len)
7687{
7688 int r = EMULATION_OK;
7689 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
7690
7691 init_emulate_ctxt(vcpu);
7692
7693 /*
7694 * We will reenter on the same instruction since we do not set
7695 * complete_userspace_io. This does not handle watchpoints yet,
7696 * those would be handled in the emulate_ops.
7697 */
7698 if (!(emulation_type & EMULTYPE_SKIP) &&
7699 kvm_vcpu_check_breakpoint(vcpu, &r))
7700 return r;
7701
Wanpeng Lib35491e2021-05-27 17:01:37 -07007702 r = x86_decode_insn(ctxt, insn, insn_len, emulation_type);
Wei Huang4aa26912021-01-26 03:18:28 -05007703
7704 trace_kvm_emulate_insn_start(vcpu);
7705 ++vcpu->stat.insn_emulation;
7706
7707 return r;
7708}
7709EXPORT_SYMBOL_GPL(x86_decode_emulated_instruction);
7710
Sean Christopherson736c2912019-12-06 15:57:14 -08007711int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
7712 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01007713{
Gleb Natapov95cb2292010-04-28 19:15:43 +03007714 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007715 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007716 bool writeback = true;
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007717 bool write_fault_to_spt;
7718
Jason Baronb36464772021-01-14 22:27:56 -05007719 if (unlikely(!static_call(kvm_x86_can_emulate_instruction)(vcpu, insn, insn_len)))
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007720 return 1;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007721
Paolo Bonzinic595cee2018-07-02 13:07:14 +02007722 vcpu->arch.l1tf_flush_l1d = true;
7723
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007724 /*
7725 * Clear write_fault_to_shadow_pgtable here to ensure it is
7726 * never reused.
7727 */
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007728 write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007729 vcpu->arch.write_fault_to_shadow_pgtable = false;
Gleb Natapov8d7d81022011-04-12 12:36:21 +03007730
Sheng Yang571008d2008-01-02 14:49:22 +08007731 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Wei Huang4aa26912021-01-26 03:18:28 -05007732 kvm_clear_exception_queue(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007733
Wei Huang4aa26912021-01-26 03:18:28 -05007734 r = x86_decode_emulated_instruction(vcpu, emulation_type,
7735 insn, insn_len);
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09007736 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07007737 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07007738 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
7739 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007740 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07007741 }
Sean Christopherson736c2912019-12-06 15:57:14 -08007742 if (reexecute_instruction(vcpu, cr2_or_gpa,
7743 write_fault_to_spt,
7744 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007745 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00007746 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00007747 /*
7748 * #UD should result in just EMULATION_FAILED, and trap-like
7749 * exception should not be encountered during decode.
7750 */
7751 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
7752 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00007753 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007754 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00007755 }
Liran Alone2366172018-03-12 13:12:49 +02007756 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007757 }
7758 }
7759
Sean Christopherson42cbf062019-08-27 14:40:31 -07007760 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
7761 !is_vmware_backdoor_opcode(ctxt)) {
7762 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007763 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07007764 }
Liran Alon04789b62018-03-12 13:12:50 +02007765
Sean Christopherson1957aa62019-08-27 14:40:39 -07007766 /*
7767 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
7768 * for kvm_skip_emulated_instruction(). The caller is responsible for
7769 * updating interruptibility state and injecting single-step #DBs.
7770 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03007771 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03007772 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03007773 if (ctxt->eflags & X86_EFLAGS_RF)
7774 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007775 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03007776 }
7777
Sean Christopherson736c2912019-12-06 15:57:14 -08007778 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007779 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007780
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007781 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03007782 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007783 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
7784 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03007785 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007786 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03007787
Gleb Natapov5cd21912010-03-18 15:20:26 +02007788restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08007789 if (emulation_type & EMULTYPE_PF) {
7790 /* Save the faulting GPA (cr2) in the address field */
7791 ctxt->exception.address = cr2_or_gpa;
7792
7793 /* With shadow page tables, cr2 contains a GVA or nGPA. */
7794 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08007795 ctxt->gpa_available = true;
7796 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08007797 }
7798 } else {
7799 /* Sanitize the address out of an abundance of paranoia. */
7800 ctxt->exception.address = 0;
7801 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05007802
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007803 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03007804
Joerg Roedel775fde82011-04-04 12:39:24 +02007805 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007806 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02007807
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007808 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08007809 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03007810 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007811 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03007812
Liran Alone2366172018-03-12 13:12:49 +02007813 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007814 }
7815
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007816 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007817 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007818 if (inject_emulated_exception(vcpu))
7819 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007820 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02007821 if (!vcpu->arch.pio.in) {
7822 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03007823 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02007824 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007825 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007826 vcpu->arch.complete_userspace_io = complete_emulated_pio;
7827 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007828 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007829 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07007830 ++vcpu->stat.mmio_exits;
7831
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007832 if (!vcpu->mmio_is_write)
7833 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007834 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007835 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007836 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03007837 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007838 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007839 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03007840
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007841 if (writeback) {
Jason Baronb36464772021-01-14 22:27:56 -05007842 unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007843 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007844 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02007845 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007846 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
7847 kvm_rip_write(vcpu, ctxt->eip);
Felipe Franciosi384dea12020-05-19 08:11:22 +00007848 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
Sean Christopherson120c2c42019-08-27 14:40:29 -07007849 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007850 if (kvm_x86_ops.update_emulated_instruction)
Jason Baronb36464772021-01-14 22:27:56 -05007851 static_call(kvm_x86_update_emulated_instruction)(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02007852 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007853 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007854
7855 /*
7856 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
7857 * do nothing, and it will be requested again as soon as
7858 * the shadow expires. But we still need to check here,
7859 * because POPF has no interrupt shadow.
7860 */
7861 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
7862 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007863 } else
7864 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03007865
Gleb Natapove85d28f2010-07-29 15:11:52 +03007866 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007867}
Sean Christophersonc60658d2018-08-23 13:56:53 -07007868
7869int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
7870{
7871 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
7872}
7873EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
7874
7875int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
7876 void *insn, int insn_len)
7877{
7878 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
7879}
7880EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007881
Sean Christopherson8764ed52019-04-29 07:04:15 -07007882static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
7883{
7884 vcpu->arch.pio.count = 0;
7885 return 1;
7886}
7887
Sean Christopherson45def772019-03-11 20:01:05 -07007888static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
7889{
7890 vcpu->arch.pio.count = 0;
7891
7892 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
7893 return 1;
7894
7895 return kvm_skip_emulated_instruction(vcpu);
7896}
7897
Sean Christophersondca7f122018-03-08 08:57:27 -08007898static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
7899 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01007900{
Sean Christophersonde3cd112019-04-30 10:36:17 -07007901 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007902 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
7903
Sean Christopherson8764ed52019-04-29 07:04:15 -07007904 if (ret)
7905 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07007906
Sean Christopherson8764ed52019-04-29 07:04:15 -07007907 /*
7908 * Workaround userspace that relies on old KVM behavior of %rip being
7909 * incremented prior to exiting to userspace to handle "OUT 0x7e".
7910 */
7911 if (port == 0x7e &&
7912 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
7913 vcpu->arch.complete_userspace_io =
7914 complete_fast_pio_out_port_0x7e;
7915 kvm_skip_emulated_instruction(vcpu);
7916 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07007917 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
7918 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
7919 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07007920 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01007921}
Carsten Ottede7d7892007-10-30 18:44:25 +01007922
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007923static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
7924{
7925 unsigned long val;
7926
7927 /* We should only ever be called with arch.pio.count equal to 1 */
7928 BUG_ON(vcpu->arch.pio.count != 1);
7929
Sean Christopherson45def772019-03-11 20:01:05 -07007930 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
7931 vcpu->arch.pio.count = 0;
7932 return 1;
7933 }
7934
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007935 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007936 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007937
7938 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007939 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007940 * the copy and tracing
7941 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007942 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07007943 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007944
Sean Christopherson45def772019-03-11 20:01:05 -07007945 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007946}
7947
Sean Christophersondca7f122018-03-08 08:57:27 -08007948static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
7949 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007950{
7951 unsigned long val;
7952 int ret;
7953
7954 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007955 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007956
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007957 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007958 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007959 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007960 return ret;
7961 }
7962
Sean Christopherson45def772019-03-11 20:01:05 -07007963 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007964 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7965
7966 return 0;
7967}
Sean Christophersondca7f122018-03-08 08:57:27 -08007968
7969int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7970{
Sean Christopherson45def772019-03-11 20:01:05 -07007971 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007972
Sean Christophersondca7f122018-03-08 08:57:27 -08007973 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007974 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007975 else
Sean Christopherson45def772019-03-11 20:01:05 -07007976 ret = kvm_fast_pio_out(vcpu, size, port);
7977 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007978}
7979EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007980
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007981static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007982{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007983 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007984 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007985}
7986
7987static void tsc_khz_changed(void *data)
7988{
7989 struct cpufreq_freqs *freq = data;
7990 unsigned long khz = 0;
7991
7992 if (data)
7993 khz = freq->new;
7994 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7995 khz = cpufreq_quick_get(raw_smp_processor_id());
7996 if (!khz)
7997 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007998 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007999}
8000
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008001#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008002static void kvm_hyperv_tsc_notifier(void)
8003{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008004 struct kvm *kvm;
8005 struct kvm_vcpu *vcpu;
8006 int cpu;
Paolo Bonzinia83829f2021-03-25 14:11:14 -04008007 unsigned long flags;
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008008
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008009 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008010 list_for_each_entry(kvm, &vm_list, vm_list)
8011 kvm_make_mclock_inprogress_request(kvm);
8012
8013 hyperv_stop_tsc_emulation();
8014
8015 /* TSC frequency always matches when on Hyper-V */
8016 for_each_present_cpu(cpu)
8017 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
8018 kvm_max_guest_tsc_khz = tsc_khz;
8019
8020 list_for_each_entry(kvm, &vm_list, vm_list) {
8021 struct kvm_arch *ka = &kvm->arch;
8022
Paolo Bonzinia83829f2021-03-25 14:11:14 -04008023 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008024 pvclock_update_vm_gtod_copy(kvm);
Paolo Bonzinia83829f2021-03-25 14:11:14 -04008025 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008026
8027 kvm_for_each_vcpu(cpu, vcpu, kvm)
8028 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
8029
8030 kvm_for_each_vcpu(cpu, vcpu, kvm)
8031 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008032 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008033 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008034}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008035#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008036
Viresh Kumardf240142019-04-29 15:03:58 +05308037static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008038{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008039 struct kvm *kvm;
8040 struct kvm_vcpu *vcpu;
8041 int i, send_ipi = 0;
8042
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008043 /*
8044 * We allow guests to temporarily run on slowing clocks,
8045 * provided we notify them after, or to run on accelerating
8046 * clocks, provided we notify them before. Thus time never
8047 * goes backwards.
8048 *
8049 * However, we have a problem. We can't atomically update
8050 * the frequency of a given CPU from this function; it is
8051 * merely a notifier, which can be called from any CPU.
8052 * Changing the TSC frequency at arbitrary points in time
8053 * requires a recomputation of local variables related to
8054 * the TSC for each VCPU. We must flag these local variables
8055 * to be updated and be sure the update takes place with the
8056 * new frequency before any guests proceed.
8057 *
8058 * Unfortunately, the combination of hotplug CPU and frequency
8059 * change creates an intractable locking scenario; the order
8060 * of when these callouts happen is undefined with respect to
8061 * CPU hotplug, and they can race with each other. As such,
8062 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
8063 * undefined; you can actually have a CPU frequency change take
8064 * place in between the computation of X and the setting of the
8065 * variable. To protect against this problem, all updates of
8066 * the per_cpu tsc_khz variable are done in an interrupt
8067 * protected IPI, and all callers wishing to update the value
8068 * must wait for a synchronous IPI to complete (which is trivial
8069 * if the caller is on the CPU already). This establishes the
8070 * necessary total order on variable updates.
8071 *
8072 * Note that because a guest time update may take place
8073 * anytime after the setting of the VCPU's request bit, the
8074 * correct TSC value must be set before the request. However,
8075 * to ensure the update actually makes it to any guest which
8076 * starts running in hardware virtualization between the set
8077 * and the acquisition of the spinlock, we must also ping the
8078 * CPU after setting the request bit.
8079 *
8080 */
8081
Viresh Kumardf240142019-04-29 15:03:58 +05308082 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008083
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008084 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008085 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03008086 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05308087 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008088 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10008089 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008090 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008091 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008092 }
8093 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008094 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008095
8096 if (freq->old < freq->new && send_ipi) {
8097 /*
8098 * We upscale the frequency. Must make the guest
8099 * doesn't see old kvmclock values while running with
8100 * the new frequency, otherwise we risk the guest sees
8101 * time go backwards.
8102 *
8103 * In case we update the frequency for another cpu
8104 * (which might be in guest context) send an interrupt
8105 * to kick the cpu out of guest context. Next time
8106 * guest context is entered kvmclock will be updated,
8107 * so the guest will not see stale values.
8108 */
Viresh Kumardf240142019-04-29 15:03:58 +05308109 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008110 }
Viresh Kumardf240142019-04-29 15:03:58 +05308111}
8112
8113static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
8114 void *data)
8115{
8116 struct cpufreq_freqs *freq = data;
8117 int cpu;
8118
8119 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
8120 return 0;
8121 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
8122 return 0;
8123
8124 for_each_cpu(cpu, freq->policy->cpus)
8125 __kvmclock_cpufreq_notifier(freq, cpu);
8126
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008127 return 0;
8128}
8129
8130static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008131 .notifier_call = kvmclock_cpufreq_notifier
8132};
8133
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008134static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008135{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008136 tsc_khz_changed(NULL);
8137 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008138}
8139
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008140static void kvm_timer_init(void)
8141{
Zachary Amsdenc2855452010-09-18 14:38:15 -10008142 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05308143
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008144 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10008145#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01008146 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02008147 int cpu;
8148
Avi Kivity3e26f232010-12-16 12:16:34 +02008149 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01008150 policy = cpufreq_cpu_get(cpu);
Wanpeng Li9a119972020-03-02 15:15:36 +08008151 if (policy) {
8152 if (policy->cpuinfo.max_freq)
8153 max_tsc_khz = policy->cpuinfo.max_freq;
8154 cpufreq_cpu_put(policy);
8155 }
Avi Kivity3e26f232010-12-16 12:16:34 +02008156 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10008157#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008158 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
8159 CPUFREQ_TRANSITION_NOTIFIER);
8160 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05308161
Thomas Gleixner73c1b412016-12-21 20:19:54 +01008162 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008163 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008164}
8165
Andi Kleendd60d212017-07-25 17:20:32 -07008166DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
8167EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008168
Gleb Natapovf5132b02011-11-10 14:57:22 +02008169int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008170{
Alex,Shi086c9852011-10-20 15:34:01 +08008171 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008172}
8173
8174static int kvm_is_user_mode(void)
8175{
8176 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008177
Alex,Shi086c9852011-10-20 15:34:01 +08008178 if (__this_cpu_read(current_vcpu))
Jason Baronb36464772021-01-14 22:27:56 -05008179 user_mode = static_call(kvm_x86_get_cpl)(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008180
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008181 return user_mode != 0;
8182}
8183
8184static unsigned long kvm_get_guest_ip(void)
8185{
8186 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008187
Alex,Shi086c9852011-10-20 15:34:01 +08008188 if (__this_cpu_read(current_vcpu))
8189 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08008190
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008191 return ip;
8192}
8193
Luwei Kang8479e042019-02-18 19:26:07 -05008194static void kvm_handle_intel_pt_intr(void)
8195{
8196 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
8197
8198 kvm_make_request(KVM_REQ_PMI, vcpu);
8199 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
8200 (unsigned long *)&vcpu->arch.pmu.global_status);
8201}
8202
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008203static struct perf_guest_info_callbacks kvm_guest_cbs = {
8204 .is_in_guest = kvm_is_in_guest,
8205 .is_user_mode = kvm_is_user_mode,
8206 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05008207 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008208};
8209
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008210#ifdef CONFIG_X86_64
8211static void pvclock_gtod_update_fn(struct work_struct *work)
8212{
Marcelo Tosattid8281992012-11-27 23:29:01 -02008213 struct kvm *kvm;
8214
8215 struct kvm_vcpu *vcpu;
8216 int i;
8217
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008218 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008219 list_for_each_entry(kvm, &vm_list, vm_list)
8220 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08008221 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008222 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08008223 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008224}
8225
8226static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
8227
8228/*
Thomas Gleixner3f804f62021-05-06 15:21:37 +02008229 * Indirection to move queue_work() out of the tk_core.seq write held
8230 * region to prevent possible deadlocks against time accessors which
8231 * are invoked with work related locks held.
8232 */
8233static void pvclock_irq_work_fn(struct irq_work *w)
8234{
8235 queue_work(system_long_wq, &pvclock_gtod_work);
8236}
8237
8238static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_irq_work_fn);
8239
8240/*
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008241 * Notification about pvclock gtod data update.
8242 */
8243static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
8244 void *priv)
8245{
8246 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
8247 struct timekeeper *tk = priv;
8248
8249 update_pvclock_gtod(tk);
8250
Thomas Gleixner3f804f62021-05-06 15:21:37 +02008251 /*
8252 * Disable master clock if host does not trust, or does not use,
8253 * TSC based clocksource. Delegate queue_work() to irq_work as
8254 * this is invoked with tk_core.seq write held.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008255 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01008256 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008257 atomic_read(&kvm_guest_has_master_clock) != 0)
Thomas Gleixner3f804f62021-05-06 15:21:37 +02008258 irq_work_queue(&pvclock_irq_work);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008259 return 0;
8260}
8261
8262static struct notifier_block pvclock_gtod_notifier = {
8263 .notifier_call = pvclock_gtod_notify,
8264};
8265#endif
8266
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008267int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02008268{
Sean Christophersond008dfd2020-03-21 13:25:56 -07008269 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008270 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008271
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008272 if (kvm_x86_ops.hardware_enable) {
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008273 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008274 r = -EEXIST;
8275 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008276 }
8277
8278 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01008279 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008280 r = -EOPNOTSUPP;
8281 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008282 }
8283 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01008284 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008285 r = -EOPNOTSUPP;
8286 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008287 }
8288
Marc Orrb666a4b2018-11-06 14:53:56 -08008289 /*
8290 * KVM explicitly assumes that the guest has an FPU and
8291 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
8292 * vCPU's FPU state as a fxregs_state struct.
8293 */
8294 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
8295 printk(KERN_ERR "kvm: inadequate fpu\n");
8296 r = -EOPNOTSUPP;
8297 goto out;
8298 }
8299
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008300 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01008301 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08008302 __alignof__(struct fpu), SLAB_ACCOUNT,
8303 NULL);
8304 if (!x86_fpu_cache) {
8305 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
8306 goto out;
8307 }
8308
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008309 x86_emulator_cache = kvm_alloc_emulator_cache();
8310 if (!x86_emulator_cache) {
8311 pr_err("kvm: failed to allocate cache for x86 emulator\n");
8312 goto out_free_x86_fpu_cache;
8313 }
8314
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07008315 user_return_msrs = alloc_percpu(struct kvm_user_return_msrs);
8316 if (!user_return_msrs) {
8317 printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008318 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008319 }
Sean Christophersone5fda4b2021-05-04 10:17:32 -07008320 kvm_nr_uret_msrs = 0;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008321
Avi Kivity97db56c2008-01-13 13:23:56 +02008322 r = kvm_mmu_module_init();
8323 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008324 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02008325
Zachary Amsdenb820cc02009-09-29 11:38:34 -10008326 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01008327
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008328 perf_register_guest_info_callbacks(&kvm_guest_cbs);
8329
Sean Christophersoncfc48182020-03-02 15:56:23 -08008330 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08008331 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08008332 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
8333 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08008334
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08008335 if (pi_inject_timer == -1)
8336 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008337#ifdef CONFIG_X86_64
8338 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008339
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008340 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008341 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008342#endif
8343
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008344 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008345
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02008346out_free_percpu:
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07008347 free_percpu(user_return_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008348out_free_x86_emulator_cache:
8349 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08008350out_free_x86_fpu_cache:
8351 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008352out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008353 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02008354}
Hollis Blanchard8776e512007-10-31 17:24:24 -05008355
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008356void kvm_arch_exit(void)
8357{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008358#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01008359 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01008360 clear_hv_tscchange_cb();
8361#endif
David Matlackcef84c32016-12-16 14:30:36 -08008362 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008363 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
8364
Jan Kiszka888d2562009-04-17 19:24:58 +02008365 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
8366 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
8367 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00008368 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008369#ifdef CONFIG_X86_64
8370 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
Thomas Gleixner3f804f62021-05-06 15:21:37 +02008371 irq_work_sync(&pvclock_irq_work);
Thomas Gleixner594b27e2021-05-05 23:48:17 +02008372 cancel_work_sync(&pvclock_gtod_work);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02008373#endif
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008374 kvm_x86_ops.hardware_enable = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008375 kvm_mmu_module_exit();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07008376 free_percpu(user_return_msrs);
Wanpeng Lidfdc0a72021-06-10 21:59:33 -07008377 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08008378 kmem_cache_destroy(x86_fpu_cache);
Paolo Bonzinib59b1532021-02-26 04:54:45 -05008379#ifdef CONFIG_KVM_XEN
Paolo Bonzinic462f852021-02-26 04:49:06 -05008380 static_key_deferred_flush(&kvm_xen_enabled);
David Woodhouse7d6bbeb2021-02-02 15:48:05 +00008381 WARN_ON(static_branch_unlikely(&kvm_xen_enabled.key));
Paolo Bonzinib59b1532021-02-26 04:54:45 -05008382#endif
Zhang Xiantao56c6d282007-11-18 20:43:21 +08008383}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08008384
Paolo Bonzini872f36e2021-01-08 05:54:44 -05008385static int __kvm_vcpu_halt(struct kvm_vcpu *vcpu, int state, int reason)
Hollis Blanchard8776e512007-10-31 17:24:24 -05008386{
8387 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02008388 if (lapic_in_kernel(vcpu)) {
Tom Lendacky647daca2021-01-04 14:20:01 -06008389 vcpu->arch.mp_state = state;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008390 return 1;
8391 } else {
Tom Lendacky647daca2021-01-04 14:20:01 -06008392 vcpu->run->exit_reason = reason;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008393 return 0;
8394 }
8395}
Tom Lendacky647daca2021-01-04 14:20:01 -06008396
8397int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
8398{
8399 return __kvm_vcpu_halt(vcpu, KVM_MP_STATE_HALTED, KVM_EXIT_HLT);
8400}
Joel Schopp5cb56052015-03-02 13:43:31 -06008401EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
8402
8403int kvm_emulate_halt(struct kvm_vcpu *vcpu)
8404{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008405 int ret = kvm_skip_emulated_instruction(vcpu);
8406 /*
8407 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
8408 * KVM_EXIT_DEBUG here.
8409 */
8410 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06008411}
Hollis Blanchard8776e512007-10-31 17:24:24 -05008412EXPORT_SYMBOL_GPL(kvm_emulate_halt);
8413
Tom Lendacky647daca2021-01-04 14:20:01 -06008414int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu)
8415{
8416 int ret = kvm_skip_emulated_instruction(vcpu);
8417
8418 return __kvm_vcpu_halt(vcpu, KVM_MP_STATE_AP_RESET_HOLD, KVM_EXIT_AP_RESET_HOLD) && ret;
8419}
8420EXPORT_SYMBOL_GPL(kvm_emulate_ap_reset_hold);
8421
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008422#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008423static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
8424 unsigned long clock_type)
8425{
8426 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02008427 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01008428 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008429 int ret;
8430
8431 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
8432 return -KVM_EOPNOTSUPP;
8433
YANG LI7ca7f3b2021-01-11 17:32:58 +08008434 if (!kvm_get_walltime_and_clockread(&ts, &cycle))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008435 return -KVM_EOPNOTSUPP;
8436
8437 clock_pairing.sec = ts.tv_sec;
8438 clock_pairing.nsec = ts.tv_nsec;
8439 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
8440 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02008441 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008442
8443 ret = 0;
8444 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
8445 sizeof(struct kvm_clock_pairing)))
8446 ret = -KVM_EFAULT;
8447
8448 return ret;
8449}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008450#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008451
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308452/*
8453 * kvm_pv_kick_cpu_op: Kick a vcpu.
8454 *
8455 * @apicid - apicid of vcpu to be kicked.
8456 */
8457static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
8458{
Raghavendra K T24d21662013-08-26 14:18:35 +05308459 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308460
Peter Xu150a84f2019-12-04 20:07:21 +01008461 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01008462 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08008463 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05308464 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06008465 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308466
Raghavendra K T24d21662013-08-26 14:18:35 +05308467 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08008468 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308469}
8470
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008471bool kvm_apicv_activated(struct kvm *kvm)
8472{
8473 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
8474}
8475EXPORT_SYMBOL_GPL(kvm_apicv_activated);
8476
Vitaly Kuznetsov4651fc52021-06-09 17:09:09 +02008477static void kvm_apicv_init(struct kvm *kvm)
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008478{
Vitaly Kuznetsov4651fc52021-06-09 17:09:09 +02008479 if (enable_apicv)
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008480 clear_bit(APICV_INHIBIT_REASON_DISABLE,
8481 &kvm->arch.apicv_inhibit_reasons);
8482 else
8483 set_bit(APICV_INHIBIT_REASON_DISABLE,
8484 &kvm->arch.apicv_inhibit_reasons);
8485}
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008486
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008487static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id)
Wanpeng Li71506292019-06-11 20:23:49 +08008488{
8489 struct kvm_vcpu *target = NULL;
8490 struct kvm_apic_map *map;
8491
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008492 vcpu->stat.directed_yield_attempted++;
8493
Wanpeng Li72b268a2021-05-18 05:00:32 -07008494 if (single_task_running())
8495 goto no_yield;
8496
Wanpeng Li71506292019-06-11 20:23:49 +08008497 rcu_read_lock();
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008498 map = rcu_dereference(vcpu->kvm->arch.apic_map);
Wanpeng Li71506292019-06-11 20:23:49 +08008499
8500 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
8501 target = map->phys_map[dest_id]->vcpu;
8502
8503 rcu_read_unlock();
8504
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008505 if (!target || !READ_ONCE(target->ready))
8506 goto no_yield;
8507
Wanpeng Lia1fa4cb2021-04-09 12:18:31 +08008508 /* Ignore requests to yield to self */
8509 if (vcpu == target)
8510 goto no_yield;
8511
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008512 if (kvm_vcpu_yield_to(target) <= 0)
8513 goto no_yield;
8514
8515 vcpu->stat.directed_yield_successful++;
8516
8517no_yield:
8518 return;
Wanpeng Li71506292019-06-11 20:23:49 +08008519}
8520
Hollis Blanchard8776e512007-10-31 17:24:24 -05008521int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
8522{
8523 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03008524 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008525
Joao Martins23200b72018-06-13 09:55:44 -04008526 if (kvm_xen_hypercall_enabled(vcpu->kvm))
8527 return kvm_xen_hypercall(vcpu);
8528
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01008529 if (kvm_hv_hypercall_enabled(vcpu))
Radim Krčmář696ca772018-05-24 17:50:56 +02008530 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02008531
Sean Christophersonde3cd112019-04-30 10:36:17 -07008532 nr = kvm_rax_read(vcpu);
8533 a0 = kvm_rbx_read(vcpu);
8534 a1 = kvm_rcx_read(vcpu);
8535 a2 = kvm_rdx_read(vcpu);
8536 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008537
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008538 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04008539
Nadav Amita449c7a2014-06-18 17:19:24 +03008540 op_64_bit = is_64_bit_mode(vcpu);
8541 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05008542 nr &= 0xFFFFFFFF;
8543 a0 &= 0xFFFFFFFF;
8544 a1 &= 0xFFFFFFFF;
8545 a2 &= 0xFFFFFFFF;
8546 a3 &= 0xFFFFFFFF;
8547 }
8548
Jason Baronb36464772021-01-14 22:27:56 -05008549 if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
Jan Kiszka07708c42009-08-03 18:43:28 +02008550 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02008551 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02008552 }
8553
Oliver Upton66570e92020-08-18 15:24:28 +00008554 ret = -KVM_ENOSYS;
8555
Hollis Blanchard8776e512007-10-31 17:24:24 -05008556 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02008557 case KVM_HC_VAPIC_POLL_IRQ:
8558 ret = 0;
8559 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308560 case KVM_HC_KICK_CPU:
Oliver Upton66570e92020-08-18 15:24:28 +00008561 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_UNHALT))
8562 break;
8563
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308564 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008565 kvm_sched_yield(vcpu, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308566 ret = 0;
8567 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008568#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008569 case KVM_HC_CLOCK_PAIRING:
8570 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
8571 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08008572#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08008573 case KVM_HC_SEND_IPI:
Oliver Upton66570e92020-08-18 15:24:28 +00008574 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SEND_IPI))
8575 break;
8576
Wanpeng Li4180bf12018-07-23 14:39:54 +08008577 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
8578 break;
Wanpeng Li71506292019-06-11 20:23:49 +08008579 case KVM_HC_SCHED_YIELD:
Oliver Upton66570e92020-08-18 15:24:28 +00008580 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SCHED_YIELD))
8581 break;
8582
Wanpeng Li4a7132e2021-04-09 12:18:30 +08008583 kvm_sched_yield(vcpu, a0);
Wanpeng Li71506292019-06-11 20:23:49 +08008584 ret = 0;
8585 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008586 default:
8587 ret = -KVM_ENOSYS;
8588 break;
8589 }
Radim Krčmář696ca772018-05-24 17:50:56 +02008590out:
Nadav Amita449c7a2014-06-18 17:19:24 +03008591 if (!op_64_bit)
8592 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07008593 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03008594
Amit Shahf11c3a82008-02-21 01:00:30 +05308595 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03008596 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008597}
8598EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
8599
Jan Kiszkab6785de2012-09-20 07:43:17 +02008600static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05008601{
Avi Kivityd6aa1002011-04-20 15:47:13 +03008602 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008603 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008604 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008605
Jason Baronb36464772021-01-14 22:27:56 -05008606 static_call(kvm_x86_patch_hypercall)(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008607
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01008608 return emulator_write_emulated(ctxt, rip, instruction, 3,
8609 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008610}
8611
Avi Kivity851ba692009-08-24 11:10:17 +03008612static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008613{
Matt Gingell782d4222015-11-16 15:26:00 -08008614 return vcpu->run->request_interrupt_window &&
8615 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008616}
8617
Avi Kivity851ba692009-08-24 11:10:17 +03008618static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008619{
Avi Kivity851ba692009-08-24 11:10:17 +03008620 struct kvm_run *kvm_run = vcpu->run;
8621
Tom Lendackyf1c63662020-12-14 10:29:50 -05008622 /*
8623 * if_flag is obsolete and useless, so do not bother
8624 * setting it for SEV-ES guests. Userspace can just
8625 * use kvm_run->ready_for_interrupt_injection.
8626 */
8627 kvm_run->if_flag = !vcpu->arch.guest_state_protected
8628 && (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
8629
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008630 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008631 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01008632 kvm_run->ready_for_interrupt_injection =
8633 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08008634 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Chenyi Qiang15aad3b2020-11-06 17:03:13 +08008635
8636 if (is_smm(vcpu))
8637 kvm_run->flags |= KVM_RUN_X86_SMM;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008638}
8639
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008640static void update_cr8_intercept(struct kvm_vcpu *vcpu)
8641{
8642 int max_irr, tpr;
8643
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008644 if (!kvm_x86_ops.update_cr8_intercept)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008645 return;
8646
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008647 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03008648 return;
8649
Andrey Smetanind62caab2015-11-10 15:36:33 +03008650 if (vcpu->arch.apicv_active)
8651 return;
8652
Gleb Natapov8db3baa2009-05-11 13:35:54 +03008653 if (!vcpu->arch.apic->vapic_addr)
8654 max_irr = kvm_lapic_find_highest_irr(vcpu);
8655 else
8656 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008657
8658 if (max_irr != -1)
8659 max_irr >>= 4;
8660
8661 tpr = kvm_lapic_get_cr8(vcpu);
8662
Jason Baronb36464772021-01-14 22:27:56 -05008663 static_call(kvm_x86_update_cr8_intercept)(vcpu, tpr, max_irr);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008664}
8665
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008666
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08008667int kvm_check_nested_events(struct kvm_vcpu *vcpu)
8668{
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08008669 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
8670 kvm_x86_ops.nested_ops->triple_fault(vcpu);
8671 return 1;
8672 }
8673
8674 return kvm_x86_ops.nested_ops->check_events(vcpu);
8675}
8676
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008677static void kvm_inject_exception(struct kvm_vcpu *vcpu)
8678{
8679 if (vcpu->arch.exception.error_code && !is_protmode(vcpu))
8680 vcpu->arch.exception.error_code = false;
8681 static_call(kvm_x86_queue_exception)(vcpu);
8682}
8683
Jim Mattsona5f69092021-06-04 10:26:03 -07008684static int inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008685{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008686 int r;
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008687 bool can_inject = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008688
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008689 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07008690
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008691 if (vcpu->arch.exception.injected) {
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008692 kvm_inject_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008693 can_inject = false;
8694 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07008695 /*
Liran Alona042c262018-03-23 03:01:32 +03008696 * Do not inject an NMI or interrupt if there is a pending
8697 * exception. Exceptions and interrupts are recognized at
8698 * instruction boundaries, i.e. the start of an instruction.
8699 * Trap-like exceptions, e.g. #DB, have higher priority than
8700 * NMIs and interrupts, i.e. traps are recognized before an
8701 * NMI/interrupt that's pending on the same instruction.
8702 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
8703 * priority, but are only generated (pended) during instruction
8704 * execution, i.e. a pending fault-like exception means the
8705 * fault occurred on the *previous* instruction and must be
8706 * serviced prior to recognizing any new events in order to
8707 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07008708 */
Liran Alon1a680e32018-03-23 03:01:33 +03008709 else if (!vcpu->arch.exception.pending) {
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008710 if (vcpu->arch.nmi_injected) {
Jason Baronb36464772021-01-14 22:27:56 -05008711 static_call(kvm_x86_set_nmi)(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008712 can_inject = false;
8713 } else if (vcpu->arch.interrupt.injected) {
Jason Baronb36464772021-01-14 22:27:56 -05008714 static_call(kvm_x86_set_irq)(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008715 can_inject = false;
8716 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07008717 }
8718
Sean Christopherson3b82b8d2020-04-22 19:25:47 -07008719 WARN_ON_ONCE(vcpu->arch.exception.injected &&
8720 vcpu->arch.exception.pending);
8721
Liran Alon1a680e32018-03-23 03:01:33 +03008722 /*
8723 * Call check_nested_events() even if we reinjected a previous event
8724 * in order for caller to determine if it should require immediate-exit
8725 * from L2 to L1 due to pending L1 events which require exit
8726 * from L2 to L1.
8727 */
Paolo Bonzini56083bd2020-04-17 10:32:53 -04008728 if (is_guest_mode(vcpu)) {
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08008729 r = kvm_check_nested_events(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008730 if (r < 0)
Jim Mattsona5f69092021-06-04 10:26:03 -07008731 goto out;
Wanpeng Li664f8e22017-08-24 03:35:09 -07008732 }
8733
8734 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03008735 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02008736 trace_kvm_inj_exception(vcpu->arch.exception.nr,
8737 vcpu->arch.exception.has_error_code,
8738 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03008739
Wanpeng Li664f8e22017-08-24 03:35:09 -07008740 vcpu->arch.exception.pending = false;
8741 vcpu->arch.exception.injected = true;
8742
Nadav Amitd6e8c852014-07-24 14:51:24 +03008743 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
8744 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
8745 X86_EFLAGS_RF);
8746
Jim Mattsonf10c7292018-10-16 14:29:23 -07008747 if (vcpu->arch.exception.nr == DB_VECTOR) {
Jim Mattsonf10c7292018-10-16 14:29:23 -07008748 kvm_deliver_exception_payload(vcpu);
8749 if (vcpu->arch.dr7 & DR7_GD) {
8750 vcpu->arch.dr7 &= ~DR7_GD;
8751 kvm_update_dr7(vcpu);
8752 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03008753 }
8754
Maxim Levitskyb97f0742021-02-25 17:41:32 +02008755 kvm_inject_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008756 can_inject = false;
Liran Alon1a680e32018-03-23 03:01:33 +03008757 }
8758
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008759 /*
8760 * Finally, inject interrupt events. If an event cannot be injected
8761 * due to architectural conditions (e.g. IF=0) a window-open exit
8762 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending
8763 * and can architecturally be injected, but we cannot do it right now:
8764 * an interrupt could have arrived just now and we have to inject it
8765 * as a vmexit, or there could already an event in the queue, which is
8766 * indicated by can_inject. In that case we request an immediate exit
8767 * in order to make progress and get back here for another iteration.
8768 * The kvm_x86_ops hooks communicate this by returning -EBUSY.
8769 */
8770 if (vcpu->arch.smi_pending) {
Jason Baronb36464772021-01-14 22:27:56 -05008771 r = can_inject ? static_call(kvm_x86_smi_allowed)(vcpu, true) : -EBUSY;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008772 if (r < 0)
Jim Mattsona5f69092021-06-04 10:26:03 -07008773 goto out;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008774 if (r) {
8775 vcpu->arch.smi_pending = false;
8776 ++vcpu->arch.smi_count;
8777 enter_smm(vcpu);
8778 can_inject = false;
8779 } else
Jason Baronb36464772021-01-14 22:27:56 -05008780 static_call(kvm_x86_enable_smi_window)(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008781 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008782
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008783 if (vcpu->arch.nmi_pending) {
Jason Baronb36464772021-01-14 22:27:56 -05008784 r = can_inject ? static_call(kvm_x86_nmi_allowed)(vcpu, true) : -EBUSY;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008785 if (r < 0)
Jim Mattsona5f69092021-06-04 10:26:03 -07008786 goto out;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008787 if (r) {
8788 --vcpu->arch.nmi_pending;
8789 vcpu->arch.nmi_injected = true;
Jason Baronb36464772021-01-14 22:27:56 -05008790 static_call(kvm_x86_set_nmi)(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008791 can_inject = false;
Jason Baronb36464772021-01-14 22:27:56 -05008792 WARN_ON(static_call(kvm_x86_nmi_allowed)(vcpu, true) < 0);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008793 }
8794 if (vcpu->arch.nmi_pending)
Jason Baronb36464772021-01-14 22:27:56 -05008795 static_call(kvm_x86_enable_nmi_window)(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008796 }
8797
8798 if (kvm_cpu_has_injectable_intr(vcpu)) {
Jason Baronb36464772021-01-14 22:27:56 -05008799 r = can_inject ? static_call(kvm_x86_interrupt_allowed)(vcpu, true) : -EBUSY;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008800 if (r < 0)
Jim Mattsona5f69092021-06-04 10:26:03 -07008801 goto out;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008802 if (r) {
8803 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false);
Jason Baronb36464772021-01-14 22:27:56 -05008804 static_call(kvm_x86_set_irq)(vcpu);
8805 WARN_ON(static_call(kvm_x86_interrupt_allowed)(vcpu, true) < 0);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008806 }
8807 if (kvm_cpu_has_injectable_intr(vcpu))
Jason Baronb36464772021-01-14 22:27:56 -05008808 static_call(kvm_x86_enable_irq_window)(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008809 }
8810
8811 if (is_guest_mode(vcpu) &&
8812 kvm_x86_ops.nested_ops->hv_timer_pending &&
8813 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
8814 *req_immediate_exit = true;
8815
8816 WARN_ON(vcpu->arch.exception.pending);
Jim Mattsona5f69092021-06-04 10:26:03 -07008817 return 0;
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008818
Jim Mattsona5f69092021-06-04 10:26:03 -07008819out:
8820 if (r == -EBUSY) {
8821 *req_immediate_exit = true;
8822 r = 0;
8823 }
8824 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008825}
8826
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008827static void process_nmi(struct kvm_vcpu *vcpu)
8828{
8829 unsigned limit = 2;
8830
8831 /*
8832 * x86 is limited to one NMI running, and one NMI pending after it.
8833 * If an NMI is already in progress, limit further NMIs to just one.
8834 * Otherwise, allow two (and we'll inject the first one immediately).
8835 */
Jason Baronb36464772021-01-14 22:27:56 -05008836 if (static_call(kvm_x86_get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008837 limit = 1;
8838
8839 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
8840 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
8841 kvm_make_request(KVM_REQ_EVENT, vcpu);
8842}
8843
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008844static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008845{
8846 u32 flags = 0;
8847 flags |= seg->g << 23;
8848 flags |= seg->db << 22;
8849 flags |= seg->l << 21;
8850 flags |= seg->avl << 20;
8851 flags |= seg->present << 15;
8852 flags |= seg->dpl << 13;
8853 flags |= seg->s << 12;
8854 flags |= seg->type << 8;
8855 return flags;
8856}
8857
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008858static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008859{
8860 struct kvm_segment seg;
8861 int offset;
8862
8863 kvm_get_segment(vcpu, &seg, n);
8864 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
8865
8866 if (n < 3)
8867 offset = 0x7f84 + n * 12;
8868 else
8869 offset = 0x7f2c + (n - 3) * 12;
8870
8871 put_smstate(u32, buf, offset + 8, seg.base);
8872 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008873 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008874}
8875
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06008876#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008877static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008878{
8879 struct kvm_segment seg;
8880 int offset;
8881 u16 flags;
8882
8883 kvm_get_segment(vcpu, &seg, n);
8884 offset = 0x7e00 + n * 16;
8885
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008886 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008887 put_smstate(u16, buf, offset, seg.selector);
8888 put_smstate(u16, buf, offset + 2, flags);
8889 put_smstate(u32, buf, offset + 4, seg.limit);
8890 put_smstate(u64, buf, offset + 8, seg.base);
8891}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06008892#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008893
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008894static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008895{
8896 struct desc_ptr dt;
8897 struct kvm_segment seg;
8898 unsigned long val;
8899 int i;
8900
8901 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
8902 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
8903 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
8904 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
8905
8906 for (i = 0; i < 8; i++)
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07008907 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read_raw(vcpu, i));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008908
8909 kvm_get_dr(vcpu, 6, &val);
8910 put_smstate(u32, buf, 0x7fcc, (u32)val);
8911 kvm_get_dr(vcpu, 7, &val);
8912 put_smstate(u32, buf, 0x7fc8, (u32)val);
8913
8914 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
8915 put_smstate(u32, buf, 0x7fc4, seg.selector);
8916 put_smstate(u32, buf, 0x7f64, seg.base);
8917 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008918 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008919
8920 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
8921 put_smstate(u32, buf, 0x7fc0, seg.selector);
8922 put_smstate(u32, buf, 0x7f80, seg.base);
8923 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008924 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008925
Jason Baronb36464772021-01-14 22:27:56 -05008926 static_call(kvm_x86_get_gdt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008927 put_smstate(u32, buf, 0x7f74, dt.address);
8928 put_smstate(u32, buf, 0x7f70, dt.size);
8929
Jason Baronb36464772021-01-14 22:27:56 -05008930 static_call(kvm_x86_get_idt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008931 put_smstate(u32, buf, 0x7f58, dt.address);
8932 put_smstate(u32, buf, 0x7f54, dt.size);
8933
8934 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008935 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008936
8937 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
8938
8939 /* revision id */
8940 put_smstate(u32, buf, 0x7efc, 0x00020000);
8941 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
8942}
8943
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008944#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008945static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008946{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008947 struct desc_ptr dt;
8948 struct kvm_segment seg;
8949 unsigned long val;
8950 int i;
8951
8952 for (i = 0; i < 16; i++)
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07008953 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read_raw(vcpu, i));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008954
8955 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
8956 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
8957
8958 kvm_get_dr(vcpu, 6, &val);
8959 put_smstate(u64, buf, 0x7f68, val);
8960 kvm_get_dr(vcpu, 7, &val);
8961 put_smstate(u64, buf, 0x7f60, val);
8962
8963 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
8964 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
8965 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
8966
8967 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
8968
8969 /* revision id */
8970 put_smstate(u32, buf, 0x7efc, 0x00020064);
8971
8972 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
8973
8974 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
8975 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008976 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008977 put_smstate(u32, buf, 0x7e94, seg.limit);
8978 put_smstate(u64, buf, 0x7e98, seg.base);
8979
Jason Baronb36464772021-01-14 22:27:56 -05008980 static_call(kvm_x86_get_idt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008981 put_smstate(u32, buf, 0x7e84, dt.size);
8982 put_smstate(u64, buf, 0x7e88, dt.address);
8983
8984 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
8985 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008986 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008987 put_smstate(u32, buf, 0x7e74, seg.limit);
8988 put_smstate(u64, buf, 0x7e78, seg.base);
8989
Jason Baronb36464772021-01-14 22:27:56 -05008990 static_call(kvm_x86_get_gdt)(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008991 put_smstate(u32, buf, 0x7e64, dt.size);
8992 put_smstate(u64, buf, 0x7e68, dt.address);
8993
8994 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008995 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008996}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008997#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008998
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008999static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02009000{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009001 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02009002 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009003 char buf[512];
9004 u32 cr0;
9005
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009006 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009007#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02009008 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009009 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009010 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009011#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009012 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009013
Ladi Prosek0234bf82017-10-11 16:54:40 +02009014 /*
9015 * Give pre_enter_smm() a chance to make ISA-specific changes to the
9016 * vCPU state (e.g. leave guest mode) after we've saved the state into
9017 * the SMM state-save area.
9018 */
Jason Baronb36464772021-01-14 22:27:56 -05009019 static_call(kvm_x86_pre_enter_smm)(vcpu, buf);
Ladi Prosek0234bf82017-10-11 16:54:40 +02009020
Sean Christophersondc872752021-06-09 11:56:15 -07009021 kvm_smm_changed(vcpu, true);
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009022 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009023
Jason Baronb36464772021-01-14 22:27:56 -05009024 if (static_call(kvm_x86_get_nmi_mask)(vcpu))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009025 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
9026 else
Jason Baronb36464772021-01-14 22:27:56 -05009027 static_call(kvm_x86_set_nmi_mask)(vcpu, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009028
9029 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
9030 kvm_rip_write(vcpu, 0x8000);
9031
9032 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
Jason Baronb36464772021-01-14 22:27:56 -05009033 static_call(kvm_x86_set_cr0)(vcpu, cr0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009034 vcpu->arch.cr0 = cr0;
9035
Jason Baronb36464772021-01-14 22:27:56 -05009036 static_call(kvm_x86_set_cr4)(vcpu, 0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009037
Paolo Bonzini18c36262015-08-07 12:27:54 +02009038 /* Undocumented: IDT limit is set to zero on entry to SMM. */
9039 dt.address = dt.size = 0;
Jason Baronb36464772021-01-14 22:27:56 -05009040 static_call(kvm_x86_set_idt)(vcpu, &dt);
Paolo Bonzini18c36262015-08-07 12:27:54 +02009041
Paolo Bonzini996ff542020-12-14 07:49:54 -05009042 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009043
9044 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
9045 cs.base = vcpu->arch.smbase;
9046
9047 ds.selector = 0;
9048 ds.base = 0;
9049
9050 cs.limit = ds.limit = 0xffffffff;
9051 cs.type = ds.type = 0x3;
9052 cs.dpl = ds.dpl = 0;
9053 cs.db = ds.db = 0;
9054 cs.s = ds.s = 1;
9055 cs.l = ds.l = 0;
9056 cs.g = ds.g = 1;
9057 cs.avl = ds.avl = 0;
9058 cs.present = ds.present = 1;
9059 cs.unusable = ds.unusable = 0;
9060 cs.padding = ds.padding = 0;
9061
9062 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9063 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
9064 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
9065 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
9066 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
9067 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
9068
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009069#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02009070 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Jason Baronb36464772021-01-14 22:27:56 -05009071 static_call(kvm_x86_set_efer)(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07009072#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009073
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08009074 kvm_update_cpuid_runtime(vcpu);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02009075 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009076}
9077
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02009078static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009079{
9080 vcpu->arch.smi_pending = true;
9081 kvm_make_request(KVM_REQ_EVENT, vcpu);
9082}
9083
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05009084void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
9085 unsigned long *vcpu_bitmap)
9086{
9087 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05009088
9089 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
9090
Mao Wenandb5a95e2019-11-19 11:06:40 +08009091 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -05009092 NULL, vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05009093
9094 free_cpumask_var(cpus);
9095}
9096
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01009097void kvm_make_scan_ioapic_request(struct kvm *kvm)
9098{
9099 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
9100}
9101
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009102void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
9103{
9104 if (!lapic_in_kernel(vcpu))
9105 return;
9106
9107 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
9108 kvm_apic_update_apicv(vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05009109 static_call(kvm_x86_refresh_apicv_exec_ctrl)(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009110}
9111EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
9112
9113/*
9114 * NOTE: Do not hold any lock prior to calling this.
9115 *
9116 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
9117 * locked, because it calls __x86_set_memory_region() which does
9118 * synchronize_srcu(&kvm->srcu).
9119 */
9120void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
9121{
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05009122 struct kvm_vcpu *except;
Paolo Bonzini8e205a62020-03-14 12:29:23 +01009123 unsigned long old, new, expected;
9124
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009125 if (!kvm_x86_ops.check_apicv_inhibit_reasons ||
Jason Baronb36464772021-01-14 22:27:56 -05009126 !static_call(kvm_x86_check_apicv_inhibit_reasons)(bit))
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06009127 return;
9128
Paolo Bonzini8e205a62020-03-14 12:29:23 +01009129 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
9130 do {
9131 expected = new = old;
9132 if (activate)
9133 __clear_bit(bit, &new);
9134 else
9135 __set_bit(bit, &new);
9136 if (new == old)
9137 break;
9138 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
9139 } while (old != expected);
9140
9141 if (!!old == !!new)
9142 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009143
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06009144 trace_kvm_apicv_update_request(activate, bit);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009145 if (kvm_x86_ops.pre_update_apicv_exec_ctrl)
Jason Baronb36464772021-01-14 22:27:56 -05009146 static_call(kvm_x86_pre_update_apicv_exec_ctrl)(kvm, activate);
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05009147
9148 /*
9149 * Sending request to update APICV for all other vcpus,
9150 * while update the calling vcpu immediately instead of
9151 * waiting for another #VMEXIT to handle the request.
9152 */
9153 except = kvm_get_running_vcpu();
9154 kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE,
9155 except);
9156 if (except)
9157 kvm_vcpu_update_apicv(except);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009158}
9159EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
9160
Yang Zhang3d81bc72013-04-11 19:25:13 +08009161static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009162{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08009163 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009164 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009165
Andrey Smetanin63086302015-11-10 15:36:32 +03009166 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009167
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07009168 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03009169 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02009170 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02009171 if (vcpu->arch.apicv_active)
Jason Baronb36464772021-01-14 22:27:56 -05009172 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08009173 if (ioapic_in_kernel(vcpu->kvm))
9174 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02009175 }
Liran Alone40ff1d2018-03-21 02:50:31 +02009176
9177 if (is_guest_mode(vcpu))
9178 vcpu->arch.load_eoi_exitmap_pending = true;
9179 else
9180 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
9181}
9182
9183static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
9184{
9185 u64 eoi_exit_bitmap[4];
9186
9187 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
9188 return;
9189
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +01009190 if (to_hv_vcpu(vcpu))
9191 bitmap_or((ulong *)eoi_exit_bitmap,
9192 vcpu->arch.ioapic_handled_vectors,
9193 to_hv_synic(vcpu)->vec_bitmap, 256);
9194
Jason Baronb36464772021-01-14 22:27:56 -05009195 static_call(kvm_x86_load_eoi_exitmap)(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009196}
9197
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +09009198void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
9199 unsigned long start, unsigned long end)
Radim Krčmářb1394e72017-11-30 19:05:45 +01009200{
9201 unsigned long apic_address;
9202
9203 /*
9204 * The physical address of apic access page is stored in the VMCS.
9205 * Update it when it becomes invalid.
9206 */
9207 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
9208 if (start <= apic_address && apic_address < end)
9209 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
9210}
9211
Tang Chen4256f432014-09-24 15:57:54 +08009212void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
9213{
Paolo Bonzini35754c92015-07-29 12:05:37 +02009214 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02009215 return;
9216
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009217 if (!kvm_x86_ops.set_apic_access_page_addr)
Tang Chen4256f432014-09-24 15:57:54 +08009218 return;
9219
Jason Baronb36464772021-01-14 22:27:56 -05009220 static_call(kvm_x86_set_apic_access_page_addr)(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08009221}
Tang Chen4256f432014-09-24 15:57:54 +08009222
Sean Christophersond264ee02018-08-27 15:21:12 -07009223void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
9224{
9225 smp_send_reschedule(vcpu->cpu);
9226}
9227EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
9228
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09009229/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01009230 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09009231 * exiting to the userspace. Otherwise, the value will be returned to the
9232 * userspace.
9233 */
Avi Kivity851ba692009-08-24 11:10:17 +03009234static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009235{
9236 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08009237 bool req_int_win =
9238 dm_request_for_irq_injection(vcpu) &&
9239 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08009240 fastpath_t exit_fastpath;
Matt Gingell62a193e2015-11-16 15:26:07 -08009241
Jan Kiszka730dca42013-04-28 10:50:52 +02009242 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009243
Peter Xufb04a1e2020-09-30 21:22:22 -04009244 /* Forbid vmenter if vcpu dirty ring is soft-full */
9245 if (unlikely(vcpu->kvm->dirty_ring_size &&
9246 kvm_dirty_ring_soft_full(&vcpu->dirty_ring))) {
9247 vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL;
9248 trace_kvm_dirty_ring_exit(vcpu);
9249 r = 0;
9250 goto out;
9251 }
9252
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02009253 if (kvm_request_pending(vcpu)) {
Paolo Bonzini729c15c2020-09-22 06:53:57 -04009254 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
Paolo Bonzini9a78e152021-01-08 11:43:08 -05009255 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) {
Jim Mattson671ddc72019-10-15 10:44:05 -07009256 r = 0;
9257 goto out;
9258 }
9259 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03009260 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05009261 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03009262 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03009263 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009264 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
9265 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03009266 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
9267 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10009268 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
9269 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10009270 if (unlikely(r))
9271 goto out;
9272 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03009273 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03009274 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05009275 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
9276 kvm_mmu_load_pgd(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07009277 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
Sean Christopherson77809382020-03-20 14:28:18 -07009278 kvm_vcpu_flush_tlb_all(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07009279
9280 /* Flushing all ASIDs flushes the current ASID... */
9281 kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
9282 }
9283 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
9284 kvm_vcpu_flush_tlb_current(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04009285 if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
9286 kvm_vcpu_flush_tlb_guest(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07009287
Avi Kivitya8eeb042010-05-10 12:34:53 +03009288 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009289 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02009290 r = 0;
9291 goto out;
9292 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03009293 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08009294 if (is_guest_mode(vcpu)) {
9295 kvm_x86_ops.nested_ops->triple_fault(vcpu);
9296 } else {
9297 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
9298 vcpu->mmio_needed = 0;
9299 r = 0;
9300 goto out;
9301 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01009302 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02009303 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
9304 /* Page is swapped out. Do synthetic halt */
9305 vcpu->arch.apf.halted = true;
9306 r = 1;
9307 goto out;
9308 }
Glauber Costac9aaa892011-07-11 15:28:14 -04009309 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
9310 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009311 if (kvm_check_request(KVM_REQ_SMI, vcpu))
9312 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009313 if (kvm_check_request(KVM_REQ_NMI, vcpu))
9314 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009315 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02009316 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009317 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02009318 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07009319 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
9320 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
9321 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03009322 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07009323 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
9324 vcpu->run->eoi.vector =
9325 vcpu->arch.pending_ioapic_eoi;
9326 r = 0;
9327 goto out;
9328 }
9329 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08009330 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
9331 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02009332 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
9333 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08009334 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
9335 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03009336 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
9337 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
9338 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
9339 r = 0;
9340 goto out;
9341 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03009342 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
9343 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
9344 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
9345 r = 0;
9346 goto out;
9347 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03009348 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01009349 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
9350
Andrey Smetanindb3975712015-11-10 15:36:35 +03009351 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01009352 vcpu->run->hyperv = hv_vcpu->exit;
Andrey Smetanindb3975712015-11-10 15:36:35 +03009353 r = 0;
9354 goto out;
9355 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03009356
9357 /*
9358 * KVM_REQ_HV_STIMER has to be processed after
9359 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
9360 * depend on the guest clock being up-to-date
9361 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009362 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
9363 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06009364 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
9365 kvm_vcpu_update_apicv(vcpu);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02009366 if (kvm_check_request(KVM_REQ_APF_READY, vcpu))
9367 kvm_check_async_pf_completion(vcpu);
Alexander Graf1a1552542020-09-25 16:34:21 +02009368 if (kvm_check_request(KVM_REQ_MSR_FILTER_CHANGED, vcpu))
Jason Baronb36464772021-01-14 22:27:56 -05009369 static_call(kvm_x86_msr_filter_changed)(vcpu);
Makarand Sonarea85863c2021-02-12 16:50:12 -08009370
9371 if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu))
9372 static_call(kvm_x86_update_cpu_dirty_logging)(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02009373 }
Avi Kivityb93463a2007-10-25 16:52:32 +02009374
David Woodhouse40da8cc2020-12-09 20:08:30 +00009375 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
9376 kvm_xen_has_interrupt(vcpu)) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01009377 ++vcpu->stat.req_event;
Jim Mattson4fe09bc2021-06-04 10:26:04 -07009378 r = kvm_apic_accept_events(vcpu);
9379 if (r < 0) {
9380 r = 0;
9381 goto out;
9382 }
Jan Kiszka66450a22013-03-13 12:42:34 +01009383 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
9384 r = 1;
9385 goto out;
9386 }
9387
Jim Mattsona5f69092021-06-04 10:26:03 -07009388 r = inject_pending_event(vcpu, &req_immediate_exit);
9389 if (r < 0) {
9390 r = 0;
9391 goto out;
9392 }
Paolo Bonzinic9d40912020-05-22 11:21:49 -04009393 if (req_int_win)
Jason Baronb36464772021-01-14 22:27:56 -05009394 static_call(kvm_x86_enable_irq_window)(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03009395
Avi Kivity3842d132010-07-27 12:30:24 +03009396 if (kvm_lapic_enabled(vcpu)) {
9397 update_cr8_intercept(vcpu);
9398 kvm_lapic_sync_to_vapic(vcpu);
9399 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009400 }
Avi Kivityb93463a2007-10-25 16:52:32 +02009401
Avi Kivityd8368af2012-05-14 18:07:56 +03009402 r = kvm_mmu_reload(vcpu);
9403 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009404 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03009405 }
9406
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009407 preempt_disable();
9408
Jason Baronb36464772021-01-14 22:27:56 -05009409 static_call(kvm_x86_prepare_guest_switch)(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009410
9411 /*
9412 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
9413 * IPI are then delayed after guest entry, which ensures that they
9414 * result in virtual interrupt delivery.
9415 */
9416 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009417 vcpu->mode = IN_GUEST_MODE;
9418
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02009419 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
9420
Lan Tianyu0f127d12016-03-13 11:10:29 +08009421 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009422 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02009423 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009424 *
Luwei Kang81b01662019-01-31 16:52:02 +08009425 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009426 * pairs with the memory barrier implicit in pi_test_and_set_on
9427 * (see vmx_deliver_posted_interrupt).
9428 *
9429 * 3) This also orders the write to mode from any reads to the page
9430 * tables done while the VCPU is running. Please see the comment
9431 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009432 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02009433 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009434
Paolo Bonzinib95234c2016-12-19 13:57:33 +01009435 /*
9436 * This handles the case where a posted interrupt was
9437 * notified with kvm_vcpu_kick.
9438 */
Liran Alonfa59cc02017-12-24 18:12:53 +02009439 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
Jason Baronb36464772021-01-14 22:27:56 -05009440 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009441
Wanpeng Li5a9f5442020-04-28 14:23:26 +08009442 if (kvm_vcpu_exit_request(vcpu)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009443 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009444 smp_wmb();
9445 local_irq_enable();
9446 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02009447 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009448 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009449 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009450 }
9451
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009452 if (req_immediate_exit) {
9453 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05009454 static_call(kvm_x86_request_immediate_exit)(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009455 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03009456
Sean Christopherson2620fe22020-01-17 11:30:51 -08009457 fpregs_assert_state_consistent();
9458 if (test_thread_flag(TIF_NEED_FPU_LOAD))
9459 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02009460
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009461 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009462 set_debugreg(0, 7);
9463 set_debugreg(vcpu->arch.eff_db[0], 0);
9464 set_debugreg(vcpu->arch.eff_db[1], 1);
9465 set_debugreg(vcpu->arch.eff_db[2], 2);
9466 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009467 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03009468 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009469 }
9470
Paolo Bonzinid89d04a2021-02-02 10:44:23 -05009471 for (;;) {
9472 exit_fastpath = static_call(kvm_x86_run)(vcpu);
9473 if (likely(exit_fastpath != EXIT_FASTPATH_REENTER_GUEST))
9474 break;
9475
9476 if (unlikely(kvm_vcpu_exit_request(vcpu))) {
9477 exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
9478 break;
9479 }
9480
9481 if (vcpu->arch.apicv_active)
9482 static_call(kvm_x86_sync_pir_to_irr)(vcpu);
9483 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009484
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009485 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009486 * Do this here before restoring debug registers on the host. And
9487 * since we do this before handling the vmexit, a DR access vmexit
9488 * can (a) read the correct value of the debug registers, (b) set
9489 * KVM_DEBUGREG_WONT_EXIT again.
9490 */
9491 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009492 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
Jason Baronb36464772021-01-14 22:27:56 -05009493 static_call(kvm_x86_sync_dirty_debug_regs)(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01009494 kvm_update_dr0123(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01009495 kvm_update_dr7(vcpu);
9496 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01009497 }
9498
9499 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009500 * If the guest has used debug registers, at least dr7
9501 * will be disabled while returning to the host.
9502 * If we don't have active breakpoints in the host, we don't
9503 * care about the messed up debug address registers. But if
9504 * we have some of them active, restore the old state.
9505 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01009506 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009507 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009508
Jim Mattsonc9671182020-06-03 16:56:23 -07009509 vcpu->arch.last_vmentry_cpu = vcpu->cpu;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08009510 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10009511
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08009512 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03009513 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08009514
Jason Baronb36464772021-01-14 22:27:56 -05009515 static_call(kvm_x86_handle_exit_irqoff)(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009516
Sean Christophersond7a08882019-07-10 09:07:34 -07009517 /*
9518 * Consume any pending interrupts, including the possible source of
9519 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
9520 * An instruction is required after local_irq_enable() to fully unblock
9521 * interrupts on processors that implement an interrupt shadow, the
9522 * stat.exits increment will do nicely.
9523 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009524 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07009525 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009526 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07009527 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009528 kvm_after_interrupt(vcpu);
9529
Wanpeng Li16045712021-05-04 17:27:30 -07009530 /*
9531 * Wait until after servicing IRQs to account guest time so that any
9532 * ticks that occurred while running the guest are properly accounted
9533 * to the guest. Waiting until IRQs are enabled degrades the accuracy
9534 * of accounting via context tracking, but the loss of accuracy is
9535 * acceptable for all known use cases.
9536 */
9537 vtime_account_guest_exit();
9538
Wanpeng Liec0671d2019-05-20 16:18:08 +08009539 if (lapic_in_kernel(vcpu)) {
9540 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
9541 if (delta != S64_MIN) {
9542 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
9543 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
9544 }
9545 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009546
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009547 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009548 preempt_enable();
9549
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009550 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03009551
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009552 /*
9553 * Profile KVM exit RIPs:
9554 */
9555 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009556 unsigned long rip = kvm_rip_read(vcpu);
9557 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009558 }
9559
Zachary Amsdencc578282012-02-03 15:43:50 -02009560 if (unlikely(vcpu->arch.tsc_always_catchup))
9561 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02009562
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03009563 if (vcpu->arch.apic_attention)
9564 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02009565
Jason Baronb36464772021-01-14 22:27:56 -05009566 r = static_call(kvm_x86_handle_exit)(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009567 return r;
9568
9569cancel_injection:
Sean Christopherson8081ad02020-04-22 19:25:40 -07009570 if (req_immediate_exit)
9571 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05009572 static_call(kvm_x86_cancel_injection)(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03009573 if (unlikely(vcpu->arch.apic_attention))
9574 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009575out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03009576 return r;
9577}
9578
Paolo Bonzini362c6982015-02-06 12:48:04 +01009579static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
9580{
Feng Wubf9f6ac2015-09-18 22:29:55 +08009581 if (!kvm_arch_vcpu_runnable(vcpu) &&
Jason Baronb36464772021-01-14 22:27:56 -05009582 (!kvm_x86_ops.pre_block || static_call(kvm_x86_pre_block)(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01009583 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
9584 kvm_vcpu_block(vcpu);
9585 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009586
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009587 if (kvm_x86_ops.post_block)
Jason Baronb36464772021-01-14 22:27:56 -05009588 static_call(kvm_x86_post_block)(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009589
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01009590 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
9591 return 1;
9592 }
Gleb Natapov09cec752009-03-23 15:11:44 +02009593
Jim Mattson4fe09bc2021-06-04 10:26:04 -07009594 if (kvm_apic_accept_events(vcpu) < 0)
9595 return 0;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009596 switch(vcpu->arch.mp_state) {
9597 case KVM_MP_STATE_HALTED:
Tom Lendacky647daca2021-01-04 14:20:01 -06009598 case KVM_MP_STATE_AP_RESET_HOLD:
Paolo Bonzini362c6982015-02-06 12:48:04 +01009599 vcpu->arch.pv.pv_unhalted = false;
9600 vcpu->arch.mp_state =
9601 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009602 fallthrough;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009603 case KVM_MP_STATE_RUNNABLE:
9604 vcpu->arch.apf.halted = false;
9605 break;
9606 case KVM_MP_STATE_INIT_RECEIVED:
9607 break;
9608 default:
9609 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009610 }
9611 return 1;
9612}
9613
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009614static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
9615{
Paolo Bonzini56083bd2020-04-17 10:32:53 -04009616 if (is_guest_mode(vcpu))
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08009617 kvm_check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01009618
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009619 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
9620 !vcpu->arch.apf.halted);
9621}
9622
Paolo Bonzini362c6982015-02-06 12:48:04 +01009623static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03009624{
9625 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009626 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03009627
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009628 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009629 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03009630
Paolo Bonzini362c6982015-02-06 12:48:04 +01009631 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02009632 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009633 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009634 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01009635 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009636 }
9637
Gleb Natapov09cec752009-03-23 15:11:44 +02009638 if (r <= 0)
9639 break;
9640
Marcelo Tosatti084071d2021-05-25 10:41:17 -03009641 kvm_clear_request(KVM_REQ_UNBLOCK, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02009642 if (kvm_cpu_has_pending_timer(vcpu))
9643 kvm_inject_pending_timer_irqs(vcpu);
9644
Matt Gingell782d4222015-11-16 15:26:00 -08009645 if (dm_request_for_irq_injection(vcpu) &&
9646 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02009647 r = 0;
9648 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02009649 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009650 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02009651 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02009652
Thomas Gleixnerf3020b82020-07-30 09:19:01 +02009653 if (__xfer_to_guest_mode_work_pending()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009654 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02009655 r = xfer_to_guest_mode_handle_work(vcpu);
9656 if (r)
9657 return r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009658 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03009659 }
9660 }
9661
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009662 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009663
9664 return r;
9665}
9666
Gleb Natapov716d51a2012-09-03 15:24:26 +03009667static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
9668{
9669 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009670
Gleb Natapov716d51a2012-09-03 15:24:26 +03009671 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07009672 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009673 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009674 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03009675}
9676
9677static int complete_emulated_pio(struct kvm_vcpu *vcpu)
9678{
9679 BUG_ON(!vcpu->arch.pio.count);
9680
9681 return complete_emulated_io(vcpu);
9682}
9683
Avi Kivityf78146b2012-04-18 19:22:47 +03009684/*
9685 * Implements the following, as a state machine:
9686 *
9687 * read:
9688 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009689 * for each mmio piece in the fragment
9690 * write gpa, len
9691 * exit
9692 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03009693 * execute insn
9694 *
9695 * write:
9696 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009697 * for each mmio piece in the fragment
9698 * write gpa, len
9699 * copy data
9700 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03009701 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03009702static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02009703{
9704 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03009705 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009706 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02009707
Gleb Natapov716d51a2012-09-03 15:24:26 +03009708 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02009709
Gleb Natapov716d51a2012-09-03 15:24:26 +03009710 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009711 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
9712 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009713 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009714 memcpy(frag->data, run->mmio.data, len);
9715
9716 if (frag->len <= 8) {
9717 /* Switch to the next fragment. */
9718 frag++;
9719 vcpu->mmio_cur_fragment++;
9720 } else {
9721 /* Go forward to the next mmio piece. */
9722 frag->data += len;
9723 frag->gpa += len;
9724 frag->len -= len;
9725 }
9726
Andrew Honiga08d3b32014-02-27 19:35:14 +01009727 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03009728 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02009729
9730 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02009731 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03009732 return 1;
9733 vcpu->mmio_read_completed = 1;
9734 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02009735 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009736
Gleb Natapov716d51a2012-09-03 15:24:26 +03009737 run->exit_reason = KVM_EXIT_MMIO;
9738 run->mmio.phys_addr = frag->gpa;
9739 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009740 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
9741 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009742 run->mmio.is_write = vcpu->mmio_is_write;
9743 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
9744 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02009745}
9746
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009747static void kvm_save_current_fpu(struct fpu *fpu)
9748{
9749 /*
9750 * If the target FPU state is not resident in the CPU registers, just
9751 * memcpy() from current, else save CPU state directly to the target.
9752 */
9753 if (test_thread_flag(TIF_NEED_FPU_LOAD))
9754 memcpy(&fpu->state, &current->thread.fpu.state,
9755 fpu_kernel_xstate_size);
9756 else
9757 copy_fpregs_to_fpstate(fpu);
9758}
9759
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009760/* Swap (qemu) user FPU context for the guest FPU context. */
9761static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
9762{
Rik van Riel5f409e22019-04-03 18:41:52 +02009763 fpregs_lock();
9764
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009765 kvm_save_current_fpu(vcpu->arch.user_fpu);
9766
Tom Lendackyed02b212020-12-10 11:10:01 -06009767 /*
9768 * Guests with protected state can't have it set by the hypervisor,
9769 * so skip trying to set it.
9770 */
9771 if (vcpu->arch.guest_fpu)
9772 /* PKRU is separately restored in kvm_x86_ops.run. */
9773 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
9774 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02009775
9776 fpregs_mark_activate();
9777 fpregs_unlock();
9778
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009779 trace_kvm_fpu(1);
9780}
9781
9782/* When vcpu_run ends, restore user space FPU context. */
9783static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
9784{
Rik van Riel5f409e22019-04-03 18:41:52 +02009785 fpregs_lock();
9786
Tom Lendackyed02b212020-12-10 11:10:01 -06009787 /*
9788 * Guests with protected state can't have it read by the hypervisor,
9789 * so skip trying to save it.
9790 */
9791 if (vcpu->arch.guest_fpu)
9792 kvm_save_current_fpu(vcpu->arch.guest_fpu);
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009793
Wanpeng Lid9a710e2019-07-22 12:26:21 +08009794 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02009795
9796 fpregs_mark_activate();
9797 fpregs_unlock();
9798
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009799 ++vcpu->stat.fpu_reload;
9800 trace_kvm_fpu(0);
9801}
9802
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009803int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009804{
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009805 struct kvm_run *kvm_run = vcpu->run;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009806 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009807
Christoffer Dallaccb7572017-12-04 21:35:25 +01009808 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01009809 kvm_sigset_activate(vcpu);
Chenyi Qiang15aad3b2020-11-06 17:03:13 +08009810 kvm_run->flags = 0;
Peter Xu5663d8f2017-12-12 17:15:02 +01009811 kvm_load_guest_fpu(vcpu);
9812
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009813 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02009814 if (kvm_run->immediate_exit) {
9815 r = -EINTR;
9816 goto out;
9817 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009818 kvm_vcpu_block(vcpu);
Jim Mattson4fe09bc2021-06-04 10:26:04 -07009819 if (kvm_apic_accept_events(vcpu) < 0) {
9820 r = 0;
9821 goto out;
9822 }
Radim Krčmář72875d82017-04-26 22:32:19 +02009823 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009824 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02009825 if (signal_pending(current)) {
9826 r = -EINTR;
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009827 kvm_run->exit_reason = KVM_EXIT_INTR;
Jan H. Schönherra0595002017-09-06 00:27:19 +02009828 ++vcpu->stat.signal_exits;
9829 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009830 goto out;
9831 }
9832
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009833 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
Ken Hofsass01643c52018-01-31 16:03:36 -08009834 r = -EINVAL;
9835 goto out;
9836 }
9837
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009838 if (kvm_run->kvm_dirty_regs) {
Ken Hofsass01643c52018-01-31 16:03:36 -08009839 r = sync_regs(vcpu);
9840 if (r != 0)
9841 goto out;
9842 }
9843
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009844 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02009845 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01009846 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
9847 r = -EINVAL;
9848 goto out;
9849 }
9850 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009851
Gleb Natapov716d51a2012-09-03 15:24:26 +03009852 if (unlikely(vcpu->arch.complete_userspace_io)) {
9853 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
9854 vcpu->arch.complete_userspace_io = NULL;
9855 r = cui(vcpu);
9856 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01009857 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03009858 } else
9859 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02009860
Paolo Bonzini460df4c2017-02-08 11:50:15 +01009861 if (kvm_run->immediate_exit)
9862 r = -EINTR;
9863 else
9864 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009865
9866out:
Peter Xu5663d8f2017-12-12 17:15:02 +01009867 kvm_put_guest_fpu(vcpu);
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009868 if (kvm_run->kvm_valid_regs)
Ken Hofsass01643c52018-01-31 16:03:36 -08009869 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03009870 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01009871 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009872
Christoffer Dallaccb7572017-12-04 21:35:25 +01009873 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009874 return r;
9875}
9876
Ken Hofsass01643c52018-01-31 16:03:36 -08009877static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009878{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009879 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
9880 /*
9881 * We are here if userspace calls get_regs() in the middle of
9882 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08009883 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009884 * that usually, but some bad designed PV devices (vmware
9885 * backdoor interface) need this to work
9886 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009887 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009888 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
9889 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07009890 regs->rax = kvm_rax_read(vcpu);
9891 regs->rbx = kvm_rbx_read(vcpu);
9892 regs->rcx = kvm_rcx_read(vcpu);
9893 regs->rdx = kvm_rdx_read(vcpu);
9894 regs->rsi = kvm_rsi_read(vcpu);
9895 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02009896 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07009897 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009898#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07009899 regs->r8 = kvm_r8_read(vcpu);
9900 regs->r9 = kvm_r9_read(vcpu);
9901 regs->r10 = kvm_r10_read(vcpu);
9902 regs->r11 = kvm_r11_read(vcpu);
9903 regs->r12 = kvm_r12_read(vcpu);
9904 regs->r13 = kvm_r13_read(vcpu);
9905 regs->r14 = kvm_r14_read(vcpu);
9906 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009907#endif
9908
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009909 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02009910 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08009911}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009912
Ken Hofsass01643c52018-01-31 16:03:36 -08009913int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
9914{
9915 vcpu_load(vcpu);
9916 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01009917 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009918 return 0;
9919}
9920
Ken Hofsass01643c52018-01-31 16:03:36 -08009921static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009922{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009923 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
9924 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
9925
Sean Christophersonde3cd112019-04-30 10:36:17 -07009926 kvm_rax_write(vcpu, regs->rax);
9927 kvm_rbx_write(vcpu, regs->rbx);
9928 kvm_rcx_write(vcpu, regs->rcx);
9929 kvm_rdx_write(vcpu, regs->rdx);
9930 kvm_rsi_write(vcpu, regs->rsi);
9931 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02009932 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07009933 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009934#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07009935 kvm_r8_write(vcpu, regs->r8);
9936 kvm_r9_write(vcpu, regs->r9);
9937 kvm_r10_write(vcpu, regs->r10);
9938 kvm_r11_write(vcpu, regs->r11);
9939 kvm_r12_write(vcpu, regs->r12);
9940 kvm_r13_write(vcpu, regs->r13);
9941 kvm_r14_write(vcpu, regs->r14);
9942 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009943#endif
9944
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009945 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08009946 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009947
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02009948 vcpu->arch.exception.pending = false;
9949
Avi Kivity3842d132010-07-27 12:30:24 +03009950 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08009951}
Avi Kivity3842d132010-07-27 12:30:24 +03009952
Ken Hofsass01643c52018-01-31 16:03:36 -08009953int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
9954{
9955 vcpu_load(vcpu);
9956 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01009957 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009958 return 0;
9959}
9960
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009961void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
9962{
9963 struct kvm_segment cs;
9964
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009965 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009966 *db = cs.db;
9967 *l = cs.l;
9968}
9969EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
9970
Ken Hofsass01643c52018-01-31 16:03:36 -08009971static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009972{
Gleb Natapov89a27f42010-02-16 10:51:48 +02009973 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009974
Tom Lendacky52657132020-12-10 11:09:59 -06009975 if (vcpu->arch.guest_state_protected)
9976 goto skip_protected_regs;
9977
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009978 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9979 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9980 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9981 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9982 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9983 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009984
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009985 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9986 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009987
Jason Baronb36464772021-01-14 22:27:56 -05009988 static_call(kvm_x86_get_idt)(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009989 sregs->idt.limit = dt.size;
9990 sregs->idt.base = dt.address;
Jason Baronb36464772021-01-14 22:27:56 -05009991 static_call(kvm_x86_get_gdt)(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009992 sregs->gdt.limit = dt.size;
9993 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009994
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009995 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009996 sregs->cr3 = kvm_read_cr3(vcpu);
Tom Lendacky52657132020-12-10 11:09:59 -06009997
9998skip_protected_regs:
9999 sregs->cr0 = kvm_read_cr0(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +020010000 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +020010001 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +020010002 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010003 sregs->apic_base = kvm_get_apic_base(vcpu);
10004
Jordan Borgner0e96f312018-10-28 12:58:28 +000010005 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010006
Liran Alon04140b42018-03-23 03:01:31 +030010007 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +030010008 set_bit(vcpu->arch.interrupt.nr,
10009 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -080010010}
Gleb Natapov16d7a192009-04-21 17:45:10 +030010011
Ken Hofsass01643c52018-01-31 16:03:36 -080010012int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
10013 struct kvm_sregs *sregs)
10014{
10015 vcpu_load(vcpu);
10016 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +010010017 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010018 return 0;
10019}
10020
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -030010021int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
10022 struct kvm_mp_state *mp_state)
10023{
Jim Mattson4fe09bc2021-06-04 10:26:04 -070010024 int r;
10025
Christoffer Dallfd232562017-12-04 21:35:30 +010010026 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -080010027 if (kvm_mpx_supported())
10028 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +010010029
Jim Mattson4fe09bc2021-06-04 10:26:04 -070010030 r = kvm_apic_accept_events(vcpu);
10031 if (r < 0)
10032 goto out;
10033 r = 0;
10034
Tom Lendacky647daca2021-01-04 14:20:01 -060010035 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED ||
10036 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) &&
10037 vcpu->arch.pv.pv_unhalted)
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +053010038 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
10039 else
10040 mp_state->mp_state = vcpu->arch.mp_state;
10041
Jim Mattson4fe09bc2021-06-04 10:26:04 -070010042out:
Sean Christophersonf958bd22019-12-09 12:19:31 -080010043 if (kvm_mpx_supported())
10044 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +010010045 vcpu_put(vcpu);
Jim Mattson4fe09bc2021-06-04 10:26:04 -070010046 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -030010047}
10048
10049int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
10050 struct kvm_mp_state *mp_state)
10051{
Christoffer Dalle83dff52017-12-04 21:35:31 +010010052 int ret = -EINVAL;
10053
10054 vcpu_load(vcpu);
10055
Paolo Bonzinibce87cc2016-01-08 13:48:51 +010010056 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +010010057 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +010010058 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +010010059
Liran Alon27cbe7d2019-11-11 11:16:40 +020010060 /*
10061 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
10062 * INIT state; latched init should be reported using
10063 * KVM_SET_VCPU_EVENTS, so reject it here.
10064 */
10065 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +010010066 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
10067 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +010010068 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +010010069
Jan Kiszka66450a22013-03-13 12:42:34 +010010070 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
10071 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
10072 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
10073 } else
10074 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +030010075 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +010010076
10077 ret = 0;
10078out:
10079 vcpu_put(vcpu);
10080 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -030010081}
10082
Kevin Wolf7f3d35f2012-02-08 14:34:38 +010010083int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
10084 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010085{
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010086 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +030010087 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010088
Mohammed Gamal8ec47222010-08-16 00:47:01 +030010089 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +020010090
Kevin Wolf7f3d35f2012-02-08 14:34:38 +010010091 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +090010092 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -070010093 if (ret) {
10094 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10095 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
10096 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -070010097 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -070010098 }
Gleb Natapovc6975182010-02-18 12:15:01 +020010099
Takuya Yoshikawa9d741912011-05-29 21:53:48 +090010100 kvm_rip_write(vcpu, ctxt->eip);
10101 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -070010102 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +020010103}
10104EXPORT_SYMBOL_GPL(kvm_task_switch);
10105
Sean Christophersonee69c922020-10-06 18:44:16 -070010106static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -050010107{
Tianyu Lan37b95952018-01-16 17:34:07 +080010108 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -050010109 /*
10110 * When EFER.LME and CR0.PG are set, the processor is in
10111 * 64-bit mode (though maybe in a 32-bit code segment).
10112 * CR4.PAE and EFER.LMA must be set.
10113 */
Sean Christophersonee69c922020-10-06 18:44:16 -070010114 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA))
10115 return false;
Sean Christophersonca29e142021-02-03 16:01:12 -080010116 if (kvm_vcpu_is_illegal_gpa(vcpu, sregs->cr3))
Paolo Bonzinic1c35cf2020-11-13 08:30:38 -050010117 return false;
Lan Tianyuf2981032017-12-14 03:01:52 -050010118 } else {
10119 /*
10120 * Not in 64-bit mode: EFER.LMA is clear and the code
10121 * segment cannot be 64-bit.
10122 */
10123 if (sregs->efer & EFER_LMA || sregs->cs.l)
Sean Christophersonee69c922020-10-06 18:44:16 -070010124 return false;
Lan Tianyuf2981032017-12-14 03:01:52 -050010125 }
10126
Sean Christophersonee69c922020-10-06 18:44:16 -070010127 return kvm_is_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -050010128}
10129
Ken Hofsass01643c52018-01-31 16:03:36 -080010130static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010131{
Jan Kiszka58cb6282014-01-24 16:48:44 +010010132 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010133 int mmu_reset_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +080010134 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +020010135 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +010010136 int ret = -EINVAL;
10137
Sean Christophersonee69c922020-10-06 18:44:16 -070010138 if (!kvm_is_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -080010139 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -050010140
Jim Mattsond3802282017-08-10 10:14:13 -070010141 apic_base_msr.data = sregs->apic_base;
10142 apic_base_msr.host_initiated = true;
10143 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +010010144 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +010010145
Tom Lendacky52657132020-12-10 11:09:59 -060010146 if (vcpu->arch.guest_state_protected)
10147 goto skip_protected_regs;
10148
Gleb Natapov89a27f42010-02-16 10:51:48 +020010149 dt.size = sregs->idt.limit;
10150 dt.address = sregs->idt.base;
Jason Baronb36464772021-01-14 22:27:56 -050010151 static_call(kvm_x86_set_idt)(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +020010152 dt.size = sregs->gdt.limit;
10153 dt.address = sregs->gdt.base;
Jason Baronb36464772021-01-14 22:27:56 -050010154 static_call(kvm_x86_set_gdt)(vcpu, &dt);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010155
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010156 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +020010157 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +020010158 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -070010159 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010160
Avi Kivity2d3ad1f2008-02-24 11:20:43 +020010161 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010162
Avi Kivityf6801df2010-01-21 15:31:50 +020010163 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Jason Baronb36464772021-01-14 22:27:56 -050010164 static_call(kvm_x86_set_efer)(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010165
Avi Kivity4d4ec082009-12-29 18:07:30 +020010166 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Jason Baronb36464772021-01-14 22:27:56 -050010167 static_call(kvm_x86_set_cr0)(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +000010168 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010169
Avi Kivityfc78f512009-12-07 12:16:48 +020010170 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Jason Baronb36464772021-01-14 22:27:56 -050010171 static_call(kvm_x86_set_cr4)(vcpu, sregs->cr4);
Xiao Guangrong63f42e02011-01-12 15:39:18 +080010172
10173 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +020010174 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +020010175 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -020010176 mmu_reset_needed = 1;
10177 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +080010178 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010179
10180 if (mmu_reset_needed)
10181 kvm_mmu_reset_context(vcpu);
10182
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +020010183 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
10184 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
10185 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
10186 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
10187 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
10188 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010189
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +020010190 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
10191 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010192
Mikhail Ershov5f0269f2009-08-03 14:58:25 +030010193 update_cr8_intercept(vcpu);
10194
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -030010195 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +030010196 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -030010197 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +020010198 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -030010199 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
10200
Tom Lendacky52657132020-12-10 11:09:59 -060010201skip_protected_regs:
10202 max_bits = KVM_NR_INTERRUPTS;
10203 pending_vec = find_first_bit(
10204 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
10205 if (pending_vec < max_bits) {
10206 kvm_queue_interrupt(vcpu, pending_vec, false);
10207 pr_debug("Set back pending irq %d\n", pending_vec);
10208 }
10209
Avi Kivity3842d132010-07-27 12:30:24 +030010210 kvm_make_request(KVM_REQ_EVENT, vcpu);
10211
Christoffer Dallb4ef9d42017-12-04 21:35:29 +010010212 ret = 0;
10213out:
Ken Hofsass01643c52018-01-31 16:03:36 -080010214 return ret;
10215}
10216
10217int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
10218 struct kvm_sregs *sregs)
10219{
10220 int ret;
10221
10222 vcpu_load(vcpu);
10223 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +010010224 vcpu_put(vcpu);
10225 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010226}
10227
Jan Kiszkad0bfb942008-12-15 13:52:10 +010010228int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
10229 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010230{
Jan Kiszka355be0b2009-10-03 00:31:21 +020010231 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010232 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010233
Tom Lendacky8d4846b2020-12-10 11:09:43 -060010234 if (vcpu->arch.guest_state_protected)
10235 return -EINVAL;
10236
Christoffer Dall66b56562017-12-04 21:35:33 +010010237 vcpu_load(vcpu);
10238
Jan Kiszka4f926bf22009-10-30 12:46:59 +010010239 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
10240 r = -EBUSY;
10241 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +030010242 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +010010243 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
10244 kvm_queue_exception(vcpu, DB_VECTOR);
10245 else
10246 kvm_queue_exception(vcpu, BP_VECTOR);
10247 }
10248
Jan Kiszka91586a32009-10-05 13:07:21 +020010249 /*
10250 * Read rflags as long as potentially injected trace flags are still
10251 * filtered out.
10252 */
10253 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +020010254
10255 vcpu->guest_debug = dbg->control;
10256 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
10257 vcpu->guest_debug = 0;
10258
10259 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010260 for (i = 0; i < KVM_NR_DB_REGS; ++i)
10261 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +020010262 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010263 } else {
10264 for (i = 0; i < KVM_NR_DB_REGS; i++)
10265 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010266 }
Jan Kiszkac8639012012-09-21 05:42:55 +020010267 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +010010268
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010269 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Yuan Yaoe87e46d2021-05-26 14:38:28 +080010270 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010271
Jan Kiszka91586a32009-10-05 13:07:21 +020010272 /*
10273 * Trigger an rflags update that will inject or remove the trace
10274 * flags.
10275 */
10276 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +010010277
Jason Baronb36464772021-01-14 22:27:56 -050010278 static_call(kvm_x86_update_exception_bitmap)(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +020010279
Jan Kiszka4f926bf22009-10-30 12:46:59 +010010280 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010281
Avi Kivity2122ff52010-05-13 11:25:04 +030010282out:
Christoffer Dall66b56562017-12-04 21:35:33 +010010283 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -050010284 return r;
10285}
10286
10287/*
Zhang Xiantao8b006792007-11-16 13:05:55 +080010288 * Translate a guest virtual address to a guest physical address.
10289 */
10290int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
10291 struct kvm_translation *tr)
10292{
10293 unsigned long vaddr = tr->linear_address;
10294 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -020010295 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +080010296
Christoffer Dall1da5b612017-12-04 21:35:32 +010010297 vcpu_load(vcpu);
10298
Marcelo Tosattif656ce02009-12-23 14:35:25 -020010299 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +020010300 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -020010301 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +080010302 tr->physical_address = gpa;
10303 tr->valid = gpa != UNMAPPED_GVA;
10304 tr->writeable = 1;
10305 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +080010306
Christoffer Dall1da5b612017-12-04 21:35:32 +010010307 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +080010308 return 0;
10309}
10310
Hollis Blanchardd0752062007-10-31 17:24:25 -050010311int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
10312{
Christoffer Dall13931232017-12-04 21:35:34 +010010313 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010314
Tom Lendackyed02b212020-12-10 11:10:01 -060010315 if (!vcpu->arch.guest_fpu)
10316 return 0;
10317
Christoffer Dall13931232017-12-04 21:35:34 +010010318 vcpu_load(vcpu);
10319
Marc Orrb666a4b2018-11-06 14:53:56 -080010320 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010321 memcpy(fpu->fpr, fxsave->st_space, 128);
10322 fpu->fcw = fxsave->cwd;
10323 fpu->fsw = fxsave->swd;
10324 fpu->ftwx = fxsave->twd;
10325 fpu->last_opcode = fxsave->fop;
10326 fpu->last_ip = fxsave->rip;
10327 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +000010328 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -050010329
Christoffer Dall13931232017-12-04 21:35:34 +010010330 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -050010331 return 0;
10332}
10333
10334int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
10335{
Christoffer Dall6a96bc72017-12-04 21:35:35 +010010336 struct fxregs_state *fxsave;
10337
Tom Lendackyed02b212020-12-10 11:10:01 -060010338 if (!vcpu->arch.guest_fpu)
10339 return 0;
10340
Christoffer Dall6a96bc72017-12-04 21:35:35 +010010341 vcpu_load(vcpu);
10342
Marc Orrb666a4b2018-11-06 14:53:56 -080010343 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010344
Hollis Blanchardd0752062007-10-31 17:24:25 -050010345 memcpy(fxsave->st_space, fpu->fpr, 128);
10346 fxsave->cwd = fpu->fcw;
10347 fxsave->swd = fpu->fsw;
10348 fxsave->twd = fpu->ftwx;
10349 fxsave->fop = fpu->last_opcode;
10350 fxsave->rip = fpu->last_ip;
10351 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +000010352 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -050010353
Christoffer Dall6a96bc72017-12-04 21:35:35 +010010354 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -050010355 return 0;
10356}
10357
Ken Hofsass01643c52018-01-31 16:03:36 -080010358static void store_regs(struct kvm_vcpu *vcpu)
10359{
10360 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
10361
10362 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
10363 __get_regs(vcpu, &vcpu->run->s.regs.regs);
10364
10365 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
10366 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
10367
10368 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
10369 kvm_vcpu_ioctl_x86_get_vcpu_events(
10370 vcpu, &vcpu->run->s.regs.events);
10371}
10372
10373static int sync_regs(struct kvm_vcpu *vcpu)
10374{
10375 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
10376 return -EINVAL;
10377
10378 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
10379 __set_regs(vcpu, &vcpu->run->s.regs.regs);
10380 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
10381 }
10382 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
10383 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
10384 return -EINVAL;
10385 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
10386 }
10387 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
10388 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
10389 vcpu, &vcpu->run->s.regs.events))
10390 return -EINVAL;
10391 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
10392 }
10393
10394 return 0;
10395}
10396
Ingo Molnar0ee6a512015-04-27 06:58:22 +020010397static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -050010398{
Tom Lendackyed02b212020-12-10 11:10:01 -060010399 if (!vcpu->arch.guest_fpu)
10400 return;
10401
Marc Orrb666a4b2018-11-06 14:53:56 -080010402 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +020010403 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -080010404 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +010010405 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010406
Dexuan Cui2acf9232010-06-10 11:27:12 +080010407 /*
10408 * Ensure guest xcr0 is valid for loading
10409 */
Dave Hansend91cab72015-09-02 16:31:26 -070010410 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +080010411
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010412 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -050010413}
Hollis Blanchardd0752062007-10-31 17:24:25 -050010414
Tom Lendackyed02b212020-12-10 11:10:01 -060010415void kvm_free_guest_fpu(struct kvm_vcpu *vcpu)
10416{
10417 if (vcpu->arch.guest_fpu) {
10418 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
10419 vcpu->arch.guest_fpu = NULL;
10420 }
10421}
10422EXPORT_SYMBOL_GPL(kvm_free_guest_fpu);
10423
Sean Christopherson897cc382019-12-18 13:55:09 -080010424int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010425{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010010426 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -080010427 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
10428 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +080010429
Sean Christopherson897cc382019-12-18 13:55:09 -080010430 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +020010431}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010432
Sean Christophersone529ef62019-12-18 13:55:15 -080010433int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +020010434{
Sean Christopherson95a0d012019-12-18 13:55:23 -080010435 struct page *page;
10436 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010437
Sean Christopherson95a0d012019-12-18 13:55:23 -080010438 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
10439 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
10440 else
10441 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
10442
Sean Christopherson95a0d012019-12-18 13:55:23 -080010443 r = kvm_mmu_create(vcpu);
10444 if (r < 0)
10445 return r;
10446
10447 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -080010448 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
10449 if (r < 0)
10450 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -060010451 if (kvm_apicv_activated(vcpu->kvm))
10452 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010453 } else
Cun Li6e4e3b42021-01-11 23:24:35 +080010454 static_branch_inc(&kvm_has_noapic_vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010455
10456 r = -ENOMEM;
10457
Shakeel Butt93bb59c2020-12-18 14:01:38 -080010458 page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010459 if (!page)
10460 goto fail_free_lapic;
10461 vcpu->arch.pio_data = page_address(page);
10462
10463 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
10464 GFP_KERNEL_ACCOUNT);
10465 if (!vcpu->arch.mce_banks)
10466 goto fail_free_pio_data;
10467 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
10468
10469 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
10470 GFP_KERNEL_ACCOUNT))
10471 goto fail_free_mce_banks;
10472
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010473 if (!alloc_emulate_ctxt(vcpu))
10474 goto free_wbinvd_dirty_mask;
10475
Sean Christopherson95a0d012019-12-18 13:55:23 -080010476 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
10477 GFP_KERNEL_ACCOUNT);
10478 if (!vcpu->arch.user_fpu) {
10479 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010480 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010481 }
10482
10483 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
10484 GFP_KERNEL_ACCOUNT);
10485 if (!vcpu->arch.guest_fpu) {
10486 pr_err("kvm: failed to allocate vcpu's fpu\n");
10487 goto free_user_fpu;
10488 }
10489 fx_init(vcpu);
10490
Sean Christopherson95a0d012019-12-18 13:55:23 -080010491 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
Sean Christophersona8ac8642021-02-03 16:01:15 -080010492 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010493
10494 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
10495
10496 kvm_async_pf_hash_reset(vcpu);
10497 kvm_pmu_init(vcpu);
10498
10499 vcpu->arch.pending_external_vector = -1;
10500 vcpu->arch.preempted_in_kernel = false;
10501
Jason Baronb36464772021-01-14 22:27:56 -050010502 r = static_call(kvm_x86_vcpu_create)(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010503 if (r)
10504 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -080010505
Sean Christopherson0cf91352019-03-07 15:43:02 -080010506 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -070010507 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +080010508 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +010010509 vcpu_load(vcpu);
Ilias Stamatis1ab92872021-06-07 11:54:38 +010010510 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010511 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +020010512 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010513 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +010010514 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010515
10516free_guest_fpu:
Tom Lendackyed02b212020-12-10 11:10:01 -060010517 kvm_free_guest_fpu(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010518free_user_fpu:
10519 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010520free_emulate_ctxt:
10521 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010522free_wbinvd_dirty_mask:
10523 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
10524fail_free_mce_banks:
10525 kfree(vcpu->arch.mce_banks);
10526fail_free_pio_data:
10527 free_page((unsigned long)vcpu->arch.pio_data);
10528fail_free_lapic:
10529 kvm_free_lapic(vcpu);
10530fail_mmu_destroy:
10531 kvm_mmu_destroy(vcpu);
10532 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010533}
10534
Dominik Dingel31928aa2014-12-04 15:47:07 +010010535void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010536{
Andrew Jones332967a2014-02-28 12:52:55 +010010537 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010538
Christoffer Dallec7660c2017-12-04 21:35:23 +010010539 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +010010540 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +010010541 vcpu_load(vcpu);
Paolo Bonzini0c899c22020-09-24 14:45:27 +020010542 kvm_synchronize_tsc(vcpu, 0);
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010543 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010544
10545 /* poll control enabled by default */
10546 vcpu->arch.msr_kvm_poll_control = 1;
10547
Christoffer Dallec7660c2017-12-04 21:35:23 +010010548 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010549
Wanpeng Lib34de572020-02-28 11:18:41 +080010550 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
10551 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
10552 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -020010553}
10554
Hollis Blanchardd40ccc62007-11-19 14:04:43 -060010555void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010556{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +010010557 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -080010558 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +020010559
Paolo Bonzini4cbc4182020-01-30 18:47:38 +010010560 kvm_release_pfn(cache->pfn, cache->dirty, cache);
10561
Sean Christopherson50b143e2019-12-18 13:55:07 -080010562 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010563
Jason Baronb36464772021-01-14 22:27:56 -050010564 static_call(kvm_x86_vcpu_free)(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -080010565
Sean Christophersonc9b8b072020-02-18 15:29:48 -080010566 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -080010567 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
10568 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Tom Lendackyed02b212020-12-10 11:10:01 -060010569 kvm_free_guest_fpu(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010570
10571 kvm_hv_vcpu_uninit(vcpu);
10572 kvm_pmu_destroy(vcpu);
10573 kfree(vcpu->arch.mce_banks);
10574 kvm_free_lapic(vcpu);
10575 idx = srcu_read_lock(&vcpu->kvm->srcu);
10576 kvm_mmu_destroy(vcpu);
10577 srcu_read_unlock(&vcpu->kvm->srcu, idx);
10578 free_page((unsigned long)vcpu->arch.pio_data);
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +020010579 kvfree(vcpu->arch.cpuid_entries);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010580 if (!lapic_in_kernel(vcpu))
Cun Li6e4e3b42021-01-11 23:24:35 +080010581 static_branch_dec(&kvm_has_noapic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010582}
10583
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010584void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010585{
Radim Krčmářb7e31be2018-03-01 15:24:25 +010010586 kvm_lapic_reset(vcpu, init_event);
10587
Paolo Bonzinie69fab52015-06-04 10:44:44 +020010588 vcpu->arch.hflags = 0;
10589
Paolo Bonzinic43203c2016-06-01 22:26:00 +020010590 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +020010591 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +030010592 atomic_set(&vcpu->arch.nmi_queued, 0);
10593 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +020010594 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +030010595 kvm_clear_interrupt_queue(vcpu);
10596 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +020010597
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010598 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +030010599 kvm_update_dr0123(vcpu);
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +080010600 vcpu->arch.dr6 = DR6_ACTIVE_LOW;
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010601 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +020010602 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010603
Nadav Amit11190222015-04-02 03:10:38 +030010604 vcpu->arch.cr2 = 0;
10605
Avi Kivity3842d132010-07-27 12:30:24 +030010606 kvm_make_request(KVM_REQ_EVENT, vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010607 vcpu->arch.apf.msr_en_val = 0;
10608 vcpu->arch.apf.msr_int_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -040010609 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +030010610
Glauber Costa12f9a482011-02-01 14:16:40 -050010611 kvmclock_reset(vcpu);
10612
Gleb Natapovaf585b92010-10-14 11:22:46 +020010613 kvm_clear_async_pf_completion_queue(vcpu);
10614 kvm_async_pf_hash_reset(vcpu);
10615 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010616
Tom Lendackyed02b212020-12-10 11:10:01 -060010617 if (vcpu->arch.guest_fpu && kvm_mpx_supported()) {
Wanpeng Lia554d202017-10-11 05:10:19 -070010618 void *mpx_state_buffer;
10619
10620 /*
10621 * To avoid have the INIT path from kvm_apic_has_events() that be
10622 * called with loaded FPU and does not let userspace fix the state.
10623 */
Rik van Rielf775b132017-11-14 16:54:23 -050010624 if (init_event)
10625 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -080010626 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +020010627 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -070010628 if (mpx_state_buffer)
10629 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -080010630 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +020010631 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -070010632 if (mpx_state_buffer)
10633 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -050010634 if (init_event)
10635 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -070010636 }
10637
Paolo Bonzini64d60672015-05-07 11:36:11 +020010638 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010639 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +020010640 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -070010641
Kyle Hueydb2336a2017-03-20 01:16:28 -070010642 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -070010643
10644 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +020010645 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020010646
Julian Stecklina66f7b722012-12-05 15:26:19 +010010647 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
10648 vcpu->arch.regs_avail = ~0;
10649 vcpu->arch.regs_dirty = ~0;
10650
Wanpeng Lia554d202017-10-11 05:10:19 -070010651 vcpu->arch.ia32_xss = 0;
10652
Jason Baronb36464772021-01-14 22:27:56 -050010653 static_call(kvm_x86_vcpu_reset)(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010654}
10655
Paolo Bonzini2b4a2732014-11-24 14:35:24 +010010656void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +010010657{
10658 struct kvm_segment cs;
10659
10660 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
10661 cs.selector = vector << 8;
10662 cs.base = vector << 12;
10663 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
10664 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010665}
Tom Lendacky647daca2021-01-04 14:20:01 -060010666EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010667
Radim Krčmář13a34e02014-08-28 15:13:03 +020010668int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010669{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -100010670 struct kvm *kvm;
10671 struct kvm_vcpu *vcpu;
10672 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010673 int ret;
10674 u64 local_tsc;
10675 u64 max_tsc = 0;
10676 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +030010677
Sean Christopherson7e34fbd2020-09-23 11:03:55 -070010678 kvm_user_return_msr_cpu_online();
Jason Baronb36464772021-01-14 22:27:56 -050010679 ret = static_call(kvm_x86_hardware_enable)();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010680 if (ret != 0)
10681 return ret;
10682
Andy Lutomirski4ea16362015-06-25 18:44:07 +020010683 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010010684 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010685 list_for_each_entry(kvm, &vm_list, vm_list) {
10686 kvm_for_each_vcpu(i, vcpu, kvm) {
10687 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +080010688 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010689 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
10690 backwards_tsc = true;
10691 if (vcpu->arch.last_host_tsc > max_tsc)
10692 max_tsc = vcpu->arch.last_host_tsc;
10693 }
10694 }
10695 }
10696
10697 /*
10698 * Sometimes, even reliable TSCs go backwards. This happens on
10699 * platforms that reset TSC during suspend or hibernate actions, but
10700 * maintain synchronization. We must compensate. Fortunately, we can
10701 * detect that condition here, which happens early in CPU bringup,
10702 * before any KVM threads can be running. Unfortunately, we can't
10703 * bring the TSCs fully up to date with real time, as we aren't yet far
10704 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020010705 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010706 * variables that haven't been updated yet.
10707 *
10708 * So we simply find the maximum observed TSC above, then record the
10709 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
10710 * the adjustment will be applied. Note that we accumulate
10711 * adjustments, in case multiple suspend cycles happen before some VCPU
10712 * gets a chance to run again. In the event that no KVM threads get a
10713 * chance to run, we will miss the entire elapsed period, as we'll have
10714 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
10715 * loose cycle time. This isn't too big a deal, since the loss will be
10716 * uniform across all VCPUs (not to mention the scenario is extremely
10717 * unlikely). It is possible that a second hibernate recovery happens
10718 * much faster than a first, causing the observed TSC here to be
10719 * smaller; this would require additional padding adjustment, which is
10720 * why we set last_host_tsc to the local tsc observed here.
10721 *
10722 * N.B. - this code below runs only on platforms with reliable TSC,
10723 * as that is the only way backwards_tsc is set above. Also note
10724 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
10725 * have the same delta_cyc adjustment applied if backwards_tsc
10726 * is detected. Note further, this adjustment is only done once,
10727 * as we reset last_host_tsc on all VCPUs to stop this from being
10728 * called multiple times (one for each physical CPU bringup).
10729 *
Guo Chao4a969982012-06-28 15:17:27 +080010730 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010731 * will be compensated by the logic in vcpu_load, which sets the TSC to
10732 * catchup mode. This will catchup all VCPUs to real time, but cannot
10733 * guarantee that they stay in perfect synchronization.
10734 */
10735 if (backwards_tsc) {
10736 u64 delta_cyc = max_tsc - local_tsc;
10737 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +020010738 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010739 kvm_for_each_vcpu(i, vcpu, kvm) {
10740 vcpu->arch.tsc_offset_adjustment += delta_cyc;
10741 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +080010742 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010743 }
10744
10745 /*
10746 * We have to disable TSC offset matching.. if you were
10747 * booting a VM while issuing an S4 host suspend....
10748 * you may have some problem. Solving this issue is
10749 * left as an exercise to the reader.
10750 */
10751 kvm->arch.last_tsc_nsec = 0;
10752 kvm->arch.last_tsc_write = 0;
10753 }
10754
10755 }
10756 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010757}
10758
Radim Krčmář13a34e02014-08-28 15:13:03 +020010759void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010760{
Jason Baronb36464772021-01-14 22:27:56 -050010761 static_call(kvm_x86_hardware_disable)();
Radim Krčmář13a34e02014-08-28 15:13:03 +020010762 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010763}
10764
Sean Christophersonb9904082020-03-21 13:25:55 -070010765int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010766{
Sean Christophersond008dfd2020-03-21 13:25:56 -070010767 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030010768 int r;
10769
Sean Christopherson91661982020-03-02 15:57:06 -080010770 rdmsrl_safe(MSR_EFER, &host_efer);
10771
Paolo Bonzini408e9a32020-03-05 16:11:56 +010010772 if (boot_cpu_has(X86_FEATURE_XSAVES))
10773 rdmsrl(MSR_IA32_XSS, host_xss);
10774
Sean Christophersond008dfd2020-03-21 13:25:56 -070010775 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030010776 if (r != 0)
10777 return r;
10778
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010779 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
Jason Baronb36464772021-01-14 22:27:56 -050010780 kvm_ops_static_call_update();
Sean Christopherson69c6f692020-03-21 13:25:59 -070010781
Paolo Bonzini408e9a32020-03-05 16:11:56 +010010782 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
10783 supported_xss = 0;
10784
Paolo Bonzini139f7422020-05-05 09:40:46 -040010785#define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f)
10786 cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_);
10787#undef __kvm_cpu_cap_has
Sean Christophersonb11306b2019-12-10 14:44:13 -080010788
Haozhong Zhang35181e82015-10-20 15:39:03 +080010789 if (kvm_has_tsc_control) {
10790 /*
10791 * Make sure the user can only configure tsc_khz values that
10792 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -030010793 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +080010794 * be 1 on all machines.
10795 */
10796 u64 max = min(0x7fffffffULL,
10797 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
10798 kvm_max_guest_tsc_khz = max;
10799
Haozhong Zhangad7218832015-10-20 15:39:02 +080010800 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +080010801 }
Haozhong Zhangad7218832015-10-20 15:39:02 +080010802
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030010803 kvm_init_msr_list();
10804 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010805}
10806
10807void kvm_arch_hardware_unsetup(void)
10808{
Jason Baronb36464772021-01-14 22:27:56 -050010809 static_call(kvm_x86_hardware_unsetup)();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010810}
10811
Sean Christophersonb9904082020-03-21 13:25:55 -070010812int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010813{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080010814 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -070010815 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080010816
10817 WARN_ON(!irqs_disabled());
10818
Paolo Bonzini139f7422020-05-05 09:40:46 -040010819 if (__cr4_reserved_bits(cpu_has, c) !=
10820 __cr4_reserved_bits(cpu_has, &boot_cpu_data))
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080010821 return -EIO;
10822
Sean Christophersond008dfd2020-03-21 13:25:56 -070010823 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010824}
10825
Paolo Bonzinid71ba782015-07-29 11:56:48 +020010826bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
10827{
10828 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
10829}
10830EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
10831
10832bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
10833{
10834 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
10835}
10836
Cun Li6e4e3b42021-01-11 23:24:35 +080010837__read_mostly DEFINE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu);
10838EXPORT_SYMBOL_GPL(kvm_has_noapic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +030010839
Radim Krčmáře790d9e2014-08-21 18:08:05 +020010840void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
10841{
Like Xub35e5542019-10-27 18:52:43 +080010842 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
10843
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010844 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +080010845 if (pmu->version && unlikely(pmu->event_count)) {
10846 pmu->need_cleanup = true;
10847 kvm_make_request(KVM_REQ_PMU, vcpu);
10848 }
Jason Baronb36464772021-01-14 22:27:56 -050010849 static_call(kvm_x86_sched_in)(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +020010850}
10851
Sean Christopherson562b6b02020-01-26 16:41:13 -080010852void kvm_arch_free_vm(struct kvm *kvm)
10853{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +010010854 kfree(to_kvm_hv(kvm)->hv_pa_pg);
Sean Christopherson562b6b02020-01-26 16:41:13 -080010855 vfree(kvm);
10856}
10857
10858
Carsten Ottee08b9632012-01-04 10:25:20 +010010859int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010860{
Carsten Ottee08b9632012-01-04 10:25:20 +010010861 if (type)
10862 return -EINVAL;
10863
Paolo Bonzini6ef768f2014-11-20 13:45:31 +010010864 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +080010865 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -070010866 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +010010867 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030010868 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010869 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010870
Sheng Yang5550af42008-10-15 20:15:06 +080010871 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
10872 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -060010873 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
10874 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
10875 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +080010876
Jan Kiszka038f8c12011-02-04 10:49:11 +010010877 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +030010878 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -020010879 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
10880
Paolo Bonzini8171cd62020-01-22 14:36:09 +010010881 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -020010882 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +010010883
Drew Schmitt6fbbde92018-08-20 10:32:15 -070010884 kvm->arch.guest_can_read_msr_platform_info = true;
10885
Andrew Jones7e44e442014-02-28 12:52:54 +010010886 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +010010887 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +010010888
Vitaly Kuznetsov4651fc52021-06-09 17:09:09 +020010889 kvm_apicv_init(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +030010890 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +080010891 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +080010892 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +080010893
Jason Baronb36464772021-01-14 22:27:56 -050010894 return static_call(kvm_x86_vm_init)(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010895}
10896
Junaid Shahid1aa9b952019-11-04 20:26:00 +010010897int kvm_arch_post_init_vm(struct kvm *kvm)
10898{
10899 return kvm_mmu_post_init_vm(kvm);
10900}
10901
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010902static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
10903{
Christoffer Dallec7660c2017-12-04 21:35:23 +010010904 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010905 kvm_mmu_unload(vcpu);
10906 vcpu_put(vcpu);
10907}
10908
10909static void kvm_free_vcpus(struct kvm *kvm)
10910{
10911 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010912 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010913
10914 /*
10915 * Unpin any mmu pages first.
10916 */
Gleb Natapovaf585b92010-10-14 11:22:46 +020010917 kvm_for_each_vcpu(i, vcpu, kvm) {
10918 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010919 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010920 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010921 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -080010922 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010923
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010924 mutex_lock(&kvm->lock);
10925 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
10926 kvm->vcpus[i] = NULL;
10927
10928 atomic_set(&kvm->online_vcpus, 0);
10929 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010930}
10931
Sheng Yangad8ba2c2009-01-06 10:03:02 +080010932void kvm_arch_sync_events(struct kvm *kvm)
10933{
Andrew Jones332967a2014-02-28 12:52:55 +010010934 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +010010935 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +080010936 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +080010937}
10938
Peter Xuff5a9832020-09-30 21:20:33 -040010939#define ERR_PTR_USR(e) ((void __user *)ERR_PTR(e))
10940
10941/**
10942 * __x86_set_memory_region: Setup KVM internal memory slot
10943 *
10944 * @kvm: the kvm pointer to the VM.
10945 * @id: the slot ID to setup.
10946 * @gpa: the GPA to install the slot (unused when @size == 0).
10947 * @size: the size of the slot. Set to zero to uninstall a slot.
10948 *
10949 * This function helps to setup a KVM internal memory slot. Specify
10950 * @size > 0 to install a new slot, while @size == 0 to uninstall a
10951 * slot. The return code can be one of the following:
10952 *
10953 * HVA: on success (uninstall will return a bogus HVA)
10954 * -errno: on error
10955 *
10956 * The caller should always use IS_ERR() to check the return value
10957 * before use. Note, the KVM internal memory slots are guaranteed to
10958 * remain valid and unchanged until the VM is destroyed, i.e., the
10959 * GPA->HVA translation will not change. However, the HVA is a user
10960 * address, i.e. its accessibility is not guaranteed, and must be
10961 * accessed via __copy_{to,from}_user().
10962 */
10963void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
10964 u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010965{
10966 int i, r;
Kees Cook3f649ab2020-06-03 13:09:38 -070010967 unsigned long hva, old_npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010968 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010969 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010970
10971 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010972 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
Peter Xuff5a9832020-09-30 21:20:33 -040010973 return ERR_PTR_USR(-EINVAL);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010974
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010975 slot = id_to_memslot(slots, id);
10976 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010977 if (slot && slot->npages)
Peter Xuff5a9832020-09-30 21:20:33 -040010978 return ERR_PTR_USR(-EEXIST);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010979
10980 /*
10981 * MAP_SHARED to prevent internal slot pages from being moved
10982 * by fork()/COW.
10983 */
10984 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
10985 MAP_SHARED | MAP_ANONYMOUS, 0);
10986 if (IS_ERR((void *)hva))
Peter Xuff5a9832020-09-30 21:20:33 -040010987 return (void __user *)hva;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010988 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010989 if (!slot || !slot->npages)
Muhammad Usama Anjum46914532021-03-05 23:08:16 +050010990 return NULL;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010991
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010992 old_npages = slot->npages;
Zheng Zhan Liangb66f9ba2021-02-01 13:53:10 +080010993 hva = slot->userspace_addr;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010994 }
10995
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010996 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010997 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010998
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010999 m.slot = id | (i << 16);
11000 m.flags = 0;
11001 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011002 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020011003 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011004 r = __kvm_set_memory_region(kvm, &m);
11005 if (r < 0)
Peter Xuff5a9832020-09-30 21:20:33 -040011006 return ERR_PTR_USR(r);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011007 }
11008
Eric Biggers103c7632018-01-31 17:30:21 -080011009 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -080011010 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020011011
Peter Xuff5a9832020-09-30 21:20:33 -040011012 return (void __user *)hva;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020011013}
11014EXPORT_SYMBOL_GPL(__x86_set_memory_region);
11015
Junaid Shahid1aa9b952019-11-04 20:26:00 +010011016void kvm_arch_pre_destroy_vm(struct kvm *kvm)
11017{
11018 kvm_mmu_pre_destroy_vm(kvm);
11019}
11020
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011021void kvm_arch_destroy_vm(struct kvm *kvm)
11022{
Andrew Honig27469d22013-04-18 09:38:14 -070011023 if (current->mm == kvm->mm) {
11024 /*
11025 * Free memory regions allocated on behalf of userspace,
11026 * unless the the memory map has changed due to process exit
11027 * or fd copying.
11028 */
Peter Xu6a3c6232020-01-09 09:57:16 -050011029 mutex_lock(&kvm->slots_lock);
11030 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
11031 0, 0);
11032 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
11033 0, 0);
11034 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
11035 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -070011036 }
Jason Baronb36464772021-01-14 22:27:56 -050011037 static_call_cond(kvm_x86_vm_destroy)(kvm);
Sean Christophersonb318e8d2021-03-16 11:44:33 -070011038 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1));
Peter Xuc7611592017-03-15 16:01:19 +080011039 kvm_pic_destroy(kvm);
11040 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011041 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +020011042 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -070011043 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +080011044 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +020011045 kvm_page_track_cleanup(kvm);
David Woodhouse7d6bbeb2021-02-02 15:48:05 +000011046 kvm_xen_destroy_vm(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +030011047 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080011048}
Zhang Xiantao0de10342007-11-20 16:25:04 +080011049
Ben Gardonc9b929b2021-05-18 10:34:08 -070011050static void memslot_rmap_free(struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011051{
11052 int i;
11053
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011054 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -080011055 kvfree(slot->arch.rmap[i]);
11056 slot->arch.rmap[i] = NULL;
Ben Gardonc9b929b2021-05-18 10:34:08 -070011057 }
11058}
Sean Christophersone96c81e2020-02-18 13:07:27 -080011059
Ben Gardonc9b929b2021-05-18 10:34:08 -070011060void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
11061{
11062 int i;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011063
Ben Gardonc9b929b2021-05-18 10:34:08 -070011064 memslot_rmap_free(slot);
11065
11066 for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -080011067 kvfree(slot->arch.lpage_info[i - 1]);
11068 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011069 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080011070
Sean Christophersone96c81e2020-02-18 13:07:27 -080011071 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011072}
11073
Ben Gardon56dd1012021-05-18 10:34:09 -070011074static int memslot_rmap_alloc(struct kvm_memory_slot *slot,
11075 unsigned long npages)
11076{
11077 const int sz = sizeof(*slot->arch.rmap[0]);
11078 int i;
11079
11080 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
11081 int level = i + 1;
11082 int lpages = gfn_to_index(slot->base_gfn + npages - 1,
11083 slot->base_gfn, level) + 1;
11084
Ben Gardond501f742021-05-18 10:34:14 -070011085 WARN_ON(slot->arch.rmap[i]);
11086
Ben Gardon56dd1012021-05-18 10:34:09 -070011087 slot->arch.rmap[i] = kvcalloc(lpages, sz, GFP_KERNEL_ACCOUNT);
11088 if (!slot->arch.rmap[i]) {
11089 memslot_rmap_free(slot);
11090 return -ENOMEM;
11091 }
11092 }
11093
11094 return 0;
11095}
11096
Ben Gardond501f742021-05-18 10:34:14 -070011097int alloc_all_memslots_rmaps(struct kvm *kvm)
11098{
11099 struct kvm_memslots *slots;
11100 struct kvm_memory_slot *slot;
11101 int r, i;
11102
11103 /*
11104 * Check if memslots alreday have rmaps early before acquiring
11105 * the slots_arch_lock below.
11106 */
11107 if (kvm_memslots_have_rmaps(kvm))
11108 return 0;
11109
11110 mutex_lock(&kvm->slots_arch_lock);
11111
11112 /*
11113 * Read memslots_have_rmaps again, under the slots arch lock,
11114 * before allocating the rmaps
11115 */
11116 if (kvm_memslots_have_rmaps(kvm)) {
11117 mutex_unlock(&kvm->slots_arch_lock);
11118 return 0;
11119 }
11120
11121 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
11122 slots = __kvm_memslots(kvm, i);
11123 kvm_for_each_memslot(slot, slots) {
11124 r = memslot_rmap_alloc(slot, slot->npages);
11125 if (r) {
11126 mutex_unlock(&kvm->slots_arch_lock);
11127 return r;
11128 }
11129 }
11130 }
11131
11132 /*
11133 * Ensure that memslots_have_rmaps becomes true strictly after
11134 * all the rmap pointers are set.
11135 */
11136 smp_store_release(&kvm->arch.memslots_have_rmaps, true);
11137 mutex_unlock(&kvm->slots_arch_lock);
11138 return 0;
11139}
11140
Ben Gardona2557402021-05-18 10:34:12 -070011141static int kvm_alloc_memslot_metadata(struct kvm *kvm,
11142 struct kvm_memory_slot *slot,
Sean Christopherson0dab98b2020-02-18 13:07:19 -080011143 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011144{
Ben Gardon56dd1012021-05-18 10:34:09 -070011145 int i, r;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011146
Sean Christophersonedd4fa32020-02-18 13:07:15 -080011147 /*
11148 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
11149 * old arrays will be freed by __kvm_set_memory_region() if installing
11150 * the new memslot is successful.
11151 */
11152 memset(&slot->arch, 0, sizeof(slot->arch));
11153
Ben Gardone2209712021-05-18 10:34:13 -070011154 if (kvm_memslots_have_rmaps(kvm)) {
Ben Gardona2557402021-05-18 10:34:12 -070011155 r = memslot_rmap_alloc(slot, npages);
11156 if (r)
11157 return r;
11158 }
Ben Gardon56dd1012021-05-18 10:34:09 -070011159
11160 for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011161 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011162 unsigned long ugfn;
11163 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011164 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011165
11166 lpages = gfn_to_index(slot->base_gfn + npages - 1,
11167 slot->base_gfn, level) + 1;
11168
Ben Gardon254272c2019-02-11 11:02:50 -080011169 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011170 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011171 goto out_free;
11172
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011173 slot->arch.lpage_info[i - 1] = linfo;
11174
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011175 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011176 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011177 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011178 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011179 ugfn = slot->userspace_addr >> PAGE_SHIFT;
11180 /*
11181 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -080011182 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011183 */
Sean Christopherson600087b2020-03-02 15:57:05 -080011184 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011185 unsigned long j;
11186
11187 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +080011188 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011189 }
11190 }
11191
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080011192 if (kvm_page_track_create_memslot(slot, npages))
11193 goto out_free;
11194
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011195 return 0;
11196
11197out_free:
Ben Gardonc9b929b2021-05-18 10:34:08 -070011198 memslot_rmap_free(slot);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011199
Ben Gardonc9b929b2021-05-18 10:34:08 -070011200 for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +010011201 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090011202 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090011203 }
11204 return -ENOMEM;
11205}
11206
Sean Christopherson15248252019-02-05 12:54:17 -080011207void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090011208{
Boris Ostrovsky91724812019-12-05 01:30:51 +000011209 struct kvm_vcpu *vcpu;
11210 int i;
11211
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +090011212 /*
11213 * memslots->generation has been incremented.
11214 * mmio generation may have reached its maximum value.
11215 */
Sean Christopherson15248252019-02-05 12:54:17 -080011216 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +000011217
11218 /* Force re-initialization of steal_time cache */
11219 kvm_for_each_vcpu(i, vcpu, kvm)
11220 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090011221}
11222
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011223int kvm_arch_prepare_memory_region(struct kvm *kvm,
11224 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +020011225 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +090011226 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +080011227{
Sean Christopherson0dab98b2020-02-18 13:07:19 -080011228 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
Ben Gardona2557402021-05-18 10:34:12 -070011229 return kvm_alloc_memslot_metadata(kvm, memslot,
Sean Christopherson0dab98b2020-02-18 13:07:19 -080011230 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011231 return 0;
11232}
11233
Makarand Sonarea85863c2021-02-12 16:50:12 -080011234
11235static void kvm_mmu_update_cpu_dirty_logging(struct kvm *kvm, bool enable)
11236{
11237 struct kvm_arch *ka = &kvm->arch;
11238
11239 if (!kvm_x86_ops.cpu_dirty_log_size)
11240 return;
11241
11242 if ((enable && ++ka->cpu_dirty_logging_count == 1) ||
11243 (!enable && --ka->cpu_dirty_logging_count == 0))
11244 kvm_make_all_cpus_request(kvm, KVM_REQ_UPDATE_CPU_DIRTY_LOGGING);
11245
11246 WARN_ON_ONCE(ka->cpu_dirty_logging_count < 0);
11247}
11248
Kai Huang88178fd2015-01-28 10:54:27 +080011249static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
Anthony Yznaga37416792020-06-02 13:07:30 -070011250 struct kvm_memory_slot *old,
11251 struct kvm_memory_slot *new,
11252 enum kvm_mr_change change)
Kai Huang88178fd2015-01-28 10:54:27 +080011253{
Makarand Sonarea85863c2021-02-12 16:50:12 -080011254 bool log_dirty_pages = new->flags & KVM_MEM_LOG_DIRTY_PAGES;
11255
Anthony Yznaga37416792020-06-02 13:07:30 -070011256 /*
Makarand Sonarea85863c2021-02-12 16:50:12 -080011257 * Update CPU dirty logging if dirty logging is being toggled. This
11258 * applies to all operations.
11259 */
11260 if ((old->flags ^ new->flags) & KVM_MEM_LOG_DIRTY_PAGES)
11261 kvm_mmu_update_cpu_dirty_logging(kvm, log_dirty_pages);
11262
11263 /*
11264 * Nothing more to do for RO slots (which can't be dirtied and can't be
Sean Christophersonb6e16ae2021-02-12 16:50:13 -080011265 * made writable) or CREATE/MOVE/DELETE of a slot.
11266 *
11267 * For a memslot with dirty logging disabled:
11268 * CREATE: No dirty mappings will already exist.
11269 * MOVE/DELETE: The old mappings will already have been cleaned up by
11270 * kvm_arch_flush_shadow_memslot()
11271 *
11272 * For a memslot with dirty logging enabled:
11273 * CREATE: No shadow pages exist, thus nothing to write-protect
11274 * and no dirty bits to clear.
11275 * MOVE/DELETE: The old mappings will already have been cleaned up by
11276 * kvm_arch_flush_shadow_memslot().
Anthony Yznaga37416792020-06-02 13:07:30 -070011277 */
11278 if ((change != KVM_MR_FLAGS_ONLY) || (new->flags & KVM_MEM_READONLY))
Kai Huang88178fd2015-01-28 10:54:27 +080011279 return;
Kai Huang88178fd2015-01-28 10:54:27 +080011280
11281 /*
Sean Christopherson52f460792021-02-12 16:50:11 -080011282 * READONLY and non-flags changes were filtered out above, and the only
11283 * other flag is LOG_DIRTY_PAGES, i.e. something is wrong if dirty
11284 * logging isn't being toggled on or off.
Anthony Yznaga37416792020-06-02 13:07:30 -070011285 */
Sean Christopherson52f460792021-02-12 16:50:11 -080011286 if (WARN_ON_ONCE(!((old->flags ^ new->flags) & KVM_MEM_LOG_DIRTY_PAGES)))
11287 return;
11288
Sean Christophersonb6e16ae2021-02-12 16:50:13 -080011289 if (!log_dirty_pages) {
11290 /*
11291 * Dirty logging tracks sptes in 4k granularity, meaning that
11292 * large sptes have to be split. If live migration succeeds,
11293 * the guest in the source machine will be destroyed and large
11294 * sptes will be created in the destination. However, if the
11295 * guest continues to run in the source machine (for example if
11296 * live migration fails), small sptes will remain around and
11297 * cause bad performance.
11298 *
11299 * Scan sptes if dirty logging has been stopped, dropping those
11300 * which can be collapsed into a single large-page spte. Later
11301 * page faults will create the large-page sptes.
11302 */
Anthony Yznaga37416792020-06-02 13:07:30 -070011303 kvm_mmu_zap_collapsible_sptes(kvm, new);
Sean Christophersonb6e16ae2021-02-12 16:50:13 -080011304 } else {
Keqian Zhu89212912021-04-29 11:41:15 +080011305 /*
11306 * Initially-all-set does not require write protecting any page,
11307 * because they're all assumed to be dirty.
11308 */
11309 if (kvm_dirty_log_manual_protect_and_init_set(kvm))
11310 return;
Anthony Yznaga37416792020-06-02 13:07:30 -070011311
Sean Christophersona018eba2021-02-12 16:50:10 -080011312 if (kvm_x86_ops.cpu_dirty_log_size) {
Keqian Zhu89212912021-04-29 11:41:15 +080011313 kvm_mmu_slot_leaf_clear_dirty(kvm, new);
11314 kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_2M);
11315 } else {
11316 kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_4K);
Jay Zhou3c9bd402020-02-27 09:32:27 +080011317 }
Kai Huang88178fd2015-01-28 10:54:27 +080011318 }
11319}
11320
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011321void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020011322 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080011323 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020011324 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090011325 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020011326{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080011327 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080011328 kvm_mmu_change_mmu_pages(kvm,
11329 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080011330
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090011331 /*
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020011332 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090011333 */
Anthony Yznaga37416792020-06-02 13:07:30 -070011334 kvm_mmu_slot_apply_flags(kvm, old, (struct kvm_memory_slot *) new, change);
Sean Christopherson21198842020-02-18 13:07:25 -080011335
11336 /* Free the arrays associated with the old memslot. */
11337 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080011338 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080011339}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080011340
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030011341void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030011342{
Sean Christopherson7390de12019-02-05 13:01:31 -080011343 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030011344}
11345
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030011346void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
11347 struct kvm_memory_slot *slot)
11348{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080011349 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030011350}
11351
Liran Alone6c67d82018-09-04 10:56:52 +030011352static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
11353{
11354 return (is_guest_mode(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070011355 kvm_x86_ops.guest_apic_has_interrupt &&
Jason Baronb36464772021-01-14 22:27:56 -050011356 static_call(kvm_x86_guest_apic_has_interrupt)(vcpu));
Liran Alone6c67d82018-09-04 10:56:52 +030011357}
11358
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011359static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
11360{
11361 if (!list_empty_careful(&vcpu->async_pf.done))
11362 return true;
11363
11364 if (kvm_apic_has_events(vcpu))
11365 return true;
11366
11367 if (vcpu->arch.pv.pv_unhalted)
11368 return true;
11369
Wanpeng Lia5f01f82017-09-13 04:04:01 -070011370 if (vcpu->arch.exception.pending)
11371 return true;
11372
ZhuangYanying47a66ee2017-05-26 13:16:48 +080011373 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
11374 (vcpu->arch.nmi_pending &&
Jason Baronb36464772021-01-14 22:27:56 -050011375 static_call(kvm_x86_nmi_allowed)(vcpu, false)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011376 return true;
11377
ZhuangYanying47a66ee2017-05-26 13:16:48 +080011378 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -040011379 (vcpu->arch.smi_pending &&
Jason Baronb36464772021-01-14 22:27:56 -050011380 static_call(kvm_x86_smi_allowed)(vcpu, false)))
Paolo Bonzini73917732015-10-13 10:19:35 +020011381 return true;
11382
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011383 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030011384 (kvm_cpu_has_interrupt(vcpu) ||
11385 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011386 return true;
11387
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030011388 if (kvm_hv_has_stimer_pending(vcpu))
11389 return true;
11390
Sean Christophersond2060bd2020-04-22 19:25:39 -070011391 if (is_guest_mode(vcpu) &&
11392 kvm_x86_ops.nested_ops->hv_timer_pending &&
11393 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
11394 return true;
11395
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011396 return false;
11397}
11398
Zhang Xiantao1d737c82007-12-14 09:35:10 +080011399int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
11400{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020011401 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080011402}
Zhang Xiantao57361992007-12-17 14:21:40 +080011403
Haiwei Li10dbdf92021-04-21 11:25:13 +080011404bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu)
11405{
11406 if (vcpu->arch.apicv_active && static_call(kvm_x86_dy_apicv_has_pending_interrupt)(vcpu))
11407 return true;
11408
11409 return false;
11410}
11411
Wanpeng Li17e433b2019-08-05 10:03:19 +080011412bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
11413{
11414 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
11415 return true;
11416
11417 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
11418 kvm_test_request(KVM_REQ_SMI, vcpu) ||
11419 kvm_test_request(KVM_REQ_EVENT, vcpu))
11420 return true;
11421
Haiwei Li10dbdf92021-04-21 11:25:13 +080011422 return kvm_arch_dy_has_pending_interrupt(vcpu);
Wanpeng Li17e433b2019-08-05 10:03:19 +080011423}
11424
Longpeng(Mike)199b5762017-08-08 12:05:32 +080011425bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
11426{
Wanpeng Lib86bb112021-04-22 16:34:19 +080011427 if (vcpu->arch.guest_state_protected)
11428 return true;
11429
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080011430 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080011431}
11432
Christoffer Dallb6d33832012-03-08 16:44:24 -050011433int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080011434{
Christoffer Dallb6d33832012-03-08 16:44:24 -050011435 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080011436}
Gleb Natapov78646122009-03-23 12:12:11 +020011437
11438int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
11439{
Jason Baronb36464772021-01-14 22:27:56 -050011440 return static_call(kvm_x86_interrupt_allowed)(vcpu, false);
Gleb Natapov78646122009-03-23 12:12:11 +020011441}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011442
Nadav Amit82b32772014-11-02 11:54:45 +020011443unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
11444{
Tom Lendacky7ed9abf2020-12-10 11:09:54 -060011445 /* Can't read the RIP when guest state is protected, just return 0 */
11446 if (vcpu->arch.guest_state_protected)
11447 return 0;
11448
Nadav Amit82b32772014-11-02 11:54:45 +020011449 if (is_64_bit_mode(vcpu))
11450 return kvm_rip_read(vcpu);
11451 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
11452 kvm_rip_read(vcpu));
11453}
11454EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
11455
Jan Kiszkaf92653e2010-02-23 17:47:55 +010011456bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
11457{
Nadav Amit82b32772014-11-02 11:54:45 +020011458 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010011459}
11460EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
11461
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011462unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
11463{
11464 unsigned long rflags;
11465
Jason Baronb36464772021-01-14 22:27:56 -050011466 rflags = static_call(kvm_x86_get_rflags)(vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011467 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010011468 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011469 return rflags;
11470}
11471EXPORT_SYMBOL_GPL(kvm_get_rflags);
11472
Paolo Bonzini6addfc42014-03-27 11:29:28 +010011473static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011474{
11475 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010011476 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010011477 rflags |= X86_EFLAGS_TF;
Jason Baronb36464772021-01-14 22:27:56 -050011478 static_call(kvm_x86_set_rflags)(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010011479}
11480
11481void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
11482{
11483 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030011484 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020011485}
11486EXPORT_SYMBOL_GPL(kvm_set_rflags);
11487
Gleb Natapov56028d02010-10-17 18:13:42 +020011488void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
11489{
11490 int r;
11491
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011492 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080011493 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020011494 return;
11495
11496 r = kvm_mmu_reload(vcpu);
11497 if (unlikely(r))
11498 return;
11499
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011500 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080011501 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080011502 return;
11503
Sean Christopherson7a026742020-02-06 14:14:34 -080011504 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020011505}
11506
Gleb Natapovaf585b92010-10-14 11:22:46 +020011507static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
11508{
Peter Xudd03bca2020-04-16 11:58:59 -040011509 BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
11510
Gleb Natapovaf585b92010-10-14 11:22:46 +020011511 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
11512}
11513
11514static inline u32 kvm_async_pf_next_probe(u32 key)
11515{
Peter Xudd03bca2020-04-16 11:58:59 -040011516 return (key + 1) & (ASYNC_PF_PER_VCPU - 1);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011517}
11518
11519static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
11520{
11521 u32 key = kvm_async_pf_hash_fn(gfn);
11522
11523 while (vcpu->arch.apf.gfns[key] != ~0)
11524 key = kvm_async_pf_next_probe(key);
11525
11526 vcpu->arch.apf.gfns[key] = gfn;
11527}
11528
11529static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
11530{
11531 int i;
11532 u32 key = kvm_async_pf_hash_fn(gfn);
11533
Peter Xudd03bca2020-04-16 11:58:59 -040011534 for (i = 0; i < ASYNC_PF_PER_VCPU &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080011535 (vcpu->arch.apf.gfns[key] != gfn &&
11536 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020011537 key = kvm_async_pf_next_probe(key);
11538
11539 return key;
11540}
11541
11542bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
11543{
11544 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
11545}
11546
11547static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
11548{
11549 u32 i, j, k;
11550
11551 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
Peter Xu0fd46042020-04-16 11:59:10 -040011552
11553 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn))
11554 return;
11555
Gleb Natapovaf585b92010-10-14 11:22:46 +020011556 while (true) {
11557 vcpu->arch.apf.gfns[i] = ~0;
11558 do {
11559 j = kvm_async_pf_next_probe(j);
11560 if (vcpu->arch.apf.gfns[j] == ~0)
11561 return;
11562 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
11563 /*
11564 * k lies cyclically in ]i,j]
11565 * | i.k.j |
11566 * |....j i.k.| or |.k..j i...|
11567 */
11568 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
11569 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
11570 i = j;
11571 }
11572}
11573
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020011574static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020011575{
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020011576 u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT;
11577
11578 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason,
11579 sizeof(reason));
11580}
11581
11582static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token)
11583{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011584 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
Paolo Bonzini4e335d92017-05-02 16:20:18 +020011585
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011586 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
11587 &token, offset, sizeof(token));
11588}
11589
11590static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu)
11591{
11592 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
11593 u32 val;
11594
11595 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
11596 &val, offset, sizeof(val)))
11597 return false;
11598
11599 return !val;
Gleb Natapov7c907052010-10-14 11:22:53 +020011600}
11601
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011602static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
11603{
11604 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
11605 return false;
11606
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011607 if (!kvm_pv_async_pf_enabled(vcpu) ||
Jason Baronb36464772021-01-14 22:27:56 -050011608 (vcpu->arch.apf.send_user_only && static_call(kvm_x86_get_cpl)(vcpu) == 0))
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011609 return false;
11610
11611 return true;
11612}
11613
11614bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
11615{
11616 if (unlikely(!lapic_in_kernel(vcpu) ||
11617 kvm_event_needs_reinjection(vcpu) ||
11618 vcpu->arch.exception.pending))
11619 return false;
11620
11621 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
11622 return false;
11623
11624 /*
11625 * If interrupts are off we cannot even use an artificial
11626 * halt state.
11627 */
Paolo Bonzinic300ab92020-04-23 14:08:58 -040011628 return kvm_arch_interrupt_allowed(vcpu);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011629}
11630
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011631bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
Gleb Natapovaf585b92010-10-14 11:22:46 +020011632 struct kvm_async_pf *work)
11633{
Avi Kivity6389ee92010-11-29 16:12:30 +020011634 struct x86_exception fault;
11635
Sean Christopherson736c2912019-12-06 15:57:14 -080011636 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011637 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020011638
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011639 if (kvm_can_deliver_async_pf(vcpu) &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020011640 !apf_put_user_notpresent(vcpu)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020011641 fault.vector = PF_VECTOR;
11642 fault.error_code_valid = true;
11643 fault.error_code = 0;
11644 fault.nested_page_fault = false;
11645 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070011646 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020011647 kvm_inject_page_fault(vcpu, &fault);
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011648 return true;
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020011649 } else {
11650 /*
11651 * It is not possible to deliver a paravirtualized asynchronous
11652 * page fault, but putting the guest in an artificial halt state
11653 * can be beneficial nevertheless: if an interrupt arrives, we
11654 * can deliver it timely and perhaps the guest will schedule
11655 * another process. When the instruction that triggered a page
11656 * fault is retried, hopefully the page will be ready in the host.
11657 */
11658 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011659 return false;
Gleb Natapov7c907052010-10-14 11:22:53 +020011660 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020011661}
11662
11663void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
11664 struct kvm_async_pf *work)
11665{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011666 struct kvm_lapic_irq irq = {
11667 .delivery_mode = APIC_DM_FIXED,
11668 .vector = vcpu->arch.apf.vec
11669 };
Avi Kivity6389ee92010-11-29 16:12:30 +020011670
chai wenf2e10662013-10-14 22:22:33 +080011671 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020011672 work->arch.token = ~0; /* broadcast wakeup */
11673 else
11674 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080011675 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020011676
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020011677 if ((work->wakeup_all || work->notpresent_injected) &&
11678 kvm_pv_async_pf_enabled(vcpu) &&
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011679 !apf_put_user_ready(vcpu, work->arch.token)) {
11680 vcpu->arch.apf.pageready_pending = true;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011681 kvm_apic_set_irq(vcpu, &irq, NULL);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011682 }
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011683
Xiao Guangronge6d53e32010-11-01 17:01:28 +080011684 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030011685 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020011686}
11687
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011688void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu)
11689{
11690 kvm_make_request(KVM_REQ_APF_READY, vcpu);
11691 if (!vcpu->arch.apf.pageready_pending)
11692 kvm_vcpu_kick(vcpu);
11693}
11694
Vitaly Kuznetsov7c0ade62020-05-25 16:41:18 +020011695bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020011696{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011697 if (!kvm_pv_async_pf_enabled(vcpu))
Gleb Natapov7c907052010-10-14 11:22:53 +020011698 return true;
11699 else
Vitaly Kuznetsov2f15d022021-04-22 11:29:48 +020011700 return kvm_lapic_enabled(vcpu) && apf_pageready_slot_free(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011701}
11702
Paolo Bonzini5544eb92015-07-07 15:41:58 +020011703void kvm_arch_start_assignment(struct kvm *kvm)
11704{
Marcelo Tosatti57ab8792021-05-25 10:41:16 -030011705 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1)
11706 static_call_cond(kvm_x86_start_assignment)(kvm);
Paolo Bonzini5544eb92015-07-07 15:41:58 +020011707}
11708EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
11709
11710void kvm_arch_end_assignment(struct kvm *kvm)
11711{
11712 atomic_dec(&kvm->arch.assigned_device_count);
11713}
11714EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
11715
11716bool kvm_arch_has_assigned_device(struct kvm *kvm)
11717{
11718 return atomic_read(&kvm->arch.assigned_device_count);
11719}
11720EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
11721
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060011722void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
11723{
11724 atomic_inc(&kvm->arch.noncoherent_dma_count);
11725}
11726EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
11727
11728void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
11729{
11730 atomic_dec(&kvm->arch.noncoherent_dma_count);
11731}
11732EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
11733
11734bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
11735{
11736 return atomic_read(&kvm->arch.noncoherent_dma_count);
11737}
11738EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
11739
Alex Williamson14717e22016-05-05 11:58:35 -060011740bool kvm_arch_has_irq_bypass(void)
11741{
Sean Christopherson92735b12019-08-02 15:06:17 -070011742 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060011743}
11744
Feng Wu87276882015-09-18 22:29:40 +080011745int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
11746 struct irq_bypass_producer *prod)
11747{
11748 struct kvm_kernel_irqfd *irqfd =
11749 container_of(cons, struct kvm_kernel_irqfd, consumer);
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011750 int ret;
Feng Wu87276882015-09-18 22:29:40 +080011751
Alex Williamson14717e22016-05-05 11:58:35 -060011752 irqfd->producer = prod;
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011753 kvm_arch_start_assignment(irqfd->kvm);
Jason Baronb36464772021-01-14 22:27:56 -050011754 ret = static_call(kvm_x86_update_pi_irte)(irqfd->kvm,
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011755 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080011756
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011757 if (ret)
11758 kvm_arch_end_assignment(irqfd->kvm);
11759
11760 return ret;
Feng Wu87276882015-09-18 22:29:40 +080011761}
11762
11763void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
11764 struct irq_bypass_producer *prod)
11765{
11766 int ret;
11767 struct kvm_kernel_irqfd *irqfd =
11768 container_of(cons, struct kvm_kernel_irqfd, consumer);
11769
Feng Wu87276882015-09-18 22:29:40 +080011770 WARN_ON(irqfd->producer != prod);
11771 irqfd->producer = NULL;
11772
11773 /*
11774 * When producer of consumer is unregistered, we change back to
11775 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020011776 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080011777 * int this case doesn't want to receive the interrupts.
11778 */
Jason Baronb36464772021-01-14 22:27:56 -050011779 ret = static_call(kvm_x86_update_pi_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 0);
Feng Wu87276882015-09-18 22:29:40 +080011780 if (ret)
11781 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
11782 " fails: %d\n", irqfd->consumer.token, ret);
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011783
11784 kvm_arch_end_assignment(irqfd->kvm);
Feng Wu87276882015-09-18 22:29:40 +080011785}
11786
11787int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
11788 uint32_t guest_irq, bool set)
11789{
Jason Baronb36464772021-01-14 22:27:56 -050011790 return static_call(kvm_x86_update_pi_irte)(kvm, host_irq, guest_irq, set);
Feng Wu87276882015-09-18 22:29:40 +080011791}
11792
Feng Wu520040142016-01-25 16:53:33 +080011793bool kvm_vector_hashing_enabled(void)
11794{
11795 return vector_hashing;
11796}
Feng Wu520040142016-01-25 16:53:33 +080011797
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030011798bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
11799{
11800 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
11801}
11802EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
11803
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011804
11805int kvm_spec_ctrl_test_value(u64 value)
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011806{
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011807 /*
11808 * test that setting IA32_SPEC_CTRL to given value
11809 * is allowed by the host processor
11810 */
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011811
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011812 u64 saved_value;
11813 unsigned long flags;
11814 int ret = 0;
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011815
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011816 local_irq_save(flags);
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011817
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011818 if (rdmsrl_safe(MSR_IA32_SPEC_CTRL, &saved_value))
11819 ret = 1;
11820 else if (wrmsrl_safe(MSR_IA32_SPEC_CTRL, value))
11821 ret = 1;
11822 else
11823 wrmsrl(MSR_IA32_SPEC_CTRL, saved_value);
11824
11825 local_irq_restore(flags);
11826
11827 return ret;
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011828}
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011829EXPORT_SYMBOL_GPL(kvm_spec_ctrl_test_value);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030011830
Mohammed Gamal89786142020-07-10 17:48:03 +020011831void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code)
11832{
11833 struct x86_exception fault;
Paolo Bonzini19cf4b72020-08-17 13:53:04 -040011834 u32 access = error_code &
11835 (PFERR_WRITE_MASK | PFERR_FETCH_MASK | PFERR_USER_MASK);
Mohammed Gamal89786142020-07-10 17:48:03 +020011836
11837 if (!(error_code & PFERR_PRESENT_MASK) ||
Paolo Bonzini19cf4b72020-08-17 13:53:04 -040011838 vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, &fault) != UNMAPPED_GVA) {
Mohammed Gamal89786142020-07-10 17:48:03 +020011839 /*
11840 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page
11841 * tables probably do not match the TLB. Just proceed
11842 * with the error code that the processor gave.
11843 */
11844 fault.vector = PF_VECTOR;
11845 fault.error_code_valid = true;
11846 fault.error_code = error_code;
11847 fault.nested_page_fault = false;
11848 fault.address = gva;
11849 }
11850 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault);
11851}
11852EXPORT_SYMBOL_GPL(kvm_fixup_and_inject_pf_error);
Carsten Otte043405e2007-10-10 17:16:19 +020011853
Babu Moger3f3393b2020-09-11 14:29:05 -050011854/*
11855 * Handles kvm_read/write_guest_virt*() result and either injects #PF or returns
11856 * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value
11857 * indicates whether exit to userspace is needed.
11858 */
11859int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
11860 struct x86_exception *e)
11861{
11862 if (r == X86EMUL_PROPAGATE_FAULT) {
11863 kvm_inject_emulated_page_fault(vcpu, e);
11864 return 1;
11865 }
11866
11867 /*
11868 * In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED
11869 * while handling a VMX instruction KVM could've handled the request
11870 * correctly by exiting to userspace and performing I/O but there
11871 * doesn't seem to be a real use-case behind such requests, just return
11872 * KVM_EXIT_INTERNAL_ERROR for now.
11873 */
11874 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
11875 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
11876 vcpu->run->internal.ndata = 0;
11877
11878 return 0;
11879}
11880EXPORT_SYMBOL_GPL(kvm_handle_memory_failure);
11881
Babu Moger97150922020-09-11 14:29:12 -050011882int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva)
11883{
11884 bool pcid_enabled;
11885 struct x86_exception e;
11886 unsigned i;
11887 unsigned long roots_to_free = 0;
11888 struct {
11889 u64 pcid;
11890 u64 gla;
11891 } operand;
11892 int r;
11893
11894 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
11895 if (r != X86EMUL_CONTINUE)
11896 return kvm_handle_memory_failure(vcpu, r, &e);
11897
11898 if (operand.pcid >> 12 != 0) {
11899 kvm_inject_gp(vcpu, 0);
11900 return 1;
11901 }
11902
11903 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
11904
11905 switch (type) {
11906 case INVPCID_TYPE_INDIV_ADDR:
11907 if ((!pcid_enabled && (operand.pcid != 0)) ||
11908 is_noncanonical_address(operand.gla, vcpu)) {
11909 kvm_inject_gp(vcpu, 0);
11910 return 1;
11911 }
11912 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
11913 return kvm_skip_emulated_instruction(vcpu);
11914
11915 case INVPCID_TYPE_SINGLE_CTXT:
11916 if (!pcid_enabled && (operand.pcid != 0)) {
11917 kvm_inject_gp(vcpu, 0);
11918 return 1;
11919 }
11920
11921 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
11922 kvm_mmu_sync_roots(vcpu);
11923 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
11924 }
11925
11926 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
11927 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd)
11928 == operand.pcid)
11929 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
11930
11931 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
11932 /*
11933 * If neither the current cr3 nor any of the prev_roots use the
11934 * given PCID, then nothing needs to be done here because a
11935 * resync will happen anyway before switching to any other CR3.
11936 */
11937
11938 return kvm_skip_emulated_instruction(vcpu);
11939
11940 case INVPCID_TYPE_ALL_NON_GLOBAL:
11941 /*
11942 * Currently, KVM doesn't mark global entries in the shadow
11943 * page tables, so a non-global flush just degenerates to a
11944 * global flush. If needed, we could optimize this later by
11945 * keeping track of global entries in shadow page tables.
11946 */
11947
11948 fallthrough;
11949 case INVPCID_TYPE_ALL_INCL_GLOBAL:
Sean Christophersonf66c53b2021-03-04 17:10:58 -080011950 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
Babu Moger97150922020-09-11 14:29:12 -050011951 return kvm_skip_emulated_instruction(vcpu);
11952
11953 default:
11954 BUG(); /* We have already checked above that type <= 3 */
11955 }
11956}
11957EXPORT_SYMBOL_GPL(kvm_handle_invpcid);
11958
Tom Lendacky8f423a82020-12-10 11:09:53 -060011959static int complete_sev_es_emulated_mmio(struct kvm_vcpu *vcpu)
11960{
11961 struct kvm_run *run = vcpu->run;
11962 struct kvm_mmio_fragment *frag;
11963 unsigned int len;
11964
11965 BUG_ON(!vcpu->mmio_needed);
11966
11967 /* Complete previous fragment */
11968 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
11969 len = min(8u, frag->len);
11970 if (!vcpu->mmio_is_write)
11971 memcpy(frag->data, run->mmio.data, len);
11972
11973 if (frag->len <= 8) {
11974 /* Switch to the next fragment. */
11975 frag++;
11976 vcpu->mmio_cur_fragment++;
11977 } else {
11978 /* Go forward to the next mmio piece. */
11979 frag->data += len;
11980 frag->gpa += len;
11981 frag->len -= len;
11982 }
11983
11984 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
11985 vcpu->mmio_needed = 0;
11986
11987 // VMG change, at this point, we're always done
11988 // RIP has already been advanced
11989 return 1;
11990 }
11991
11992 // More MMIO is needed
11993 run->mmio.phys_addr = frag->gpa;
11994 run->mmio.len = min(8u, frag->len);
11995 run->mmio.is_write = vcpu->mmio_is_write;
11996 if (run->mmio.is_write)
11997 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
11998 run->exit_reason = KVM_EXIT_MMIO;
11999
12000 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio;
12001
12002 return 0;
12003}
12004
12005int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes,
12006 void *data)
12007{
12008 int handled;
12009 struct kvm_mmio_fragment *frag;
12010
12011 if (!data)
12012 return -EINVAL;
12013
12014 handled = write_emultor.read_write_mmio(vcpu, gpa, bytes, data);
12015 if (handled == bytes)
12016 return 1;
12017
12018 bytes -= handled;
12019 gpa += handled;
12020 data += handled;
12021
12022 /*TODO: Check if need to increment number of frags */
12023 frag = vcpu->mmio_fragments;
12024 vcpu->mmio_nr_fragments = 1;
12025 frag->len = bytes;
12026 frag->gpa = gpa;
12027 frag->data = data;
12028
12029 vcpu->mmio_needed = 1;
12030 vcpu->mmio_cur_fragment = 0;
12031
12032 vcpu->run->mmio.phys_addr = gpa;
12033 vcpu->run->mmio.len = min(8u, frag->len);
12034 vcpu->run->mmio.is_write = 1;
12035 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
12036 vcpu->run->exit_reason = KVM_EXIT_MMIO;
12037
12038 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio;
12039
12040 return 0;
12041}
12042EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_write);
12043
12044int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes,
12045 void *data)
12046{
12047 int handled;
12048 struct kvm_mmio_fragment *frag;
12049
12050 if (!data)
12051 return -EINVAL;
12052
12053 handled = read_emultor.read_write_mmio(vcpu, gpa, bytes, data);
12054 if (handled == bytes)
12055 return 1;
12056
12057 bytes -= handled;
12058 gpa += handled;
12059 data += handled;
12060
12061 /*TODO: Check if need to increment number of frags */
12062 frag = vcpu->mmio_fragments;
12063 vcpu->mmio_nr_fragments = 1;
12064 frag->len = bytes;
12065 frag->gpa = gpa;
12066 frag->data = data;
12067
12068 vcpu->mmio_needed = 1;
12069 vcpu->mmio_cur_fragment = 0;
12070
12071 vcpu->run->mmio.phys_addr = gpa;
12072 vcpu->run->mmio.len = min(8u, frag->len);
12073 vcpu->run->mmio.is_write = 0;
12074 vcpu->run->exit_reason = KVM_EXIT_MMIO;
12075
12076 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio;
12077
12078 return 0;
12079}
12080EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_read);
12081
Tom Lendacky7ed9abf2020-12-10 11:09:54 -060012082static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu)
12083{
12084 memcpy(vcpu->arch.guest_ins_data, vcpu->arch.pio_data,
12085 vcpu->arch.pio.count * vcpu->arch.pio.size);
12086 vcpu->arch.pio.count = 0;
12087
12088 return 1;
12089}
12090
12091static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size,
12092 unsigned int port, void *data, unsigned int count)
12093{
12094 int ret;
12095
12096 ret = emulator_pio_out_emulated(vcpu->arch.emulate_ctxt, size, port,
12097 data, count);
12098 if (ret)
12099 return ret;
12100
12101 vcpu->arch.pio.count = 0;
12102
12103 return 0;
12104}
12105
12106static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size,
12107 unsigned int port, void *data, unsigned int count)
12108{
12109 int ret;
12110
12111 ret = emulator_pio_in_emulated(vcpu->arch.emulate_ctxt, size, port,
12112 data, count);
12113 if (ret) {
12114 vcpu->arch.pio.count = 0;
12115 } else {
12116 vcpu->arch.guest_ins_data = data;
12117 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;
12118 }
12119
12120 return 0;
12121}
12122
12123int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
12124 unsigned int port, void *data, unsigned int count,
12125 int in)
12126{
12127 return in ? kvm_sev_es_ins(vcpu, size, port, data, count)
12128 : kvm_sev_es_outs(vcpu, size, port, data, count);
12129}
12130EXPORT_SYMBOL_GPL(kvm_sev_es_string_io);
12131
Lorenzo Bresciad95df952020-12-23 14:45:07 +000012132EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry);
Carsten Otte043405e2007-10-10 17:16:19 +020012133EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
12134EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
12135EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
12136EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
12137EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
12138EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
12139EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
12140EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
12141EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
12142EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070012143EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Carsten Otte043405e2007-10-10 17:16:19 +020012144EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
12145EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
12146EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
12147EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080012148EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Carsten Otte043405e2007-10-10 17:16:19 +020012149EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
12150EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
12151EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
12152EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050012153EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060012154EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);
Tom Lendackyd523ab6b2020-12-10 11:09:48 -060012155EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter);
12156EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit);
Tom Lendacky59e38b52020-12-10 11:09:52 -060012157EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter);
12158EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit);