blob: 773cb52cb77583a4be7626540d59ec70beba6fb3 [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"
Carsten Otte313a3dc2007-10-11 19:16:52 +020032
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020033#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030034#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020035#include <linux/kvm.h>
36#include <linux/fs.h>
37#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040038#include <linux/export.h>
39#include <linux/moduleparam.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080040#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050041#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010042#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030043#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010044#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030045#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020046#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080048#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080049#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020050#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020051#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020052#include <linux/timekeeper_internal.h>
53#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080054#include <linux/kvm_irqfd.h>
55#include <linux/irqbypass.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010056#include <linux/sched/stat.h>
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080057#include <linux/sched/isolation.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050058#include <linux/mem_encrypt.h>
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +020059#include <linux/entry-kvm.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010060
Avi Kivityaec51dc2009-07-01 16:01:02 +030061#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080062
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020063#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080064#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020065#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080066#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020067#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020068#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100069#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030070#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080071#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010072#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010073#include <asm/hypervisor.h>
Babu Moger97150922020-09-11 14:29:12 -050074#include <asm/tlbflush.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080075#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090076#include <asm/emulate_prefix.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000077#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020078
Dave Hansend1898b72016-06-01 10:42:20 -070079#define CREATE_TRACE_POINTS
80#include "trace.h"
81
Carsten Otte313a3dc2007-10-11 19:16:52 +020082#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080083#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080084u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
85EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080086
Avi Kivity0f65dd72011-04-20 13:37:53 +030087#define emul_to_vcpu(ctxt) \
Sean Christophersonc9b8b072020-02-18 15:29:48 -080088 ((struct kvm_vcpu *)(ctxt)->vcpu)
Avi Kivity0f65dd72011-04-20 13:37:53 +030089
Joerg Roedel50a37eb2008-01-31 14:57:38 +010090/* EFER defaults:
91 * - enable syscall per default because its emulated by KVM
92 * - enable LME and LMA per default on 64 bit KVM
93 */
94#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080095static
96u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010097#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080098static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010099#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +0200100
Sean Christophersonb11306b2019-12-10 14:44:13 -0800101static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
102
Radim Krčmářc5192652016-07-12 22:09:28 +0200103#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
104 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200105
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300106static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300107static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200108static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100109static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800110static void store_regs(struct kvm_vcpu *vcpu);
111static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200112
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700113struct kvm_x86_ops kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300114EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800115
Paolo Bonzini893590c2015-11-06 11:46:24 +0100116static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030117module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200118
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200119static bool __read_mostly report_ignored_msrs = true;
120module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
121
Wanpeng Li4c276252018-05-05 04:02:32 -0700122unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200123module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
124
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300125static bool __read_mostly kvmclock_periodic_sync = true;
126module_param(kvmclock_periodic_sync, bool, S_IRUGO);
127
Paolo Bonzini893590c2015-11-06 11:46:24 +0100128bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100129EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100130u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100131EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800132u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
133EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
134u64 __read_mostly kvm_max_tsc_scaling_ratio;
135EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700136u64 __read_mostly kvm_default_tsc_scaling_ratio;
137EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100138
Zachary Amsdencc578282012-02-03 15:43:50 -0200139/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100140static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200141module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
142
Sean Christophersonc3941d92019-04-17 10:15:33 -0700143/*
144 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
145 * adaptive tuning starting from default advancment of 1000ns. '0' disables
146 * advancement entirely. Any other value is used as-is and disables adaptive
147 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
148 */
149static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800150module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500151
Feng Wu520040142016-01-25 16:53:33 +0800152static bool __read_mostly vector_hashing = true;
153module_param(vector_hashing, bool, S_IRUGO);
154
Liran Alonc4ae60e2018-03-12 13:12:47 +0200155bool __read_mostly enable_vmware_backdoor = false;
156module_param(enable_vmware_backdoor, bool, S_IRUGO);
157EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
158
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700159static bool __read_mostly force_emulation_prefix = false;
160module_param(force_emulation_prefix, bool, S_IRUGO);
161
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800162int __read_mostly pi_inject_timer = -1;
163module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
164
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700165/*
166 * Restoring the host value for MSRs that are only consumed when running in
167 * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
168 * returns to userspace, i.e. the kernel can run with the guest's value.
169 */
170#define KVM_MAX_NR_USER_RETURN_MSRS 16
Avi Kivity18863bd2009-09-07 11:12:18 +0300171
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700172struct kvm_user_return_msrs_global {
Avi Kivity18863bd2009-09-07 11:12:18 +0300173 int nr;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700174 u32 msrs[KVM_MAX_NR_USER_RETURN_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300175};
176
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700177struct kvm_user_return_msrs {
Avi Kivity18863bd2009-09-07 11:12:18 +0300178 struct user_return_notifier urn;
179 bool registered;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700180 struct kvm_user_return_msr_values {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800181 u64 host;
182 u64 curr;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700183 } values[KVM_MAX_NR_USER_RETURN_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300184};
185
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700186static struct kvm_user_return_msrs_global __read_mostly user_return_msrs_global;
187static struct kvm_user_return_msrs __percpu *user_return_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300188
Sean Christophersoncfc48182020-03-02 15:56:23 -0800189#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
190 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
191 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
192 | XFEATURE_MASK_PKRU)
193
Sean Christopherson91661982020-03-02 15:57:06 -0800194u64 __read_mostly host_efer;
195EXPORT_SYMBOL_GPL(host_efer);
196
Mohammed Gamalb96e6502020-09-03 16:11:22 +0200197bool __read_mostly allow_smaller_maxphyaddr = 0;
Mohammed Gamal3edd6832020-07-10 17:48:11 +0200198EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr);
199
Aaron Lewis139a12c2019-10-21 16:30:25 -0700200static u64 __read_mostly host_xss;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100201u64 __read_mostly supported_xss;
202EXPORT_SYMBOL_GPL(supported_xss);
Aaron Lewis139a12c2019-10-21 16:30:25 -0700203
Hollis Blanchard417bc302007-10-31 17:24:23 -0500204struct kvm_stats_debugfs_item debugfs_entries[] = {
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200205 VCPU_STAT("pf_fixed", pf_fixed),
206 VCPU_STAT("pf_guest", pf_guest),
207 VCPU_STAT("tlb_flush", tlb_flush),
208 VCPU_STAT("invlpg", invlpg),
209 VCPU_STAT("exits", exits),
210 VCPU_STAT("io_exits", io_exits),
211 VCPU_STAT("mmio_exits", mmio_exits),
212 VCPU_STAT("signal_exits", signal_exits),
213 VCPU_STAT("irq_window", irq_window_exits),
214 VCPU_STAT("nmi_window", nmi_window_exits),
215 VCPU_STAT("halt_exits", halt_exits),
216 VCPU_STAT("halt_successful_poll", halt_successful_poll),
217 VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
218 VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
219 VCPU_STAT("halt_wakeup", halt_wakeup),
220 VCPU_STAT("hypercalls", hypercalls),
221 VCPU_STAT("request_irq", request_irq_exits),
222 VCPU_STAT("irq_exits", irq_exits),
223 VCPU_STAT("host_state_reload", host_state_reload),
224 VCPU_STAT("fpu_reload", fpu_reload),
225 VCPU_STAT("insn_emulation", insn_emulation),
226 VCPU_STAT("insn_emulation_fail", insn_emulation_fail),
227 VCPU_STAT("irq_injections", irq_injections),
228 VCPU_STAT("nmi_injections", nmi_injections),
229 VCPU_STAT("req_event", req_event),
230 VCPU_STAT("l1d_flush", l1d_flush),
David Matlackcb953122020-05-08 11:22:40 -0700231 VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
232 VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200233 VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped),
234 VM_STAT("mmu_pte_write", mmu_pte_write),
235 VM_STAT("mmu_pte_updated", mmu_pte_updated),
236 VM_STAT("mmu_pde_zapped", mmu_pde_zapped),
237 VM_STAT("mmu_flooded", mmu_flooded),
238 VM_STAT("mmu_recycled", mmu_recycled),
239 VM_STAT("mmu_cache_miss", mmu_cache_miss),
240 VM_STAT("mmu_unsync", mmu_unsync),
241 VM_STAT("remote_tlb_flush", remote_tlb_flush),
242 VM_STAT("largepages", lpages, .mode = 0444),
243 VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444),
244 VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions),
Hollis Blanchard417bc302007-10-31 17:24:23 -0500245 { NULL }
246};
247
Dexuan Cui2acf9232010-06-10 11:27:12 +0800248u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800249u64 __read_mostly supported_xcr0;
250EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800251
Sean Christopherson80fbd282020-06-08 11:02:18 -0700252static struct kmem_cache *x86_fpu_cache;
Marc Orrb666a4b2018-11-06 14:53:56 -0800253
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800254static struct kmem_cache *x86_emulator_cache;
255
Peter Xu6abe9c12020-06-22 18:04:41 -0400256/*
257 * When called, it means the previous get/set msr reached an invalid msr.
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200258 * Return true if we want to ignore/silent this failed msr access.
Peter Xu6abe9c12020-06-22 18:04:41 -0400259 */
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200260static bool kvm_msr_ignored_check(struct kvm_vcpu *vcpu, u32 msr,
261 u64 data, bool write)
Peter Xu6abe9c12020-06-22 18:04:41 -0400262{
263 const char *op = write ? "wrmsr" : "rdmsr";
264
265 if (ignore_msrs) {
266 if (report_ignored_msrs)
Takashi Iwaid383b312020-10-30 16:14:14 +0100267 kvm_pr_unimpl("ignored %s: 0x%x data 0x%llx\n",
268 op, msr, data);
Peter Xu6abe9c12020-06-22 18:04:41 -0400269 /* Mask the error */
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200270 return true;
Peter Xu6abe9c12020-06-22 18:04:41 -0400271 } else {
Takashi Iwaid383b312020-10-30 16:14:14 +0100272 kvm_debug_ratelimited("unhandled %s: 0x%x data 0x%llx\n",
273 op, msr, data);
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200274 return false;
Peter Xu6abe9c12020-06-22 18:04:41 -0400275 }
276}
277
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800278static struct kmem_cache *kvm_alloc_emulator_cache(void)
279{
Sean Christopherson06add252020-02-18 15:29:50 -0800280 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
281 unsigned int size = sizeof(struct x86_emulate_ctxt);
282
283 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800284 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800285 SLAB_ACCOUNT, useroffset,
286 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800287}
288
Jan Kiszkab6785de2012-09-20 07:43:17 +0200289static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300290
Gleb Natapovaf585b92010-10-14 11:22:46 +0200291static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
292{
293 int i;
Peter Xudd03bca2020-04-16 11:58:59 -0400294 for (i = 0; i < ASYNC_PF_PER_VCPU; i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +0200295 vcpu->arch.apf.gfns[i] = ~0;
296}
297
Avi Kivity18863bd2009-09-07 11:12:18 +0300298static void kvm_on_user_return(struct user_return_notifier *urn)
299{
300 unsigned slot;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700301 struct kvm_user_return_msrs *msrs
302 = container_of(urn, struct kvm_user_return_msrs, urn);
303 struct kvm_user_return_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700304 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300305
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700306 /*
307 * Disabling irqs at this point since the following code could be
308 * interrupted and executed through kvm_arch_hardware_disable()
309 */
310 local_irq_save(flags);
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700311 if (msrs->registered) {
312 msrs->registered = false;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700313 user_return_notifier_unregister(urn);
314 }
315 local_irq_restore(flags);
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700316 for (slot = 0; slot < user_return_msrs_global.nr; ++slot) {
317 values = &msrs->values[slot];
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800318 if (values->host != values->curr) {
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700319 wrmsrl(user_return_msrs_global.msrs[slot], values->host);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800320 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300321 }
322 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300323}
324
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700325void kvm_define_user_return_msr(unsigned slot, u32 msr)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800326{
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700327 BUG_ON(slot >= KVM_MAX_NR_USER_RETURN_MSRS);
328 user_return_msrs_global.msrs[slot] = msr;
329 if (slot >= user_return_msrs_global.nr)
330 user_return_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300331}
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700332EXPORT_SYMBOL_GPL(kvm_define_user_return_msr);
Avi Kivity18863bd2009-09-07 11:12:18 +0300333
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700334static void kvm_user_return_msr_cpu_online(void)
Avi Kivity18863bd2009-09-07 11:12:18 +0300335{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800336 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700337 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Sean Christopherson05c19c22019-11-22 12:04:50 -0800338 u64 value;
339 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300340
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700341 for (i = 0; i < user_return_msrs_global.nr; ++i) {
342 rdmsrl_safe(user_return_msrs_global.msrs[i], &value);
343 msrs->values[i].host = value;
344 msrs->values[i].curr = value;
Sean Christopherson05c19c22019-11-22 12:04:50 -0800345 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300346}
347
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700348int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300349{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200350 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700351 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700352 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300353
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700354 value = (value & mask) | (msrs->values[slot].host & ~mask);
355 if (value == msrs->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700356 return 0;
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700357 err = wrmsrl_safe(user_return_msrs_global.msrs[slot], value);
Andy Honig8b3c3102014-08-27 11:16:44 -0700358 if (err)
359 return 1;
360
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700361 msrs->values[slot].curr = value;
362 if (!msrs->registered) {
363 msrs->urn.on_user_return = kvm_on_user_return;
364 user_return_notifier_register(&msrs->urn);
365 msrs->registered = true;
Avi Kivity18863bd2009-09-07 11:12:18 +0300366 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700367 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300368}
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700369EXPORT_SYMBOL_GPL(kvm_set_user_return_msr);
Avi Kivity18863bd2009-09-07 11:12:18 +0300370
Radim Krčmář13a34e02014-08-28 15:13:03 +0200371static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200372{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200373 unsigned int cpu = smp_processor_id();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700374 struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200375
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700376 if (msrs->registered)
377 kvm_on_user_return(&msrs->urn);
Avi Kivity3548bab2009-11-28 14:18:47 +0200378}
379
Carsten Otte6866b832007-10-29 16:09:10 +0100380u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
381{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300382 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100383}
384EXPORT_SYMBOL_GPL(kvm_get_apic_base);
385
Jim Mattson58871642018-05-09 16:56:04 -0400386enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
387{
388 return kvm_apic_mode(kvm_get_apic_base(vcpu));
389}
390EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
391
Jan Kiszka58cb6282014-01-24 16:48:44 +0100392int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100393{
Jim Mattson58871642018-05-09 16:56:04 -0400394 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
395 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200396 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
397 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100398
Jim Mattson58871642018-05-09 16:56:04 -0400399 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700400 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400401 if (!msr_info->host_initiated) {
402 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
403 return 1;
404 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
405 return 1;
406 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100407
408 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800409 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100410 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100411}
412EXPORT_SYMBOL_GPL(kvm_set_apic_base);
413
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +0200414asmlinkage __visible noinstr void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000415{
416 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200417 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000418}
419EXPORT_SYMBOL_GPL(kvm_spurious_fault);
420
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200421#define EXCPT_BENIGN 0
422#define EXCPT_CONTRIBUTORY 1
423#define EXCPT_PF 2
424
425static int exception_class(int vector)
426{
427 switch (vector) {
428 case PF_VECTOR:
429 return EXCPT_PF;
430 case DE_VECTOR:
431 case TS_VECTOR:
432 case NP_VECTOR:
433 case SS_VECTOR:
434 case GP_VECTOR:
435 return EXCPT_CONTRIBUTORY;
436 default:
437 break;
438 }
439 return EXCPT_BENIGN;
440}
441
Nadav Amitd6e8c852014-07-24 14:51:24 +0300442#define EXCPT_FAULT 0
443#define EXCPT_TRAP 1
444#define EXCPT_ABORT 2
445#define EXCPT_INTERRUPT 3
446
447static int exception_type(int vector)
448{
449 unsigned int mask;
450
451 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
452 return EXCPT_INTERRUPT;
453
454 mask = 1 << vector;
455
456 /* #DB is trap, as instruction watchpoints are handled elsewhere */
457 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
458 return EXCPT_TRAP;
459
460 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
461 return EXCPT_ABORT;
462
463 /* Reserved exceptions will result in fault */
464 return EXCPT_FAULT;
465}
466
Jim Mattsonda998b42018-10-16 14:29:22 -0700467void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
468{
469 unsigned nr = vcpu->arch.exception.nr;
470 bool has_payload = vcpu->arch.exception.has_payload;
471 unsigned long payload = vcpu->arch.exception.payload;
472
473 if (!has_payload)
474 return;
475
476 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700477 case DB_VECTOR:
478 /*
479 * "Certain debug exceptions may clear bit 0-3. The
480 * remaining contents of the DR6 register are never
481 * cleared by the processor".
482 */
483 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
484 /*
485 * DR6.RTM is set by all #DB exceptions that don't clear it.
486 */
487 vcpu->arch.dr6 |= DR6_RTM;
488 vcpu->arch.dr6 |= payload;
489 /*
490 * Bit 16 should be set in the payload whenever the #DB
491 * exception should clear DR6.RTM. This makes the payload
492 * compatible with the pending debug exceptions under VMX.
493 * Though not currently documented in the SDM, this also
494 * makes the payload compatible with the exit qualification
495 * for #DB exceptions under VMX.
496 */
497 vcpu->arch.dr6 ^= payload & DR6_RTM;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800498
499 /*
500 * The #DB payload is defined as compatible with the 'pending
501 * debug exceptions' field under VMX, not DR6. While bit 12 is
502 * defined in the 'pending debug exceptions' field (enabled
503 * breakpoint), it is reserved and must be zero in DR6.
504 */
505 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700506 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700507 case PF_VECTOR:
508 vcpu->arch.cr2 = payload;
509 break;
510 }
511
512 vcpu->arch.exception.has_payload = false;
513 vcpu->arch.exception.payload = 0;
514}
515EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
516
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200517static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200518 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700519 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200520{
521 u32 prev_nr;
522 int class1, class2;
523
Avi Kivity3842d132010-07-27 12:30:24 +0300524 kvm_make_request(KVM_REQ_EVENT, vcpu);
525
Wanpeng Li664f8e22017-08-24 03:35:09 -0700526 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200527 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200528 if (has_error && !is_protmode(vcpu))
529 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700530 if (reinject) {
531 /*
532 * On vmentry, vcpu->arch.exception.pending is only
533 * true if an event injection was blocked by
534 * nested_run_pending. In that case, however,
535 * vcpu_enter_guest requests an immediate exit,
536 * and the guest shouldn't proceed far enough to
537 * need reinjection.
538 */
539 WARN_ON_ONCE(vcpu->arch.exception.pending);
540 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700541 if (WARN_ON_ONCE(has_payload)) {
542 /*
543 * A reinjected event has already
544 * delivered its payload.
545 */
546 has_payload = false;
547 payload = 0;
548 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700549 } else {
550 vcpu->arch.exception.pending = true;
551 vcpu->arch.exception.injected = false;
552 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200553 vcpu->arch.exception.has_error_code = has_error;
554 vcpu->arch.exception.nr = nr;
555 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700556 vcpu->arch.exception.has_payload = has_payload;
557 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800558 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700559 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200560 return;
561 }
562
563 /* to check exception */
564 prev_nr = vcpu->arch.exception.nr;
565 if (prev_nr == DF_VECTOR) {
566 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300567 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200568 return;
569 }
570 class1 = exception_class(prev_nr);
571 class2 = exception_class(nr);
572 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
573 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700574 /*
575 * Generate double fault per SDM Table 5-5. Set
576 * exception.pending = true so that the double fault
577 * can trigger a nested vmexit.
578 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200579 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700580 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200581 vcpu->arch.exception.has_error_code = true;
582 vcpu->arch.exception.nr = DF_VECTOR;
583 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700584 vcpu->arch.exception.has_payload = false;
585 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200586 } else
587 /* replace previous exception with a new one in a hope
588 that instruction re-execution will regenerate lost
589 exception */
590 goto queue;
591}
592
Avi Kivity298101d2007-11-25 13:41:11 +0200593void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
594{
Jim Mattson91e86d22018-10-16 14:29:21 -0700595 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200596}
597EXPORT_SYMBOL_GPL(kvm_queue_exception);
598
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200599void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
600{
Jim Mattson91e86d22018-10-16 14:29:21 -0700601 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200602}
603EXPORT_SYMBOL_GPL(kvm_requeue_exception);
604
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400605void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
606 unsigned long payload)
Jim Mattsonf10c7292018-10-16 14:29:23 -0700607{
608 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
609}
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400610EXPORT_SYMBOL_GPL(kvm_queue_exception_p);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700611
Jim Mattsonda998b42018-10-16 14:29:22 -0700612static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
613 u32 error_code, unsigned long payload)
614{
615 kvm_multiple_exception(vcpu, nr, true, error_code,
616 true, payload, false);
617}
618
Kyle Huey6affcbe2016-11-29 12:40:40 -0800619int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200620{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100621 if (err)
622 kvm_inject_gp(vcpu, 0);
623 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800624 return kvm_skip_emulated_instruction(vcpu);
625
626 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100627}
628EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200629
Avi Kivity6389ee92010-11-29 16:12:30 +0200630void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200631{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200632 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700633 vcpu->arch.exception.nested_apf =
634 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700635 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700636 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700637 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
638 } else {
639 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
640 fault->address);
641 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200642}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300643EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200644
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700645bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
646 struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200647{
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400648 struct kvm_mmu *fault_mmu;
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700649 WARN_ON_ONCE(fault->vector != PF_VECTOR);
650
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400651 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu :
652 vcpu->arch.walk_mmu;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200653
Junaid Shahidee1fa202020-03-20 14:28:03 -0700654 /*
655 * Invalidate the TLB entry for the faulting address, if it exists,
656 * else the access will fault indefinitely (and to emulate hardware).
657 */
658 if ((fault->error_code & PFERR_PRESENT_MASK) &&
659 !(fault->error_code & PFERR_RSVD_MASK))
660 kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address,
661 fault_mmu->root_hpa);
662
663 fault_mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200664 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200665}
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700666EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200667
Sheng Yang3419ffc2008-05-15 09:52:48 +0800668void kvm_inject_nmi(struct kvm_vcpu *vcpu)
669{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300670 atomic_inc(&vcpu->arch.nmi_queued);
671 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800672}
673EXPORT_SYMBOL_GPL(kvm_inject_nmi);
674
Avi Kivity298101d2007-11-25 13:41:11 +0200675void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
676{
Jim Mattson91e86d22018-10-16 14:29:21 -0700677 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200678}
679EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
680
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200681void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
682{
Jim Mattson91e86d22018-10-16 14:29:21 -0700683 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200684}
685EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
686
Carsten Ottea03490e2007-10-29 16:09:35 +0100687/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300688 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
689 * a #GP and return false.
690 */
691bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200692{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700693 if (kvm_x86_ops.get_cpl(vcpu) <= required_cpl)
Avi Kivity0a79b002009-09-01 12:03:25 +0300694 return true;
695 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
696 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100697}
Avi Kivity0a79b002009-09-01 12:03:25 +0300698EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100699
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300700bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
701{
702 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
703 return true;
704
705 kvm_queue_exception(vcpu, UD_VECTOR);
706 return false;
707}
708EXPORT_SYMBOL_GPL(kvm_require_dr);
709
Carsten Ottea03490e2007-10-29 16:09:35 +0100710/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200711 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200712 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200713 * can read from guest physical or from the guest's guest physical memory.
714 */
715int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
716 gfn_t ngfn, void *data, int offset, int len,
717 u32 access)
718{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200719 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200720 gfn_t real_gfn;
721 gpa_t ngpa;
722
723 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200724 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200725 if (real_gfn == UNMAPPED_GVA)
726 return -EFAULT;
727
728 real_gfn = gpa_to_gfn(real_gfn);
729
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200730 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200731}
732EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
733
Fengguang Wu69b00492015-01-19 22:33:39 +0800734static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200735 void *data, int offset, int len, u32 access)
736{
737 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
738 data, offset, len, access);
739}
740
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700741static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
742{
743 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
744 rsvd_bits(1, 2);
745}
746
Joerg Roedelec92fe42010-09-10 17:30:51 +0200747/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700748 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100749 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200750int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100751{
752 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
753 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
754 int i;
755 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200756 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100757
Joerg Roedelff03a072010-09-10 17:30:57 +0200758 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
759 offset * sizeof(u64), sizeof(pdpte),
760 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100761 if (ret < 0) {
762 ret = 0;
763 goto out;
764 }
765 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400766 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700767 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100768 ret = 0;
769 goto out;
770 }
771 }
772 ret = 1;
773
Joerg Roedelff03a072010-09-10 17:30:57 +0200774 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700775 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
776
Carsten Ottea03490e2007-10-29 16:09:35 +0100777out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100778
779 return ret;
780}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100781EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100782
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100783bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200784{
Joerg Roedelff03a072010-09-10 17:30:57 +0200785 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200786 int offset;
787 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200788 int r;
789
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200790 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200791 return false;
792
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700793 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300794 return true;
795
Paolo Bonzinia5121772017-07-24 18:54:38 +0200796 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
797 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200798 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
799 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200800 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800801 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200802
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800803 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200804}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100805EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200806
Avi Kivity49a9b072010-06-10 17:02:14 +0300807int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100808{
Sheng Yangaad82702010-05-12 16:40:42 +0800809 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Jim Mattsond42e3fa2020-07-07 15:36:30 -0700810 unsigned long pdptr_bits = X86_CR0_CD | X86_CR0_NW | X86_CR0_PG;
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800811 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800812
Avi Kivityf9a48e62010-01-06 19:10:22 +0200813 cr0 |= X86_CR0_ET;
814
Gleb Natapovab344822010-01-21 15:28:46 +0200815#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300816 if (cr0 & 0xffffffff00000000UL)
817 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200818#endif
819
820 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100821
Gleb Natapov0f122442010-04-28 19:15:31 +0300822 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
823 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100824
Gleb Natapov0f122442010-04-28 19:15:31 +0300825 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
826 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100827
Carsten Ottea03490e2007-10-29 16:09:35 +0100828#ifdef CONFIG_X86_64
Sean Christopherson05487212020-07-13 18:57:32 -0700829 if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) &&
830 (cr0 & X86_CR0_PG)) {
831 int cs_db, cs_l;
Carsten Ottea03490e2007-10-29 16:09:35 +0100832
Sean Christopherson05487212020-07-13 18:57:32 -0700833 if (!is_pae(vcpu))
834 return 1;
835 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
836 if (cs_l)
Gleb Natapov0f122442010-04-28 19:15:31 +0300837 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100838 }
Sean Christopherson05487212020-07-13 18:57:32 -0700839#endif
840 if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) &&
841 is_pae(vcpu) && ((cr0 ^ old_cr0) & pdptr_bits) &&
842 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)))
843 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100844
Mao, Junjiead756a12012-07-02 01:18:48 +0000845 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
846 return 1;
847
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700848 kvm_x86_ops.set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100849
Lai Jiangshand170c412011-02-21 11:21:30 +0800850 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800851 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800852 kvm_async_pf_hash_reset(vcpu);
853 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800854
Sheng Yangaad82702010-05-12 16:40:42 +0800855 if ((cr0 ^ old_cr0) & update_bits)
856 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800857
Laszlo Ersek879ae182015-11-04 12:54:41 +0100858 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
859 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
860 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800861 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
862
Gleb Natapov0f122442010-04-28 19:15:31 +0300863 return 0;
864}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200865EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100866
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200867void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100868{
Avi Kivity49a9b072010-06-10 17:02:14 +0300869 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100870}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200871EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100872
Aaron Lewis139a12c2019-10-21 16:30:25 -0700873void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300874{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700875 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300876
Aaron Lewis139a12c2019-10-21 16:30:25 -0700877 if (vcpu->arch.xcr0 != host_xcr0)
878 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
879
880 if (vcpu->arch.xsaves_enabled &&
881 vcpu->arch.ia32_xss != host_xss)
882 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
883 }
Babu Moger37486132020-05-12 18:59:06 -0500884
885 if (static_cpu_has(X86_FEATURE_PKU) &&
886 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
887 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) &&
888 vcpu->arch.pkru != vcpu->arch.host_pkru)
889 __write_pkru(vcpu->arch.pkru);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300890}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700891EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
892
893void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
894{
Babu Moger37486132020-05-12 18:59:06 -0500895 if (static_cpu_has(X86_FEATURE_PKU) &&
896 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
897 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) {
898 vcpu->arch.pkru = rdpkru();
899 if (vcpu->arch.pkru != vcpu->arch.host_pkru)
900 __write_pkru(vcpu->arch.host_pkru);
901 }
902
Aaron Lewis139a12c2019-10-21 16:30:25 -0700903 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
904
905 if (vcpu->arch.xcr0 != host_xcr0)
906 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
907
908 if (vcpu->arch.xsaves_enabled &&
909 vcpu->arch.ia32_xss != host_xss)
910 wrmsrl(MSR_IA32_XSS, host_xss);
911 }
912
913}
914EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300915
Fengguang Wu69b00492015-01-19 22:33:39 +0800916static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800917{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000918 u64 xcr0 = xcr;
919 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200920 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800921
922 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
923 if (index != XCR_XFEATURE_ENABLED_MASK)
924 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700925 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800926 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700927 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800928 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200929
930 /*
931 * Do not allow the guest to set bits that we do not support
932 * saving. However, xcr0 bit 0 is always set, even if the
933 * emulated CPU does not support XSAVE (see fx_init).
934 */
Dave Hansend91cab72015-09-02 16:31:26 -0700935 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200936 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800937 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200938
Dave Hansend91cab72015-09-02 16:31:26 -0700939 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
940 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000941 return 1;
942
Dave Hansend91cab72015-09-02 16:31:26 -0700943 if (xcr0 & XFEATURE_MASK_AVX512) {
944 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800945 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700946 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800947 return 1;
948 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800949 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000950
Dave Hansend91cab72015-09-02 16:31:26 -0700951 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Xiaoyao Liaedbaf42020-07-09 12:34:23 +0800952 kvm_update_cpuid_runtime(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800953 return 0;
954}
955
956int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
957{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700958 if (kvm_x86_ops.get_cpl(vcpu) != 0 ||
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000959 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800960 kvm_inject_gp(vcpu, 0);
961 return 1;
962 }
963 return 0;
964}
965EXPORT_SYMBOL_GPL(kvm_set_xcr);
966
Sean Christophersonee69c922020-10-06 18:44:16 -0700967bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Wanpeng Li3ca94192019-09-18 17:50:10 +0800968{
Sean Christophersonb11306b2019-12-10 14:44:13 -0800969 if (cr4 & cr4_reserved_bits)
Sean Christophersonee69c922020-10-06 18:44:16 -0700970 return false;
Wanpeng Li3ca94192019-09-18 17:50:10 +0800971
Krish Sadhukhanb899c132020-07-08 00:39:55 +0000972 if (cr4 & vcpu->arch.cr4_guest_rsvd_bits)
Sean Christophersonee69c922020-10-06 18:44:16 -0700973 return false;
Wanpeng Li3ca94192019-09-18 17:50:10 +0800974
Sean Christophersonee69c922020-10-06 18:44:16 -0700975 return kvm_x86_ops.is_valid_cr4(vcpu, cr4);
Wanpeng Li3ca94192019-09-18 17:50:10 +0800976}
Sean Christophersonee69c922020-10-06 18:44:16 -0700977EXPORT_SYMBOL_GPL(kvm_is_valid_cr4);
Wanpeng Li3ca94192019-09-18 17:50:10 +0800978
Avi Kivitya83b29c2010-06-10 17:02:15 +0300979int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100980{
Avi Kivityfc78f512009-12-07 12:16:48 +0200981 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800982 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Jim Mattsoncb957ad2020-08-17 11:16:54 -0700983 X86_CR4_SMEP;
Sean Christopherson8d214c42020-09-23 14:53:52 -0700984 unsigned long mmu_role_bits = pdptr_bits | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800985
Sean Christophersonee69c922020-10-06 18:44:16 -0700986 if (!kvm_is_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200987 return 1;
988
Carsten Ottea03490e2007-10-29 16:09:35 +0100989 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300990 if (!(cr4 & X86_CR4_PAE))
991 return 1;
Sean Christophersond74fcfc2020-07-02 19:17:14 -0700992 if ((cr4 ^ old_cr4) & X86_CR4_LA57)
993 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300994 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
995 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200996 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
997 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300998 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100999
Mao, Junjiead756a12012-07-02 01:18:48 +00001000 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +02001001 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +00001002 return 1;
1003
1004 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
1005 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
1006 return 1;
1007 }
1008
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07001009 kvm_x86_ops.set_cr4(vcpu, cr4);
Gleb Natapov0f122442010-04-28 19:15:31 +03001010
Sean Christopherson8d214c42020-09-23 14:53:52 -07001011 if (((cr4 ^ old_cr4) & mmu_role_bits) ||
Mao, Junjiead756a12012-07-02 01:18:48 +00001012 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +08001013 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +03001014
Huaitong Hanb9baba82016-03-22 16:51:21 +08001015 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08001016 kvm_update_cpuid_runtime(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001017
Gleb Natapov0f122442010-04-28 19:15:31 +03001018 return 0;
1019}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001020EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +01001021
Avi Kivity23902182010-06-10 17:02:16 +03001022int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +01001023{
Junaid Shahidade61e22018-06-27 14:59:15 -07001024 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +01001025#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -07001026 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
1027
Junaid Shahidade61e22018-06-27 14:59:15 -07001028 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001029 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1030 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001031 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001032#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001033
Avi Kivity9f8fe502010-12-05 17:30:00 +02001034 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001035 if (!skip_tlb_flush) {
1036 kvm_mmu_sync_roots(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001037 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001038 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001039 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001040 }
1041
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001042 if (is_long_mode(vcpu) &&
Babu Moger01079732020-11-12 16:17:56 -06001043 (cr3 & vcpu->arch.cr3_lm_rsvd_bits))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001044 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02001045 else if (is_pae_paging(vcpu) &&
1046 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001047 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001048
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07001049 kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001050 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001051 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001052
Gleb Natapov0f122442010-04-28 19:15:31 +03001053 return 0;
1054}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001055EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001056
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001057int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001058{
Gleb Natapov0f122442010-04-28 19:15:31 +03001059 if (cr8 & CR8_RESERVED_BITS)
1060 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001061 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001062 kvm_lapic_set_tpr(vcpu, cr8);
1063 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001064 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001065 return 0;
1066}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001067EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001068
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001069unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001070{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001071 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001072 return kvm_lapic_get_cr8(vcpu);
1073 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001074 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001075}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001076EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001077
Nadav Amitae561ed2015-04-02 03:10:37 +03001078static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1079{
1080 int i;
1081
1082 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1083 for (i = 0; i < KVM_NR_DB_REGS; i++)
1084 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1085 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1086 }
1087}
1088
Paolo Bonzini7c866632020-05-16 08:42:28 -04001089void kvm_update_dr7(struct kvm_vcpu *vcpu)
Jan Kiszkac8639012012-09-21 05:42:55 +02001090{
1091 unsigned long dr7;
1092
1093 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1094 dr7 = vcpu->arch.guest_debug_dr7;
1095 else
1096 dr7 = vcpu->arch.dr7;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001097 kvm_x86_ops.set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001098 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1099 if (dr7 & DR7_BP_EN_MASK)
1100 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001101}
Paolo Bonzini7c866632020-05-16 08:42:28 -04001102EXPORT_SYMBOL_GPL(kvm_update_dr7);
Jan Kiszkac8639012012-09-21 05:42:55 +02001103
Nadav Amit6f43ed02014-07-15 17:37:46 +03001104static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1105{
1106 u64 fixed = DR6_FIXED_1;
1107
Radim Krčmářd6321d42017-08-05 00:12:49 +02001108 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001109 fixed |= DR6_RTM;
1110 return fixed;
1111}
1112
Gleb Natapov338dbc92010-04-28 19:15:32 +03001113static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001114{
Marios Pomonisea740052019-12-11 12:47:52 -08001115 size_t size = ARRAY_SIZE(vcpu->arch.db);
1116
Gleb Natapov020df072010-04-13 10:05:23 +03001117 switch (dr) {
1118 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001119 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001120 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1121 vcpu->arch.eff_db[dr] = val;
1122 break;
1123 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001124 case 6:
Krish Sadhukhanf5f61452020-05-22 18:19:51 -04001125 if (!kvm_dr6_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001126 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001127 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001128 break;
1129 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001130 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001131 if (!kvm_dr7_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001132 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001133 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001134 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001135 break;
1136 }
1137
1138 return 0;
1139}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001140
1141int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1142{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001143 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001144 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001145 return 1;
1146 }
1147 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001148}
Gleb Natapov020df072010-04-13 10:05:23 +03001149EXPORT_SYMBOL_GPL(kvm_set_dr);
1150
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001151int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001152{
Marios Pomonisea740052019-12-11 12:47:52 -08001153 size_t size = ARRAY_SIZE(vcpu->arch.db);
1154
Gleb Natapov020df072010-04-13 10:05:23 +03001155 switch (dr) {
1156 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001157 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001158 break;
1159 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001160 case 6:
Paolo Bonzini5679b802020-05-04 11:28:25 -04001161 *val = vcpu->arch.dr6;
Gleb Natapov020df072010-04-13 10:05:23 +03001162 break;
1163 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001164 default: /* 7 */
1165 *val = vcpu->arch.dr7;
1166 break;
1167 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001168 return 0;
1169}
Gleb Natapov020df072010-04-13 10:05:23 +03001170EXPORT_SYMBOL_GPL(kvm_get_dr);
1171
Avi Kivity022cd0e2011-11-10 14:57:23 +02001172bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1173{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001174 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001175 u64 data;
1176 int err;
1177
Wei Huangc6702c92015-06-19 13:44:45 +02001178 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001179 if (err)
1180 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001181 kvm_rax_write(vcpu, (u32)data);
1182 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001183 return err;
1184}
1185EXPORT_SYMBOL_GPL(kvm_rdpmc);
1186
Carsten Otte043405e2007-10-10 17:16:19 +02001187/*
1188 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1189 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1190 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001191 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1192 * extract the supported MSRs from the related const lists.
1193 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001194 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001195 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001196 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001197 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001198
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001199static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001200 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001201 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001202#ifdef CONFIG_X86_64
1203 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1204#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001205 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001206 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001207 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001208 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1209 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1210 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1211 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1212 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1213 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001214 MSR_IA32_UMWAIT_CONTROL,
1215
Jim Mattsone2ada662019-08-21 11:20:04 -07001216 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1217 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1218 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1219 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1220 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1221 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1222 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1223 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1224 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1225 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1226 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1227 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1228 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001229 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1230 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1231 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1232 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1233 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1234 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1235 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1236 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1237 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001238};
1239
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001240static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001241static unsigned num_msrs_to_save;
1242
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001243static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001244 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1245 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1246 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1247 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001248 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001249 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1250 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001251 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001252 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001253 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001254 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001255 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001256 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001257 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1258 HV_X64_MSR_TSC_EMULATION_STATUS,
Jon Doronf97f5a52020-05-29 16:45:40 +03001259 HV_X64_MSR_SYNDBG_OPTIONS,
1260 HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS,
1261 HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER,
1262 HV_X64_MSR_SYNDBG_PENDING_BUFFER,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001263
1264 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02001265 MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001266
Will Auldba904632012-11-29 12:42:50 -08001267 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001268 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001269 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001270 MSR_IA32_PERF_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001271 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001272 MSR_IA32_MCG_STATUS,
1273 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001274 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001275 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001276 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001277 MSR_PLATFORM_INFO,
1278 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001279 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001280 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001281 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001282
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001283 /*
1284 * The following list leaves out MSRs whose values are determined
1285 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1286 * We always support the "true" VMX control MSRs, even if the host
1287 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001288 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001289 */
1290 MSR_IA32_VMX_BASIC,
1291 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1292 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1293 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1294 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1295 MSR_IA32_VMX_MISC,
1296 MSR_IA32_VMX_CR0_FIXED0,
1297 MSR_IA32_VMX_CR4_FIXED0,
1298 MSR_IA32_VMX_VMCS_ENUM,
1299 MSR_IA32_VMX_PROCBASED_CTLS2,
1300 MSR_IA32_VMX_EPT_VPID_CAP,
1301 MSR_IA32_VMX_VMFUNC,
1302
Borislav Petkov191c8132019-04-18 18:32:50 +02001303 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001304 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001305};
1306
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001307static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001308static unsigned num_emulated_msrs;
1309
Tom Lendacky801e4592018-02-21 13:39:51 -06001310/*
1311 * List of msr numbers which are used to expose MSR-based features that
1312 * can be used by a hypervisor to validate requested CPU features.
1313 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001314static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001315 MSR_IA32_VMX_BASIC,
1316 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1317 MSR_IA32_VMX_PINBASED_CTLS,
1318 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1319 MSR_IA32_VMX_PROCBASED_CTLS,
1320 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1321 MSR_IA32_VMX_EXIT_CTLS,
1322 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1323 MSR_IA32_VMX_ENTRY_CTLS,
1324 MSR_IA32_VMX_MISC,
1325 MSR_IA32_VMX_CR0_FIXED0,
1326 MSR_IA32_VMX_CR0_FIXED1,
1327 MSR_IA32_VMX_CR4_FIXED0,
1328 MSR_IA32_VMX_CR4_FIXED1,
1329 MSR_IA32_VMX_VMCS_ENUM,
1330 MSR_IA32_VMX_PROCBASED_CTLS2,
1331 MSR_IA32_VMX_EPT_VPID_CAP,
1332 MSR_IA32_VMX_VMFUNC,
1333
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001334 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001335 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001336 MSR_IA32_ARCH_CAPABILITIES,
Like Xu27461da32020-05-29 15:43:45 +08001337 MSR_IA32_PERF_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001338};
1339
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001340static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001341static unsigned int num_msr_based_features;
1342
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001343static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001344{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001345 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001346
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001347 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1348 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001349
1350 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001351 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1352 * the nested hypervisor runs with NX huge pages. If it is not,
1353 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1354 * L1 guests, so it need not worry about its own (L2) guests.
1355 */
1356 data |= ARCH_CAP_PSCHANGE_MC_NO;
1357
1358 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001359 * If we're doing cache flushes (either "always" or "cond")
1360 * we will do one whenever the guest does a vmlaunch/vmresume.
1361 * If an outer hypervisor is doing the cache flush for us
1362 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1363 * capability to the guest too, and if EPT is disabled we're not
1364 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1365 * require a nested hypervisor to do a flush of its own.
1366 */
1367 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1368 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1369
Paolo Bonzini0c549142019-08-19 17:24:07 +02001370 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1371 data |= ARCH_CAP_RDCL_NO;
1372 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1373 data |= ARCH_CAP_SSB_NO;
1374 if (!boot_cpu_has_bug(X86_BUG_MDS))
1375 data |= ARCH_CAP_MDS_NO;
1376
Pawan Guptae1d38b62019-10-23 12:23:33 +02001377 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001378 * On TAA affected systems:
1379 * - nothing to do if TSX is disabled on the host.
1380 * - we emulate TSX_CTRL if present on the host.
1381 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001382 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001383 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001384 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001385 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1386 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001387
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001388 return data;
1389}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001390
Wanpeng Li66421c12018-02-28 14:03:30 +08001391static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1392{
1393 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001394 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001395 msr->data = kvm_get_arch_capabilities();
1396 break;
1397 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001398 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001399 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001400 default:
Peter Xu12bc2132020-06-22 18:04:42 -04001401 return kvm_x86_ops.get_msr_feature(msr);
Wanpeng Li66421c12018-02-28 14:03:30 +08001402 }
1403 return 0;
1404}
1405
Tom Lendacky801e4592018-02-21 13:39:51 -06001406static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1407{
1408 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001409 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001410
1411 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001412 r = kvm_get_msr_feature(&msr);
Peter Xu12bc2132020-06-22 18:04:42 -04001413
1414 if (r == KVM_MSR_RET_INVALID) {
1415 /* Unconditionally clear the output for simplicity */
1416 *data = 0;
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001417 if (kvm_msr_ignored_check(vcpu, index, 0, false))
1418 r = 0;
Peter Xu12bc2132020-06-22 18:04:42 -04001419 }
1420
Wanpeng Li66421c12018-02-28 14:03:30 +08001421 if (r)
1422 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001423
1424 *data = msr.data;
1425
1426 return 0;
1427}
1428
Sean Christopherson11988492019-04-02 08:19:15 -07001429static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1430{
1431 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1432 return false;
1433
1434 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1435 return false;
1436
Sean Christopherson0a629562019-04-02 08:19:16 -07001437 if (efer & (EFER_LME | EFER_LMA) &&
1438 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1439 return false;
1440
1441 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1442 return false;
1443
Sean Christopherson11988492019-04-02 08:19:15 -07001444 return true;
1445
1446}
Jan Kiszka384bb782013-04-20 10:52:36 +02001447bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001448{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001449 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001450 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001451
Sean Christopherson11988492019-04-02 08:19:15 -07001452 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001453}
1454EXPORT_SYMBOL_GPL(kvm_valid_efer);
1455
Sean Christopherson11988492019-04-02 08:19:15 -07001456static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001457{
1458 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001459 u64 efer = msr_info->data;
Maxim Levitsky72f211e2020-10-01 14:29:53 +03001460 int r;
Jan Kiszka384bb782013-04-20 10:52:36 +02001461
Sean Christopherson11988492019-04-02 08:19:15 -07001462 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001463 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001464
Sean Christopherson11988492019-04-02 08:19:15 -07001465 if (!msr_info->host_initiated) {
1466 if (!__kvm_valid_efer(vcpu, efer))
1467 return 1;
1468
1469 if (is_paging(vcpu) &&
1470 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1471 return 1;
1472 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001473
Carsten Otte15c4a642007-10-30 18:44:17 +01001474 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001475 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001476
Maxim Levitsky72f211e2020-10-01 14:29:53 +03001477 r = kvm_x86_ops.set_efer(vcpu, efer);
1478 if (r) {
1479 WARN_ON(r > 0);
1480 return r;
1481 }
Sheng Yanga3d204e2010-05-12 16:40:40 +08001482
Sheng Yangaad82702010-05-12 16:40:42 +08001483 /* Update reserved bits */
1484 if ((efer ^ old_efer) & EFER_NX)
1485 kvm_mmu_reset_context(vcpu);
1486
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001487 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001488}
1489
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001490void kvm_enable_efer_bits(u64 mask)
1491{
1492 efer_reserved_bits &= ~mask;
1493}
1494EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1495
Alexander Graf51de8152020-09-25 16:34:17 +02001496bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type)
1497{
Alexander Graf1a1552542020-09-25 16:34:21 +02001498 struct kvm *kvm = vcpu->kvm;
1499 struct msr_bitmap_range *ranges = kvm->arch.msr_filter.ranges;
1500 u32 count = kvm->arch.msr_filter.count;
1501 u32 i;
1502 bool r = kvm->arch.msr_filter.default_allow;
1503 int idx;
1504
Sean Christopherson9389b9d2020-10-05 12:55:32 -07001505 /* MSR filtering not set up or x2APIC enabled, allow everything */
1506 if (!count || (index >= 0x800 && index <= 0x8ff))
Alexander Graf1a1552542020-09-25 16:34:21 +02001507 return true;
1508
1509 /* Prevent collision with set_msr_filter */
1510 idx = srcu_read_lock(&kvm->srcu);
1511
1512 for (i = 0; i < count; i++) {
1513 u32 start = ranges[i].base;
1514 u32 end = start + ranges[i].nmsrs;
1515 u32 flags = ranges[i].flags;
1516 unsigned long *bitmap = ranges[i].bitmap;
1517
1518 if ((index >= start) && (index < end) && (flags & type)) {
1519 r = !!test_bit(index - start, bitmap);
1520 break;
1521 }
1522 }
1523
1524 srcu_read_unlock(&kvm->srcu, idx);
1525
1526 return r;
Alexander Graf51de8152020-09-25 16:34:17 +02001527}
1528EXPORT_SYMBOL_GPL(kvm_msr_allowed);
1529
Carsten Otte15c4a642007-10-30 18:44:17 +01001530/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001531 * Write @data into the MSR specified by @index. Select MSR specific fault
1532 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001533 * Returns 0 on success, non-0 otherwise.
1534 * Assumes vcpu_load() was already called.
1535 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001536static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1537 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001538{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001539 struct msr_data msr;
1540
Alexander Graf1a1552542020-09-25 16:34:21 +02001541 if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_WRITE))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001542 return KVM_MSR_RET_FILTERED;
Alexander Graf1a1552542020-09-25 16:34:21 +02001543
Sean Christophersonf20935d2019-09-05 14:22:54 -07001544 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001545 case MSR_FS_BASE:
1546 case MSR_GS_BASE:
1547 case MSR_KERNEL_GS_BASE:
1548 case MSR_CSTAR:
1549 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001550 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001551 return 1;
1552 break;
1553 case MSR_IA32_SYSENTER_EIP:
1554 case MSR_IA32_SYSENTER_ESP:
1555 /*
1556 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1557 * non-canonical address is written on Intel but not on
1558 * AMD (which ignores the top 32-bits, because it does
1559 * not implement 64-bit SYSENTER).
1560 *
1561 * 64-bit code should hence be able to write a non-canonical
1562 * value on AMD. Making the address canonical ensures that
1563 * vmentry does not fail on Intel after writing a non-canonical
1564 * value, and that something deterministic happens if the guest
1565 * invokes 64-bit SYSENTER.
1566 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001567 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001568 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001569
1570 msr.data = data;
1571 msr.index = index;
1572 msr.host_initiated = host_initiated;
1573
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001574 return kvm_x86_ops.set_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001575}
1576
Peter Xu6abe9c12020-06-22 18:04:41 -04001577static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu,
1578 u32 index, u64 data, bool host_initiated)
1579{
1580 int ret = __kvm_set_msr(vcpu, index, data, host_initiated);
1581
1582 if (ret == KVM_MSR_RET_INVALID)
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001583 if (kvm_msr_ignored_check(vcpu, index, data, true))
1584 ret = 0;
Peter Xu6abe9c12020-06-22 18:04:41 -04001585
1586 return ret;
1587}
1588
Sean Christophersonf20935d2019-09-05 14:22:54 -07001589/*
1590 * Read the MSR specified by @index into @data. Select MSR specific fault
1591 * checks are bypassed if @host_initiated is %true.
1592 * Returns 0 on success, non-0 otherwise.
1593 * Assumes vcpu_load() was already called.
1594 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001595int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1596 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001597{
1598 struct msr_data msr;
1599 int ret;
1600
Alexander Graf1a1552542020-09-25 16:34:21 +02001601 if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_READ))
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001602 return KVM_MSR_RET_FILTERED;
Alexander Graf1a1552542020-09-25 16:34:21 +02001603
Sean Christophersonf20935d2019-09-05 14:22:54 -07001604 msr.index = index;
1605 msr.host_initiated = host_initiated;
1606
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001607 ret = kvm_x86_ops.get_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001608 if (!ret)
1609 *data = msr.data;
1610 return ret;
1611}
1612
Peter Xu6abe9c12020-06-22 18:04:41 -04001613static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu,
1614 u32 index, u64 *data, bool host_initiated)
1615{
1616 int ret = __kvm_get_msr(vcpu, index, data, host_initiated);
1617
1618 if (ret == KVM_MSR_RET_INVALID) {
1619 /* Unconditionally clear *data for simplicity */
1620 *data = 0;
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001621 if (kvm_msr_ignored_check(vcpu, index, 0, false))
1622 ret = 0;
Peter Xu6abe9c12020-06-22 18:04:41 -04001623 }
1624
1625 return ret;
1626}
1627
Sean Christophersonf20935d2019-09-05 14:22:54 -07001628int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1629{
Peter Xu6abe9c12020-06-22 18:04:41 -04001630 return kvm_get_msr_ignored_check(vcpu, index, data, false);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001631}
1632EXPORT_SYMBOL_GPL(kvm_get_msr);
1633
1634int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1635{
Peter Xu6abe9c12020-06-22 18:04:41 -04001636 return kvm_set_msr_ignored_check(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001637}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001638EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001639
Alexander Graf1ae09952020-09-25 16:34:16 +02001640static int complete_emulated_msr(struct kvm_vcpu *vcpu, bool is_read)
1641{
1642 if (vcpu->run->msr.error) {
1643 kvm_inject_gp(vcpu, 0);
1644 return 1;
1645 } else if (is_read) {
1646 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data);
1647 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32);
1648 }
1649
1650 return kvm_skip_emulated_instruction(vcpu);
1651}
1652
1653static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu)
1654{
1655 return complete_emulated_msr(vcpu, true);
1656}
1657
1658static int complete_emulated_wrmsr(struct kvm_vcpu *vcpu)
1659{
1660 return complete_emulated_msr(vcpu, false);
1661}
1662
1663static u64 kvm_msr_reason(int r)
1664{
1665 switch (r) {
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001666 case KVM_MSR_RET_INVALID:
Alexander Graf1ae09952020-09-25 16:34:16 +02001667 return KVM_MSR_EXIT_REASON_UNKNOWN;
Maxim Levitskycc4cb012020-11-01 13:55:23 +02001668 case KVM_MSR_RET_FILTERED:
Alexander Graf1a1552542020-09-25 16:34:21 +02001669 return KVM_MSR_EXIT_REASON_FILTER;
Alexander Graf1ae09952020-09-25 16:34:16 +02001670 default:
1671 return KVM_MSR_EXIT_REASON_INVAL;
1672 }
1673}
1674
1675static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index,
1676 u32 exit_reason, u64 data,
1677 int (*completion)(struct kvm_vcpu *vcpu),
1678 int r)
1679{
1680 u64 msr_reason = kvm_msr_reason(r);
1681
1682 /* Check if the user wanted to know about this MSR fault */
1683 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason))
1684 return 0;
1685
1686 vcpu->run->exit_reason = exit_reason;
1687 vcpu->run->msr.error = 0;
1688 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad));
1689 vcpu->run->msr.reason = msr_reason;
1690 vcpu->run->msr.index = index;
1691 vcpu->run->msr.data = data;
1692 vcpu->arch.complete_userspace_io = completion;
1693
1694 return 1;
1695}
1696
1697static int kvm_get_msr_user_space(struct kvm_vcpu *vcpu, u32 index, int r)
1698{
1699 return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_RDMSR, 0,
1700 complete_emulated_rdmsr, r);
1701}
1702
1703static int kvm_set_msr_user_space(struct kvm_vcpu *vcpu, u32 index, u64 data, int r)
1704{
1705 return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_WRMSR, data,
1706 complete_emulated_wrmsr, r);
1707}
1708
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001709int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1710{
1711 u32 ecx = kvm_rcx_read(vcpu);
1712 u64 data;
Alexander Graf1ae09952020-09-25 16:34:16 +02001713 int r;
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001714
Alexander Graf1ae09952020-09-25 16:34:16 +02001715 r = kvm_get_msr(vcpu, ecx, &data);
1716
1717 /* MSR read failed? See if we should ask user space */
1718 if (r && kvm_get_msr_user_space(vcpu, ecx, r)) {
1719 /* Bounce to user space */
1720 return 0;
1721 }
1722
1723 /* MSR read failed? Inject a #GP */
1724 if (r) {
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001725 trace_kvm_msr_read_ex(ecx);
1726 kvm_inject_gp(vcpu, 0);
1727 return 1;
1728 }
1729
1730 trace_kvm_msr_read(ecx, data);
1731
1732 kvm_rax_write(vcpu, data & -1u);
1733 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1734 return kvm_skip_emulated_instruction(vcpu);
1735}
1736EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1737
1738int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1739{
1740 u32 ecx = kvm_rcx_read(vcpu);
1741 u64 data = kvm_read_edx_eax(vcpu);
Alexander Graf1ae09952020-09-25 16:34:16 +02001742 int r;
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001743
Alexander Graf1ae09952020-09-25 16:34:16 +02001744 r = kvm_set_msr(vcpu, ecx, data);
1745
1746 /* MSR write failed? See if we should ask user space */
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03001747 if (r && kvm_set_msr_user_space(vcpu, ecx, data, r))
Alexander Graf1ae09952020-09-25 16:34:16 +02001748 /* Bounce to user space */
1749 return 0;
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03001750
1751 /* Signal all other negative errors to userspace */
1752 if (r < 0)
1753 return r;
Alexander Graf1ae09952020-09-25 16:34:16 +02001754
1755 /* MSR write failed? Inject a #GP */
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03001756 if (r > 0) {
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001757 trace_kvm_msr_write_ex(ecx, data);
1758 kvm_inject_gp(vcpu, 0);
1759 return 1;
1760 }
1761
1762 trace_kvm_msr_write(ecx, data);
1763 return kvm_skip_emulated_instruction(vcpu);
1764}
1765EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1766
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001767bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)
1768{
1769 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02001770 xfer_to_guest_mode_work_pending();
Wanpeng Li5a9f5442020-04-28 14:23:26 +08001771}
1772EXPORT_SYMBOL_GPL(kvm_vcpu_exit_request);
1773
Carsten Otte313a3dc2007-10-11 19:16:52 +02001774/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001775 * The fast path for frequent and performance sensitive wrmsr emulation,
1776 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1777 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1778 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1779 * other cases which must be called after interrupts are enabled on the host.
1780 */
1781static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1782{
Wanpeng Lie1be9ac2020-03-26 10:20:01 +08001783 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1784 return 1;
1785
1786 if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001787 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
Wanpeng Li4064a4c2020-04-02 16:20:26 +08001788 ((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
1789 ((u32)(data >> 32) != X2APIC_BROADCAST)) {
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001790
Wanpeng Lid5361672020-03-26 10:20:02 +08001791 data &= ~(1 << 12);
1792 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001793 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001794 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1795 trace_kvm_apic_write(APIC_ICR, (u32)data);
1796 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001797 }
1798
1799 return 1;
1800}
1801
Wanpeng Liae95f562020-04-28 14:23:28 +08001802static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data)
1803{
1804 if (!kvm_can_use_hv_timer(vcpu))
1805 return 1;
1806
1807 kvm_set_lapic_tscdeadline_msr(vcpu, data);
1808 return 0;
1809}
1810
Wanpeng Li404d5d72020-04-28 14:23:25 +08001811fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001812{
1813 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001814 u64 data;
Wanpeng Li404d5d72020-04-28 14:23:25 +08001815 fastpath_t ret = EXIT_FASTPATH_NONE;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001816
1817 switch (msr) {
1818 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001819 data = kvm_read_edx_eax(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08001820 if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) {
1821 kvm_skip_emulated_instruction(vcpu);
1822 ret = EXIT_FASTPATH_EXIT_HANDLED;
Haiwei Li80bc97f2020-05-18 09:31:38 +08001823 }
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001824 break;
Wanpeng Liae95f562020-04-28 14:23:28 +08001825 case MSR_IA32_TSCDEADLINE:
1826 data = kvm_read_edx_eax(vcpu);
1827 if (!handle_fastpath_set_tscdeadline(vcpu, data)) {
1828 kvm_skip_emulated_instruction(vcpu);
1829 ret = EXIT_FASTPATH_REENTER_GUEST;
1830 }
1831 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001832 default:
Wanpeng Li404d5d72020-04-28 14:23:25 +08001833 break;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001834 }
1835
Wanpeng Li404d5d72020-04-28 14:23:25 +08001836 if (ret != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001837 trace_kvm_msr_write(msr, data);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001838
Wanpeng Li404d5d72020-04-28 14:23:25 +08001839 return ret;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001840}
1841EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1842
1843/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001844 * Adapt set_msr() to msr_io()'s calling convention
1845 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001846static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1847{
Peter Xu6abe9c12020-06-22 18:04:41 -04001848 return kvm_get_msr_ignored_check(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001849}
1850
Carsten Otte313a3dc2007-10-11 19:16:52 +02001851static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1852{
Peter Xu6abe9c12020-06-22 18:04:41 -04001853 return kvm_set_msr_ignored_check(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001854}
1855
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001856#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001857struct pvclock_clock {
1858 int vclock_mode;
1859 u64 cycle_last;
1860 u64 mask;
1861 u32 mult;
1862 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001863 u64 base_cycles;
1864 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001865};
1866
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001867struct pvclock_gtod_data {
1868 seqcount_t seq;
1869
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001870 struct pvclock_clock clock; /* extract of a clocksource struct */
1871 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001872
Paolo Bonzini917f9472020-01-22 14:32:20 +01001873 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001874 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001875};
1876
1877static struct pvclock_gtod_data pvclock_gtod_data;
1878
1879static void update_pvclock_gtod(struct timekeeper *tk)
1880{
1881 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1882
1883 write_seqcount_begin(&vdata->seq);
1884
1885 /* copy pvclock gtod data */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001886 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001887 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1888 vdata->clock.mask = tk->tkr_mono.mask;
1889 vdata->clock.mult = tk->tkr_mono.mult;
1890 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001891 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1892 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001893
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001894 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001895 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1896 vdata->raw_clock.mask = tk->tkr_raw.mask;
1897 vdata->raw_clock.mult = tk->tkr_raw.mult;
1898 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001899 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1900 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001901
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001902 vdata->wall_time_sec = tk->xtime_sec;
1903
Paolo Bonzini917f9472020-01-22 14:32:20 +01001904 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001905
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001906 write_seqcount_end(&vdata->seq);
1907}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001908
1909static s64 get_kvmclock_base_ns(void)
1910{
1911 /* Count up from boot time, but with the frequency of the raw clock. */
1912 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1913}
1914#else
1915static s64 get_kvmclock_base_ns(void)
1916{
1917 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1918 return ktime_get_boottime_ns();
1919}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001920#endif
1921
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001922static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1923{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001924 int version;
1925 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001926 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001927 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001928
Oliver Upton210dfd92020-08-18 15:24:27 +00001929 kvm->arch.wall_clock = wall_clock;
1930
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001931 if (!wall_clock)
1932 return;
1933
Avi Kivity9ed3c442010-05-04 15:00:37 +03001934 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1935 if (r)
1936 return;
1937
1938 if (version & 1)
1939 ++version; /* first time write, random junk */
1940
1941 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001942
Nicholas Krause1dab1342015-12-30 13:08:46 -05001943 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1944 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001945
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001946 /*
1947 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001948 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001949 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001950 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001951 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001952
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001953 wc.nsec = do_div(wall_nsec, 1000000000);
1954 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001955 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001956
1957 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1958
1959 version++;
1960 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001961}
1962
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00001963static void kvm_write_system_time(struct kvm_vcpu *vcpu, gpa_t system_time,
1964 bool old_msr, bool host_initiated)
1965{
1966 struct kvm_arch *ka = &vcpu->kvm->arch;
1967
1968 if (vcpu->vcpu_id == 0 && !host_initiated) {
Oliver Upton1e293d12020-10-27 16:10:43 -07001969 if (ka->boot_vcpu_runs_old_kvmclock != old_msr)
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00001970 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1971
1972 ka->boot_vcpu_runs_old_kvmclock = old_msr;
1973 }
1974
1975 vcpu->arch.time = system_time;
1976 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
1977
1978 /* we verify if the enable bit is set... */
1979 vcpu->arch.pv_time_enabled = false;
1980 if (!(system_time & 1))
1981 return;
1982
1983 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
1984 &vcpu->arch.pv_time, system_time & ~1ULL,
1985 sizeof(struct pvclock_vcpu_time_info)))
1986 vcpu->arch.pv_time_enabled = true;
1987
1988 return;
1989}
1990
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001991static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1992{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001993 do_shl32_div32(dividend, divisor);
1994 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001995}
1996
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001997static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001998 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001999{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002000 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002001 int32_t shift = 0;
2002 uint64_t tps64;
2003 uint32_t tps32;
2004
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002005 tps64 = base_hz;
2006 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02002007 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002008 tps64 >>= 1;
2009 shift--;
2010 }
2011
2012 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02002013 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
2014 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002015 scaled64 >>= 1;
2016 else
2017 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002018 shift++;
2019 }
2020
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002021 *pshift = shift;
2022 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002023}
2024
Marcelo Tosattid8281992012-11-27 23:29:01 -02002025#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002026static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002027#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02002028
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002029static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08002030static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002031
Zachary Amsdencc578282012-02-03 15:43:50 -02002032static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10002033{
Zachary Amsdencc578282012-02-03 15:43:50 -02002034 u64 v = (u64)khz * (1000000 + ppm);
2035 do_div(v, 1000000);
2036 return v;
2037}
2038
Haozhong Zhang381d5852015-10-20 15:39:04 +08002039static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
2040{
2041 u64 ratio;
2042
2043 /* Guest TSC same frequency as host TSC? */
2044 if (!scale) {
2045 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
2046 return 0;
2047 }
2048
2049 /* TSC scaling supported? */
2050 if (!kvm_has_tsc_control) {
2051 if (user_tsc_khz > tsc_khz) {
2052 vcpu->arch.tsc_catchup = 1;
2053 vcpu->arch.tsc_always_catchup = 1;
2054 return 0;
2055 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02002056 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08002057 return -1;
2058 }
2059 }
2060
2061 /* TSC scaling required - calculate ratio */
2062 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
2063 user_tsc_khz, tsc_khz);
2064
2065 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02002066 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
2067 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08002068 return -1;
2069 }
2070
2071 vcpu->arch.tsc_scaling_ratio = ratio;
2072 return 0;
2073}
2074
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002075static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02002076{
2077 u32 thresh_lo, thresh_hi;
2078 int use_scaling = 0;
2079
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002080 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002081 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08002082 /* set tsc_scaling_ratio to a safe value */
2083 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08002084 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08002085 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002086
Zachary Amsdenc2855452010-09-18 14:38:15 -10002087 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002088 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02002089 &vcpu->arch.virtual_tsc_shift,
2090 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002091 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02002092
2093 /*
2094 * Compute the variation in TSC rate which is acceptable
2095 * within the range of tolerance and decide if the
2096 * rate being applied is within that bounds of the hardware
2097 * rate. If so, no scaling or compensation need be done.
2098 */
2099 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
2100 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002101 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
2102 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 -02002103 use_scaling = 1;
2104 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01002105 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002106}
2107
2108static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
2109{
Zachary Amsdene26101b2012-02-03 15:43:57 -02002110 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02002111 vcpu->arch.virtual_tsc_mult,
2112 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002113 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002114 return tsc;
2115}
2116
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002117static inline int gtod_is_based_on_tsc(int mode)
2118{
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002119 return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002120}
2121
Fengguang Wu69b00492015-01-19 22:33:39 +08002122static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002123{
2124#ifdef CONFIG_X86_64
2125 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002126 struct kvm_arch *ka = &vcpu->kvm->arch;
2127 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2128
2129 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2130 atomic_read(&vcpu->kvm->online_vcpus));
2131
Marcelo Tosatti7f187922014-11-04 21:30:44 -02002132 /*
2133 * Once the masterclock is enabled, always perform request in
2134 * order to update it.
2135 *
2136 * In order to enable masterclock, the host clocksource must be TSC
2137 * and the vcpus need to have matched TSCs. When that happens,
2138 * perform request to enable masterclock.
2139 */
2140 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002141 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002142 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
2143
2144 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
2145 atomic_read(&vcpu->kvm->online_vcpus),
2146 ka->use_master_clock, gtod->clock.vclock_mode);
2147#endif
2148}
2149
Haozhong Zhang35181e82015-10-20 15:39:03 +08002150/*
2151 * Multiply tsc by a fixed point number represented by ratio.
2152 *
2153 * The most significant 64-N bits (mult) of ratio represent the
2154 * integral part of the fixed point number; the remaining N bits
2155 * (frac) represent the fractional part, ie. ratio represents a fixed
2156 * point number (mult + frac * 2^(-N)).
2157 *
2158 * N equals to kvm_tsc_scaling_ratio_frac_bits.
2159 */
2160static inline u64 __scale_tsc(u64 ratio, u64 tsc)
2161{
2162 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
2163}
2164
2165u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
2166{
2167 u64 _tsc = tsc;
2168 u64 ratio = vcpu->arch.tsc_scaling_ratio;
2169
2170 if (ratio != kvm_default_tsc_scaling_ratio)
2171 _tsc = __scale_tsc(ratio, tsc);
2172
2173 return _tsc;
2174}
2175EXPORT_SYMBOL_GPL(kvm_scale_tsc);
2176
Haozhong Zhang07c14192015-10-20 15:39:05 +08002177static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2178{
2179 u64 tsc;
2180
2181 tsc = kvm_scale_tsc(vcpu, rdtsc());
2182
2183 return target_tsc - tsc;
2184}
2185
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002186u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
2187{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002188 return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002189}
2190EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
2191
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002192static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
2193{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002194 vcpu->arch.l1_tsc_offset = offset;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07002195 vcpu->arch.tsc_offset = kvm_x86_ops.write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002196}
2197
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002198static inline bool kvm_check_tsc_unstable(void)
2199{
2200#ifdef CONFIG_X86_64
2201 /*
2202 * TSC is marked unstable when we're running on Hyper-V,
2203 * 'TSC page' clocksource is good.
2204 */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002205 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002206 return false;
2207#endif
2208 return check_tsc_unstable();
2209}
2210
Paolo Bonzini0c899c22020-09-24 14:45:27 +02002211static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data)
Zachary Amsden99e3e302010-08-19 22:07:17 -10002212{
2213 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002214 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002215 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002216 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002217 bool already_matched;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002218 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002219
Jan Kiszka038f8c12011-02-04 10:49:11 +01002220 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08002221 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002222 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002223 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002224
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002225 if (vcpu->arch.virtual_tsc_khz) {
Paolo Bonzini0c899c22020-09-24 14:45:27 +02002226 if (data == 0) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03002227 /*
2228 * detection of vcpu initialization -- need to sync
2229 * with other vCPUs. This particularly helps to keep
2230 * kvm_clock stable after CPU hotplug
2231 */
2232 synchronizing = true;
2233 } else {
2234 u64 tsc_exp = kvm->arch.last_tsc_write +
2235 nsec_to_cycles(vcpu, elapsed);
2236 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
2237 /*
2238 * Special case: TSC write with a small delta (1 second)
2239 * of virtual cycle time against real time is
2240 * interpreted as an attempt to synchronize the CPU.
2241 */
2242 synchronizing = data < tsc_exp + tsc_hz &&
2243 data + tsc_hz > tsc_exp;
2244 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002245 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002246
2247 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002248 * For a reliable TSC, we can match TSC offsets, and for an unstable
2249 * TSC, we add elapsed time in this computation. We could let the
2250 * compensation code attempt to catch up if we fall behind, but
2251 * it's better to try to match offsets from the beginning.
2252 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002253 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002254 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002255 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002256 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002257 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002258 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002259 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002260 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002261 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002262 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002263 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002264 } else {
2265 /*
2266 * We split periods of matched TSC writes into generations.
2267 * For each generation, we track the original measured
2268 * nanosecond time, offset, and write, so if TSCs are in
2269 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002270 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002271 *
2272 * These values are tracked in kvm->arch.cur_xxx variables.
2273 */
2274 kvm->arch.cur_tsc_generation++;
2275 kvm->arch.cur_tsc_nsec = ns;
2276 kvm->arch.cur_tsc_write = data;
2277 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002278 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002279 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002280
2281 /*
2282 * We also track th most recent recorded KHZ, write and time to
2283 * allow the matching interval to be extended at each write.
2284 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002285 kvm->arch.last_tsc_nsec = ns;
2286 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002287 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002288
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002289 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002290
2291 /* Keep track of which generation this VCPU has synchronized to */
2292 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2293 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2294 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2295
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002296 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002297 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002298
2299 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002300 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002301 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002302 } else if (!already_matched) {
2303 kvm->arch.nr_vcpus_matched_tsc++;
2304 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002305
2306 kvm_track_tsc_matching(vcpu);
2307 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002308}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002309
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002310static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2311 s64 adjustment)
2312{
Sean Christopherson56ba77a2020-05-01 21:32:25 -07002313 u64 tsc_offset = vcpu->arch.l1_tsc_offset;
Leonid Shatz326e7422018-11-06 12:14:25 +02002314 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002315}
2316
2317static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2318{
2319 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2320 WARN_ON(adjustment < 0);
2321 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002322 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002323}
2324
Marcelo Tosattid8281992012-11-27 23:29:01 -02002325#ifdef CONFIG_X86_64
2326
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002327static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002328{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002329 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002330 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002331
2332 if (likely(ret >= last))
2333 return ret;
2334
2335 /*
2336 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002337 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002338 * very likely) and there's a data dependence, so force GCC
2339 * to generate a branch instead. I don't barrier() because
2340 * we don't actually need a barrier, and if this function
2341 * ever gets inlined it will generate worse code.
2342 */
2343 asm volatile ("");
2344 return last;
2345}
2346
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002347static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2348 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002349{
2350 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002351 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002352
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002353 switch (clock->vclock_mode) {
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002354 case VDSO_CLOCKMODE_HVCLOCK:
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002355 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2356 tsc_timestamp);
2357 if (tsc_pg_val != U64_MAX) {
2358 /* TSC page valid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002359 *mode = VDSO_CLOCKMODE_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002360 v = (tsc_pg_val - clock->cycle_last) &
2361 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002362 } else {
2363 /* TSC page invalid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002364 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002365 }
2366 break;
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002367 case VDSO_CLOCKMODE_TSC:
2368 *mode = VDSO_CLOCKMODE_TSC;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002369 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002370 v = (*tsc_timestamp - clock->cycle_last) &
2371 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002372 break;
2373 default:
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002374 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002375 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002376
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002377 if (*mode == VDSO_CLOCKMODE_NONE)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002378 *tsc_timestamp = v = 0;
2379
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002380 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002381}
2382
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002383static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002384{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002385 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002386 unsigned long seq;
2387 int mode;
2388 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002389
Marcelo Tosattid8281992012-11-27 23:29:01 -02002390 do {
2391 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002392 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002393 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002394 ns >>= gtod->raw_clock.shift;
2395 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002396 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002397 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002398
2399 return mode;
2400}
2401
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002402static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002403{
2404 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2405 unsigned long seq;
2406 int mode;
2407 u64 ns;
2408
2409 do {
2410 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002411 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002412 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002413 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002414 ns >>= gtod->clock.shift;
2415 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2416
2417 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2418 ts->tv_nsec = ns;
2419
2420 return mode;
2421}
2422
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002423/* returns true if host is using TSC based clocksource */
2424static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002425{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002426 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002427 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002428 return false;
2429
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002430 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002431 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002432}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002433
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002434/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002435static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002436 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002437{
2438 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002439 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002440 return false;
2441
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002442 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002443}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002444#endif
2445
2446/*
2447 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002448 * Assuming a stable TSC across physical CPUS, and a stable TSC
2449 * across virtual CPUs, the following condition is possible.
2450 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002451 * CPUs at the next numbered event.
2452 *
2453 * "timespecX" represents host monotonic time. "tscX" represents
2454 * RDTSC value.
2455 *
2456 * VCPU0 on CPU0 | VCPU1 on CPU1
2457 *
2458 * 1. read timespec0,tsc0
2459 * 2. | timespec1 = timespec0 + N
2460 * | tsc1 = tsc0 + M
2461 * 3. transition to guest | transition to guest
2462 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2463 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2464 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2465 *
2466 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2467 *
2468 * - ret0 < ret1
2469 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2470 * ...
2471 * - 0 < N - M => M < N
2472 *
2473 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2474 * always the case (the difference between two distinct xtime instances
2475 * might be smaller then the difference between corresponding TSC reads,
2476 * when updating guest vcpus pvclock areas).
2477 *
2478 * To avoid that problem, do not allow visibility of distinct
2479 * system_timestamp/tsc_timestamp values simultaneously: use a master
2480 * copy of host monotonic time values. Update that master copy
2481 * in lockstep.
2482 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002483 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002484 *
2485 */
2486
2487static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2488{
2489#ifdef CONFIG_X86_64
2490 struct kvm_arch *ka = &kvm->arch;
2491 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002492 bool host_tsc_clocksource, vcpus_matched;
2493
2494 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2495 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002496
2497 /*
2498 * If the host uses TSC clock, then passthrough TSC as stable
2499 * to the guest.
2500 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002501 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002502 &ka->master_kernel_ns,
2503 &ka->master_cycle_now);
2504
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002505 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002506 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002507 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002508
Marcelo Tosattid8281992012-11-27 23:29:01 -02002509 if (ka->use_master_clock)
2510 atomic_set(&kvm_guest_has_master_clock, 1);
2511
2512 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002513 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2514 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002515#endif
2516}
2517
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002518void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2519{
2520 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2521}
2522
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002523static void kvm_gen_update_masterclock(struct kvm *kvm)
2524{
2525#ifdef CONFIG_X86_64
2526 int i;
2527 struct kvm_vcpu *vcpu;
2528 struct kvm_arch *ka = &kvm->arch;
2529
2530 spin_lock(&ka->pvclock_gtod_sync_lock);
2531 kvm_make_mclock_inprogress_request(kvm);
2532 /* no guest entries from this point */
2533 pvclock_update_vm_gtod_copy(kvm);
2534
2535 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002536 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002537
2538 /* guest entries allowed */
2539 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002540 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002541
2542 spin_unlock(&ka->pvclock_gtod_sync_lock);
2543#endif
2544}
2545
Marcelo Tosattie891a322017-04-17 12:51:37 -03002546u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002547{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002548 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002549 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002550 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002551
Paolo Bonzini8b953442016-11-16 18:31:30 +01002552 spin_lock(&ka->pvclock_gtod_sync_lock);
2553 if (!ka->use_master_clock) {
2554 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002555 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002556 }
2557
Paolo Bonzini8b953442016-11-16 18:31:30 +01002558 hv_clock.tsc_timestamp = ka->master_cycle_now;
2559 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2560 spin_unlock(&ka->pvclock_gtod_sync_lock);
2561
Wanpeng Lie2c22062017-05-11 18:12:05 -07002562 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2563 get_cpu();
2564
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002565 if (__this_cpu_read(cpu_tsc_khz)) {
2566 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2567 &hv_clock.tsc_shift,
2568 &hv_clock.tsc_to_system_mul);
2569 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2570 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002571 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002572
2573 put_cpu();
2574
2575 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002576}
2577
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002578static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2579{
2580 struct kvm_vcpu_arch *vcpu = &v->arch;
2581 struct pvclock_vcpu_time_info guest_hv_clock;
2582
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002583 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002584 &guest_hv_clock, sizeof(guest_hv_clock))))
2585 return;
2586
2587 /* This VCPU is paused, but it's legal for a guest to read another
2588 * VCPU's kvmclock, so we really have to follow the specification where
2589 * it says that version is odd if data is being modified, and even after
2590 * it is consistent.
2591 *
2592 * Version field updates must be kept separate. This is because
2593 * kvm_write_guest_cached might use a "rep movs" instruction, and
2594 * writes within a string instruction are weakly ordered. So there
2595 * are three writes overall.
2596 *
2597 * As a small optimization, only write the version field in the first
2598 * and third write. The vcpu->pv_time cache is still valid, because the
2599 * version field is the first in the struct.
2600 */
2601 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2602
Liran Alon51c4b8b2017-11-05 16:11:30 +02002603 if (guest_hv_clock.version & 1)
2604 ++guest_hv_clock.version; /* first time write, random junk */
2605
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002606 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002607 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2608 &vcpu->hv_clock,
2609 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002610
2611 smp_wmb();
2612
2613 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2614 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2615
2616 if (vcpu->pvclock_set_guest_stopped_request) {
2617 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2618 vcpu->pvclock_set_guest_stopped_request = false;
2619 }
2620
2621 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2622
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002623 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2624 &vcpu->hv_clock,
2625 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002626
2627 smp_wmb();
2628
2629 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002630 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2631 &vcpu->hv_clock,
2632 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002633}
2634
Zachary Amsden34c238a2010-09-18 14:38:14 -10002635static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002636{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002637 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002638 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002639 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002640 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002641 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002642 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002643 bool use_master_clock;
2644
2645 kernel_ns = 0;
2646 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002647
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002648 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002649 * If the host uses TSC clock, then passthrough TSC as stable
2650 * to the guest.
2651 */
2652 spin_lock(&ka->pvclock_gtod_sync_lock);
2653 use_master_clock = ka->use_master_clock;
2654 if (use_master_clock) {
2655 host_tsc = ka->master_cycle_now;
2656 kernel_ns = ka->master_kernel_ns;
2657 }
2658 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002659
2660 /* Keep irq disabled to prevent changes to the clock */
2661 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002662 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2663 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002664 local_irq_restore(flags);
2665 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2666 return 1;
2667 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002668 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002669 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002670 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002671 }
2672
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002673 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002674
2675 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002676 * We may have to catch up the TSC to match elapsed wall clock
2677 * time for two reasons, even if kvmclock is used.
2678 * 1) CPU could have been running below the maximum TSC rate
2679 * 2) Broken TSC compensation resets the base at each VCPU
2680 * entry to avoid unknown leaps of TSC even when running
2681 * again on the same CPU. This may cause apparent elapsed
2682 * time to disappear, and the guest to stand still or run
2683 * very slowly.
2684 */
2685 if (vcpu->tsc_catchup) {
2686 u64 tsc = compute_guest_tsc(v, kernel_ns);
2687 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002688 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002689 tsc_timestamp = tsc;
2690 }
2691 }
2692
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002693 local_irq_restore(flags);
2694
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002695 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002696
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002697 if (kvm_has_tsc_control)
2698 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2699
2700 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002701 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002702 &vcpu->hv_clock.tsc_shift,
2703 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002704 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002705 }
2706
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002707 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002708 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002709 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002710
Marcelo Tosattid8281992012-11-27 23:29:01 -02002711 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002712 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002713 if (use_master_clock)
2714 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2715
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002716 vcpu->hv_clock.flags = pvclock_flags;
2717
Paolo Bonzini095cf552016-02-08 12:54:12 +01002718 if (vcpu->pv_time_enabled)
2719 kvm_setup_pvclock_page(v);
2720 if (v == kvm_get_vcpu(v->kvm, 0))
2721 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002722 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002723}
2724
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002725/*
2726 * kvmclock updates which are isolated to a given vcpu, such as
2727 * vcpu->cpu migration, should not allow system_timestamp from
2728 * the rest of the vcpus to remain static. Otherwise ntp frequency
2729 * correction applies to one vcpu's system_timestamp but not
2730 * the others.
2731 *
2732 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002733 * We need to rate-limit these requests though, as they can
2734 * considerably slow guests that have a large number of vcpus.
2735 * The time for a remote vcpu to update its kvmclock is bound
2736 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002737 */
2738
Andrew Jones7e44e442014-02-28 12:52:54 +01002739#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2740
2741static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002742{
2743 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002744 struct delayed_work *dwork = to_delayed_work(work);
2745 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2746 kvmclock_update_work);
2747 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002748 struct kvm_vcpu *vcpu;
2749
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 Tosatti0061d53d2013-05-09 20:21:41 -03002752 kvm_vcpu_kick(vcpu);
2753 }
2754}
2755
Andrew Jones7e44e442014-02-28 12:52:54 +01002756static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2757{
2758 struct kvm *kvm = v->kvm;
2759
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002760 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002761 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2762 KVMCLOCK_UPDATE_DELAY);
2763}
2764
Andrew Jones332967a2014-02-28 12:52:55 +01002765#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2766
2767static void kvmclock_sync_fn(struct work_struct *work)
2768{
2769 struct delayed_work *dwork = to_delayed_work(work);
2770 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2771 kvmclock_sync_work);
2772 struct kvm *kvm = container_of(ka, struct kvm, arch);
2773
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002774 if (!kvmclock_periodic_sync)
2775 return;
2776
Andrew Jones332967a2014-02-28 12:52:55 +01002777 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2778 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2779 KVMCLOCK_SYNC_PERIOD);
2780}
2781
Borislav Petkov191c8132019-04-18 18:32:50 +02002782/*
2783 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2784 */
2785static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2786{
2787 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08002788 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02002789 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2790
2791 return false;
2792}
2793
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002794static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002795{
2796 u64 mcg_cap = vcpu->arch.mcg_cap;
2797 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002798 u32 msr = msr_info->index;
2799 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002800
2801 switch (msr) {
2802 case MSR_IA32_MCG_STATUS:
2803 vcpu->arch.mcg_status = data;
2804 break;
2805 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002806 if (!(mcg_cap & MCG_CTL_P) &&
2807 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002808 return 1;
2809 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002810 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002811 vcpu->arch.mcg_ctl = data;
2812 break;
2813 default:
2814 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002815 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002816 u32 offset = array_index_nospec(
2817 msr - MSR_IA32_MC0_CTL,
2818 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2819
Andre Przywara114be422010-03-24 17:46:42 +01002820 /* only 0 or all 1s can be written to IA32_MCi_CTL
2821 * some Linux kernels though clear bit 10 in bank 4 to
2822 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2823 * this to avoid an uncatched #GP in the guest
2824 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002825 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002826 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002827 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002828
2829 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002830 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002831 (offset & 0x3) == 1 && data != 0) {
2832 if (!can_set_mci_status(vcpu))
2833 return -1;
2834 }
2835
Huang Ying890ca9a2009-05-11 16:48:15 +08002836 vcpu->arch.mce_banks[offset] = data;
2837 break;
2838 }
2839 return 1;
2840 }
2841 return 0;
2842}
2843
Ed Swierkffde22a2009-10-15 15:21:43 -07002844static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2845{
2846 struct kvm *kvm = vcpu->kvm;
2847 int lm = is_long_mode(vcpu);
2848 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2849 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2850 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2851 : kvm->arch.xen_hvm_config.blob_size_32;
2852 u32 page_num = data & ~PAGE_MASK;
2853 u64 page_addr = data & PAGE_MASK;
2854 u8 *page;
Ed Swierkffde22a2009-10-15 15:21:43 -07002855
Ed Swierkffde22a2009-10-15 15:21:43 -07002856 if (page_num >= blob_size)
Maxim Levitsky36385cc2020-10-01 14:29:51 +03002857 return 1;
2858
Sasha Levinff5c2c02011-12-04 19:36:29 +02002859 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
Maxim Levitsky36385cc2020-10-01 14:29:51 +03002860 if (IS_ERR(page))
2861 return PTR_ERR(page);
2862
2863 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) {
2864 kfree(page);
2865 return 1;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002866 }
Maxim Levitsky36385cc2020-10-01 14:29:51 +03002867 return 0;
Ed Swierkffde22a2009-10-15 15:21:43 -07002868}
2869
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002870static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu)
2871{
2872 u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
2873
2874 return (vcpu->arch.apf.msr_en_val & mask) == mask;
2875}
2876
Gleb Natapov344d9582010-10-14 11:22:50 +02002877static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2878{
2879 gpa_t gpa = data & ~0x3f;
2880
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002881 /* Bits 4:5 are reserved, Should be zero */
2882 if (data & 0x30)
Gleb Natapov344d9582010-10-14 11:22:50 +02002883 return 1;
2884
Oliver Upton66570e92020-08-18 15:24:28 +00002885 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_VMEXIT) &&
2886 (data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT))
2887 return 1;
2888
2889 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT) &&
2890 (data & KVM_ASYNC_PF_DELIVERY_AS_INT))
2891 return 1;
2892
Wanpeng Li9d3c4472020-06-29 18:26:31 +08002893 if (!lapic_in_kernel(vcpu))
Vitaly Kuznetsovd831de12020-09-11 11:31:47 +02002894 return data ? 1 : 0;
Wanpeng Li9d3c4472020-06-29 18:26:31 +08002895
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002896 vcpu->arch.apf.msr_en_val = data;
Gleb Natapov344d9582010-10-14 11:22:50 +02002897
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002898 if (!kvm_pv_async_pf_enabled(vcpu)) {
Gleb Natapov344d9582010-10-14 11:22:50 +02002899 kvm_clear_async_pf_completion_queue(vcpu);
2900 kvm_async_pf_hash_reset(vcpu);
2901 return 0;
2902 }
2903
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002904 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02002905 sizeof(u64)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002906 return 1;
2907
Gleb Natapov6adba522010-10-14 11:22:55 +02002908 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002909 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002910
Gleb Natapov344d9582010-10-14 11:22:50 +02002911 kvm_async_pf_wakeup_all(vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02002912
2913 return 0;
2914}
2915
2916static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data)
2917{
2918 /* Bits 8-63 are reserved */
2919 if (data >> 8)
2920 return 1;
2921
2922 if (!lapic_in_kernel(vcpu))
2923 return 1;
2924
2925 vcpu->arch.apf.msr_int_val = data;
2926
2927 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK;
2928
Gleb Natapov344d9582010-10-14 11:22:50 +02002929 return 0;
2930}
2931
Glauber Costa12f9a482011-02-01 14:16:40 -05002932static void kvmclock_reset(struct kvm_vcpu *vcpu)
2933{
Andy Honig0b794592013-02-20 14:48:10 -08002934 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002935 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002936}
2937
Sean Christopherson77809382020-03-20 14:28:18 -07002938static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002939{
2940 ++vcpu->stat.tlb_flush;
Sean Christopherson77809382020-03-20 14:28:18 -07002941 kvm_x86_ops.tlb_flush_all(vcpu);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002942}
2943
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04002944static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
2945{
2946 ++vcpu->stat.tlb_flush;
2947 kvm_x86_ops.tlb_flush_guest(vcpu);
2948}
2949
Glauber Costac9aaa892011-07-11 15:28:14 -04002950static void record_steal_time(struct kvm_vcpu *vcpu)
2951{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002952 struct kvm_host_map map;
2953 struct kvm_steal_time *st;
2954
Glauber Costac9aaa892011-07-11 15:28:14 -04002955 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2956 return;
2957
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002958 /* -EAGAIN is returned in atomic context so we can just return. */
2959 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2960 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002961 return;
2962
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002963 st = map.hva +
2964 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2965
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002966 /*
2967 * Doing a TLB flush here, on the guest's behalf, can avoid
2968 * expensive IPIs.
2969 */
Oliver Upton66570e92020-08-18 15:24:28 +00002970 if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) {
2971 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
2972 st->preempted & KVM_VCPU_FLUSH_TLB);
2973 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
2974 kvm_vcpu_flush_tlb_guest(vcpu);
2975 }
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002976
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002977 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002978
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002979 if (st->version & 1)
2980 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002981
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002982 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002983
2984 smp_wmb();
2985
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002986 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002987 vcpu->arch.st.last_steal;
2988 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002989
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002990 smp_wmb();
2991
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002992 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002993
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002994 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002995}
2996
Will Auld8fe8ab42012-11-29 12:42:12 -08002997int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002998{
Gleb Natapov57537852012-01-15 14:17:22 +02002999 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08003000 u32 msr = msr_info->index;
3001 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02003002
Carsten Otte15c4a642007-10-30 18:44:17 +01003003 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01003004 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003005 case MSR_IA32_UCODE_WRITE:
3006 case MSR_VM_HSAVE_PA:
3007 case MSR_AMD64_PATCH_LOADER:
3008 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02003009 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003010 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003011 break;
3012
Wanpeng Li518e7b92018-02-28 14:03:31 +08003013 case MSR_IA32_UCODE_REV:
3014 if (msr_info->host_initiated)
3015 vcpu->arch.microcode_version = data;
3016 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003017 case MSR_IA32_ARCH_CAPABILITIES:
3018 if (!msr_info->host_initiated)
3019 return 1;
3020 vcpu->arch.arch_capabilities = data;
3021 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02003022 case MSR_IA32_PERF_CAPABILITIES: {
3023 struct kvm_msr_entry msr_ent = {.index = msr, .data = 0};
3024
3025 if (!msr_info->host_initiated)
3026 return 1;
3027 if (guest_cpuid_has(vcpu, X86_FEATURE_PDCM) && kvm_get_msr_feature(&msr_ent))
3028 return 1;
3029 if (data & ~msr_ent.data)
3030 return 1;
3031
3032 vcpu->arch.perf_capabilities = data;
3033
3034 return 0;
3035 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003036 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07003037 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02003038 case MSR_K7_HWCR:
3039 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01003040 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08003041 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02003042
3043 /* Handle McStatusWrEn */
3044 if (data == BIT_ULL(18)) {
3045 vcpu->arch.msr_hwcr = data;
3046 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003047 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
3048 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02003049 return 1;
3050 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003051 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003052 case MSR_FAM10H_MMIO_CONF_BASE:
3053 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003054 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
3055 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003056 return 1;
3057 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003058 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003059 case MSR_IA32_DEBUGCTLMSR:
3060 if (!data) {
3061 /* We support the non-activated case already */
3062 break;
3063 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
3064 /* Values other than LBR and BTF are vendor-specific,
3065 thus reserved and should throw a #GP */
3066 return 1;
Pankaj Gupta2cdef912020-11-05 16:39:32 +01003067 } else if (report_ignored_msrs)
3068 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
3069 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003070 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003071 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003072 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003073 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01003074 return kvm_set_apic_base(vcpu, msr_info);
Xiaoyao Libf10bd02020-06-16 15:33:07 +08003075 case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff:
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003076 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003077 case MSR_IA32_TSCDEADLINE:
3078 kvm_set_lapic_tscdeadline_msr(vcpu, data);
3079 break;
Will Auldba904632012-11-29 12:42:50 -08003080 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003081 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08003082 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06003083 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08003084 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08003085 }
3086 vcpu->arch.ia32_tsc_adjust_msr = data;
3087 }
3088 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003089 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08003090 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
3091 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
3092 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
3093 return 1;
3094 vcpu->arch.ia32_misc_enable_msr = data;
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08003095 kvm_update_cpuid_runtime(vcpu);
Wanpeng Li511a85562019-05-21 14:06:54 +08003096 } else {
3097 vcpu->arch.ia32_misc_enable_msr = data;
3098 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003099 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003100 case MSR_IA32_SMBASE:
3101 if (!msr_info->host_initiated)
3102 return 1;
3103 vcpu->arch.smbase = data;
3104 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003105 case MSR_IA32_POWER_CTL:
3106 vcpu->arch.msr_ia32_power_ctl = data;
3107 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003108 case MSR_IA32_TSC:
Paolo Bonzini0c899c22020-09-24 14:45:27 +02003109 if (msr_info->host_initiated) {
3110 kvm_synchronize_tsc(vcpu, data);
3111 } else {
3112 u64 adj = kvm_compute_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset;
3113 adjust_tsc_offset_guest(vcpu, adj);
3114 vcpu->arch.ia32_tsc_adjust_msr += adj;
3115 }
Paolo Bonzinidd259932018-04-13 11:38:35 +02003116 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003117 case MSR_IA32_XSS:
3118 if (!msr_info->host_initiated &&
3119 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3120 return 1;
3121 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08003122 * KVM supports exposing PT to the guest, but does not support
3123 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
3124 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003125 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01003126 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003127 return 1;
3128 vcpu->arch.ia32_xss = data;
3129 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003130 case MSR_SMI_COUNT:
3131 if (!msr_info->host_initiated)
3132 return 1;
3133 vcpu->arch.smi_count = data;
3134 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003135 case MSR_KVM_WALL_CLOCK_NEW:
Oliver Upton66570e92020-08-18 15:24:28 +00003136 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3137 return 1;
3138
3139 kvm_write_wall_clock(vcpu->kvm, data);
3140 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003141 case MSR_KVM_WALL_CLOCK:
Oliver Upton66570e92020-08-18 15:24:28 +00003142 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3143 return 1;
3144
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003145 kvm_write_wall_clock(vcpu->kvm, data);
3146 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003147 case MSR_KVM_SYSTEM_TIME_NEW:
Oliver Upton66570e92020-08-18 15:24:28 +00003148 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3149 return 1;
3150
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003151 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003152 break;
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003153 case MSR_KVM_SYSTEM_TIME:
Oliver Upton66570e92020-08-18 15:24:28 +00003154 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3155 return 1;
3156
3157 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated);
Oliver Upton5b9bb0e2020-08-18 15:24:26 +00003158 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003159 case MSR_KVM_ASYNC_PF_EN:
Oliver Upton66570e92020-08-18 15:24:28 +00003160 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3161 return 1;
3162
Gleb Natapov344d9582010-10-14 11:22:50 +02003163 if (kvm_pv_enable_async_pf(vcpu, data))
3164 return 1;
3165 break;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003166 case MSR_KVM_ASYNC_PF_INT:
Oliver Upton66570e92020-08-18 15:24:28 +00003167 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
3168 return 1;
3169
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003170 if (kvm_pv_enable_async_pf_int(vcpu, data))
3171 return 1;
3172 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003173 case MSR_KVM_ASYNC_PF_ACK:
Oliver Upton66570e92020-08-18 15:24:28 +00003174 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3175 return 1;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003176 if (data & 0x1) {
3177 vcpu->arch.apf.pageready_pending = false;
3178 kvm_check_async_pf_completion(vcpu);
3179 }
3180 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003181 case MSR_KVM_STEAL_TIME:
Oliver Upton66570e92020-08-18 15:24:28 +00003182 if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME))
3183 return 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04003184
3185 if (unlikely(!sched_info_on()))
3186 return 1;
3187
3188 if (data & KVM_STEAL_RESERVED_MASK)
3189 return 1;
3190
Glauber Costac9aaa892011-07-11 15:28:14 -04003191 vcpu->arch.st.msr_val = data;
3192
3193 if (!(data & KVM_MSR_ENABLED))
3194 break;
3195
Glauber Costac9aaa892011-07-11 15:28:14 -04003196 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
3197
3198 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003199 case MSR_KVM_PV_EOI_EN:
Oliver Upton66570e92020-08-18 15:24:28 +00003200 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI))
3201 return 1;
3202
Ladi Prosek72bbf932018-10-16 18:49:59 +02003203 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03003204 return 1;
3205 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003206
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003207 case MSR_KVM_POLL_CONTROL:
Oliver Upton66570e92020-08-18 15:24:28 +00003208 if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL))
3209 return 1;
3210
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003211 /* only enable bit supported */
3212 if (data & (-1ULL << 1))
3213 return 1;
3214
3215 vcpu->arch.msr_kvm_poll_control = data;
3216 break;
3217
Huang Ying890ca9a2009-05-11 16:48:15 +08003218 case MSR_IA32_MCG_CTL:
3219 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003220 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07003221 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02003222
Wei Huang6912ac32015-06-12 01:34:56 -04003223 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3224 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003225 pr = true;
3226 fallthrough;
Wei Huang6912ac32015-06-12 01:34:56 -04003227 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3228 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003229 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003230 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02003231
3232 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03003233 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
3234 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02003235 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003236 case MSR_K7_CLK_CTL:
3237 /*
3238 * Ignore all writes to this no longer documented MSR.
3239 * Writes are only relevant for old K7 processors,
3240 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08003241 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003242 * affected processor models on the command line, hence
3243 * the need to ignore the workaround.
3244 */
3245 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003246 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003247 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3248 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003249 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3250 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003251 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003252 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3253 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3254 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003255 return kvm_hv_set_msr_common(vcpu, msr, data,
3256 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003257 case MSR_IA32_BBL_CR_CTL3:
3258 /* Drop writes to this legacy MSR -- see rdmsr
3259 * counterpart for further detail.
3260 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003261 if (report_ignored_msrs)
3262 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
3263 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05003264 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003265 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003266 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003267 return 1;
3268 vcpu->arch.osvw.length = data;
3269 break;
3270 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003271 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003272 return 1;
3273 vcpu->arch.osvw.status = data;
3274 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003275 case MSR_PLATFORM_INFO:
3276 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07003277 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
3278 cpuid_fault_enabled(vcpu)))
3279 return 1;
3280 vcpu->arch.msr_platform_info = data;
3281 break;
3282 case MSR_MISC_FEATURES_ENABLES:
3283 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
3284 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3285 !supports_cpuid_fault(vcpu)))
3286 return 1;
3287 vcpu->arch.msr_misc_features_enables = data;
3288 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003289 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07003290 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
3291 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02003292 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003293 return kvm_pmu_set_msr(vcpu, msr_info);
Peter Xu6abe9c12020-06-22 18:04:41 -04003294 return KVM_MSR_RET_INVALID;
Carsten Otte15c4a642007-10-30 18:44:17 +01003295 }
3296 return 0;
3297}
3298EXPORT_SYMBOL_GPL(kvm_set_msr_common);
3299
Paolo Bonzini44883f02018-07-26 13:01:52 +02003300static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003301{
3302 u64 data;
3303 u64 mcg_cap = vcpu->arch.mcg_cap;
3304 unsigned bank_num = mcg_cap & 0xff;
3305
3306 switch (msr) {
3307 case MSR_IA32_P5_MC_ADDR:
3308 case MSR_IA32_P5_MC_TYPE:
3309 data = 0;
3310 break;
3311 case MSR_IA32_MCG_CAP:
3312 data = vcpu->arch.mcg_cap;
3313 break;
3314 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003315 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003316 return 1;
3317 data = vcpu->arch.mcg_ctl;
3318 break;
3319 case MSR_IA32_MCG_STATUS:
3320 data = vcpu->arch.mcg_status;
3321 break;
3322 default:
3323 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003324 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003325 u32 offset = array_index_nospec(
3326 msr - MSR_IA32_MC0_CTL,
3327 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3328
Huang Ying890ca9a2009-05-11 16:48:15 +08003329 data = vcpu->arch.mce_banks[offset];
3330 break;
3331 }
3332 return 1;
3333 }
3334 *pdata = data;
3335 return 0;
3336}
3337
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003338int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003339{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003340 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003341 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003342 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003343 case MSR_IA32_DEBUGCTLMSR:
3344 case MSR_IA32_LASTBRANCHFROMIP:
3345 case MSR_IA32_LASTBRANCHTOIP:
3346 case MSR_IA32_LASTINTFROMIP:
3347 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303348 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003349 case MSR_K8_TSEG_ADDR:
3350 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003351 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003352 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003353 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003354 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003355 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003356 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003357 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003358 case MSR_F15H_EX_CFG:
Venkatesh Srinivas2ca1a062020-04-16 11:42:54 -07003359 /*
3360 * Intel Sandy Bridge CPUs must support the RAPL (running average power
3361 * limit) MSRs. Just return 0, as we do not want to expose the host
3362 * data here. Do not conditionalize this on CPUID, as KVM does not do
3363 * so for existing CPU-specific MSRs.
3364 */
3365 case MSR_RAPL_POWER_UNIT:
3366 case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */
3367 case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */
3368 case MSR_PKG_ENERGY_STATUS: /* Total package */
3369 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003370 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003371 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003372 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003373 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3374 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3375 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3376 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003377 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003378 return kvm_pmu_get_msr(vcpu, msr_info);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003379 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003380 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003381 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003382 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003383 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003384 case MSR_IA32_ARCH_CAPABILITIES:
3385 if (!msr_info->host_initiated &&
3386 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3387 return 1;
3388 msr_info->data = vcpu->arch.arch_capabilities;
3389 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02003390 case MSR_IA32_PERF_CAPABILITIES:
3391 if (!msr_info->host_initiated &&
3392 !guest_cpuid_has(vcpu, X86_FEATURE_PDCM))
3393 return 1;
3394 msr_info->data = vcpu->arch.perf_capabilities;
3395 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003396 case MSR_IA32_POWER_CTL:
3397 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3398 break;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003399 case MSR_IA32_TSC: {
3400 /*
3401 * Intel SDM states that MSR_IA32_TSC read adds the TSC offset
3402 * even when not intercepted. AMD manual doesn't explicitly
3403 * state this but appears to behave the same.
3404 *
Maxim Levitskyee6fa052020-09-21 13:38:05 +03003405 * On userspace reads and writes, however, we unconditionally
Paolo Bonzinic0623f52020-10-21 18:05:58 -04003406 * return L1's TSC value to ensure backwards-compatible
Maxim Levitskyee6fa052020-09-21 13:38:05 +03003407 * behavior for migration.
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003408 */
3409 u64 tsc_offset = msr_info->host_initiated ? vcpu->arch.l1_tsc_offset :
3410 vcpu->arch.tsc_offset;
3411
3412 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + tsc_offset;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003413 break;
Maxim Levitskycc5b54d2020-09-21 13:38:05 +03003414 }
Avi Kivity9ba075a2008-05-26 20:06:35 +03003415 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003416 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003417 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003418 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003419 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003420 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003421 /*
3422 * MSR_EBC_FREQUENCY_ID
3423 * Conservative value valid for even the basic CPU models.
3424 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3425 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3426 * and 266MHz for model 3, or 4. Set Core Clock
3427 * Frequency to System Bus Frequency Ratio to 1 (bits
3428 * 31:24) even though these are only valid for CPU
3429 * models > 2, however guests may end up dividing or
3430 * multiplying by zero otherwise.
3431 */
3432 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003433 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003434 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003435 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003436 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003437 break;
Xiaoyao Libf10bd02020-06-16 15:33:07 +08003438 case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003439 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003440 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003441 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003442 break;
Will Auldba904632012-11-29 12:42:50 -08003443 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003444 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003445 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003446 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003447 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003448 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003449 case MSR_IA32_SMBASE:
3450 if (!msr_info->host_initiated)
3451 return 1;
3452 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003453 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003454 case MSR_SMI_COUNT:
3455 msr_info->data = vcpu->arch.smi_count;
3456 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003457 case MSR_IA32_PERF_STATUS:
3458 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003459 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003460 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003461 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003462 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003463 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003464 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003465 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003466 case MSR_KVM_WALL_CLOCK:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003467 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3468 return 1;
3469
3470 msr_info->data = vcpu->kvm->arch.wall_clock;
3471 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003472 case MSR_KVM_WALL_CLOCK_NEW:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003473 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3474 return 1;
3475
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003476 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003477 break;
3478 case MSR_KVM_SYSTEM_TIME:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003479 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE))
3480 return 1;
3481
3482 msr_info->data = vcpu->arch.time;
3483 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04003484 case MSR_KVM_SYSTEM_TIME_NEW:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003485 if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2))
3486 return 1;
3487
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003488 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003489 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003490 case MSR_KVM_ASYNC_PF_EN:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003491 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3492 return 1;
3493
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003494 msr_info->data = vcpu->arch.apf.msr_en_val;
3495 break;
3496 case MSR_KVM_ASYNC_PF_INT:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003497 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT))
3498 return 1;
3499
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +02003500 msr_info->data = vcpu->arch.apf.msr_int_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003501 break;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003502 case MSR_KVM_ASYNC_PF_ACK:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003503 if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF))
3504 return 1;
3505
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02003506 msr_info->data = 0;
3507 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003508 case MSR_KVM_STEAL_TIME:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003509 if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME))
3510 return 1;
3511
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003512 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003513 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003514 case MSR_KVM_PV_EOI_EN:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003515 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI))
3516 return 1;
3517
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003518 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003519 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003520 case MSR_KVM_POLL_CONTROL:
Oliver Upton1930e5d2020-10-27 16:10:41 -07003521 if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL))
3522 return 1;
3523
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003524 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3525 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003526 case MSR_IA32_P5_MC_ADDR:
3527 case MSR_IA32_P5_MC_TYPE:
3528 case MSR_IA32_MCG_CAP:
3529 case MSR_IA32_MCG_CTL:
3530 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003531 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003532 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3533 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003534 case MSR_IA32_XSS:
3535 if (!msr_info->host_initiated &&
3536 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3537 return 1;
3538 msr_info->data = vcpu->arch.ia32_xss;
3539 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003540 case MSR_K7_CLK_CTL:
3541 /*
3542 * Provide expected ramp-up count for K7. All other
3543 * are set to zero, indicating minimum divisors for
3544 * every field.
3545 *
3546 * This prevents guest kernels on AMD host with CPU
3547 * type 6, model 8 and higher from exploding due to
3548 * the rdmsr failing.
3549 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003550 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003551 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003552 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Jon Doronf97f5a52020-05-29 16:45:40 +03003553 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
3554 case HV_X64_MSR_SYNDBG_OPTIONS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003555 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3556 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003557 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003558 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3559 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3560 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003561 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003562 msr_info->index, &msr_info->data,
3563 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003564 case MSR_IA32_BBL_CR_CTL3:
3565 /* This legacy MSR exists but isn't fully documented in current
3566 * silicon. It is however accessed by winxp in very narrow
3567 * scenarios where it sets bit #19, itself documented as
3568 * a "reserved" bit. Best effort attempt to source coherent
3569 * read data here should the balance of the register be
3570 * interpreted by the guest:
3571 *
3572 * L2 cache control register 3: 64GB range, 256KB size,
3573 * enabled, latency 0x1, configured
3574 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003575 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003576 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003577 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003578 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003579 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003580 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003581 break;
3582 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003583 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003584 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003585 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003586 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003587 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003588 if (!msr_info->host_initiated &&
3589 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3590 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003591 msr_info->data = vcpu->arch.msr_platform_info;
3592 break;
3593 case MSR_MISC_FEATURES_ENABLES:
3594 msr_info->data = vcpu->arch.msr_misc_features_enables;
3595 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003596 case MSR_K7_HWCR:
3597 msr_info->data = vcpu->arch.msr_hwcr;
3598 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003599 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003600 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Wei Wangcbd71752020-05-29 15:43:44 +08003601 return kvm_pmu_get_msr(vcpu, msr_info);
Peter Xu6abe9c12020-06-22 18:04:41 -04003602 return KVM_MSR_RET_INVALID;
Carsten Otte15c4a642007-10-30 18:44:17 +01003603 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003604 return 0;
3605}
3606EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3607
Carsten Otte313a3dc2007-10-11 19:16:52 +02003608/*
3609 * Read or write a bunch of msrs. All parameters are kernel addresses.
3610 *
3611 * @return number of msrs set successfully.
3612 */
3613static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3614 struct kvm_msr_entry *entries,
3615 int (*do_msr)(struct kvm_vcpu *vcpu,
3616 unsigned index, u64 *data))
3617{
Tom Lendacky801e4592018-02-21 13:39:51 -06003618 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003619
Carsten Otte313a3dc2007-10-11 19:16:52 +02003620 for (i = 0; i < msrs->nmsrs; ++i)
3621 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3622 break;
3623
Carsten Otte313a3dc2007-10-11 19:16:52 +02003624 return i;
3625}
3626
3627/*
3628 * Read or write a bunch of msrs. Parameters are user addresses.
3629 *
3630 * @return number of msrs set successfully.
3631 */
3632static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3633 int (*do_msr)(struct kvm_vcpu *vcpu,
3634 unsigned index, u64 *data),
3635 int writeback)
3636{
3637 struct kvm_msrs msrs;
3638 struct kvm_msr_entry *entries;
3639 int r, n;
3640 unsigned size;
3641
3642 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003643 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003644 goto out;
3645
3646 r = -E2BIG;
3647 if (msrs.nmsrs >= MAX_IO_MSRS)
3648 goto out;
3649
Carsten Otte313a3dc2007-10-11 19:16:52 +02003650 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003651 entries = memdup_user(user_msrs->entries, size);
3652 if (IS_ERR(entries)) {
3653 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003654 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003655 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003656
3657 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3658 if (r < 0)
3659 goto out_free;
3660
3661 r = -EFAULT;
3662 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3663 goto out_free;
3664
3665 r = n;
3666
3667out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003668 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003669out:
3670 return r;
3671}
3672
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003673static inline bool kvm_can_mwait_in_guest(void)
3674{
3675 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003676 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3677 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003678}
3679
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02003680static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu,
3681 struct kvm_cpuid2 __user *cpuid_arg)
3682{
3683 struct kvm_cpuid2 cpuid;
3684 int r;
3685
3686 r = -EFAULT;
3687 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
3688 return r;
3689
3690 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries);
3691 if (r)
3692 return r;
3693
3694 r = -EFAULT;
3695 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
3696 return r;
3697
3698 return 0;
3699}
3700
Alexander Graf784aa3d2014-07-14 18:27:35 +02003701int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003702{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003703 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003704
3705 switch (ext) {
3706 case KVM_CAP_IRQCHIP:
3707 case KVM_CAP_HLT:
3708 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003709 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003710 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003711 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003712 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003713 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003714 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003715 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003716 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003717 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003718 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003719 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003720 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003721 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003722 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003723 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003724 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003725 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003726 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003727 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003728 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003729 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003730 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003731 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003732 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003733 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003734 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003735 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003736 case KVM_CAP_HYPERV_CPUID:
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02003737 case KVM_CAP_SYS_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003738 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003739 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003740 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003741 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003742 case KVM_CAP_ASYNC_PF:
Vitaly Kuznetsov72de5fa4c2020-05-25 16:41:22 +02003743 case KVM_CAP_ASYNC_PF_INT:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003744 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003745 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003746 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003747 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003748 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003749 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003750 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003751 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003752 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003753 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003754 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003755 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003756 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003757 case KVM_CAP_EXCEPTION_PAYLOAD:
Peter Xub9b27822020-05-05 11:47:50 -04003758 case KVM_CAP_SET_GUEST_DEBUG:
Jim Mattson1aa561b2020-06-03 16:56:21 -07003759 case KVM_CAP_LAST_CPU:
Alexander Graf1ae09952020-09-25 16:34:16 +02003760 case KVM_CAP_X86_USER_SPACE_MSR:
Alexander Graf1a1552542020-09-25 16:34:21 +02003761 case KVM_CAP_X86_MSR_FILTER:
Oliver Upton66570e92020-08-18 15:24:28 +00003762 case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003763 r = 1;
3764 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003765 case KVM_CAP_SYNC_REGS:
3766 r = KVM_SYNC_X86_VALID_FIELDS;
3767 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003768 case KVM_CAP_ADJUST_CLOCK:
3769 r = KVM_CLOCK_TSC_STABLE;
3770 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003771 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003772 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3773 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003774 if(kvm_can_mwait_in_guest())
3775 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003776 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003777 case KVM_CAP_X86_SMM:
3778 /* SMBASE is usually relocated above 1M on modern chipsets,
3779 * and SMM handlers might indeed rely on 4G segment limits,
3780 * so do not report SMM to be available if real mode is
3781 * emulated via vm86 mode. Still, do not go to great lengths
3782 * to avoid userspace's usage of the feature, because it is a
3783 * fringe case that is not enabled except via specific settings
3784 * of the module parameters.
3785 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003786 r = kvm_x86_ops.has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003787 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003788 case KVM_CAP_VAPIC:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003789 r = !kvm_x86_ops.cpu_has_accelerated_tpr();
Avi Kivity774ead32007-12-26 13:57:04 +02003790 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003791 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003792 r = KVM_SOFT_MAX_VCPUS;
3793 break;
3794 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003795 r = KVM_MAX_VCPUS;
3796 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003797 case KVM_CAP_MAX_VCPU_ID:
3798 r = KVM_MAX_VCPU_ID;
3799 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003800 case KVM_CAP_PV_MMU: /* obsolete */
3801 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003802 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003803 case KVM_CAP_MCE:
3804 r = KVM_MAX_MCE_BANKS;
3805 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003806 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003807 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003808 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003809 case KVM_CAP_TSC_CONTROL:
3810 r = kvm_has_tsc_control;
3811 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003812 case KVM_CAP_X2APIC_API:
3813 r = KVM_X2APIC_API_VALID_FLAGS;
3814 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003815 case KVM_CAP_NESTED_STATE:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003816 r = kvm_x86_ops.nested_ops->get_state ?
3817 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003818 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003819 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003820 r = kvm_x86_ops.enable_direct_tlbflush != NULL;
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003821 break;
3822 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04003823 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003824 break;
Mohammed Gamal3edd6832020-07-10 17:48:11 +02003825 case KVM_CAP_SMALLER_MAXPHYADDR:
3826 r = (int) allow_smaller_maxphyaddr;
3827 break;
Andrew Jones004a0122020-08-04 19:06:04 +02003828 case KVM_CAP_STEAL_TIME:
3829 r = sched_info_on();
3830 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003831 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003832 break;
3833 }
3834 return r;
3835
3836}
3837
Carsten Otte043405e2007-10-10 17:16:19 +02003838long kvm_arch_dev_ioctl(struct file *filp,
3839 unsigned int ioctl, unsigned long arg)
3840{
3841 void __user *argp = (void __user *)arg;
3842 long r;
3843
3844 switch (ioctl) {
3845 case KVM_GET_MSR_INDEX_LIST: {
3846 struct kvm_msr_list __user *user_msr_list = argp;
3847 struct kvm_msr_list msr_list;
3848 unsigned n;
3849
3850 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003851 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003852 goto out;
3853 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003854 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003855 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003856 goto out;
3857 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003858 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003859 goto out;
3860 r = -EFAULT;
3861 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3862 num_msrs_to_save * sizeof(u32)))
3863 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003864 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003865 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003866 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003867 goto out;
3868 r = 0;
3869 break;
3870 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003871 case KVM_GET_SUPPORTED_CPUID:
3872 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003873 struct kvm_cpuid2 __user *cpuid_arg = argp;
3874 struct kvm_cpuid2 cpuid;
3875
3876 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003877 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003878 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003879
3880 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3881 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003882 if (r)
3883 goto out;
3884
3885 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003886 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003887 goto out;
3888 r = 0;
3889 break;
3890 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003891 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08003892 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003893 if (copy_to_user(argp, &kvm_mce_cap_supported,
3894 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003895 goto out;
3896 r = 0;
3897 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003898 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3899 struct kvm_msr_list __user *user_msr_list = argp;
3900 struct kvm_msr_list msr_list;
3901 unsigned int n;
3902
3903 r = -EFAULT;
3904 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3905 goto out;
3906 n = msr_list.nmsrs;
3907 msr_list.nmsrs = num_msr_based_features;
3908 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3909 goto out;
3910 r = -E2BIG;
3911 if (n < msr_list.nmsrs)
3912 goto out;
3913 r = -EFAULT;
3914 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3915 num_msr_based_features * sizeof(u32)))
3916 goto out;
3917 r = 0;
3918 break;
3919 }
3920 case KVM_GET_MSRS:
3921 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3922 break;
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02003923 case KVM_GET_SUPPORTED_HV_CPUID:
3924 r = kvm_ioctl_get_supported_hv_cpuid(NULL, argp);
3925 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003926 default:
3927 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003928 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003929 }
3930out:
3931 return r;
3932}
3933
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003934static void wbinvd_ipi(void *garbage)
3935{
3936 wbinvd();
3937}
3938
3939static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3940{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003941 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003942}
3943
Carsten Otte313a3dc2007-10-11 19:16:52 +02003944void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3945{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003946 /* Address WBINVD may be executed by guest */
3947 if (need_emulate_wbinvd(vcpu)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003948 if (kvm_x86_ops.has_wbinvd_exit())
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003949 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3950 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3951 smp_call_function_single(vcpu->cpu,
3952 wbinvd_ipi, NULL, 1);
3953 }
3954
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003955 kvm_x86_ops.vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003956
Babu Moger37486132020-05-12 18:59:06 -05003957 /* Save host pkru register if supported */
3958 vcpu->arch.host_pkru = read_pkru();
3959
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003960 /* Apply any externally detected TSC adjustments (due to suspend) */
3961 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3962 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3963 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003964 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003965 }
3966
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003967 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003968 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003969 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003970 if (tsc_delta < 0)
3971 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003972
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003973 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003974 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003975 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003976 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003977 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003978 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003979
3980 if (kvm_lapic_hv_timer_in_use(vcpu))
3981 kvm_lapic_restart_hv_timer(vcpu);
3982
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003983 /*
3984 * On a host with synchronized TSC, there is no need to update
3985 * kvmclock on vcpu->cpu migration
3986 */
3987 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003988 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003989 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003990 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003991 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003992 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003993
Glauber Costac9aaa892011-07-11 15:28:14 -04003994 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003995}
3996
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003997static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3998{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003999 struct kvm_host_map map;
4000 struct kvm_steal_time *st;
4001
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004002 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
4003 return;
4004
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00004005 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00004006 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004007
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004008 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
4009 &vcpu->arch.st.cache, true))
4010 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004011
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004012 st = map.hva +
4013 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
4014
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00004015 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00004016
4017 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004018}
4019
Carsten Otte313a3dc2007-10-11 19:16:52 +02004020void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
4021{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01004022 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004023
4024 if (vcpu->preempted)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004025 vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl(vcpu);
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004026
Andrea Arcangeli931f2612016-12-17 18:43:52 +01004027 /*
4028 * Disable page faults because we're in atomic context here.
4029 * kvm_write_guest_offset_cached() would call might_fault()
4030 * that relies on pagefault_disable() to tell if there's a
4031 * bug. NOTE: the write to guest memory may not go through if
4032 * during postcopy live migration or if there's heavy guest
4033 * paging.
4034 */
4035 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01004036 /*
4037 * kvm_memslots() will be called by
4038 * kvm_write_guest_offset_cached() so take the srcu lock.
4039 */
4040 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04004041 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01004042 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01004043 pagefault_enable();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004044 kvm_x86_ops.vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02004045 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01004046 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02004047 * If userspace has set any breakpoints or watchpoints, dr6 is restored
4048 * on every vmexit, but if not, we might have a stale dr6 from the
4049 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01004050 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02004051 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004052}
4053
Carsten Otte313a3dc2007-10-11 19:16:52 +02004054static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
4055 struct kvm_lapic_state *s)
4056{
Liran Alonfa59cc02017-12-24 18:12:53 +02004057 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004058 kvm_x86_ops.sync_pir_to_irr(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004059
Radim Krčmářa92e2542016-07-12 22:09:22 +02004060 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004061}
4062
4063static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
4064 struct kvm_lapic_state *s)
4065{
Radim Krčmářa92e2542016-07-12 22:09:22 +02004066 int r;
4067
4068 r = kvm_apic_set_state(vcpu, s);
4069 if (r)
4070 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03004071 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004072
4073 return 0;
4074}
4075
Matt Gingell127a4572015-11-17 17:32:05 +01004076static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
4077{
4078 return (!lapic_in_kernel(vcpu) ||
4079 kvm_apic_accept_pic_intr(vcpu));
4080}
4081
Matt Gingell782d4222015-11-16 15:26:00 -08004082/*
4083 * if userspace requested an interrupt window, check that the
4084 * interrupt window is open.
4085 *
4086 * No need to exit to userspace if we already have an interrupt queued.
4087 */
4088static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
4089{
4090 return kvm_arch_interrupt_allowed(vcpu) &&
4091 !kvm_cpu_has_interrupt(vcpu) &&
4092 !kvm_event_needs_reinjection(vcpu) &&
4093 kvm_cpu_accept_dm_intr(vcpu);
4094}
4095
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004096static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
4097 struct kvm_interrupt *irq)
4098{
Chen Gang02cdb502013-02-27 11:33:25 +08004099 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004100 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004101
4102 if (!irqchip_in_kernel(vcpu->kvm)) {
4103 kvm_queue_interrupt(vcpu, irq->irq, false);
4104 kvm_make_request(KVM_REQ_EVENT, vcpu);
4105 return 0;
4106 }
4107
4108 /*
4109 * With in-kernel LAPIC, we only use this to inject EXTINT, so
4110 * fail for in-kernel 8259.
4111 */
4112 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004113 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004114
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004115 if (vcpu->arch.pending_external_vector != -1)
4116 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004117
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02004118 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08004119 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004120 return 0;
4121}
4122
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004123static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
4124{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004125 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004126
4127 return 0;
4128}
4129
Paolo Bonzinif0778252015-04-01 15:06:40 +02004130static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
4131{
Paolo Bonzini64d60672015-05-07 11:36:11 +02004132 kvm_make_request(KVM_REQ_SMI, vcpu);
4133
Paolo Bonzinif0778252015-04-01 15:06:40 +02004134 return 0;
4135}
4136
Avi Kivityb209749f2007-10-22 16:50:39 +02004137static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
4138 struct kvm_tpr_access_ctl *tac)
4139{
4140 if (tac->flags)
4141 return -EINVAL;
4142 vcpu->arch.tpr_access_reporting = !!tac->enabled;
4143 return 0;
4144}
4145
Huang Ying890ca9a2009-05-11 16:48:15 +08004146static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
4147 u64 mcg_cap)
4148{
4149 int r;
4150 unsigned bank_num = mcg_cap & 0xff, bank;
4151
4152 r = -EINVAL;
Jim Mattsonc4e0e4ab2020-05-11 15:56:16 -07004153 if (!bank_num || bank_num > KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08004154 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004155 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08004156 goto out;
4157 r = 0;
4158 vcpu->arch.mcg_cap = mcg_cap;
4159 /* Init IA32_MCG_CTL to all 1s */
4160 if (mcg_cap & MCG_CTL_P)
4161 vcpu->arch.mcg_ctl = ~(u64)0;
4162 /* Init IA32_MCi_CTL to all 1s */
4163 for (bank = 0; bank < bank_num; bank++)
4164 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004165
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004166 kvm_x86_ops.setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08004167out:
4168 return r;
4169}
4170
4171static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
4172 struct kvm_x86_mce *mce)
4173{
4174 u64 mcg_cap = vcpu->arch.mcg_cap;
4175 unsigned bank_num = mcg_cap & 0xff;
4176 u64 *banks = vcpu->arch.mce_banks;
4177
4178 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
4179 return -EINVAL;
4180 /*
4181 * if IA32_MCG_CTL is not all 1s, the uncorrected error
4182 * reporting is disabled
4183 */
4184 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
4185 vcpu->arch.mcg_ctl != ~(u64)0)
4186 return 0;
4187 banks += 4 * mce->bank;
4188 /*
4189 * if IA32_MCi_CTL is not all 1s, the uncorrected error
4190 * reporting is disabled for the bank
4191 */
4192 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
4193 return 0;
4194 if (mce->status & MCI_STATUS_UC) {
4195 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02004196 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03004197 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08004198 return 0;
4199 }
4200 if (banks[1] & MCI_STATUS_VAL)
4201 mce->status |= MCI_STATUS_OVER;
4202 banks[2] = mce->addr;
4203 banks[3] = mce->misc;
4204 vcpu->arch.mcg_status = mce->mcg_status;
4205 banks[1] = mce->status;
4206 kvm_queue_exception(vcpu, MC_VECTOR);
4207 } else if (!(banks[1] & MCI_STATUS_VAL)
4208 || !(banks[1] & MCI_STATUS_UC)) {
4209 if (banks[1] & MCI_STATUS_VAL)
4210 mce->status |= MCI_STATUS_OVER;
4211 banks[2] = mce->addr;
4212 banks[3] = mce->misc;
4213 banks[1] = mce->status;
4214 } else
4215 banks[1] |= MCI_STATUS_OVER;
4216 return 0;
4217}
4218
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004219static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
4220 struct kvm_vcpu_events *events)
4221{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004222 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07004223
Wanpeng Li664f8e22017-08-24 03:35:09 -07004224 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08004225 * In guest mode, payload delivery should be deferred,
4226 * so that the L1 hypervisor can intercept #PF before
4227 * CR2 is modified (or intercept #DB before DR6 is
4228 * modified under nVMX). Unless the per-VM capability,
4229 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
4230 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
4231 * opportunistically defer the exception payload, deliver it if the
4232 * capability hasn't been requested before processing a
4233 * KVM_GET_VCPU_EVENTS.
4234 */
4235 if (!vcpu->kvm->arch.exception_payload_enabled &&
4236 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
4237 kvm_deliver_exception_payload(vcpu);
4238
4239 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07004240 * The API doesn't provide the instruction length for software
4241 * exceptions, so don't report them. As long as the guest RIP
4242 * isn't advanced, we should expect to encounter the exception
4243 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07004244 */
Jim Mattson59073aa2018-10-16 14:29:20 -07004245 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
4246 events->exception.injected = 0;
4247 events->exception.pending = 0;
4248 } else {
4249 events->exception.injected = vcpu->arch.exception.injected;
4250 events->exception.pending = vcpu->arch.exception.pending;
4251 /*
4252 * For ABI compatibility, deliberately conflate
4253 * pending and injected exceptions when
4254 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
4255 */
4256 if (!vcpu->kvm->arch.exception_payload_enabled)
4257 events->exception.injected |=
4258 vcpu->arch.exception.pending;
4259 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004260 events->exception.nr = vcpu->arch.exception.nr;
4261 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
4262 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07004263 events->exception_has_payload = vcpu->arch.exception.has_payload;
4264 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004265
Jan Kiszka03b82a32010-02-15 10:45:41 +01004266 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03004267 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004268 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01004269 events->interrupt.soft = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004270 events->interrupt.shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004271
4272 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004273 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004274 events->nmi.masked = kvm_x86_ops.get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004275 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004276
Jan Kiszka66450a22013-03-13 12:42:34 +01004277 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004278
Paolo Bonzinif0778252015-04-01 15:06:40 +02004279 events->smi.smm = is_smm(vcpu);
4280 events->smi.pending = vcpu->arch.smi_pending;
4281 events->smi.smm_inside_nmi =
4282 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
4283 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
4284
Jan Kiszkadab4b912009-12-06 18:24:15 +01004285 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02004286 | KVM_VCPUEVENT_VALID_SHADOW
4287 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07004288 if (vcpu->kvm->arch.exception_payload_enabled)
4289 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
4290
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004291 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004292}
4293
Sean Christophersonc5833c72019-04-02 08:03:10 -07004294static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004295
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004296static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
4297 struct kvm_vcpu_events *events)
4298{
Jan Kiszkadab4b912009-12-06 18:24:15 +01004299 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01004300 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02004301 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07004302 | KVM_VCPUEVENT_VALID_SMM
4303 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004304 return -EINVAL;
4305
Jim Mattson59073aa2018-10-16 14:29:20 -07004306 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
4307 if (!vcpu->kvm->arch.exception_payload_enabled)
4308 return -EINVAL;
4309 if (events->exception.pending)
4310 events->exception.injected = 0;
4311 else
4312 events->exception_has_payload = 0;
4313 } else {
4314 events->exception.pending = 0;
4315 events->exception_has_payload = 0;
4316 }
4317
4318 if ((events->exception.injected || events->exception.pending) &&
4319 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02004320 return -EINVAL;
4321
David Hildenbrand28bf2882017-03-23 11:46:03 +01004322 /* INITs are latched while in SMM */
4323 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
4324 (events->smi.smm || events->smi.pending) &&
4325 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4326 return -EINVAL;
4327
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004328 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07004329 vcpu->arch.exception.injected = events->exception.injected;
4330 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004331 vcpu->arch.exception.nr = events->exception.nr;
4332 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
4333 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07004334 vcpu->arch.exception.has_payload = events->exception_has_payload;
4335 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004336
Liran Alon04140b42018-03-23 03:01:31 +03004337 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004338 vcpu->arch.interrupt.nr = events->interrupt.nr;
4339 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01004340 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004341 kvm_x86_ops.set_interrupt_shadow(vcpu,
Jan Kiszka48005f62010-02-19 19:38:07 +01004342 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004343
4344 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01004345 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
4346 vcpu->arch.nmi_pending = events->nmi.pending;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004347 kvm_x86_ops.set_nmi_mask(vcpu, events->nmi.masked);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004348
Jan Kiszka66450a22013-03-13 12:42:34 +01004349 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004350 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01004351 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004352
Paolo Bonzinif0778252015-04-01 15:06:40 +02004353 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07004354 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
4355 if (events->smi.smm)
4356 vcpu->arch.hflags |= HF_SMM_MASK;
4357 else
4358 vcpu->arch.hflags &= ~HF_SMM_MASK;
4359 kvm_smm_changed(vcpu);
4360 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01004361
Paolo Bonzinif0778252015-04-01 15:06:40 +02004362 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004363
4364 if (events->smi.smm) {
4365 if (events->smi.smm_inside_nmi)
4366 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02004367 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07004368 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02004369 }
4370
4371 if (lapic_in_kernel(vcpu)) {
4372 if (events->smi.latched_init)
4373 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
4374 else
4375 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02004376 }
4377 }
4378
Avi Kivity3842d132010-07-27 12:30:24 +03004379 kvm_make_request(KVM_REQ_EVENT, vcpu);
4380
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004381 return 0;
4382}
4383
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004384static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
4385 struct kvm_debugregs *dbgregs)
4386{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004387 unsigned long val;
4388
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004389 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004390 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004391 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004392 dbgregs->dr7 = vcpu->arch.dr7;
4393 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004394 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004395}
4396
4397static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
4398 struct kvm_debugregs *dbgregs)
4399{
4400 if (dbgregs->flags)
4401 return -EINVAL;
4402
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004403 if (dbgregs->dr6 & ~0xffffffffull)
4404 return -EINVAL;
4405 if (dbgregs->dr7 & ~0xffffffffull)
4406 return -EINVAL;
4407
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004408 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004409 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004410 vcpu->arch.dr6 = dbgregs->dr6;
4411 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004412 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004413
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004414 return 0;
4415}
4416
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004417#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4418
4419static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4420{
Marc Orrb666a4b2018-11-06 14:53:56 -08004421 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004422 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004423 u64 valid;
4424
4425 /*
4426 * Copy legacy XSAVE area, to avoid complications with CPUID
4427 * leaves 0 and 1 in the loop below.
4428 */
4429 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4430
4431 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004432 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004433 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4434
4435 /*
4436 * Copy each region from the possibly compacted offset to the
4437 * non-compacted offset.
4438 */
Dave Hansend91cab72015-09-02 16:31:26 -07004439 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004440 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004441 u64 xfeature_mask = valid & -valid;
4442 int xfeature_nr = fls64(xfeature_mask) - 1;
4443 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004444
4445 if (src) {
4446 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004447 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004448 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004449 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004450 memcpy(dest + offset, &vcpu->arch.pkru,
4451 sizeof(vcpu->arch.pkru));
4452 else
4453 memcpy(dest + offset, src, size);
4454
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004455 }
4456
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004457 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004458 }
4459}
4460
4461static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4462{
Marc Orrb666a4b2018-11-06 14:53:56 -08004463 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004464 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4465 u64 valid;
4466
4467 /*
4468 * Copy legacy XSAVE area, to avoid complications with CPUID
4469 * leaves 0 and 1 in the loop below.
4470 */
4471 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4472
4473 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004474 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004475 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004476 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004477
4478 /*
4479 * Copy each region from the non-compacted offset to the
4480 * possibly compacted offset.
4481 */
Dave Hansend91cab72015-09-02 16:31:26 -07004482 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004483 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004484 u64 xfeature_mask = valid & -valid;
4485 int xfeature_nr = fls64(xfeature_mask) - 1;
4486 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004487
4488 if (dest) {
4489 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004490 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004491 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004492 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004493 memcpy(&vcpu->arch.pkru, src + offset,
4494 sizeof(vcpu->arch.pkru));
4495 else
4496 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004497 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004498
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004499 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004500 }
4501}
4502
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004503static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4504 struct kvm_xsave *guest_xsave)
4505{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004506 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004507 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4508 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004509 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004510 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004511 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004512 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004513 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004514 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004515 }
4516}
4517
Wanpeng Lia5758132017-05-11 02:58:55 -07004518#define XSAVE_MXCSR_OFFSET 24
4519
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004520static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4521 struct kvm_xsave *guest_xsave)
4522{
4523 u64 xstate_bv =
4524 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004525 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004526
Borislav Petkovd366bf72016-04-04 22:25:02 +02004527 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004528 /*
4529 * Here we allow setting states that are not present in
4530 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4531 * with old userspace.
4532 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004533 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004534 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004535 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004536 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004537 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4538 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004539 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004540 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004541 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004542 }
4543 return 0;
4544}
4545
4546static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4547 struct kvm_xcrs *guest_xcrs)
4548{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004549 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004550 guest_xcrs->nr_xcrs = 0;
4551 return;
4552 }
4553
4554 guest_xcrs->nr_xcrs = 1;
4555 guest_xcrs->flags = 0;
4556 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4557 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4558}
4559
4560static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4561 struct kvm_xcrs *guest_xcrs)
4562{
4563 int i, r = 0;
4564
Borislav Petkovd366bf72016-04-04 22:25:02 +02004565 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004566 return -EINVAL;
4567
4568 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4569 return -EINVAL;
4570
4571 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4572 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004573 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004574 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004575 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004576 break;
4577 }
4578 if (r)
4579 r = -EINVAL;
4580 return r;
4581}
4582
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004583/*
4584 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4585 * stopped by the hypervisor. This function will be called from the host only.
4586 * EINVAL is returned when the host attempts to set the flag for a guest that
4587 * does not support pv clocks.
4588 */
4589static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4590{
Andy Honig0b794592013-02-20 14:48:10 -08004591 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004592 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004593 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004594 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4595 return 0;
4596}
4597
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004598static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4599 struct kvm_enable_cap *cap)
4600{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004601 int r;
4602 uint16_t vmcs_version;
4603 void __user *user_ptr;
4604
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004605 if (cap->flags)
4606 return -EINVAL;
4607
4608 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004609 case KVM_CAP_HYPERV_SYNIC2:
4610 if (cap->args[0])
4611 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004612 fallthrough;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004613
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004614 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004615 if (!irqchip_in_kernel(vcpu->kvm))
4616 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004617 return kvm_hv_activate_synic(vcpu, cap->cap ==
4618 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004619 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Paolo Bonzini33b22172020-04-17 10:24:18 -04004620 if (!kvm_x86_ops.nested_ops->enable_evmcs)
Sean Christopherson51589172018-12-03 13:53:10 -08004621 return -ENOTTY;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004622 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004623 if (!r) {
4624 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4625 if (copy_to_user(user_ptr, &vmcs_version,
4626 sizeof(vmcs_version)))
4627 r = -EFAULT;
4628 }
4629 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004630 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004631 if (!kvm_x86_ops.enable_direct_tlbflush)
Tianyu Lan344c6c82019-08-22 22:30:20 +08004632 return -ENOTTY;
4633
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004634 return kvm_x86_ops.enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004635
Oliver Upton66570e92020-08-18 15:24:28 +00004636 case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
4637 vcpu->arch.pv_cpuid.enforce = cap->args[0];
Oliver Upton01b4f512020-10-27 16:10:42 -07004638 if (vcpu->arch.pv_cpuid.enforce)
4639 kvm_update_pv_runtime(vcpu);
Oliver Upton66570e92020-08-18 15:24:28 +00004640
4641 return 0;
4642
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004643 default:
4644 return -EINVAL;
4645 }
4646}
4647
Carsten Otte313a3dc2007-10-11 19:16:52 +02004648long kvm_arch_vcpu_ioctl(struct file *filp,
4649 unsigned int ioctl, unsigned long arg)
4650{
4651 struct kvm_vcpu *vcpu = filp->private_data;
4652 void __user *argp = (void __user *)arg;
4653 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004654 union {
4655 struct kvm_lapic_state *lapic;
4656 struct kvm_xsave *xsave;
4657 struct kvm_xcrs *xcrs;
4658 void *buffer;
4659 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004660
Christoffer Dall9b0624712017-12-04 21:35:36 +01004661 vcpu_load(vcpu);
4662
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004663 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004664 switch (ioctl) {
4665 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004666 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004667 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004668 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004669 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4670 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004671
Dave Hansenb772ff32008-08-11 10:01:47 -07004672 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004673 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004674 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004675 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004676 if (r)
4677 goto out;
4678 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004679 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004680 goto out;
4681 r = 0;
4682 break;
4683 }
4684 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004685 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004686 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004687 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004688 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004689 if (IS_ERR(u.lapic)) {
4690 r = PTR_ERR(u.lapic);
4691 goto out_nofree;
4692 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004693
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004694 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004695 break;
4696 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004697 case KVM_INTERRUPT: {
4698 struct kvm_interrupt irq;
4699
4700 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004701 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004702 goto out;
4703 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004704 break;
4705 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004706 case KVM_NMI: {
4707 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004708 break;
4709 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004710 case KVM_SMI: {
4711 r = kvm_vcpu_ioctl_smi(vcpu);
4712 break;
4713 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004714 case KVM_SET_CPUID: {
4715 struct kvm_cpuid __user *cpuid_arg = argp;
4716 struct kvm_cpuid cpuid;
4717
4718 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004719 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004720 goto out;
4721 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004722 break;
4723 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004724 case KVM_SET_CPUID2: {
4725 struct kvm_cpuid2 __user *cpuid_arg = argp;
4726 struct kvm_cpuid2 cpuid;
4727
4728 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004729 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004730 goto out;
4731 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004732 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004733 break;
4734 }
4735 case KVM_GET_CPUID2: {
4736 struct kvm_cpuid2 __user *cpuid_arg = argp;
4737 struct kvm_cpuid2 cpuid;
4738
4739 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004740 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004741 goto out;
4742 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004743 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004744 if (r)
4745 goto out;
4746 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004747 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004748 goto out;
4749 r = 0;
4750 break;
4751 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004752 case KVM_GET_MSRS: {
4753 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004754 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004755 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004756 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004757 }
4758 case KVM_SET_MSRS: {
4759 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004760 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004761 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004762 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004763 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004764 case KVM_TPR_ACCESS_REPORTING: {
4765 struct kvm_tpr_access_ctl tac;
4766
4767 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004768 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004769 goto out;
4770 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4771 if (r)
4772 goto out;
4773 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004774 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004775 goto out;
4776 r = 0;
4777 break;
4778 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004779 case KVM_SET_VAPIC_ADDR: {
4780 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004781 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004782
4783 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004784 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004785 goto out;
4786 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004787 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004788 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004789 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004790 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004791 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004792 break;
4793 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004794 case KVM_X86_SETUP_MCE: {
4795 u64 mcg_cap;
4796
4797 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004798 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004799 goto out;
4800 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4801 break;
4802 }
4803 case KVM_X86_SET_MCE: {
4804 struct kvm_x86_mce mce;
4805
4806 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004807 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004808 goto out;
4809 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4810 break;
4811 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004812 case KVM_GET_VCPU_EVENTS: {
4813 struct kvm_vcpu_events events;
4814
4815 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4816
4817 r = -EFAULT;
4818 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4819 break;
4820 r = 0;
4821 break;
4822 }
4823 case KVM_SET_VCPU_EVENTS: {
4824 struct kvm_vcpu_events events;
4825
4826 r = -EFAULT;
4827 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4828 break;
4829
4830 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4831 break;
4832 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004833 case KVM_GET_DEBUGREGS: {
4834 struct kvm_debugregs dbgregs;
4835
4836 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4837
4838 r = -EFAULT;
4839 if (copy_to_user(argp, &dbgregs,
4840 sizeof(struct kvm_debugregs)))
4841 break;
4842 r = 0;
4843 break;
4844 }
4845 case KVM_SET_DEBUGREGS: {
4846 struct kvm_debugregs dbgregs;
4847
4848 r = -EFAULT;
4849 if (copy_from_user(&dbgregs, argp,
4850 sizeof(struct kvm_debugregs)))
4851 break;
4852
4853 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4854 break;
4855 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004856 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004857 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004858 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004859 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004860 break;
4861
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004862 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004863
4864 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004865 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004866 break;
4867 r = 0;
4868 break;
4869 }
4870 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004871 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004872 if (IS_ERR(u.xsave)) {
4873 r = PTR_ERR(u.xsave);
4874 goto out_nofree;
4875 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004876
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004877 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004878 break;
4879 }
4880 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004881 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004882 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004883 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004884 break;
4885
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004886 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004887
4888 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004889 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004890 sizeof(struct kvm_xcrs)))
4891 break;
4892 r = 0;
4893 break;
4894 }
4895 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004896 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004897 if (IS_ERR(u.xcrs)) {
4898 r = PTR_ERR(u.xcrs);
4899 goto out_nofree;
4900 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004901
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004902 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004903 break;
4904 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004905 case KVM_SET_TSC_KHZ: {
4906 u32 user_tsc_khz;
4907
4908 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004909 user_tsc_khz = (u32)arg;
4910
Marcelo Tosatti26769f92020-06-16 08:47:41 -03004911 if (kvm_has_tsc_control &&
4912 user_tsc_khz >= kvm_max_guest_tsc_khz)
Joerg Roedel92a1f122011-03-25 09:44:51 +01004913 goto out;
4914
Zachary Amsdencc578282012-02-03 15:43:50 -02004915 if (user_tsc_khz == 0)
4916 user_tsc_khz = tsc_khz;
4917
Haozhong Zhang381d5852015-10-20 15:39:04 +08004918 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4919 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004920
Joerg Roedel92a1f122011-03-25 09:44:51 +01004921 goto out;
4922 }
4923 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004924 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004925 goto out;
4926 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004927 case KVM_KVMCLOCK_CTRL: {
4928 r = kvm_set_guest_paused(vcpu);
4929 goto out;
4930 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004931 case KVM_ENABLE_CAP: {
4932 struct kvm_enable_cap cap;
4933
4934 r = -EFAULT;
4935 if (copy_from_user(&cap, argp, sizeof(cap)))
4936 goto out;
4937 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4938 break;
4939 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004940 case KVM_GET_NESTED_STATE: {
4941 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4942 u32 user_data_size;
4943
4944 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004945 if (!kvm_x86_ops.nested_ops->get_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004946 break;
4947
4948 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004949 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004950 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004951 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004952
Paolo Bonzini33b22172020-04-17 10:24:18 -04004953 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state,
4954 user_data_size);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004955 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004956 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004957
4958 if (r > user_data_size) {
4959 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004960 r = -EFAULT;
4961 else
4962 r = -E2BIG;
4963 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004964 }
Liran Alon26b471c2018-09-16 14:28:20 +03004965
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004966 r = 0;
4967 break;
4968 }
4969 case KVM_SET_NESTED_STATE: {
4970 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4971 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004972 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004973
4974 r = -EINVAL;
Paolo Bonzini33b22172020-04-17 10:24:18 -04004975 if (!kvm_x86_ops.nested_ops->set_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004976 break;
4977
Liran Alon26b471c2018-09-16 14:28:20 +03004978 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004979 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004980 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004981
Liran Alon26b471c2018-09-16 14:28:20 +03004982 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004983 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004984 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004985
4986 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004987 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04004988 | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING
4989 | KVM_STATE_NESTED_GIF_SET))
Liran Alon26b471c2018-09-16 14:28:20 +03004990 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004991
4992 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004993 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4994 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004995 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004996
Sean Christophersonad5996d2019-11-22 08:58:18 -08004997 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini33b22172020-04-17 10:24:18 -04004998 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004999 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02005000 break;
5001 }
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02005002 case KVM_GET_SUPPORTED_HV_CPUID:
5003 r = kvm_ioctl_get_supported_hv_cpuid(vcpu, argp);
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01005004 break;
Carsten Otte313a3dc2007-10-11 19:16:52 +02005005 default:
5006 r = -EINVAL;
5007 }
5008out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03005009 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01005010out_nofree:
5011 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02005012 return r;
5013}
5014
Souptick Joarder1499fa82018-04-19 00:49:58 +05305015vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01005016{
5017 return VM_FAULT_SIGBUS;
5018}
5019
Carsten Otte1fe779f2007-10-29 16:08:35 +01005020static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
5021{
5022 int ret;
5023
5024 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08005025 return -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005026 ret = kvm_x86_ops.set_tss_addr(kvm, addr);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005027 return ret;
5028}
5029
Sheng Yangb927a3c2009-07-21 10:42:48 +08005030static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
5031 u64 ident_addr)
5032{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005033 return kvm_x86_ops.set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08005034}
5035
Carsten Otte1fe779f2007-10-29 16:08:35 +01005036static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005037 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01005038{
5039 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
5040 return -EINVAL;
5041
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005042 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005043
5044 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005045 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005046
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005047 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005048 return 0;
5049}
5050
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005051static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01005052{
Dave Hansen39de71e2010-08-19 18:11:14 -07005053 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005054}
5055
Carsten Otte1fe779f2007-10-29 16:08:35 +01005056static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
5057{
David Hildenbrand90bca052017-04-07 10:50:23 +02005058 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005059 int r;
5060
5061 r = 0;
5062 switch (chip->chip_id) {
5063 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02005064 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01005065 sizeof(struct kvm_pic_state));
5066 break;
5067 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02005068 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01005069 sizeof(struct kvm_pic_state));
5070 break;
5071 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02005072 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005073 break;
5074 default:
5075 r = -EINVAL;
5076 break;
5077 }
5078 return r;
5079}
5080
5081static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
5082{
David Hildenbrand90bca052017-04-07 10:50:23 +02005083 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005084 int r;
5085
5086 r = 0;
5087 switch (chip->chip_id) {
5088 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02005089 spin_lock(&pic->lock);
5090 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01005091 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02005092 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005093 break;
5094 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02005095 spin_lock(&pic->lock);
5096 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01005097 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02005098 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005099 break;
5100 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02005101 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005102 break;
5103 default:
5104 r = -EINVAL;
5105 break;
5106 }
David Hildenbrand90bca052017-04-07 10:50:23 +02005107 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005108 return r;
5109}
5110
Sheng Yange0f63cb2008-03-04 00:50:59 +08005111static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
5112{
Radim Krčmář34f39412016-03-02 22:56:50 +01005113 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
5114
5115 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
5116
5117 mutex_lock(&kps->lock);
5118 memcpy(ps, &kps->channels, sizeof(*ps));
5119 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305120 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08005121}
5122
5123static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
5124{
Andrew Honig01856042015-11-18 14:50:23 -08005125 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01005126 struct kvm_pit *pit = kvm->arch.vpit;
5127
5128 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01005129 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08005130 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01005131 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
5132 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305133 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04005134}
5135
5136static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
5137{
Beth Kone9f42752009-07-07 11:50:38 -04005138 mutex_lock(&kvm->arch.vpit->pit_state.lock);
5139 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
5140 sizeof(ps->channels));
5141 ps->flags = kvm->arch.vpit->pit_state.flags;
5142 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005143 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305144 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04005145}
5146
5147static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
5148{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305149 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08005150 int i;
Beth Kone9f42752009-07-07 11:50:38 -04005151 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01005152 struct kvm_pit *pit = kvm->arch.vpit;
5153
5154 mutex_lock(&pit->pit_state.lock);
5155 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04005156 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
5157 if (!prev_legacy && cur_legacy)
5158 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01005159 memcpy(&pit->pit_state.channels, &ps->channels,
5160 sizeof(pit->pit_state.channels));
5161 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08005162 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01005163 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01005164 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01005165 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05305166 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08005167}
5168
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005169static int kvm_vm_ioctl_reinject(struct kvm *kvm,
5170 struct kvm_reinject_control *control)
5171{
Radim Krčmář71474e22016-03-02 22:56:45 +01005172 struct kvm_pit *pit = kvm->arch.vpit;
5173
Radim Krčmář71474e22016-03-02 22:56:45 +01005174 /* pit->pit_state.lock was overloaded to prevent userspace from getting
5175 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
5176 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
5177 */
5178 mutex_lock(&pit->pit_state.lock);
5179 kvm_pit_set_reinject(pit, control->pit_reinject);
5180 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01005181
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005182 return 0;
5183}
5184
Sean Christopherson0dff0842020-02-18 13:07:29 -08005185void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08005186{
Kai Huang88178fd2015-01-28 10:54:27 +08005187 /*
5188 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
5189 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005190 if (kvm_x86_ops.flush_log_dirty)
5191 kvm_x86_ops.flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08005192}
5193
Yang Zhangaa2fbe62013-04-11 19:21:40 +08005194int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
5195 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04005196{
5197 if (!irqchip_in_kernel(kvm))
5198 return -ENXIO;
5199
5200 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08005201 irq_event->irq, irq_event->level,
5202 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04005203 return 0;
5204}
5205
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01005206int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
5207 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03005208{
5209 int r;
5210
5211 if (cap->flags)
5212 return -EINVAL;
5213
5214 switch (cap->cap) {
5215 case KVM_CAP_DISABLE_QUIRKS:
5216 kvm->arch.disabled_quirks = cap->args[0];
5217 r = 0;
5218 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07005219 case KVM_CAP_SPLIT_IRQCHIP: {
5220 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07005221 r = -EINVAL;
5222 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
5223 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07005224 r = -EEXIST;
5225 if (irqchip_in_kernel(kvm))
5226 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02005227 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07005228 goto split_irqchip_unlock;
5229 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02005230 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07005231 goto split_irqchip_unlock;
5232 /* Pairs with irqchip_in_kernel. */
5233 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005234 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07005235 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07005236 r = 0;
5237split_irqchip_unlock:
5238 mutex_unlock(&kvm->lock);
5239 break;
5240 }
Radim Krčmář371313132016-07-12 22:09:27 +02005241 case KVM_CAP_X2APIC_API:
5242 r = -EINVAL;
5243 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
5244 break;
5245
5246 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
5247 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02005248 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
5249 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02005250
5251 r = 0;
5252 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005253 case KVM_CAP_X86_DISABLE_EXITS:
5254 r = -EINVAL;
5255 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
5256 break;
5257
5258 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
5259 kvm_can_mwait_in_guest())
5260 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03005261 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07005262 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005263 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
5264 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08005265 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
5266 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005267 r = 0;
5268 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07005269 case KVM_CAP_MSR_PLATFORM_INFO:
5270 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
5271 r = 0;
5272 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07005273 case KVM_CAP_EXCEPTION_PAYLOAD:
5274 kvm->arch.exception_payload_enabled = cap->args[0];
5275 r = 0;
5276 break;
Alexander Graf1ae09952020-09-25 16:34:16 +02005277 case KVM_CAP_X86_USER_SPACE_MSR:
5278 kvm->arch.user_space_msr_mask = cap->args[0];
5279 r = 0;
5280 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03005281 default:
5282 r = -EINVAL;
5283 break;
5284 }
5285 return r;
5286}
5287
Alexander Graf1a1552542020-09-25 16:34:21 +02005288static void kvm_clear_msr_filter(struct kvm *kvm)
5289{
5290 u32 i;
5291 u32 count = kvm->arch.msr_filter.count;
5292 struct msr_bitmap_range ranges[16];
5293
5294 mutex_lock(&kvm->lock);
5295 kvm->arch.msr_filter.count = 0;
5296 memcpy(ranges, kvm->arch.msr_filter.ranges, count * sizeof(ranges[0]));
5297 mutex_unlock(&kvm->lock);
5298 synchronize_srcu(&kvm->srcu);
5299
5300 for (i = 0; i < count; i++)
5301 kfree(ranges[i].bitmap);
5302}
5303
5304static int kvm_add_msr_filter(struct kvm *kvm, struct kvm_msr_filter_range *user_range)
5305{
5306 struct msr_bitmap_range *ranges = kvm->arch.msr_filter.ranges;
5307 struct msr_bitmap_range range;
5308 unsigned long *bitmap = NULL;
5309 size_t bitmap_size;
5310 int r;
5311
5312 if (!user_range->nmsrs)
5313 return 0;
5314
5315 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long);
5316 if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE)
5317 return -EINVAL;
5318
5319 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size);
5320 if (IS_ERR(bitmap))
5321 return PTR_ERR(bitmap);
5322
5323 range = (struct msr_bitmap_range) {
5324 .flags = user_range->flags,
5325 .base = user_range->base,
5326 .nmsrs = user_range->nmsrs,
5327 .bitmap = bitmap,
5328 };
5329
5330 if (range.flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE)) {
5331 r = -EINVAL;
5332 goto err;
5333 }
5334
5335 if (!range.flags) {
5336 r = -EINVAL;
5337 goto err;
5338 }
5339
5340 /* Everything ok, add this range identifier to our global pool */
5341 ranges[kvm->arch.msr_filter.count] = range;
5342 /* Make sure we filled the array before we tell anyone to walk it */
5343 smp_wmb();
5344 kvm->arch.msr_filter.count++;
5345
5346 return 0;
5347err:
5348 kfree(bitmap);
5349 return r;
5350}
5351
5352static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp)
5353{
5354 struct kvm_msr_filter __user *user_msr_filter = argp;
5355 struct kvm_msr_filter filter;
5356 bool default_allow;
5357 int r = 0;
Paolo Bonzini043248b2020-10-20 10:57:01 -04005358 bool empty = true;
Alexander Graf1a1552542020-09-25 16:34:21 +02005359 u32 i;
5360
5361 if (copy_from_user(&filter, user_msr_filter, sizeof(filter)))
5362 return -EFAULT;
5363
Paolo Bonzini043248b2020-10-20 10:57:01 -04005364 for (i = 0; i < ARRAY_SIZE(filter.ranges); i++)
5365 empty &= !filter.ranges[i].nmsrs;
Alexander Graf1a1552542020-09-25 16:34:21 +02005366
5367 default_allow = !(filter.flags & KVM_MSR_FILTER_DEFAULT_DENY);
Paolo Bonzini043248b2020-10-20 10:57:01 -04005368 if (empty && !default_allow)
5369 return -EINVAL;
5370
5371 kvm_clear_msr_filter(kvm);
5372
Alexander Graf1a1552542020-09-25 16:34:21 +02005373 kvm->arch.msr_filter.default_allow = default_allow;
5374
5375 /*
5376 * Protect from concurrent calls to this function that could trigger
5377 * a TOCTOU violation on kvm->arch.msr_filter.count.
5378 */
5379 mutex_lock(&kvm->lock);
5380 for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) {
5381 r = kvm_add_msr_filter(kvm, &filter.ranges[i]);
5382 if (r)
5383 break;
5384 }
5385
5386 kvm_make_all_cpus_request(kvm, KVM_REQ_MSR_FILTER_CHANGED);
5387 mutex_unlock(&kvm->lock);
5388
5389 return r;
5390}
5391
Carsten Otte1fe779f2007-10-29 16:08:35 +01005392long kvm_arch_vm_ioctl(struct file *filp,
5393 unsigned int ioctl, unsigned long arg)
5394{
5395 struct kvm *kvm = filp->private_data;
5396 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03005397 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07005398 /*
5399 * This union makes it completely explicit to gcc-3.x
5400 * that these two variables' stack usage should be
5401 * combined, not added together.
5402 */
5403 union {
5404 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04005405 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005406 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07005407 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005408
5409 switch (ioctl) {
5410 case KVM_SET_TSS_ADDR:
5411 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005412 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005413 case KVM_SET_IDENTITY_MAP_ADDR: {
5414 u64 ident_addr;
5415
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005416 mutex_lock(&kvm->lock);
5417 r = -EINVAL;
5418 if (kvm->created_vcpus)
5419 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005420 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005421 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005422 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005423 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02005424set_identity_unlock:
5425 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08005426 break;
5427 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01005428 case KVM_SET_NR_MMU_PAGES:
5429 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005430 break;
5431 case KVM_GET_NR_MMU_PAGES:
5432 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
5433 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005434 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005435 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01005436
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005437 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01005438 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005439 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005440
Avi Kivity3e515702012-03-05 14:23:29 +02005441 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02005442 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02005443 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005444
5445 r = kvm_pic_init(kvm);
5446 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005447 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01005448
5449 r = kvm_ioapic_init(kvm);
5450 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01005451 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005452 goto create_irqchip_unlock;
5453 }
5454
Avi Kivity399ec802008-11-19 13:58:46 +02005455 r = kvm_setup_default_irq_routing(kvm);
5456 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08005457 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01005458 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005459 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02005460 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01005461 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02005462 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01005463 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005464 create_irqchip_unlock:
5465 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005466 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02005467 }
Sheng Yang78376992008-01-28 05:10:22 +08005468 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005469 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
5470 goto create_pit;
5471 case KVM_CREATE_PIT2:
5472 r = -EFAULT;
5473 if (copy_from_user(&u.pit_config, argp,
5474 sizeof(struct kvm_pit_config)))
5475 goto out;
5476 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005477 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02005478 r = -EEXIST;
5479 if (kvm->arch.vpit)
5480 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08005481 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02005482 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08005483 if (kvm->arch.vpit)
5484 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02005485 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005486 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08005487 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005488 case KVM_GET_IRQCHIP: {
5489 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005490 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005491
Sasha Levinff5c2c02011-12-04 19:36:29 +02005492 chip = memdup_user(argp, sizeof(*chip));
5493 if (IS_ERR(chip)) {
5494 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005495 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005496 }
5497
Carsten Otte1fe779f2007-10-29 16:08:35 +01005498 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005499 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005500 goto get_irqchip_out;
5501 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5502 if (r)
5503 goto get_irqchip_out;
5504 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005505 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005506 goto get_irqchip_out;
5507 r = 0;
5508 get_irqchip_out:
5509 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005510 break;
5511 }
5512 case KVM_SET_IRQCHIP: {
5513 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005514 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005515
Sasha Levinff5c2c02011-12-04 19:36:29 +02005516 chip = memdup_user(argp, sizeof(*chip));
5517 if (IS_ERR(chip)) {
5518 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005519 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005520 }
5521
Carsten Otte1fe779f2007-10-29 16:08:35 +01005522 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005523 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005524 goto set_irqchip_out;
5525 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005526 set_irqchip_out:
5527 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005528 break;
5529 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005530 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005531 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005532 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005533 goto out;
5534 r = -ENXIO;
5535 if (!kvm->arch.vpit)
5536 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005537 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005538 if (r)
5539 goto out;
5540 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005541 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005542 goto out;
5543 r = 0;
5544 break;
5545 }
5546 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005547 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005548 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005549 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005550 mutex_lock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005551 r = -ENXIO;
5552 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005553 goto set_pit_out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005554 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005555set_pit_out:
5556 mutex_unlock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005557 break;
5558 }
Beth Kone9f42752009-07-07 11:50:38 -04005559 case KVM_GET_PIT2: {
5560 r = -ENXIO;
5561 if (!kvm->arch.vpit)
5562 goto out;
5563 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5564 if (r)
5565 goto out;
5566 r = -EFAULT;
5567 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5568 goto out;
5569 r = 0;
5570 break;
5571 }
5572 case KVM_SET_PIT2: {
5573 r = -EFAULT;
5574 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5575 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005576 mutex_lock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005577 r = -ENXIO;
5578 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005579 goto set_pit2_out;
Beth Kone9f42752009-07-07 11:50:38 -04005580 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005581set_pit2_out:
5582 mutex_unlock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005583 break;
5584 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005585 case KVM_REINJECT_CONTROL: {
5586 struct kvm_reinject_control control;
5587 r = -EFAULT;
5588 if (copy_from_user(&control, argp, sizeof(control)))
5589 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005590 r = -ENXIO;
5591 if (!kvm->arch.vpit)
5592 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005593 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005594 break;
5595 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005596 case KVM_SET_BOOT_CPU_ID:
5597 r = 0;
5598 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005599 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005600 r = -EBUSY;
5601 else
5602 kvm->arch.bsp_vcpu_id = arg;
5603 mutex_unlock(&kvm->lock);
5604 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005605 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005606 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005607 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005608 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005609 goto out;
5610 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005611 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005612 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005613 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005614 r = 0;
5615 break;
5616 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005617 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005618 struct kvm_clock_data user_ns;
5619 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005620
5621 r = -EFAULT;
5622 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5623 goto out;
5624
5625 r = -EINVAL;
5626 if (user_ns.flags)
5627 goto out;
5628
5629 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005630 /*
5631 * TODO: userspace has to take care of races with VCPU_RUN, so
5632 * kvm_gen_update_masterclock() can be cut down to locked
5633 * pvclock_update_vm_gtod_copy().
5634 */
5635 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005636 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005637 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005638 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005639 break;
5640 }
5641 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005642 struct kvm_clock_data user_ns;
5643 u64 now_ns;
5644
Marcelo Tosattie891a322017-04-17 12:51:37 -03005645 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005646 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005647 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005648 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005649
5650 r = -EFAULT;
5651 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5652 goto out;
5653 r = 0;
5654 break;
5655 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005656 case KVM_MEMORY_ENCRYPT_OP: {
5657 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005658 if (kvm_x86_ops.mem_enc_op)
5659 r = kvm_x86_ops.mem_enc_op(kvm, argp);
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005660 break;
5661 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005662 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5663 struct kvm_enc_region region;
5664
5665 r = -EFAULT;
5666 if (copy_from_user(&region, argp, sizeof(region)))
5667 goto out;
5668
5669 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005670 if (kvm_x86_ops.mem_enc_reg_region)
5671 r = kvm_x86_ops.mem_enc_reg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005672 break;
5673 }
5674 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5675 struct kvm_enc_region region;
5676
5677 r = -EFAULT;
5678 if (copy_from_user(&region, argp, sizeof(region)))
5679 goto out;
5680
5681 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005682 if (kvm_x86_ops.mem_enc_unreg_region)
5683 r = kvm_x86_ops.mem_enc_unreg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005684 break;
5685 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005686 case KVM_HYPERV_EVENTFD: {
5687 struct kvm_hyperv_eventfd hvevfd;
5688
5689 r = -EFAULT;
5690 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5691 goto out;
5692 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5693 break;
5694 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005695 case KVM_SET_PMU_EVENT_FILTER:
5696 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5697 break;
Alexander Graf1a1552542020-09-25 16:34:21 +02005698 case KVM_X86_SET_MSR_FILTER:
5699 r = kvm_vm_ioctl_set_msr_filter(kvm, argp);
5700 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005701 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005702 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005703 }
5704out:
5705 return r;
5706}
5707
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005708static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005709{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005710 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005711 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005712 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005713
Jim Mattsone2ada662019-08-21 11:20:04 -07005714 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005715 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005716
5717 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005718
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005719 num_msrs_to_save = 0;
5720 num_emulated_msrs = 0;
5721 num_msr_based_features = 0;
5722
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005723 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5724 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005725 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005726
5727 /*
5728 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005729 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005730 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005731 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005732 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005733 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005734 continue;
5735 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005736 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005737 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005738 continue;
5739 break;
Maxim Levitskyf4cfcd22020-05-23 19:14:55 +03005740 case MSR_IA32_UMWAIT_CONTROL:
5741 if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG))
5742 continue;
5743 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005744 case MSR_IA32_RTIT_CTL:
5745 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005746 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005747 continue;
5748 break;
5749 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005750 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005751 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5752 continue;
5753 break;
5754 case MSR_IA32_RTIT_OUTPUT_BASE:
5755 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005756 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005757 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5758 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5759 continue;
5760 break;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08005761 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005762 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005763 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005764 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5765 continue;
5766 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005767 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005768 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005769 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5770 continue;
5771 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005772 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005773 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005774 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5775 continue;
Sean Christopherson7cb85fc2020-02-18 15:40:10 -08005776 break;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005777 default:
5778 break;
5779 }
5780
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005781 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005782 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005783
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005784 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005785 if (!kvm_x86_ops.has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005786 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005787
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005788 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005789 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005790
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005791 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005792 struct kvm_msr_entry msr;
5793
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005794 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005795 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005796 continue;
5797
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005798 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005799 }
Carsten Otte043405e2007-10-10 17:16:19 +02005800}
5801
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005802static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5803 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005804{
Avi Kivity70252a12010-01-19 12:51:22 +02005805 int handled = 0;
5806 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005807
Avi Kivity70252a12010-01-19 12:51:22 +02005808 do {
5809 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005810 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005811 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5812 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005813 break;
5814 handled += n;
5815 addr += n;
5816 len -= n;
5817 v += n;
5818 } while (len);
5819
5820 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005821}
5822
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005823static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005824{
Avi Kivity70252a12010-01-19 12:51:22 +02005825 int handled = 0;
5826 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005827
Avi Kivity70252a12010-01-19 12:51:22 +02005828 do {
5829 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005830 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005831 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5832 addr, n, v))
5833 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005834 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005835 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005836 handled += n;
5837 addr += n;
5838 len -= n;
5839 v += n;
5840 } while (len);
5841
5842 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005843}
5844
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005845static void kvm_set_segment(struct kvm_vcpu *vcpu,
5846 struct kvm_segment *var, int seg)
5847{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005848 kvm_x86_ops.set_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005849}
5850
5851void kvm_get_segment(struct kvm_vcpu *vcpu,
5852 struct kvm_segment *var, int seg)
5853{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005854 kvm_x86_ops.get_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005855}
5856
Paolo Bonzini54987b72014-09-02 13:23:06 +02005857gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5858 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005859{
5860 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005861
5862 BUG_ON(!mmu_is_nested(vcpu));
5863
5864 /* NPT walks are always user-walks */
5865 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005866 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005867
5868 return t_gpa;
5869}
5870
Avi Kivityab9ae312010-11-22 17:53:26 +02005871gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5872 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005873{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005874 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005875 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005876}
5877
Avi Kivityab9ae312010-11-22 17:53:26 +02005878 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5879 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005880{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005881 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005882 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005883 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005884}
5885
Avi Kivityab9ae312010-11-22 17:53:26 +02005886gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5887 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005888{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005889 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005890 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005891 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005892}
5893
5894/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005895gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5896 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005897{
Avi Kivityab9ae312010-11-22 17:53:26 +02005898 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005899}
5900
5901static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5902 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005903 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005904{
5905 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005906 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005907
5908 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005909 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005910 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005911 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005912 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005913 int ret;
5914
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005915 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005916 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005917 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5918 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005919 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005920 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005921 goto out;
5922 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005923
Izik Eidus77c20022008-12-29 01:42:19 +02005924 bytes -= toread;
5925 data += toread;
5926 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005927 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005928out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005929 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005930}
Izik Eidus77c20022008-12-29 01:42:19 +02005931
Gleb Natapov1871c602010-02-10 14:21:32 +02005932/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005933static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5934 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005935 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005936{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005937 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005938 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005939 unsigned offset;
5940 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005941
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005942 /* Inline kvm_read_guest_virt_helper for speed. */
5943 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5944 exception);
5945 if (unlikely(gpa == UNMAPPED_GVA))
5946 return X86EMUL_PROPAGATE_FAULT;
5947
5948 offset = addr & (PAGE_SIZE-1);
5949 if (WARN_ON(offset + bytes > PAGE_SIZE))
5950 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005951 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5952 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005953 if (unlikely(ret < 0))
5954 return X86EMUL_IO_NEEDED;
5955
5956 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005957}
5958
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005959int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005960 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005961 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005962{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005963 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005964
Paolo Bonzini353c0952019-01-29 18:41:16 +01005965 /*
5966 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5967 * is returned, but our callers are not ready for that and they blindly
5968 * call kvm_inject_page_fault. Ensure that they at least do not leak
5969 * uninitialized kernel stack memory into cr2 and error code.
5970 */
5971 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005972 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005973 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005974}
Nadav Har'El064aea72011-05-25 23:04:56 +03005975EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005976
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005977static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5978 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005979 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005980{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005981 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005982 u32 access = 0;
5983
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005984 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005985 access |= PFERR_USER_MASK;
5986
5987 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005988}
5989
Radim Krčmář7a036a62015-10-30 16:36:24 +01005990static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5991 unsigned long addr, void *val, unsigned int bytes)
5992{
5993 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5994 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5995
5996 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5997}
5998
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005999static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
6000 struct kvm_vcpu *vcpu, u32 access,
6001 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02006002{
6003 void *data = val;
6004 int r = X86EMUL_CONTINUE;
6005
6006 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02006007 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006008 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02006009 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02006010 unsigned offset = addr & (PAGE_SIZE-1);
6011 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
6012 int ret;
6013
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006014 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02006015 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006016 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02006017 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006018 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02006019 goto out;
6020 }
6021
6022 bytes -= towrite;
6023 data += towrite;
6024 addr += towrite;
6025 }
6026out:
6027 return r;
6028}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006029
6030static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006031 unsigned int bytes, struct x86_exception *exception,
6032 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006033{
6034 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006035 u32 access = PFERR_WRITE_MASK;
6036
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006037 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006038 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006039
6040 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006041 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006042}
6043
6044int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
6045 unsigned int bytes, struct x86_exception *exception)
6046{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006047 /* kvm_write_guest_virt_system can pull in tons of pages. */
6048 vcpu->arch.l1tf_flush_l1d = true;
6049
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006050 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
6051 PFERR_WRITE_MASK, exception);
6052}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006053EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02006054
Wanpeng Li082d06e2018-04-03 16:28:48 -07006055int handle_ud(struct kvm_vcpu *vcpu)
6056{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09006057 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006058 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006059 char sig[5]; /* ud2; .ascii "kvm" */
6060 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07006061
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07006062 if (unlikely(!kvm_x86_ops.can_emulate_instruction(vcpu, NULL, 0)))
6063 return 1;
6064
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006065 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02006066 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
6067 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09006068 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006069 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07006070 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07006071 }
6072
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006073 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07006074}
6075EXPORT_SYMBOL_GPL(handle_ud);
6076
Tom Lendacky0f89b202016-12-14 14:59:23 -05006077static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
6078 gpa_t gpa, bool write)
6079{
6080 /* For APIC access vmexit */
6081 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
6082 return 1;
6083
6084 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
6085 trace_vcpu_match_mmio(gva, gpa, write, true);
6086 return 1;
6087 }
6088
6089 return 0;
6090}
6091
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006092static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
6093 gpa_t *gpa, struct x86_exception *exception,
6094 bool write)
6095{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006096 u32 access = ((kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
Avi Kivity97d64b72012-09-12 14:52:00 +03006097 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006098
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08006099 /*
6100 * currently PKRU is only applied to ept enabled guest so
6101 * there is no pkey in EPT page table for L1 guest or EPT
6102 * shadow page table for L2 guest.
6103 */
Avi Kivity97d64b72012-09-12 14:52:00 +03006104 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08006105 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07006106 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08006107 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
6108 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08006109 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08006110 return 1;
6111 }
6112
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006113 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
6114
6115 if (*gpa == UNMAPPED_GVA)
6116 return -1;
6117
Tom Lendacky0f89b202016-12-14 14:59:23 -05006118 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006119}
6120
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006121int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02006122 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02006123{
6124 int ret;
6125
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006126 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02006127 if (ret < 0)
6128 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08006129 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02006130 return 1;
6131}
6132
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006133struct read_write_emulator_ops {
6134 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
6135 int bytes);
6136 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
6137 void *val, int bytes);
6138 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
6139 int bytes, void *val);
6140 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
6141 void *val, int bytes);
6142 bool write;
6143};
6144
6145static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
6146{
6147 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006148 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006149 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006150 vcpu->mmio_read_completed = 0;
6151 return 1;
6152 }
6153
6154 return 0;
6155}
6156
6157static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
6158 void *val, int bytes)
6159{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006160 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006161}
6162
6163static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
6164 void *val, int bytes)
6165{
6166 return emulator_write_phys(vcpu, gpa, val, bytes);
6167}
6168
6169static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
6170{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006171 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006172 return vcpu_mmio_write(vcpu, gpa, bytes, val);
6173}
6174
6175static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
6176 void *val, int bytes)
6177{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08006178 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006179 return X86EMUL_IO_NEEDED;
6180}
6181
6182static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
6183 void *val, int bytes)
6184{
Avi Kivityf78146b2012-04-18 19:22:47 +03006185 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
6186
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006187 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006188 return X86EMUL_CONTINUE;
6189}
6190
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006191static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006192 .read_write_prepare = read_prepare,
6193 .read_write_emulate = read_emulate,
6194 .read_write_mmio = vcpu_mmio_read,
6195 .read_write_exit_mmio = read_exit_mmio,
6196};
6197
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006198static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08006199 .read_write_emulate = write_emulate,
6200 .read_write_mmio = write_mmio,
6201 .read_write_exit_mmio = write_exit_mmio,
6202 .write = true,
6203};
6204
Xiao Guangrong22388a32011-07-13 14:32:31 +08006205static int emulator_read_write_onepage(unsigned long addr, void *val,
6206 unsigned int bytes,
6207 struct x86_exception *exception,
6208 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006209 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006210{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08006211 gpa_t gpa;
6212 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08006213 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03006214 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006215 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05006216
6217 /*
6218 * If the exit was due to a NPF we may already have a GPA.
6219 * If the GPA is present, use it to avoid the GVA to GPA table walk.
6220 * Note, this cannot be used on string operations since string
6221 * operation using rep will only have the initial GPA from the NPF
6222 * occurred.
6223 */
Sean Christopherson744e6992020-02-18 15:03:09 -08006224 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
6225 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
6226 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02006227 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
6228 } else {
6229 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
6230 if (ret < 0)
6231 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05006232 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08006233
Brijesh Singh618232e2017-08-17 18:36:57 +02006234 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01006235 return X86EMUL_CONTINUE;
6236
Carsten Ottebbd9b642007-10-30 18:44:21 +01006237 /*
6238 * Is this MMIO handled locally?
6239 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08006240 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02006241 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006242 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006243
Avi Kivity70252a12010-01-19 12:51:22 +02006244 gpa += handled;
6245 bytes -= handled;
6246 val += handled;
6247
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006248 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
6249 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
6250 frag->gpa = gpa;
6251 frag->data = val;
6252 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03006253 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006254}
6255
Xiubo Li52eb5a62015-03-13 17:39:45 +08006256static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
6257 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08006258 void *val, unsigned int bytes,
6259 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02006260 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006261{
Avi Kivity0f65dd72011-04-20 13:37:53 +03006262 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03006263 gpa_t gpa;
6264 int rc;
6265
6266 if (ops->read_write_prepare &&
6267 ops->read_write_prepare(vcpu, val, bytes))
6268 return X86EMUL_CONTINUE;
6269
6270 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006271
Carsten Ottebbd9b642007-10-30 18:44:21 +01006272 /* Crossing a page boundary? */
6273 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03006274 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006275
6276 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08006277 rc = emulator_read_write_onepage(addr, val, now, exception,
6278 vcpu, ops);
6279
Carsten Ottebbd9b642007-10-30 18:44:21 +01006280 if (rc != X86EMUL_CONTINUE)
6281 return rc;
6282 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02006283 if (ctxt->mode != X86EMUL_MODE_PROT64)
6284 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006285 val += now;
6286 bytes -= now;
6287 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08006288
Avi Kivityf78146b2012-04-18 19:22:47 +03006289 rc = emulator_read_write_onepage(addr, val, bytes, exception,
6290 vcpu, ops);
6291 if (rc != X86EMUL_CONTINUE)
6292 return rc;
6293
6294 if (!vcpu->mmio_nr_fragments)
6295 return rc;
6296
6297 gpa = vcpu->mmio_fragments[0].gpa;
6298
6299 vcpu->mmio_needed = 1;
6300 vcpu->mmio_cur_fragment = 0;
6301
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006302 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03006303 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
6304 vcpu->run->exit_reason = KVM_EXIT_MMIO;
6305 vcpu->run->mmio.phys_addr = gpa;
6306
6307 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08006308}
6309
6310static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
6311 unsigned long addr,
6312 void *val,
6313 unsigned int bytes,
6314 struct x86_exception *exception)
6315{
6316 return emulator_read_write(ctxt, addr, val, bytes,
6317 exception, &read_emultor);
6318}
6319
Xiubo Li52eb5a62015-03-13 17:39:45 +08006320static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08006321 unsigned long addr,
6322 const void *val,
6323 unsigned int bytes,
6324 struct x86_exception *exception)
6325{
6326 return emulator_read_write(ctxt, addr, (void *)val, bytes,
6327 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006328}
Carsten Ottebbd9b642007-10-30 18:44:21 +01006329
Avi Kivitydaea3e72010-03-15 13:59:54 +02006330#define CMPXCHG_TYPE(t, ptr, old, new) \
6331 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
6332
6333#ifdef CONFIG_X86_64
6334# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
6335#else
6336# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01006337 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02006338#endif
6339
Avi Kivity0f65dd72011-04-20 13:37:53 +03006340static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
6341 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006342 const void *old,
6343 const void *new,
6344 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03006345 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006346{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006347 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03006348 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02006349 u64 page_line_mask;
Avi Kivitydaea3e72010-03-15 13:59:54 +02006350 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02006351 char *kaddr;
6352 bool exchanged;
6353
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006354 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02006355 if (bytes > 8 || (bytes & (bytes - 1)))
6356 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006357
Avi Kivitydaea3e72010-03-15 13:59:54 +02006358 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05006359
Avi Kivitydaea3e72010-03-15 13:59:54 +02006360 if (gpa == UNMAPPED_GVA ||
6361 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
6362 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006363
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02006364 /*
6365 * Emulate the atomic as a straight write to avoid #AC if SLD is
6366 * enabled in the host and the access splits a cache line.
6367 */
6368 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
6369 page_line_mask = ~(cache_line_size() - 1);
6370 else
6371 page_line_mask = PAGE_MASK;
6372
6373 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask))
Avi Kivitydaea3e72010-03-15 13:59:54 +02006374 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006375
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006376 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08006377 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02006378
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006379 kaddr = map.hva + offset_in_page(gpa);
6380
Avi Kivitydaea3e72010-03-15 13:59:54 +02006381 switch (bytes) {
6382 case 1:
6383 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
6384 break;
6385 case 2:
6386 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
6387 break;
6388 case 4:
6389 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
6390 break;
6391 case 8:
6392 exchanged = CMPXCHG64(kaddr, old, new);
6393 break;
6394 default:
6395 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006396 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01006397
6398 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02006399
6400 if (!exchanged)
6401 return X86EMUL_CMPXCHG_FAILED;
6402
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08006403 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03006404
6405 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02006406
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006407emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02006408 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05006409
Avi Kivity0f65dd72011-04-20 13:37:53 +03006410 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006411}
6412
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006413static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
6414{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006415 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006416
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006417 for (i = 0; i < vcpu->arch.pio.count; i++) {
6418 if (vcpu->arch.pio.in)
6419 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
6420 vcpu->arch.pio.size, pd);
6421 else
6422 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
6423 vcpu->arch.pio.port, vcpu->arch.pio.size,
6424 pd);
6425 if (r)
6426 break;
6427 pd += vcpu->arch.pio.size;
6428 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006429 return r;
6430}
6431
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006432static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
6433 unsigned short port, void *val,
6434 unsigned int count, bool in)
6435{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006436 vcpu->arch.pio.port = port;
6437 vcpu->arch.pio.in = in;
6438 vcpu->arch.pio.count = count;
6439 vcpu->arch.pio.size = size;
6440
6441 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
6442 vcpu->arch.pio.count = 0;
6443 return 1;
6444 }
6445
6446 vcpu->run->exit_reason = KVM_EXIT_IO;
6447 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
6448 vcpu->run->io.size = size;
6449 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
6450 vcpu->run->io.count = count;
6451 vcpu->run->io.port = port;
6452
6453 return 0;
6454}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006455
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006456static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6457 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006458{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006459 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03006460
Gleb Natapov79729952010-03-18 15:20:24 +02006461 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006462 goto data_avail;
6463
Wanpeng Licbfc6c92017-05-19 02:46:56 -07006464 memset(vcpu->arch.pio_data, 0, size * count);
6465
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006466 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
6467 if (ret) {
6468data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006469 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006470 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02006471 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006472 return 1;
6473 }
6474
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006475 return 0;
6476}
6477
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006478static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
6479 int size, unsigned short port, void *val,
6480 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006481{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006482 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006483
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006484}
6485
6486static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
6487 unsigned short port, const void *val,
6488 unsigned int count)
6489{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006490 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02006491 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08006492 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006493}
6494
Carsten Ottebbd9b642007-10-30 18:44:21 +01006495static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
6496 int size, unsigned short port,
6497 const void *val, unsigned int count)
6498{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006499 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006500}
6501
6502static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
6503{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006504 return kvm_x86_ops.get_segment_base(vcpu, seg);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006505}
6506
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006507static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006508{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006509 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006510}
6511
Jiang Biaoae6a2372016-11-07 08:54:51 +08006512static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006513{
6514 if (!need_emulate_wbinvd(vcpu))
6515 return X86EMUL_CONTINUE;
6516
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006517 if (kvm_x86_ops.has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01006518 int cpu = get_cpu();
6519
6520 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006521 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
6522 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006523 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006524 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006525 } else
6526 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006527 return X86EMUL_CONTINUE;
6528}
Joel Schopp5cb56052015-03-02 13:43:31 -06006529
6530int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6531{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006532 kvm_emulate_wbinvd_noskip(vcpu);
6533 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006534}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006535EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6536
Joel Schopp5cb56052015-03-02 13:43:31 -06006537
6538
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006539static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6540{
Joel Schopp5cb56052015-03-02 13:43:31 -06006541 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006542}
6543
Xiubo Li52eb5a62015-03-13 17:39:45 +08006544static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6545 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006546{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006547 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006548}
6549
Xiubo Li52eb5a62015-03-13 17:39:45 +08006550static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6551 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006552{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006553
Avi Kivity717746e2011-04-20 13:37:53 +03006554 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006555}
6556
Gleb Natapov52a46612010-03-18 15:20:03 +02006557static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6558{
6559 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6560}
6561
Avi Kivity717746e2011-04-20 13:37:53 +03006562static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006563{
Avi Kivity717746e2011-04-20 13:37:53 +03006564 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006565 unsigned long value;
6566
6567 switch (cr) {
6568 case 0:
6569 value = kvm_read_cr0(vcpu);
6570 break;
6571 case 2:
6572 value = vcpu->arch.cr2;
6573 break;
6574 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006575 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006576 break;
6577 case 4:
6578 value = kvm_read_cr4(vcpu);
6579 break;
6580 case 8:
6581 value = kvm_get_cr8(vcpu);
6582 break;
6583 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006584 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006585 return 0;
6586 }
6587
6588 return value;
6589}
6590
Avi Kivity717746e2011-04-20 13:37:53 +03006591static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006592{
Avi Kivity717746e2011-04-20 13:37:53 +03006593 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006594 int res = 0;
6595
Gleb Natapov52a46612010-03-18 15:20:03 +02006596 switch (cr) {
6597 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006598 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006599 break;
6600 case 2:
6601 vcpu->arch.cr2 = val;
6602 break;
6603 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006604 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006605 break;
6606 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006607 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006608 break;
6609 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006610 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006611 break;
6612 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006613 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006614 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006615 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006616
6617 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006618}
6619
Avi Kivity717746e2011-04-20 13:37:53 +03006620static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006621{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006622 return kvm_x86_ops.get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006623}
6624
Avi Kivity4bff1e862011-04-20 13:37:53 +03006625static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006626{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006627 kvm_x86_ops.get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006628}
6629
Avi Kivity4bff1e862011-04-20 13:37:53 +03006630static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006631{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006632 kvm_x86_ops.get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006633}
6634
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006635static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6636{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006637 kvm_x86_ops.set_gdt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006638}
6639
6640static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6641{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006642 kvm_x86_ops.set_idt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006643}
6644
Avi Kivity4bff1e862011-04-20 13:37:53 +03006645static unsigned long emulator_get_cached_segment_base(
6646 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006647{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006648 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006649}
6650
Avi Kivity1aa36612011-04-27 13:20:30 +03006651static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6652 struct desc_struct *desc, u32 *base3,
6653 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006654{
6655 struct kvm_segment var;
6656
Avi Kivity4bff1e862011-04-20 13:37:53 +03006657 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006658 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006659
Gleb Natapov378a8b02013-01-21 15:36:48 +02006660 if (var.unusable) {
6661 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006662 if (base3)
6663 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006664 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006665 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006666
6667 if (var.g)
6668 var.limit >>= 12;
6669 set_desc_limit(desc, var.limit);
6670 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006671#ifdef CONFIG_X86_64
6672 if (base3)
6673 *base3 = var.base >> 32;
6674#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006675 desc->type = var.type;
6676 desc->s = var.s;
6677 desc->dpl = var.dpl;
6678 desc->p = var.present;
6679 desc->avl = var.avl;
6680 desc->l = var.l;
6681 desc->d = var.db;
6682 desc->g = var.g;
6683
6684 return true;
6685}
6686
Avi Kivity1aa36612011-04-27 13:20:30 +03006687static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6688 struct desc_struct *desc, u32 base3,
6689 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006690{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006691 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006692 struct kvm_segment var;
6693
Avi Kivity1aa36612011-04-27 13:20:30 +03006694 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006695 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006696#ifdef CONFIG_X86_64
6697 var.base |= ((u64)base3) << 32;
6698#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006699 var.limit = get_desc_limit(desc);
6700 if (desc->g)
6701 var.limit = (var.limit << 12) | 0xfff;
6702 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006703 var.dpl = desc->dpl;
6704 var.db = desc->d;
6705 var.s = desc->s;
6706 var.l = desc->l;
6707 var.g = desc->g;
6708 var.avl = desc->avl;
6709 var.present = desc->p;
6710 var.unusable = !var.present;
6711 var.padding = 0;
6712
6713 kvm_set_segment(vcpu, &var, seg);
6714 return;
6715}
6716
Avi Kivity717746e2011-04-20 13:37:53 +03006717static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6718 u32 msr_index, u64 *pdata)
6719{
Alexander Graf1ae09952020-09-25 16:34:16 +02006720 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6721 int r;
6722
6723 r = kvm_get_msr(vcpu, msr_index, pdata);
6724
6725 if (r && kvm_get_msr_user_space(vcpu, msr_index, r)) {
6726 /* Bounce to user space */
6727 return X86EMUL_IO_NEEDED;
6728 }
6729
6730 return r;
Avi Kivity717746e2011-04-20 13:37:53 +03006731}
6732
6733static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6734 u32 msr_index, u64 data)
6735{
Alexander Graf1ae09952020-09-25 16:34:16 +02006736 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6737 int r;
6738
6739 r = kvm_set_msr(vcpu, msr_index, data);
6740
6741 if (r && kvm_set_msr_user_space(vcpu, msr_index, data, r)) {
6742 /* Bounce to user space */
6743 return X86EMUL_IO_NEEDED;
6744 }
6745
6746 return r;
Avi Kivity717746e2011-04-20 13:37:53 +03006747}
6748
Paolo Bonzini64d60672015-05-07 11:36:11 +02006749static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6750{
6751 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6752
6753 return vcpu->arch.smbase;
6754}
6755
6756static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6757{
6758 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6759
6760 vcpu->arch.smbase = smbase;
6761}
6762
Nadav Amit67f4d422014-06-02 18:34:09 +03006763static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6764 u32 pmc)
6765{
Like Xu98ff80f2019-10-27 18:52:40 +08006766 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006767}
6768
Avi Kivity222d21a2011-11-10 14:57:30 +02006769static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6770 u32 pmc, u64 *pdata)
6771{
Wei Huangc6702c92015-06-19 13:44:45 +02006772 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006773}
6774
Avi Kivity6c3287f2011-04-20 15:43:05 +03006775static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6776{
6777 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6778}
6779
Avi Kivity29535382011-04-20 13:37:53 +03006780static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006781 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006782 enum x86_intercept_stage stage)
6783{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006784 return kvm_x86_ops.check_intercept(emul_to_vcpu(ctxt), info, stage,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006785 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006786}
6787
Yu Zhange911eb32017-08-24 20:27:52 +08006788static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08006789 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
6790 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006791{
Sean Christophersonf91af512020-03-04 17:34:37 -08006792 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006793}
6794
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006795static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6796{
6797 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6798}
6799
6800static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6801{
6802 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6803}
6804
6805static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6806{
6807 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6808}
6809
Avi Kivitydd856ef2012-08-27 23:46:17 +03006810static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6811{
6812 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6813}
6814
6815static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6816{
6817 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6818}
6819
Nadav Amit801806d2015-01-26 09:32:23 +02006820static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6821{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006822 kvm_x86_ops.set_nmi_mask(emul_to_vcpu(ctxt), masked);
Nadav Amit801806d2015-01-26 09:32:23 +02006823}
6824
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006825static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6826{
6827 return emul_to_vcpu(ctxt)->arch.hflags;
6828}
6829
6830static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6831{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006832 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006833}
6834
Sean Christophersoned193212019-04-02 08:03:09 -07006835static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6836 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006837{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006838 return kvm_x86_ops.pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006839}
6840
Sean Christophersonc5833c72019-04-02 08:03:10 -07006841static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6842{
6843 kvm_smm_changed(emul_to_vcpu(ctxt));
6844}
6845
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006846static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6847{
6848 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6849}
6850
Mathias Krause0225fb52012-08-30 01:30:16 +02006851static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006852 .read_gpr = emulator_read_gpr,
6853 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006854 .read_std = emulator_read_std,
6855 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006856 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006857 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006858 .read_emulated = emulator_read_emulated,
6859 .write_emulated = emulator_write_emulated,
6860 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006861 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006862 .pio_in_emulated = emulator_pio_in_emulated,
6863 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006864 .get_segment = emulator_get_segment,
6865 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006866 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006867 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006868 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006869 .set_gdt = emulator_set_gdt,
6870 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006871 .get_cr = emulator_get_cr,
6872 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006873 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006874 .get_dr = emulator_get_dr,
6875 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006876 .get_smbase = emulator_get_smbase,
6877 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006878 .set_msr = emulator_set_msr,
6879 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006880 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006881 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006882 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006883 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006884 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006885 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006886 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006887 .guest_has_long_mode = emulator_guest_has_long_mode,
6888 .guest_has_movbe = emulator_guest_has_movbe,
6889 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006890 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006891 .get_hflags = emulator_get_hflags,
6892 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006893 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006894 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006895 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006896};
6897
Gleb Natapov95cb2292010-04-28 19:15:43 +03006898static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6899{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006900 u32 int_shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006901 /*
6902 * an sti; sti; sequence only disable interrupts for the first
6903 * instruction. So, if the last instruction, be it emulated or
6904 * not, left the system with the INT_STI flag enabled, it
6905 * means that the last instruction is an sti. We should not
6906 * leave the flag on in this case. The same goes for mov ss
6907 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006908 if (int_shadow & mask)
6909 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006910 if (unlikely(int_shadow || mask)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006911 kvm_x86_ops.set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006912 if (!mask)
6913 kvm_make_request(KVM_REQ_EVENT, vcpu);
6914 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006915}
6916
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006917static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006918{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006919 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006920 if (ctxt->exception.vector == PF_VECTOR)
Sean Christopherson53b3d8e2020-03-20 14:28:01 -07006921 return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006922
6923 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006924 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6925 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006926 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006927 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006928 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006929}
6930
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006931static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6932{
6933 struct x86_emulate_ctxt *ctxt;
6934
6935 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6936 if (!ctxt) {
6937 pr_err("kvm: failed to allocate vcpu's emulator\n");
6938 return NULL;
6939 }
6940
6941 ctxt->vcpu = vcpu;
6942 ctxt->ops = &emulate_ops;
6943 vcpu->arch.emulate_ctxt = ctxt;
6944
6945 return ctxt;
6946}
6947
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006948static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6949{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006950 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006951 int cs_db, cs_l;
6952
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006953 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006954
Sean Christopherson744e6992020-02-18 15:03:09 -08006955 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006956 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006957 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6958
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006959 ctxt->eip = kvm_rip_read(vcpu);
6960 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6961 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006962 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006963 cs_db ? X86EMUL_MODE_PROT32 :
6964 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006965 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006966 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6967 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006968
Avi Kivitydd856ef2012-08-27 23:46:17 +03006969 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006970 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006971}
6972
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006973void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006974{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006975 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006976 int ret;
6977
6978 init_emulate_ctxt(vcpu);
6979
Avi Kivity9dac77f2011-06-01 15:34:25 +03006980 ctxt->op_bytes = 2;
6981 ctxt->ad_bytes = 2;
6982 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006983 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006984
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006985 if (ret != X86EMUL_CONTINUE) {
6986 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6987 } else {
6988 ctxt->eip = ctxt->_eip;
6989 kvm_rip_write(vcpu, ctxt->eip);
6990 kvm_set_rflags(vcpu, ctxt->eflags);
6991 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006992}
6993EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6994
Liran Alone2366172018-03-12 13:12:49 +02006995static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006996{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006997 ++vcpu->stat.insn_emulation_fail;
6998 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006999
Sean Christopherson42cbf062019-08-27 14:40:31 -07007000 if (emulation_type & EMULTYPE_VMWARE_GP) {
7001 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007002 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07007003 }
Liran Alone2366172018-03-12 13:12:49 +02007004
Sean Christopherson738fece2019-08-27 14:40:34 -07007005 if (emulation_type & EMULTYPE_SKIP) {
7006 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7007 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7008 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007009 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07007010 }
7011
Sean Christopherson22da61c2019-08-27 14:40:28 -07007012 kvm_queue_exception(vcpu, UD_VECTOR);
7013
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007014 if (!is_guest_mode(vcpu) && kvm_x86_ops.get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01007015 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7016 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7017 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007018 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01007019 }
Liran Alone2366172018-03-12 13:12:49 +02007020
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007021 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03007022}
7023
Sean Christopherson736c2912019-12-06 15:57:14 -08007024static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03007025 bool write_fault_to_shadow_pgtable,
7026 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03007027{
Sean Christopherson736c2912019-12-06 15:57:14 -08007028 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08007029 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03007030
Sean Christopherson92daa482020-02-18 15:03:08 -08007031 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03007032 return false;
7033
Sean Christopherson92daa482020-02-18 15:03:08 -08007034 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
7035 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07007036 return false;
7037
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007038 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007039 /*
7040 * Write permission should be allowed since only
7041 * write access need to be emulated.
7042 */
Sean Christopherson736c2912019-12-06 15:57:14 -08007043 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03007044
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007045 /*
7046 * If the mapping is invalid in guest, let cpu retry
7047 * it to generate fault.
7048 */
7049 if (gpa == UNMAPPED_GVA)
7050 return true;
7051 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03007052
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08007053 /*
7054 * Do not retry the unhandleable instruction if it faults on the
7055 * readonly host memory, otherwise it will goto a infinite loop:
7056 * retry instruction -> write #PF -> emulation fail -> retry
7057 * instruction -> ...
7058 */
7059 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007060
7061 /*
7062 * If the instruction failed on the error pfn, it can not be fixed,
7063 * report the error to userspace.
7064 */
7065 if (is_error_noslot_pfn(pfn))
7066 return false;
7067
7068 kvm_release_pfn_clean(pfn);
7069
7070 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007071 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007072 unsigned int indirect_shadow_pages;
7073
7074 spin_lock(&vcpu->kvm->mmu_lock);
7075 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
7076 spin_unlock(&vcpu->kvm->mmu_lock);
7077
7078 if (indirect_shadow_pages)
7079 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
7080
Gleb Natapova6f177e2010-07-08 12:41:12 +03007081 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08007082 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03007083
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08007084 /*
7085 * if emulation was due to access to shadowed page table
7086 * and it failed try to unshadow page and re-enter the
7087 * guest to let CPU execute the instruction.
7088 */
7089 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007090
7091 /*
7092 * If the access faults on its page table, it can not
7093 * be fixed by unprotecting shadow page and it should
7094 * be reported to userspace.
7095 */
7096 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03007097}
7098
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007099static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08007100 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007101{
7102 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08007103 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007104
7105 last_retry_eip = vcpu->arch.last_retry_eip;
7106 last_retry_addr = vcpu->arch.last_retry_addr;
7107
7108 /*
7109 * If the emulation is caused by #PF and it is non-page_table
7110 * writing instruction, it means the VM-EXIT is caused by shadow
7111 * page protected, we can zap the shadow page and retry this
7112 * instruction directly.
7113 *
7114 * Note: if the guest uses a non-page-table modifying instruction
7115 * on the PDE that points to the instruction, then we will unmap
7116 * the instruction and go to an infinite loop. So, we cache the
7117 * last retried eip and the last fault address, if we meet the eip
7118 * and the address again, we can break out of the potential infinite
7119 * loop.
7120 */
7121 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
7122
Sean Christopherson92daa482020-02-18 15:03:08 -08007123 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007124 return false;
7125
Sean Christopherson92daa482020-02-18 15:03:08 -08007126 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
7127 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07007128 return false;
7129
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007130 if (x86_page_table_writing_insn(ctxt))
7131 return false;
7132
Sean Christopherson736c2912019-12-06 15:57:14 -08007133 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007134 return false;
7135
7136 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08007137 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007138
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007139 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08007140 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007141
Xiao Guangrong22368022013-01-13 23:44:12 +08007142 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007143
7144 return true;
7145}
7146
Gleb Natapov716d51a2012-09-03 15:24:26 +03007147static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
7148static int complete_emulated_pio(struct kvm_vcpu *vcpu);
7149
Paolo Bonzini64d60672015-05-07 11:36:11 +02007150static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02007151{
Paolo Bonzini64d60672015-05-07 11:36:11 +02007152 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007153 /* This is a good place to trace that we are exiting SMM. */
7154 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
7155
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007156 /* Process a latched INIT or SMI, if any. */
7157 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007158 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02007159
7160 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007161}
7162
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007163static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
7164 unsigned long *db)
7165{
7166 u32 dr6 = 0;
7167 int i;
7168 u32 enable, rwlen;
7169
7170 enable = dr7;
7171 rwlen = dr7 >> 16;
7172 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
7173 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
7174 dr6 |= (1 << i);
7175 return dr6;
7176}
7177
Sean Christopherson120c2c42019-08-27 14:40:29 -07007178static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007179{
7180 struct kvm_run *kvm_run = vcpu->run;
7181
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007182 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
7183 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
Peter Xud5d260c2020-05-05 16:49:59 -04007184 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007185 kvm_run->debug.arch.exception = DB_VECTOR;
7186 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007187 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007188 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07007189 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007190 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02007191}
7192
Kyle Huey6affcbe2016-11-29 12:40:40 -08007193int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
7194{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007195 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02007196 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007197
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007198 r = kvm_x86_ops.skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007199 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02007200 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02007201
7202 /*
7203 * rflags is the old, "raw" value of the flags. The new value has
7204 * not been saved yet.
7205 *
7206 * This is correct even for TF set by the guest, because "the
7207 * processor will not generate this exception after the instruction
7208 * that sets the TF flag".
7209 */
7210 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07007211 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007212 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007213}
7214EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
7215
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007216static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
7217{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007218 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
7219 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02007220 struct kvm_run *kvm_run = vcpu->run;
7221 unsigned long eip = kvm_get_linear_rip(vcpu);
7222 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007223 vcpu->arch.guest_debug_dr7,
7224 vcpu->arch.eff_db);
7225
7226 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03007227 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02007228 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007229 kvm_run->debug.arch.exception = DB_VECTOR;
7230 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007231 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007232 return true;
7233 }
7234 }
7235
Nadav Amit4161a562014-07-17 01:19:31 +03007236 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
7237 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02007238 unsigned long eip = kvm_get_linear_rip(vcpu);
7239 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007240 vcpu->arch.dr7,
7241 vcpu->arch.db);
7242
7243 if (dr6 != 0) {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04007244 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007245 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007246 return true;
7247 }
7248 }
7249
7250 return false;
7251}
7252
Liran Alon04789b62018-03-12 13:12:50 +02007253static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
7254{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02007255 switch (ctxt->opcode_len) {
7256 case 1:
7257 switch (ctxt->b) {
7258 case 0xe4: /* IN */
7259 case 0xe5:
7260 case 0xec:
7261 case 0xed:
7262 case 0xe6: /* OUT */
7263 case 0xe7:
7264 case 0xee:
7265 case 0xef:
7266 case 0x6c: /* INS */
7267 case 0x6d:
7268 case 0x6e: /* OUTS */
7269 case 0x6f:
7270 return true;
7271 }
7272 break;
7273 case 2:
7274 switch (ctxt->b) {
7275 case 0x33: /* RDPMC */
7276 return true;
7277 }
7278 break;
Liran Alon04789b62018-03-12 13:12:50 +02007279 }
7280
7281 return false;
7282}
7283
Sean Christopherson736c2912019-12-06 15:57:14 -08007284int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
7285 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01007286{
Gleb Natapov95cb2292010-04-28 19:15:43 +03007287 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007288 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007289 bool writeback = true;
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007290 bool write_fault_to_spt;
7291
7292 if (unlikely(!kvm_x86_ops.can_emulate_instruction(vcpu, insn, insn_len)))
7293 return 1;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007294
Paolo Bonzinic595cee2018-07-02 13:07:14 +02007295 vcpu->arch.l1tf_flush_l1d = true;
7296
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007297 /*
7298 * Clear write_fault_to_shadow_pgtable here to ensure it is
7299 * never reused.
7300 */
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007301 write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08007302 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03007303 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03007304
Sheng Yang571008d2008-01-02 14:49:22 +08007305 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007306 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007307
7308 /*
7309 * We will reenter on the same instruction since
7310 * we do not set complete_userspace_io. This does not
7311 * handle watchpoints yet, those would be handled in
7312 * the emulate_ops.
7313 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007314 if (!(emulation_type & EMULTYPE_SKIP) &&
7315 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02007316 return r;
7317
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007318 ctxt->interruptibility = 0;
7319 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02007320 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007321 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007322
Borislav Petkovb51e9742013-09-22 16:44:52 +02007323 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02007324
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007325 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02007326
Avi Kivitye46479f2010-04-11 13:05:16 +03007327 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02007328 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09007329 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07007330 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07007331 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
7332 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007333 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07007334 }
Sean Christopherson736c2912019-12-06 15:57:14 -08007335 if (reexecute_instruction(vcpu, cr2_or_gpa,
7336 write_fault_to_spt,
7337 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007338 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00007339 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00007340 /*
7341 * #UD should result in just EMULATION_FAILED, and trap-like
7342 * exception should not be encountered during decode.
7343 */
7344 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
7345 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00007346 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007347 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00007348 }
Liran Alone2366172018-03-12 13:12:49 +02007349 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007350 }
7351 }
7352
Sean Christopherson42cbf062019-08-27 14:40:31 -07007353 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
7354 !is_vmware_backdoor_opcode(ctxt)) {
7355 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007356 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07007357 }
Liran Alon04789b62018-03-12 13:12:50 +02007358
Sean Christopherson1957aa62019-08-27 14:40:39 -07007359 /*
7360 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
7361 * for kvm_skip_emulated_instruction(). The caller is responsible for
7362 * updating interruptibility state and injecting single-step #DBs.
7363 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03007364 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03007365 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03007366 if (ctxt->eflags & X86_EFLAGS_RF)
7367 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007368 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03007369 }
7370
Sean Christopherson736c2912019-12-06 15:57:14 -08007371 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007372 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08007373
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007374 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03007375 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007376 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
7377 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03007378 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007379 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03007380
Gleb Natapov5cd21912010-03-18 15:20:26 +02007381restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08007382 if (emulation_type & EMULTYPE_PF) {
7383 /* Save the faulting GPA (cr2) in the address field */
7384 ctxt->exception.address = cr2_or_gpa;
7385
7386 /* With shadow page tables, cr2 contains a GVA or nGPA. */
7387 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08007388 ctxt->gpa_available = true;
7389 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08007390 }
7391 } else {
7392 /* Sanitize the address out of an abundance of paranoia. */
7393 ctxt->exception.address = 0;
7394 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05007395
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007396 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03007397
Joerg Roedel775fde82011-04-04 12:39:24 +02007398 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007399 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02007400
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007401 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08007402 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03007403 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007404 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03007405
Liran Alone2366172018-03-12 13:12:49 +02007406 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007407 }
7408
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007409 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007410 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02007411 if (inject_emulated_exception(vcpu))
7412 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007413 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02007414 if (!vcpu->arch.pio.in) {
7415 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03007416 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02007417 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007418 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007419 vcpu->arch.complete_userspace_io = complete_emulated_pio;
7420 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007421 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007422 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07007423 ++vcpu->stat.mmio_exits;
7424
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007425 if (!vcpu->mmio_is_write)
7426 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007427 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007428 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007429 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03007430 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03007431 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07007432 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03007433
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007434 if (writeback) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007435 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007436 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007437 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02007438 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007439 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
7440 kvm_rip_write(vcpu, ctxt->eip);
Felipe Franciosi384dea12020-05-19 08:11:22 +00007441 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
Sean Christopherson120c2c42019-08-27 14:40:29 -07007442 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007443 if (kvm_x86_ops.update_emulated_instruction)
7444 kvm_x86_ops.update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02007445 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07007446 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007447
7448 /*
7449 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
7450 * do nothing, and it will be requested again as soon as
7451 * the shadow expires. But we still need to check here,
7452 * because POPF has no interrupt shadow.
7453 */
7454 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
7455 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007456 } else
7457 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03007458
Gleb Natapove85d28f2010-07-29 15:11:52 +03007459 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01007460}
Sean Christophersonc60658d2018-08-23 13:56:53 -07007461
7462int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
7463{
7464 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
7465}
7466EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
7467
7468int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
7469 void *insn, int insn_len)
7470{
7471 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
7472}
7473EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01007474
Sean Christopherson8764ed52019-04-29 07:04:15 -07007475static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
7476{
7477 vcpu->arch.pio.count = 0;
7478 return 1;
7479}
7480
Sean Christopherson45def772019-03-11 20:01:05 -07007481static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
7482{
7483 vcpu->arch.pio.count = 0;
7484
7485 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
7486 return 1;
7487
7488 return kvm_skip_emulated_instruction(vcpu);
7489}
7490
Sean Christophersondca7f122018-03-08 08:57:27 -08007491static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
7492 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01007493{
Sean Christophersonde3cd112019-04-30 10:36:17 -07007494 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007495 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
7496
Sean Christopherson8764ed52019-04-29 07:04:15 -07007497 if (ret)
7498 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07007499
Sean Christopherson8764ed52019-04-29 07:04:15 -07007500 /*
7501 * Workaround userspace that relies on old KVM behavior of %rip being
7502 * incremented prior to exiting to userspace to handle "OUT 0x7e".
7503 */
7504 if (port == 0x7e &&
7505 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
7506 vcpu->arch.complete_userspace_io =
7507 complete_fast_pio_out_port_0x7e;
7508 kvm_skip_emulated_instruction(vcpu);
7509 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07007510 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
7511 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
7512 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07007513 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01007514}
Carsten Ottede7d7892007-10-30 18:44:25 +01007515
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007516static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
7517{
7518 unsigned long val;
7519
7520 /* We should only ever be called with arch.pio.count equal to 1 */
7521 BUG_ON(vcpu->arch.pio.count != 1);
7522
Sean Christopherson45def772019-03-11 20:01:05 -07007523 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
7524 vcpu->arch.pio.count = 0;
7525 return 1;
7526 }
7527
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007528 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007529 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007530
7531 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007532 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007533 * the copy and tracing
7534 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007535 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07007536 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007537
Sean Christopherson45def772019-03-11 20:01:05 -07007538 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007539}
7540
Sean Christophersondca7f122018-03-08 08:57:27 -08007541static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
7542 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007543{
7544 unsigned long val;
7545 int ret;
7546
7547 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007548 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007549
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007550 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007551 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007552 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007553 return ret;
7554 }
7555
Sean Christopherson45def772019-03-11 20:01:05 -07007556 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007557 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7558
7559 return 0;
7560}
Sean Christophersondca7f122018-03-08 08:57:27 -08007561
7562int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7563{
Sean Christopherson45def772019-03-11 20:01:05 -07007564 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007565
Sean Christophersondca7f122018-03-08 08:57:27 -08007566 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007567 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007568 else
Sean Christopherson45def772019-03-11 20:01:05 -07007569 ret = kvm_fast_pio_out(vcpu, size, port);
7570 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007571}
7572EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007573
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007574static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007575{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007576 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007577 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007578}
7579
7580static void tsc_khz_changed(void *data)
7581{
7582 struct cpufreq_freqs *freq = data;
7583 unsigned long khz = 0;
7584
7585 if (data)
7586 khz = freq->new;
7587 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7588 khz = cpufreq_quick_get(raw_smp_processor_id());
7589 if (!khz)
7590 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007591 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007592}
7593
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007594#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007595static void kvm_hyperv_tsc_notifier(void)
7596{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007597 struct kvm *kvm;
7598 struct kvm_vcpu *vcpu;
7599 int cpu;
7600
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007601 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007602 list_for_each_entry(kvm, &vm_list, vm_list)
7603 kvm_make_mclock_inprogress_request(kvm);
7604
7605 hyperv_stop_tsc_emulation();
7606
7607 /* TSC frequency always matches when on Hyper-V */
7608 for_each_present_cpu(cpu)
7609 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7610 kvm_max_guest_tsc_khz = tsc_khz;
7611
7612 list_for_each_entry(kvm, &vm_list, vm_list) {
7613 struct kvm_arch *ka = &kvm->arch;
7614
7615 spin_lock(&ka->pvclock_gtod_sync_lock);
7616
7617 pvclock_update_vm_gtod_copy(kvm);
7618
7619 kvm_for_each_vcpu(cpu, vcpu, kvm)
7620 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7621
7622 kvm_for_each_vcpu(cpu, vcpu, kvm)
7623 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7624
7625 spin_unlock(&ka->pvclock_gtod_sync_lock);
7626 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007627 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007628}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007629#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007630
Viresh Kumardf240142019-04-29 15:03:58 +05307631static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007632{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007633 struct kvm *kvm;
7634 struct kvm_vcpu *vcpu;
7635 int i, send_ipi = 0;
7636
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007637 /*
7638 * We allow guests to temporarily run on slowing clocks,
7639 * provided we notify them after, or to run on accelerating
7640 * clocks, provided we notify them before. Thus time never
7641 * goes backwards.
7642 *
7643 * However, we have a problem. We can't atomically update
7644 * the frequency of a given CPU from this function; it is
7645 * merely a notifier, which can be called from any CPU.
7646 * Changing the TSC frequency at arbitrary points in time
7647 * requires a recomputation of local variables related to
7648 * the TSC for each VCPU. We must flag these local variables
7649 * to be updated and be sure the update takes place with the
7650 * new frequency before any guests proceed.
7651 *
7652 * Unfortunately, the combination of hotplug CPU and frequency
7653 * change creates an intractable locking scenario; the order
7654 * of when these callouts happen is undefined with respect to
7655 * CPU hotplug, and they can race with each other. As such,
7656 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7657 * undefined; you can actually have a CPU frequency change take
7658 * place in between the computation of X and the setting of the
7659 * variable. To protect against this problem, all updates of
7660 * the per_cpu tsc_khz variable are done in an interrupt
7661 * protected IPI, and all callers wishing to update the value
7662 * must wait for a synchronous IPI to complete (which is trivial
7663 * if the caller is on the CPU already). This establishes the
7664 * necessary total order on variable updates.
7665 *
7666 * Note that because a guest time update may take place
7667 * anytime after the setting of the VCPU's request bit, the
7668 * correct TSC value must be set before the request. However,
7669 * to ensure the update actually makes it to any guest which
7670 * starts running in hardware virtualization between the set
7671 * and the acquisition of the spinlock, we must also ping the
7672 * CPU after setting the request bit.
7673 *
7674 */
7675
Viresh Kumardf240142019-04-29 15:03:58 +05307676 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007677
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007678 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007679 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007680 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307681 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007682 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007683 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007684 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007685 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007686 }
7687 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007688 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007689
7690 if (freq->old < freq->new && send_ipi) {
7691 /*
7692 * We upscale the frequency. Must make the guest
7693 * doesn't see old kvmclock values while running with
7694 * the new frequency, otherwise we risk the guest sees
7695 * time go backwards.
7696 *
7697 * In case we update the frequency for another cpu
7698 * (which might be in guest context) send an interrupt
7699 * to kick the cpu out of guest context. Next time
7700 * guest context is entered kvmclock will be updated,
7701 * so the guest will not see stale values.
7702 */
Viresh Kumardf240142019-04-29 15:03:58 +05307703 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007704 }
Viresh Kumardf240142019-04-29 15:03:58 +05307705}
7706
7707static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7708 void *data)
7709{
7710 struct cpufreq_freqs *freq = data;
7711 int cpu;
7712
7713 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7714 return 0;
7715 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7716 return 0;
7717
7718 for_each_cpu(cpu, freq->policy->cpus)
7719 __kvmclock_cpufreq_notifier(freq, cpu);
7720
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007721 return 0;
7722}
7723
7724static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007725 .notifier_call = kvmclock_cpufreq_notifier
7726};
7727
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007728static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007729{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007730 tsc_khz_changed(NULL);
7731 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007732}
7733
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007734static void kvm_timer_init(void)
7735{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007736 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307737
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007738 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007739#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007740 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007741 int cpu;
7742
Avi Kivity3e26f232010-12-16 12:16:34 +02007743 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007744 policy = cpufreq_cpu_get(cpu);
Wanpeng Li9a119972020-03-02 15:15:36 +08007745 if (policy) {
7746 if (policy->cpuinfo.max_freq)
7747 max_tsc_khz = policy->cpuinfo.max_freq;
7748 cpufreq_cpu_put(policy);
7749 }
Avi Kivity3e26f232010-12-16 12:16:34 +02007750 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007751#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007752 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7753 CPUFREQ_TRANSITION_NOTIFIER);
7754 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307755
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007756 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007757 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007758}
7759
Andi Kleendd60d212017-07-25 17:20:32 -07007760DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7761EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007762
Gleb Natapovf5132b02011-11-10 14:57:22 +02007763int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007764{
Alex,Shi086c9852011-10-20 15:34:01 +08007765 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007766}
7767
7768static int kvm_is_user_mode(void)
7769{
7770 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007771
Alex,Shi086c9852011-10-20 15:34:01 +08007772 if (__this_cpu_read(current_vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007773 user_mode = kvm_x86_ops.get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007774
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007775 return user_mode != 0;
7776}
7777
7778static unsigned long kvm_get_guest_ip(void)
7779{
7780 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007781
Alex,Shi086c9852011-10-20 15:34:01 +08007782 if (__this_cpu_read(current_vcpu))
7783 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007784
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007785 return ip;
7786}
7787
Luwei Kang8479e042019-02-18 19:26:07 -05007788static void kvm_handle_intel_pt_intr(void)
7789{
7790 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7791
7792 kvm_make_request(KVM_REQ_PMI, vcpu);
7793 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7794 (unsigned long *)&vcpu->arch.pmu.global_status);
7795}
7796
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007797static struct perf_guest_info_callbacks kvm_guest_cbs = {
7798 .is_in_guest = kvm_is_in_guest,
7799 .is_user_mode = kvm_is_user_mode,
7800 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007801 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007802};
7803
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007804#ifdef CONFIG_X86_64
7805static void pvclock_gtod_update_fn(struct work_struct *work)
7806{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007807 struct kvm *kvm;
7808
7809 struct kvm_vcpu *vcpu;
7810 int i;
7811
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007812 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007813 list_for_each_entry(kvm, &vm_list, vm_list)
7814 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007815 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007816 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007817 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007818}
7819
7820static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7821
7822/*
7823 * Notification about pvclock gtod data update.
7824 */
7825static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7826 void *priv)
7827{
7828 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7829 struct timekeeper *tk = priv;
7830
7831 update_pvclock_gtod(tk);
7832
7833 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007834 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007835 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007836 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007837 atomic_read(&kvm_guest_has_master_clock) != 0)
7838 queue_work(system_long_wq, &pvclock_gtod_work);
7839
7840 return 0;
7841}
7842
7843static struct notifier_block pvclock_gtod_notifier = {
7844 .notifier_call = pvclock_gtod_notify,
7845};
7846#endif
7847
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007848int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007849{
Sean Christophersond008dfd2020-03-21 13:25:56 -07007850 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007851 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007852
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007853 if (kvm_x86_ops.hardware_enable) {
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007854 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007855 r = -EEXIST;
7856 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007857 }
7858
7859 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007860 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007861 r = -EOPNOTSUPP;
7862 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007863 }
7864 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007865 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007866 r = -EOPNOTSUPP;
7867 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007868 }
7869
Marc Orrb666a4b2018-11-06 14:53:56 -08007870 /*
7871 * KVM explicitly assumes that the guest has an FPU and
7872 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7873 * vCPU's FPU state as a fxregs_state struct.
7874 */
7875 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7876 printk(KERN_ERR "kvm: inadequate fpu\n");
7877 r = -EOPNOTSUPP;
7878 goto out;
7879 }
7880
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007881 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007882 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007883 __alignof__(struct fpu), SLAB_ACCOUNT,
7884 NULL);
7885 if (!x86_fpu_cache) {
7886 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7887 goto out;
7888 }
7889
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007890 x86_emulator_cache = kvm_alloc_emulator_cache();
7891 if (!x86_emulator_cache) {
7892 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7893 goto out_free_x86_fpu_cache;
7894 }
7895
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07007896 user_return_msrs = alloc_percpu(struct kvm_user_return_msrs);
7897 if (!user_return_msrs) {
7898 printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007899 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007900 }
7901
Avi Kivity97db56c2008-01-13 13:23:56 +02007902 r = kvm_mmu_module_init();
7903 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007904 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007905
Sheng Yang7b523452008-04-25 21:13:50 +08007906 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007907 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007908 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007909 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007910
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007911 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7912
Sean Christophersoncfc48182020-03-02 15:56:23 -08007913 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007914 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007915 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7916 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007917
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007918 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007919 if (pi_inject_timer == -1)
7920 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007921#ifdef CONFIG_X86_64
7922 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007923
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007924 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007925 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007926#endif
7927
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007928 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007929
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007930out_free_percpu:
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07007931 free_percpu(user_return_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007932out_free_x86_emulator_cache:
7933 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007934out_free_x86_fpu_cache:
7935 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007936out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007937 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007938}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007939
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007940void kvm_arch_exit(void)
7941{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007942#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007943 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007944 clear_hv_tscchange_cb();
7945#endif
David Matlackcef84c32016-12-16 14:30:36 -08007946 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007947 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7948
Jan Kiszka888d2562009-04-17 19:24:58 +02007949 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7950 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7951 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007952 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007953#ifdef CONFIG_X86_64
7954 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7955#endif
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007956 kvm_x86_ops.hardware_enable = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007957 kvm_mmu_module_exit();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07007958 free_percpu(user_return_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007959 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007960}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007961
Joel Schopp5cb56052015-03-02 13:43:31 -06007962int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007963{
7964 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007965 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007966 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007967 return 1;
7968 } else {
7969 vcpu->run->exit_reason = KVM_EXIT_HLT;
7970 return 0;
7971 }
7972}
Joel Schopp5cb56052015-03-02 13:43:31 -06007973EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7974
7975int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7976{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007977 int ret = kvm_skip_emulated_instruction(vcpu);
7978 /*
7979 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7980 * KVM_EXIT_DEBUG here.
7981 */
7982 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007983}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007984EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7985
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007986#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007987static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7988 unsigned long clock_type)
7989{
7990 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007991 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007992 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007993 int ret;
7994
7995 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7996 return -KVM_EOPNOTSUPP;
7997
7998 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7999 return -KVM_EOPNOTSUPP;
8000
8001 clock_pairing.sec = ts.tv_sec;
8002 clock_pairing.nsec = ts.tv_nsec;
8003 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
8004 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02008005 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008006
8007 ret = 0;
8008 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
8009 sizeof(struct kvm_clock_pairing)))
8010 ret = -KVM_EFAULT;
8011
8012 return ret;
8013}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008014#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008015
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308016/*
8017 * kvm_pv_kick_cpu_op: Kick a vcpu.
8018 *
8019 * @apicid - apicid of vcpu to be kicked.
8020 */
8021static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
8022{
Raghavendra K T24d21662013-08-26 14:18:35 +05308023 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308024
Peter Xu150a84f2019-12-04 20:07:21 +01008025 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01008026 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08008027 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05308028 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06008029 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308030
Raghavendra K T24d21662013-08-26 14:18:35 +05308031 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08008032 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308033}
8034
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06008035bool kvm_apicv_activated(struct kvm *kvm)
8036{
8037 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
8038}
8039EXPORT_SYMBOL_GPL(kvm_apicv_activated);
8040
8041void kvm_apicv_init(struct kvm *kvm, bool enable)
8042{
8043 if (enable)
8044 clear_bit(APICV_INHIBIT_REASON_DISABLE,
8045 &kvm->arch.apicv_inhibit_reasons);
8046 else
8047 set_bit(APICV_INHIBIT_REASON_DISABLE,
8048 &kvm->arch.apicv_inhibit_reasons);
8049}
8050EXPORT_SYMBOL_GPL(kvm_apicv_init);
8051
Wanpeng Li71506292019-06-11 20:23:49 +08008052static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
8053{
8054 struct kvm_vcpu *target = NULL;
8055 struct kvm_apic_map *map;
8056
8057 rcu_read_lock();
8058 map = rcu_dereference(kvm->arch.apic_map);
8059
8060 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
8061 target = map->phys_map[dest_id]->vcpu;
8062
8063 rcu_read_unlock();
8064
Wanpeng Li266e85a2019-07-24 17:43:13 +08008065 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08008066 kvm_vcpu_yield_to(target);
8067}
8068
Hollis Blanchard8776e512007-10-31 17:24:24 -05008069int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
8070{
8071 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03008072 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008073
Radim Krčmář696ca772018-05-24 17:50:56 +02008074 if (kvm_hv_hypercall_enabled(vcpu->kvm))
8075 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02008076
Sean Christophersonde3cd112019-04-30 10:36:17 -07008077 nr = kvm_rax_read(vcpu);
8078 a0 = kvm_rbx_read(vcpu);
8079 a1 = kvm_rcx_read(vcpu);
8080 a2 = kvm_rdx_read(vcpu);
8081 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008082
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008083 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04008084
Nadav Amita449c7a2014-06-18 17:19:24 +03008085 op_64_bit = is_64_bit_mode(vcpu);
8086 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05008087 nr &= 0xFFFFFFFF;
8088 a0 &= 0xFFFFFFFF;
8089 a1 &= 0xFFFFFFFF;
8090 a2 &= 0xFFFFFFFF;
8091 a3 &= 0xFFFFFFFF;
8092 }
8093
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008094 if (kvm_x86_ops.get_cpl(vcpu) != 0) {
Jan Kiszka07708c42009-08-03 18:43:28 +02008095 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02008096 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02008097 }
8098
Oliver Upton66570e92020-08-18 15:24:28 +00008099 ret = -KVM_ENOSYS;
8100
Hollis Blanchard8776e512007-10-31 17:24:24 -05008101 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02008102 case KVM_HC_VAPIC_POLL_IRQ:
8103 ret = 0;
8104 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308105 case KVM_HC_KICK_CPU:
Oliver Upton66570e92020-08-18 15:24:28 +00008106 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_UNHALT))
8107 break;
8108
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308109 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08008110 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308111 ret = 0;
8112 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01008113#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02008114 case KVM_HC_CLOCK_PAIRING:
8115 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
8116 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08008117#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08008118 case KVM_HC_SEND_IPI:
Oliver Upton66570e92020-08-18 15:24:28 +00008119 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SEND_IPI))
8120 break;
8121
Wanpeng Li4180bf12018-07-23 14:39:54 +08008122 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
8123 break;
Wanpeng Li71506292019-06-11 20:23:49 +08008124 case KVM_HC_SCHED_YIELD:
Oliver Upton66570e92020-08-18 15:24:28 +00008125 if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SCHED_YIELD))
8126 break;
8127
Wanpeng Li71506292019-06-11 20:23:49 +08008128 kvm_sched_yield(vcpu->kvm, a0);
8129 ret = 0;
8130 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05008131 default:
8132 ret = -KVM_ENOSYS;
8133 break;
8134 }
Radim Krčmář696ca772018-05-24 17:50:56 +02008135out:
Nadav Amita449c7a2014-06-18 17:19:24 +03008136 if (!op_64_bit)
8137 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07008138 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03008139
Amit Shahf11c3a82008-02-21 01:00:30 +05308140 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03008141 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008142}
8143EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
8144
Jan Kiszkab6785de2012-09-20 07:43:17 +02008145static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05008146{
Avi Kivityd6aa1002011-04-20 15:47:13 +03008147 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008148 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008149 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008150
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008151 kvm_x86_ops.patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008152
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01008153 return emulator_write_emulated(ctxt, rip, instruction, 3,
8154 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05008155}
8156
Avi Kivity851ba692009-08-24 11:10:17 +03008157static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008158{
Matt Gingell782d4222015-11-16 15:26:00 -08008159 return vcpu->run->request_interrupt_window &&
8160 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008161}
8162
Avi Kivity851ba692009-08-24 11:10:17 +03008163static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008164{
Avi Kivity851ba692009-08-24 11:10:17 +03008165 struct kvm_run *kvm_run = vcpu->run;
8166
Jan Kiszka91586a32009-10-05 13:07:21 +02008167 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02008168 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008169 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008170 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01008171 kvm_run->ready_for_interrupt_injection =
8172 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08008173 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008174}
8175
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008176static void update_cr8_intercept(struct kvm_vcpu *vcpu)
8177{
8178 int max_irr, tpr;
8179
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008180 if (!kvm_x86_ops.update_cr8_intercept)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008181 return;
8182
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008183 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03008184 return;
8185
Andrey Smetanind62caab2015-11-10 15:36:33 +03008186 if (vcpu->arch.apicv_active)
8187 return;
8188
Gleb Natapov8db3baa2009-05-11 13:35:54 +03008189 if (!vcpu->arch.apic->vapic_addr)
8190 max_irr = kvm_lapic_find_highest_irr(vcpu);
8191 else
8192 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008193
8194 if (max_irr != -1)
8195 max_irr >>= 4;
8196
8197 tpr = kvm_lapic_get_cr8(vcpu);
8198
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008199 kvm_x86_ops.update_cr8_intercept(vcpu, tpr, max_irr);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008200}
8201
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008202static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008203{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008204 int r;
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008205 bool can_inject = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008206
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008207 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07008208
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008209 if (vcpu->arch.exception.injected) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008210 kvm_x86_ops.queue_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008211 can_inject = false;
8212 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07008213 /*
Liran Alona042c262018-03-23 03:01:32 +03008214 * Do not inject an NMI or interrupt if there is a pending
8215 * exception. Exceptions and interrupts are recognized at
8216 * instruction boundaries, i.e. the start of an instruction.
8217 * Trap-like exceptions, e.g. #DB, have higher priority than
8218 * NMIs and interrupts, i.e. traps are recognized before an
8219 * NMI/interrupt that's pending on the same instruction.
8220 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
8221 * priority, but are only generated (pended) during instruction
8222 * execution, i.e. a pending fault-like exception means the
8223 * fault occurred on the *previous* instruction and must be
8224 * serviced prior to recognizing any new events in order to
8225 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07008226 */
Liran Alon1a680e32018-03-23 03:01:33 +03008227 else if (!vcpu->arch.exception.pending) {
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008228 if (vcpu->arch.nmi_injected) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008229 kvm_x86_ops.set_nmi(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008230 can_inject = false;
8231 } else if (vcpu->arch.interrupt.injected) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008232 kvm_x86_ops.set_irq(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008233 can_inject = false;
8234 }
Wanpeng Li664f8e22017-08-24 03:35:09 -07008235 }
8236
Sean Christopherson3b82b8d2020-04-22 19:25:47 -07008237 WARN_ON_ONCE(vcpu->arch.exception.injected &&
8238 vcpu->arch.exception.pending);
8239
Liran Alon1a680e32018-03-23 03:01:33 +03008240 /*
8241 * Call check_nested_events() even if we reinjected a previous event
8242 * in order for caller to determine if it should require immediate-exit
8243 * from L2 to L1 due to pending L1 events which require exit
8244 * from L2 to L1.
8245 */
Paolo Bonzini56083bd2020-04-17 10:32:53 -04008246 if (is_guest_mode(vcpu)) {
Paolo Bonzini33b22172020-04-17 10:24:18 -04008247 r = kvm_x86_ops.nested_ops->check_events(vcpu);
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008248 if (r < 0)
8249 goto busy;
Wanpeng Li664f8e22017-08-24 03:35:09 -07008250 }
8251
8252 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03008253 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02008254 trace_kvm_inj_exception(vcpu->arch.exception.nr,
8255 vcpu->arch.exception.has_error_code,
8256 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03008257
Wanpeng Li664f8e22017-08-24 03:35:09 -07008258 vcpu->arch.exception.pending = false;
8259 vcpu->arch.exception.injected = true;
8260
Nadav Amitd6e8c852014-07-24 14:51:24 +03008261 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
8262 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
8263 X86_EFLAGS_RF);
8264
Jim Mattsonf10c7292018-10-16 14:29:23 -07008265 if (vcpu->arch.exception.nr == DB_VECTOR) {
Jim Mattsonf10c7292018-10-16 14:29:23 -07008266 kvm_deliver_exception_payload(vcpu);
8267 if (vcpu->arch.dr7 & DR7_GD) {
8268 vcpu->arch.dr7 &= ~DR7_GD;
8269 kvm_update_dr7(vcpu);
8270 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03008271 }
8272
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008273 kvm_x86_ops.queue_exception(vcpu);
Paolo Bonzinic6b22f52020-05-26 09:05:27 -04008274 can_inject = false;
Liran Alon1a680e32018-03-23 03:01:33 +03008275 }
8276
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008277 /*
8278 * Finally, inject interrupt events. If an event cannot be injected
8279 * due to architectural conditions (e.g. IF=0) a window-open exit
8280 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending
8281 * and can architecturally be injected, but we cannot do it right now:
8282 * an interrupt could have arrived just now and we have to inject it
8283 * as a vmexit, or there could already an event in the queue, which is
8284 * indicated by can_inject. In that case we request an immediate exit
8285 * in order to make progress and get back here for another iteration.
8286 * The kvm_x86_ops hooks communicate this by returning -EBUSY.
8287 */
8288 if (vcpu->arch.smi_pending) {
8289 r = can_inject ? kvm_x86_ops.smi_allowed(vcpu, true) : -EBUSY;
8290 if (r < 0)
8291 goto busy;
8292 if (r) {
8293 vcpu->arch.smi_pending = false;
8294 ++vcpu->arch.smi_count;
8295 enter_smm(vcpu);
8296 can_inject = false;
8297 } else
8298 kvm_x86_ops.enable_smi_window(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008299 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008300
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008301 if (vcpu->arch.nmi_pending) {
8302 r = can_inject ? kvm_x86_ops.nmi_allowed(vcpu, true) : -EBUSY;
8303 if (r < 0)
8304 goto busy;
8305 if (r) {
8306 --vcpu->arch.nmi_pending;
8307 vcpu->arch.nmi_injected = true;
8308 kvm_x86_ops.set_nmi(vcpu);
8309 can_inject = false;
8310 WARN_ON(kvm_x86_ops.nmi_allowed(vcpu, true) < 0);
8311 }
8312 if (vcpu->arch.nmi_pending)
8313 kvm_x86_ops.enable_nmi_window(vcpu);
8314 }
8315
8316 if (kvm_cpu_has_injectable_intr(vcpu)) {
8317 r = can_inject ? kvm_x86_ops.interrupt_allowed(vcpu, true) : -EBUSY;
8318 if (r < 0)
8319 goto busy;
8320 if (r) {
8321 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false);
8322 kvm_x86_ops.set_irq(vcpu);
8323 WARN_ON(kvm_x86_ops.interrupt_allowed(vcpu, true) < 0);
8324 }
8325 if (kvm_cpu_has_injectable_intr(vcpu))
8326 kvm_x86_ops.enable_irq_window(vcpu);
8327 }
8328
8329 if (is_guest_mode(vcpu) &&
8330 kvm_x86_ops.nested_ops->hv_timer_pending &&
8331 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
8332 *req_immediate_exit = true;
8333
8334 WARN_ON(vcpu->arch.exception.pending);
8335 return;
8336
8337busy:
8338 *req_immediate_exit = true;
8339 return;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008340}
8341
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008342static void process_nmi(struct kvm_vcpu *vcpu)
8343{
8344 unsigned limit = 2;
8345
8346 /*
8347 * x86 is limited to one NMI running, and one NMI pending after it.
8348 * If an NMI is already in progress, limit further NMIs to just one.
8349 * Otherwise, allow two (and we'll inject the first one immediately).
8350 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008351 if (kvm_x86_ops.get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008352 limit = 1;
8353
8354 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
8355 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
8356 kvm_make_request(KVM_REQ_EVENT, vcpu);
8357}
8358
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008359static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008360{
8361 u32 flags = 0;
8362 flags |= seg->g << 23;
8363 flags |= seg->db << 22;
8364 flags |= seg->l << 21;
8365 flags |= seg->avl << 20;
8366 flags |= seg->present << 15;
8367 flags |= seg->dpl << 13;
8368 flags |= seg->s << 12;
8369 flags |= seg->type << 8;
8370 return flags;
8371}
8372
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008373static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008374{
8375 struct kvm_segment seg;
8376 int offset;
8377
8378 kvm_get_segment(vcpu, &seg, n);
8379 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
8380
8381 if (n < 3)
8382 offset = 0x7f84 + n * 12;
8383 else
8384 offset = 0x7f2c + (n - 3) * 12;
8385
8386 put_smstate(u32, buf, offset + 8, seg.base);
8387 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008388 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008389}
8390
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06008391#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008392static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008393{
8394 struct kvm_segment seg;
8395 int offset;
8396 u16 flags;
8397
8398 kvm_get_segment(vcpu, &seg, n);
8399 offset = 0x7e00 + n * 16;
8400
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008401 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008402 put_smstate(u16, buf, offset, seg.selector);
8403 put_smstate(u16, buf, offset + 2, flags);
8404 put_smstate(u32, buf, offset + 4, seg.limit);
8405 put_smstate(u64, buf, offset + 8, seg.base);
8406}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06008407#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008408
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008409static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008410{
8411 struct desc_ptr dt;
8412 struct kvm_segment seg;
8413 unsigned long val;
8414 int i;
8415
8416 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
8417 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
8418 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
8419 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
8420
8421 for (i = 0; i < 8; i++)
8422 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
8423
8424 kvm_get_dr(vcpu, 6, &val);
8425 put_smstate(u32, buf, 0x7fcc, (u32)val);
8426 kvm_get_dr(vcpu, 7, &val);
8427 put_smstate(u32, buf, 0x7fc8, (u32)val);
8428
8429 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
8430 put_smstate(u32, buf, 0x7fc4, seg.selector);
8431 put_smstate(u32, buf, 0x7f64, seg.base);
8432 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008433 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008434
8435 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
8436 put_smstate(u32, buf, 0x7fc0, seg.selector);
8437 put_smstate(u32, buf, 0x7f80, seg.base);
8438 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008439 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008440
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008441 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008442 put_smstate(u32, buf, 0x7f74, dt.address);
8443 put_smstate(u32, buf, 0x7f70, dt.size);
8444
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008445 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008446 put_smstate(u32, buf, 0x7f58, dt.address);
8447 put_smstate(u32, buf, 0x7f54, dt.size);
8448
8449 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008450 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008451
8452 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
8453
8454 /* revision id */
8455 put_smstate(u32, buf, 0x7efc, 0x00020000);
8456 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
8457}
8458
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008459#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008460static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008461{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008462 struct desc_ptr dt;
8463 struct kvm_segment seg;
8464 unsigned long val;
8465 int i;
8466
8467 for (i = 0; i < 16; i++)
8468 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
8469
8470 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
8471 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
8472
8473 kvm_get_dr(vcpu, 6, &val);
8474 put_smstate(u64, buf, 0x7f68, val);
8475 kvm_get_dr(vcpu, 7, &val);
8476 put_smstate(u64, buf, 0x7f60, val);
8477
8478 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
8479 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
8480 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
8481
8482 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
8483
8484 /* revision id */
8485 put_smstate(u32, buf, 0x7efc, 0x00020064);
8486
8487 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
8488
8489 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
8490 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008491 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008492 put_smstate(u32, buf, 0x7e94, seg.limit);
8493 put_smstate(u64, buf, 0x7e98, seg.base);
8494
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008495 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008496 put_smstate(u32, buf, 0x7e84, dt.size);
8497 put_smstate(u64, buf, 0x7e88, dt.address);
8498
8499 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
8500 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008501 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008502 put_smstate(u32, buf, 0x7e74, seg.limit);
8503 put_smstate(u64, buf, 0x7e78, seg.base);
8504
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008505 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008506 put_smstate(u32, buf, 0x7e64, dt.size);
8507 put_smstate(u64, buf, 0x7e68, dt.address);
8508
8509 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008510 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008511}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008512#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008513
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008514static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02008515{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008516 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02008517 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008518 char buf[512];
8519 u32 cr0;
8520
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008521 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008522 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008523#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008524 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008525 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008526 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008527#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008528 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008529
Ladi Prosek0234bf82017-10-11 16:54:40 +02008530 /*
8531 * Give pre_enter_smm() a chance to make ISA-specific changes to the
8532 * vCPU state (e.g. leave guest mode) after we've saved the state into
8533 * the SMM state-save area.
8534 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008535 kvm_x86_ops.pre_enter_smm(vcpu, buf);
Ladi Prosek0234bf82017-10-11 16:54:40 +02008536
8537 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008538 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008539
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008540 if (kvm_x86_ops.get_nmi_mask(vcpu))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008541 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
8542 else
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008543 kvm_x86_ops.set_nmi_mask(vcpu, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008544
8545 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
8546 kvm_rip_write(vcpu, 0x8000);
8547
8548 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008549 kvm_x86_ops.set_cr0(vcpu, cr0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008550 vcpu->arch.cr0 = cr0;
8551
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008552 kvm_x86_ops.set_cr4(vcpu, 0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008553
Paolo Bonzini18c36262015-08-07 12:27:54 +02008554 /* Undocumented: IDT limit is set to zero on entry to SMM. */
8555 dt.address = dt.size = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008556 kvm_x86_ops.set_idt(vcpu, &dt);
Paolo Bonzini18c36262015-08-07 12:27:54 +02008557
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008558 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
8559
8560 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
8561 cs.base = vcpu->arch.smbase;
8562
8563 ds.selector = 0;
8564 ds.base = 0;
8565
8566 cs.limit = ds.limit = 0xffffffff;
8567 cs.type = ds.type = 0x3;
8568 cs.dpl = ds.dpl = 0;
8569 cs.db = ds.db = 0;
8570 cs.s = ds.s = 1;
8571 cs.l = ds.l = 0;
8572 cs.g = ds.g = 1;
8573 cs.avl = ds.avl = 0;
8574 cs.present = ds.present = 1;
8575 cs.unusable = ds.unusable = 0;
8576 cs.padding = ds.padding = 0;
8577
8578 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
8579 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
8580 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
8581 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
8582 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
8583 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
8584
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008585#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008586 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008587 kvm_x86_ops.set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008588#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008589
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08008590 kvm_update_cpuid_runtime(vcpu);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008591 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008592}
8593
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008594static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008595{
8596 vcpu->arch.smi_pending = true;
8597 kvm_make_request(KVM_REQ_EVENT, vcpu);
8598}
8599
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008600void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8601 unsigned long *vcpu_bitmap)
8602{
8603 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008604
8605 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8606
Mao Wenandb5a95e2019-11-19 11:06:40 +08008607 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -05008608 NULL, vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008609
8610 free_cpumask_var(cpus);
8611}
8612
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008613void kvm_make_scan_ioapic_request(struct kvm *kvm)
8614{
8615 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8616}
8617
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008618void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8619{
8620 if (!lapic_in_kernel(vcpu))
8621 return;
8622
8623 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8624 kvm_apic_update_apicv(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008625 kvm_x86_ops.refresh_apicv_exec_ctrl(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008626}
8627EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8628
8629/*
8630 * NOTE: Do not hold any lock prior to calling this.
8631 *
8632 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8633 * locked, because it calls __x86_set_memory_region() which does
8634 * synchronize_srcu(&kvm->srcu).
8635 */
8636void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8637{
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008638 struct kvm_vcpu *except;
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008639 unsigned long old, new, expected;
8640
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008641 if (!kvm_x86_ops.check_apicv_inhibit_reasons ||
8642 !kvm_x86_ops.check_apicv_inhibit_reasons(bit))
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008643 return;
8644
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008645 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
8646 do {
8647 expected = new = old;
8648 if (activate)
8649 __clear_bit(bit, &new);
8650 else
8651 __set_bit(bit, &new);
8652 if (new == old)
8653 break;
8654 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
8655 } while (old != expected);
8656
8657 if (!!old == !!new)
8658 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008659
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008660 trace_kvm_apicv_update_request(activate, bit);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008661 if (kvm_x86_ops.pre_update_apicv_exec_ctrl)
8662 kvm_x86_ops.pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008663
8664 /*
8665 * Sending request to update APICV for all other vcpus,
8666 * while update the calling vcpu immediately instead of
8667 * waiting for another #VMEXIT to handle the request.
8668 */
8669 except = kvm_get_running_vcpu();
8670 kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE,
8671 except);
8672 if (except)
8673 kvm_vcpu_update_apicv(except);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008674}
8675EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8676
Yang Zhang3d81bc72013-04-11 19:25:13 +08008677static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008678{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008679 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008680 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008681
Andrey Smetanin63086302015-11-10 15:36:32 +03008682 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008683
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008684 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008685 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008686 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008687 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008688 kvm_x86_ops.sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008689 if (ioapic_in_kernel(vcpu->kvm))
8690 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008691 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008692
8693 if (is_guest_mode(vcpu))
8694 vcpu->arch.load_eoi_exitmap_pending = true;
8695 else
8696 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8697}
8698
8699static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8700{
8701 u64 eoi_exit_bitmap[4];
8702
8703 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8704 return;
8705
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008706 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8707 vcpu_to_synic(vcpu)->vec_bitmap, 256);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008708 kvm_x86_ops.load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008709}
8710
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +09008711void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8712 unsigned long start, unsigned long end)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008713{
8714 unsigned long apic_address;
8715
8716 /*
8717 * The physical address of apic access page is stored in the VMCS.
8718 * Update it when it becomes invalid.
8719 */
8720 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8721 if (start <= apic_address && apic_address < end)
8722 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
8723}
8724
Tang Chen4256f432014-09-24 15:57:54 +08008725void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8726{
Paolo Bonzini35754c92015-07-29 12:05:37 +02008727 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008728 return;
8729
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008730 if (!kvm_x86_ops.set_apic_access_page_addr)
Tang Chen4256f432014-09-24 15:57:54 +08008731 return;
8732
Sean Christophersona4148b72020-03-20 14:28:24 -07008733 kvm_x86_ops.set_apic_access_page_addr(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008734}
Tang Chen4256f432014-09-24 15:57:54 +08008735
Sean Christophersond264ee02018-08-27 15:21:12 -07008736void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8737{
8738 smp_send_reschedule(vcpu->cpu);
8739}
8740EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8741
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008742/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008743 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008744 * exiting to the userspace. Otherwise, the value will be returned to the
8745 * userspace.
8746 */
Avi Kivity851ba692009-08-24 11:10:17 +03008747static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008748{
8749 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008750 bool req_int_win =
8751 dm_request_for_irq_injection(vcpu) &&
8752 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08008753 fastpath_t exit_fastpath;
Matt Gingell62a193e2015-11-16 15:26:07 -08008754
Jan Kiszka730dca42013-04-28 10:50:52 +02008755 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008756
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008757 if (kvm_request_pending(vcpu)) {
Paolo Bonzini729c15c2020-09-22 06:53:57 -04008758 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
8759 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008760 r = 0;
8761 goto out;
8762 }
8763 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008764 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008765 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008766 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008767 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008768 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8769 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03008770 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8771 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008772 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8773 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008774 if (unlikely(r))
8775 goto out;
8776 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008777 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008778 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05008779 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
8780 kvm_mmu_load_pgd(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008781 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
Sean Christopherson77809382020-03-20 14:28:18 -07008782 kvm_vcpu_flush_tlb_all(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008783
8784 /* Flushing all ASIDs flushes the current ASID... */
8785 kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
8786 }
8787 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
8788 kvm_vcpu_flush_tlb_current(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04008789 if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
8790 kvm_vcpu_flush_tlb_guest(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008791
Avi Kivitya8eeb042010-05-10 12:34:53 +03008792 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008793 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008794 r = 0;
8795 goto out;
8796 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008797 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008798 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008799 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008800 r = 0;
8801 goto out;
8802 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008803 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8804 /* Page is swapped out. Do synthetic halt */
8805 vcpu->arch.apf.halted = true;
8806 r = 1;
8807 goto out;
8808 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008809 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8810 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008811 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8812 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008813 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8814 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008815 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008816 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008817 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008818 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008819 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8820 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8821 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008822 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008823 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8824 vcpu->run->eoi.vector =
8825 vcpu->arch.pending_ioapic_eoi;
8826 r = 0;
8827 goto out;
8828 }
8829 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008830 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8831 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008832 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8833 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008834 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8835 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008836 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8837 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8838 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8839 r = 0;
8840 goto out;
8841 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008842 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8843 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8844 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8845 r = 0;
8846 goto out;
8847 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008848 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8849 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8850 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8851 r = 0;
8852 goto out;
8853 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008854
8855 /*
8856 * KVM_REQ_HV_STIMER has to be processed after
8857 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8858 * depend on the guest clock being up-to-date
8859 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008860 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8861 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008862 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8863 kvm_vcpu_update_apicv(vcpu);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +02008864 if (kvm_check_request(KVM_REQ_APF_READY, vcpu))
8865 kvm_check_async_pf_completion(vcpu);
Alexander Graf1a1552542020-09-25 16:34:21 +02008866 if (kvm_check_request(KVM_REQ_MSR_FILTER_CHANGED, vcpu))
8867 kvm_x86_ops.msr_filter_changed(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008868 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008869
Avi Kivityb463a6f2010-07-20 15:06:17 +03008870 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008871 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008872 kvm_apic_accept_events(vcpu);
8873 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8874 r = 1;
8875 goto out;
8876 }
8877
Paolo Bonzinic9d40912020-05-22 11:21:49 -04008878 inject_pending_event(vcpu, &req_immediate_exit);
8879 if (req_int_win)
8880 kvm_x86_ops.enable_irq_window(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008881
Avi Kivity3842d132010-07-27 12:30:24 +03008882 if (kvm_lapic_enabled(vcpu)) {
8883 update_cr8_intercept(vcpu);
8884 kvm_lapic_sync_to_vapic(vcpu);
8885 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008886 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008887
Avi Kivityd8368af2012-05-14 18:07:56 +03008888 r = kvm_mmu_reload(vcpu);
8889 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008890 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008891 }
8892
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008893 preempt_disable();
8894
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008895 kvm_x86_ops.prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008896
8897 /*
8898 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8899 * IPI are then delayed after guest entry, which ensures that they
8900 * result in virtual interrupt delivery.
8901 */
8902 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008903 vcpu->mode = IN_GUEST_MODE;
8904
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008905 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8906
Lan Tianyu0f127d12016-03-13 11:10:29 +08008907 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008908 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008909 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008910 *
Luwei Kang81b01662019-01-31 16:52:02 +08008911 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008912 * pairs with the memory barrier implicit in pi_test_and_set_on
8913 * (see vmx_deliver_posted_interrupt).
8914 *
8915 * 3) This also orders the write to mode from any reads to the page
8916 * tables done while the VCPU is running. Please see the comment
8917 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008918 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008919 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008920
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008921 /*
8922 * This handles the case where a posted interrupt was
8923 * notified with kvm_vcpu_kick.
8924 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008925 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008926 kvm_x86_ops.sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008927
Wanpeng Li5a9f5442020-04-28 14:23:26 +08008928 if (kvm_vcpu_exit_request(vcpu)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008929 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008930 smp_wmb();
8931 local_irq_enable();
8932 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008933 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008934 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008935 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008936 }
8937
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008938 if (req_immediate_exit) {
8939 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008940 kvm_x86_ops.request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008941 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008942
Sean Christophersonb2d52252020-09-23 13:13:43 -07008943 trace_kvm_entry(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008944
Sean Christopherson2620fe22020-01-17 11:30:51 -08008945 fpregs_assert_state_consistent();
8946 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8947 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008948
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008949 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008950 set_debugreg(0, 7);
8951 set_debugreg(vcpu->arch.eff_db[0], 0);
8952 set_debugreg(vcpu->arch.eff_db[1], 1);
8953 set_debugreg(vcpu->arch.eff_db[2], 2);
8954 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008955 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008956 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008957 }
8958
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008959 exit_fastpath = kvm_x86_ops.run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008960
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008961 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008962 * Do this here before restoring debug registers on the host. And
8963 * since we do this before handling the vmexit, a DR access vmexit
8964 * can (a) read the correct value of the debug registers, (b) set
8965 * KVM_DEBUGREG_WONT_EXIT again.
8966 */
8967 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008968 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008969 kvm_x86_ops.sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008970 kvm_update_dr0123(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008971 kvm_update_dr7(vcpu);
8972 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008973 }
8974
8975 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008976 * If the guest has used debug registers, at least dr7
8977 * will be disabled while returning to the host.
8978 * If we don't have active breakpoints in the host, we don't
8979 * care about the messed up debug address registers. But if
8980 * we have some of them active, restore the old state.
8981 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008982 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008983 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008984
Jim Mattsonc9671182020-06-03 16:56:23 -07008985 vcpu->arch.last_vmentry_cpu = vcpu->cpu;
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008986 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008987
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008988 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008989 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008990
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008991 kvm_x86_ops.handle_exit_irqoff(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008992
Sean Christophersond7a08882019-07-10 09:07:34 -07008993 /*
8994 * Consume any pending interrupts, including the possible source of
8995 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8996 * An instruction is required after local_irq_enable() to fully unblock
8997 * interrupts on processors that implement an interrupt shadow, the
8998 * stat.exits increment will do nicely.
8999 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009000 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07009001 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009002 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07009003 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009004 kvm_after_interrupt(vcpu);
9005
Wanpeng Liec0671d2019-05-20 16:18:08 +08009006 if (lapic_in_kernel(vcpu)) {
9007 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
9008 if (delta != S64_MIN) {
9009 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
9010 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
9011 }
9012 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009013
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009014 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009015 preempt_enable();
9016
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009017 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03009018
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009019 /*
9020 * Profile KVM exit RIPs:
9021 */
9022 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009023 unsigned long rip = kvm_rip_read(vcpu);
9024 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009025 }
9026
Zachary Amsdencc578282012-02-03 15:43:50 -02009027 if (unlikely(vcpu->arch.tsc_always_catchup))
9028 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02009029
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03009030 if (vcpu->arch.apic_attention)
9031 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02009032
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009033 r = kvm_x86_ops.handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03009034 return r;
9035
9036cancel_injection:
Sean Christopherson8081ad02020-04-22 19:25:40 -07009037 if (req_immediate_exit)
9038 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009039 kvm_x86_ops.cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03009040 if (unlikely(vcpu->arch.apic_attention))
9041 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009042out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03009043 return r;
9044}
9045
Paolo Bonzini362c6982015-02-06 12:48:04 +01009046static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
9047{
Feng Wubf9f6ac2015-09-18 22:29:55 +08009048 if (!kvm_arch_vcpu_runnable(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009049 (!kvm_x86_ops.pre_block || kvm_x86_ops.pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01009050 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
9051 kvm_vcpu_block(vcpu);
9052 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009053
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009054 if (kvm_x86_ops.post_block)
9055 kvm_x86_ops.post_block(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009056
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01009057 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
9058 return 1;
9059 }
Gleb Natapov09cec752009-03-23 15:11:44 +02009060
Paolo Bonzini362c6982015-02-06 12:48:04 +01009061 kvm_apic_accept_events(vcpu);
9062 switch(vcpu->arch.mp_state) {
9063 case KVM_MP_STATE_HALTED:
9064 vcpu->arch.pv.pv_unhalted = false;
9065 vcpu->arch.mp_state =
9066 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009067 fallthrough;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009068 case KVM_MP_STATE_RUNNABLE:
9069 vcpu->arch.apf.halted = false;
9070 break;
9071 case KVM_MP_STATE_INIT_RECEIVED:
9072 break;
9073 default:
9074 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009075 }
9076 return 1;
9077}
9078
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009079static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
9080{
Paolo Bonzini56083bd2020-04-17 10:32:53 -04009081 if (is_guest_mode(vcpu))
Paolo Bonzini33b22172020-04-17 10:24:18 -04009082 kvm_x86_ops.nested_ops->check_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01009083
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009084 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
9085 !vcpu->arch.apf.halted);
9086}
9087
Paolo Bonzini362c6982015-02-06 12:48:04 +01009088static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03009089{
9090 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009091 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03009092
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009093 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009094 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03009095
Paolo Bonzini362c6982015-02-06 12:48:04 +01009096 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02009097 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009098 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009099 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01009100 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08009101 }
9102
Gleb Natapov09cec752009-03-23 15:11:44 +02009103 if (r <= 0)
9104 break;
9105
Radim Krčmář72875d82017-04-26 22:32:19 +02009106 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02009107 if (kvm_cpu_has_pending_timer(vcpu))
9108 kvm_inject_pending_timer_irqs(vcpu);
9109
Matt Gingell782d4222015-11-16 15:26:00 -08009110 if (dm_request_for_irq_injection(vcpu) &&
9111 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02009112 r = 0;
9113 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02009114 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01009115 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02009116 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02009117
Thomas Gleixnerf3020b82020-07-30 09:19:01 +02009118 if (__xfer_to_guest_mode_work_pending()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009119 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02009120 r = xfer_to_guest_mode_handle_work(vcpu);
9121 if (r)
9122 return r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009123 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03009124 }
9125 }
9126
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009127 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009128
9129 return r;
9130}
9131
Gleb Natapov716d51a2012-09-03 15:24:26 +03009132static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
9133{
9134 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009135
Gleb Natapov716d51a2012-09-03 15:24:26 +03009136 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07009137 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009138 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009139 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03009140}
9141
9142static int complete_emulated_pio(struct kvm_vcpu *vcpu)
9143{
9144 BUG_ON(!vcpu->arch.pio.count);
9145
9146 return complete_emulated_io(vcpu);
9147}
9148
Avi Kivityf78146b2012-04-18 19:22:47 +03009149/*
9150 * Implements the following, as a state machine:
9151 *
9152 * read:
9153 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009154 * for each mmio piece in the fragment
9155 * write gpa, len
9156 * exit
9157 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03009158 * execute insn
9159 *
9160 * write:
9161 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009162 * for each mmio piece in the fragment
9163 * write gpa, len
9164 * copy data
9165 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03009166 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03009167static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02009168{
9169 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03009170 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009171 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02009172
Gleb Natapov716d51a2012-09-03 15:24:26 +03009173 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02009174
Gleb Natapov716d51a2012-09-03 15:24:26 +03009175 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009176 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
9177 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009178 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009179 memcpy(frag->data, run->mmio.data, len);
9180
9181 if (frag->len <= 8) {
9182 /* Switch to the next fragment. */
9183 frag++;
9184 vcpu->mmio_cur_fragment++;
9185 } else {
9186 /* Go forward to the next mmio piece. */
9187 frag->data += len;
9188 frag->gpa += len;
9189 frag->len -= len;
9190 }
9191
Andrew Honiga08d3b32014-02-27 19:35:14 +01009192 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03009193 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02009194
9195 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02009196 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03009197 return 1;
9198 vcpu->mmio_read_completed = 1;
9199 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02009200 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009201
Gleb Natapov716d51a2012-09-03 15:24:26 +03009202 run->exit_reason = KVM_EXIT_MMIO;
9203 run->mmio.phys_addr = frag->gpa;
9204 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08009205 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
9206 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03009207 run->mmio.is_write = vcpu->mmio_is_write;
9208 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
9209 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02009210}
9211
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009212static void kvm_save_current_fpu(struct fpu *fpu)
9213{
9214 /*
9215 * If the target FPU state is not resident in the CPU registers, just
9216 * memcpy() from current, else save CPU state directly to the target.
9217 */
9218 if (test_thread_flag(TIF_NEED_FPU_LOAD))
9219 memcpy(&fpu->state, &current->thread.fpu.state,
9220 fpu_kernel_xstate_size);
9221 else
9222 copy_fpregs_to_fpstate(fpu);
9223}
9224
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009225/* Swap (qemu) user FPU context for the guest FPU context. */
9226static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
9227{
Rik van Riel5f409e22019-04-03 18:41:52 +02009228 fpregs_lock();
9229
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009230 kvm_save_current_fpu(vcpu->arch.user_fpu);
9231
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009232 /* PKRU is separately restored in kvm_x86_ops.run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08009233 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009234 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02009235
9236 fpregs_mark_activate();
9237 fpregs_unlock();
9238
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009239 trace_kvm_fpu(1);
9240}
9241
9242/* When vcpu_run ends, restore user space FPU context. */
9243static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
9244{
Rik van Riel5f409e22019-04-03 18:41:52 +02009245 fpregs_lock();
9246
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08009247 kvm_save_current_fpu(vcpu->arch.guest_fpu);
9248
Wanpeng Lid9a710e2019-07-22 12:26:21 +08009249 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02009250
9251 fpregs_mark_activate();
9252 fpregs_unlock();
9253
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02009254 ++vcpu->stat.fpu_reload;
9255 trace_kvm_fpu(0);
9256}
9257
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009258int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009259{
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009260 struct kvm_run *kvm_run = vcpu->run;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009261 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009262
Christoffer Dallaccb7572017-12-04 21:35:25 +01009263 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01009264 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01009265 kvm_load_guest_fpu(vcpu);
9266
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009267 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02009268 if (kvm_run->immediate_exit) {
9269 r = -EINTR;
9270 goto out;
9271 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009272 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01009273 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02009274 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009275 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02009276 if (signal_pending(current)) {
9277 r = -EINTR;
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009278 kvm_run->exit_reason = KVM_EXIT_INTR;
Jan H. Schönherra0595002017-09-06 00:27:19 +02009279 ++vcpu->stat.signal_exits;
9280 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03009281 goto out;
9282 }
9283
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009284 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
Ken Hofsass01643c52018-01-31 16:03:36 -08009285 r = -EINVAL;
9286 goto out;
9287 }
9288
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009289 if (kvm_run->kvm_dirty_regs) {
Ken Hofsass01643c52018-01-31 16:03:36 -08009290 r = sync_regs(vcpu);
9291 if (r != 0)
9292 goto out;
9293 }
9294
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009295 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02009296 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01009297 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
9298 r = -EINVAL;
9299 goto out;
9300 }
9301 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009302
Gleb Natapov716d51a2012-09-03 15:24:26 +03009303 if (unlikely(vcpu->arch.complete_userspace_io)) {
9304 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
9305 vcpu->arch.complete_userspace_io = NULL;
9306 r = cui(vcpu);
9307 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01009308 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03009309 } else
9310 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02009311
Paolo Bonzini460df4c2017-02-08 11:50:15 +01009312 if (kvm_run->immediate_exit)
9313 r = -EINTR;
9314 else
9315 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009316
9317out:
Peter Xu5663d8f2017-12-12 17:15:02 +01009318 kvm_put_guest_fpu(vcpu);
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08009319 if (kvm_run->kvm_valid_regs)
Ken Hofsass01643c52018-01-31 16:03:36 -08009320 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03009321 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01009322 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009323
Christoffer Dallaccb7572017-12-04 21:35:25 +01009324 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009325 return r;
9326}
9327
Ken Hofsass01643c52018-01-31 16:03:36 -08009328static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009329{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009330 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
9331 /*
9332 * We are here if userspace calls get_regs() in the middle of
9333 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08009334 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009335 * that usually, but some bad designed PV devices (vmware
9336 * backdoor interface) need this to work
9337 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009338 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009339 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
9340 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07009341 regs->rax = kvm_rax_read(vcpu);
9342 regs->rbx = kvm_rbx_read(vcpu);
9343 regs->rcx = kvm_rcx_read(vcpu);
9344 regs->rdx = kvm_rdx_read(vcpu);
9345 regs->rsi = kvm_rsi_read(vcpu);
9346 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02009347 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07009348 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009349#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07009350 regs->r8 = kvm_r8_read(vcpu);
9351 regs->r9 = kvm_r9_read(vcpu);
9352 regs->r10 = kvm_r10_read(vcpu);
9353 regs->r11 = kvm_r11_read(vcpu);
9354 regs->r12 = kvm_r12_read(vcpu);
9355 regs->r13 = kvm_r13_read(vcpu);
9356 regs->r14 = kvm_r14_read(vcpu);
9357 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009358#endif
9359
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009360 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02009361 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08009362}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009363
Ken Hofsass01643c52018-01-31 16:03:36 -08009364int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
9365{
9366 vcpu_load(vcpu);
9367 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01009368 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009369 return 0;
9370}
9371
Ken Hofsass01643c52018-01-31 16:03:36 -08009372static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009373{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02009374 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
9375 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
9376
Sean Christophersonde3cd112019-04-30 10:36:17 -07009377 kvm_rax_write(vcpu, regs->rax);
9378 kvm_rbx_write(vcpu, regs->rbx);
9379 kvm_rcx_write(vcpu, regs->rcx);
9380 kvm_rdx_write(vcpu, regs->rdx);
9381 kvm_rsi_write(vcpu, regs->rsi);
9382 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02009383 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07009384 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009385#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07009386 kvm_r8_write(vcpu, regs->r8);
9387 kvm_r9_write(vcpu, regs->r9);
9388 kvm_r10_write(vcpu, regs->r10);
9389 kvm_r11_write(vcpu, regs->r11);
9390 kvm_r12_write(vcpu, regs->r12);
9391 kvm_r13_write(vcpu, regs->r13);
9392 kvm_r14_write(vcpu, regs->r14);
9393 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009394#endif
9395
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009396 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08009397 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009398
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02009399 vcpu->arch.exception.pending = false;
9400
Avi Kivity3842d132010-07-27 12:30:24 +03009401 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08009402}
Avi Kivity3842d132010-07-27 12:30:24 +03009403
Ken Hofsass01643c52018-01-31 16:03:36 -08009404int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
9405{
9406 vcpu_load(vcpu);
9407 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01009408 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009409 return 0;
9410}
9411
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009412void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
9413{
9414 struct kvm_segment cs;
9415
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009416 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009417 *db = cs.db;
9418 *l = cs.l;
9419}
9420EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
9421
Ken Hofsass01643c52018-01-31 16:03:36 -08009422static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009423{
Gleb Natapov89a27f42010-02-16 10:51:48 +02009424 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009425
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009426 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9427 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9428 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9429 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9430 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9431 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009432
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009433 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9434 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009435
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009436 kvm_x86_ops.get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009437 sregs->idt.limit = dt.size;
9438 sregs->idt.base = dt.address;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009439 kvm_x86_ops.get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009440 sregs->gdt.limit = dt.size;
9441 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009442
Avi Kivity4d4ec082009-12-29 18:07:30 +02009443 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009444 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009445 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02009446 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009447 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02009448 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009449 sregs->apic_base = kvm_get_apic_base(vcpu);
9450
Jordan Borgner0e96f312018-10-28 12:58:28 +00009451 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009452
Liran Alon04140b42018-03-23 03:01:31 +03009453 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03009454 set_bit(vcpu->arch.interrupt.nr,
9455 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08009456}
Gleb Natapov16d7a192009-04-21 17:45:10 +03009457
Ken Hofsass01643c52018-01-31 16:03:36 -08009458int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
9459 struct kvm_sregs *sregs)
9460{
9461 vcpu_load(vcpu);
9462 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01009463 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009464 return 0;
9465}
9466
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009467int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
9468 struct kvm_mp_state *mp_state)
9469{
Christoffer Dallfd232562017-12-04 21:35:30 +01009470 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08009471 if (kvm_mpx_supported())
9472 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01009473
Jan Kiszka66450a22013-03-13 12:42:34 +01009474 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05309475 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
9476 vcpu->arch.pv.pv_unhalted)
9477 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
9478 else
9479 mp_state->mp_state = vcpu->arch.mp_state;
9480
Sean Christophersonf958bd22019-12-09 12:19:31 -08009481 if (kvm_mpx_supported())
9482 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01009483 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009484 return 0;
9485}
9486
9487int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
9488 struct kvm_mp_state *mp_state)
9489{
Christoffer Dalle83dff52017-12-04 21:35:31 +01009490 int ret = -EINVAL;
9491
9492 vcpu_load(vcpu);
9493
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009494 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01009495 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01009496 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01009497
Liran Alon27cbe7d2019-11-11 11:16:40 +02009498 /*
9499 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
9500 * INIT state; latched init should be reported using
9501 * KVM_SET_VCPU_EVENTS, so reject it here.
9502 */
9503 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01009504 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
9505 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01009506 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01009507
Jan Kiszka66450a22013-03-13 12:42:34 +01009508 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
9509 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
9510 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
9511 } else
9512 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03009513 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01009514
9515 ret = 0;
9516out:
9517 vcpu_put(vcpu);
9518 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03009519}
9520
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009521int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
9522 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009523{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009524 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009525 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009526
Mohammed Gamal8ec47222010-08-16 00:47:01 +03009527 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02009528
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009529 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009530 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07009531 if (ret) {
9532 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9533 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
9534 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009535 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07009536 }
Gleb Natapovc6975182010-02-18 12:15:01 +02009537
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009538 kvm_rip_write(vcpu, ctxt->eip);
9539 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009540 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02009541}
9542EXPORT_SYMBOL_GPL(kvm_task_switch);
9543
Sean Christophersonee69c922020-10-06 18:44:16 -07009544static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05009545{
Tianyu Lan37b95952018-01-16 17:34:07 +08009546 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05009547 /*
9548 * When EFER.LME and CR0.PG are set, the processor is in
9549 * 64-bit mode (though maybe in a 32-bit code segment).
9550 * CR4.PAE and EFER.LMA must be set.
9551 */
Sean Christophersonee69c922020-10-06 18:44:16 -07009552 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA))
9553 return false;
Lan Tianyuf2981032017-12-14 03:01:52 -05009554 } else {
9555 /*
9556 * Not in 64-bit mode: EFER.LMA is clear and the code
9557 * segment cannot be 64-bit.
9558 */
9559 if (sregs->efer & EFER_LMA || sregs->cs.l)
Sean Christophersonee69c922020-10-06 18:44:16 -07009560 return false;
Lan Tianyuf2981032017-12-14 03:01:52 -05009561 }
9562
Sean Christophersonee69c922020-10-06 18:44:16 -07009563 return kvm_is_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05009564}
9565
Ken Hofsass01643c52018-01-31 16:03:36 -08009566static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009567{
Jan Kiszka58cb6282014-01-24 16:48:44 +01009568 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009569 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05009570 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009571 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02009572 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009573 int ret = -EINVAL;
9574
Sean Christophersonee69c922020-10-06 18:44:16 -07009575 if (!kvm_is_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009576 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009577
Jim Mattsond3802282017-08-10 10:14:13 -07009578 apic_base_msr.data = sregs->apic_base;
9579 apic_base_msr.host_initiated = true;
9580 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009581 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009582
Gleb Natapov89a27f42010-02-16 10:51:48 +02009583 dt.size = sregs->idt.limit;
9584 dt.address = sregs->idt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009585 kvm_x86_ops.set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009586 dt.size = sregs->gdt.limit;
9587 dt.address = sregs->gdt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009588 kvm_x86_ops.set_gdt(vcpu, &dt);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009589
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009590 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009591 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009592 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009593 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009594
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009595 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009596
Avi Kivityf6801df2010-01-21 15:31:50 +02009597 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009598 kvm_x86_ops.set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009599
Avi Kivity4d4ec082009-12-29 18:07:30 +02009600 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009601 kvm_x86_ops.set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009602 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009603
Avi Kivityfc78f512009-12-07 12:16:48 +02009604 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009605 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9606 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009607 kvm_x86_ops.set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009608 if (cpuid_update_needed)
Xiaoyao Liaedbaf42020-07-09 12:34:23 +08009609 kvm_update_cpuid_runtime(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009610
9611 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009612 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009613 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009614 mmu_reset_needed = 1;
9615 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009616 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009617
9618 if (mmu_reset_needed)
9619 kvm_mmu_reset_context(vcpu);
9620
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009621 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009622 pending_vec = find_first_bit(
9623 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9624 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009625 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009626 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009627 }
9628
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009629 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9630 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9631 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9632 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9633 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9634 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009635
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009636 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9637 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009638
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009639 update_cr8_intercept(vcpu);
9640
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009641 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009642 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009643 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009644 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009645 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9646
Avi Kivity3842d132010-07-27 12:30:24 +03009647 kvm_make_request(KVM_REQ_EVENT, vcpu);
9648
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009649 ret = 0;
9650out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009651 return ret;
9652}
9653
9654int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9655 struct kvm_sregs *sregs)
9656{
9657 int ret;
9658
9659 vcpu_load(vcpu);
9660 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009661 vcpu_put(vcpu);
9662 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009663}
9664
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009665int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9666 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009667{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009668 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009669 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009670
Christoffer Dall66b56562017-12-04 21:35:33 +01009671 vcpu_load(vcpu);
9672
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009673 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9674 r = -EBUSY;
9675 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009676 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009677 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9678 kvm_queue_exception(vcpu, DB_VECTOR);
9679 else
9680 kvm_queue_exception(vcpu, BP_VECTOR);
9681 }
9682
Jan Kiszka91586a32009-10-05 13:07:21 +02009683 /*
9684 * Read rflags as long as potentially injected trace flags are still
9685 * filtered out.
9686 */
9687 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009688
9689 vcpu->guest_debug = dbg->control;
9690 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9691 vcpu->guest_debug = 0;
9692
9693 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009694 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9695 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009696 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009697 } else {
9698 for (i = 0; i < KVM_NR_DB_REGS; i++)
9699 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009700 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009701 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009702
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009703 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9704 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9705 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009706
Jan Kiszka91586a32009-10-05 13:07:21 +02009707 /*
9708 * Trigger an rflags update that will inject or remove the trace
9709 * flags.
9710 */
9711 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009712
Paolo Bonzini69869822020-07-10 17:48:06 +02009713 kvm_x86_ops.update_exception_bitmap(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009714
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009715 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009716
Avi Kivity2122ff52010-05-13 11:25:04 +03009717out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009718 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009719 return r;
9720}
9721
9722/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009723 * Translate a guest virtual address to a guest physical address.
9724 */
9725int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9726 struct kvm_translation *tr)
9727{
9728 unsigned long vaddr = tr->linear_address;
9729 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009730 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009731
Christoffer Dall1da5b612017-12-04 21:35:32 +01009732 vcpu_load(vcpu);
9733
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009734 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009735 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009736 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009737 tr->physical_address = gpa;
9738 tr->valid = gpa != UNMAPPED_GVA;
9739 tr->writeable = 1;
9740 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009741
Christoffer Dall1da5b612017-12-04 21:35:32 +01009742 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009743 return 0;
9744}
9745
Hollis Blanchardd0752062007-10-31 17:24:25 -05009746int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9747{
Christoffer Dall13931232017-12-04 21:35:34 +01009748 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009749
Christoffer Dall13931232017-12-04 21:35:34 +01009750 vcpu_load(vcpu);
9751
Marc Orrb666a4b2018-11-06 14:53:56 -08009752 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009753 memcpy(fpu->fpr, fxsave->st_space, 128);
9754 fpu->fcw = fxsave->cwd;
9755 fpu->fsw = fxsave->swd;
9756 fpu->ftwx = fxsave->twd;
9757 fpu->last_opcode = fxsave->fop;
9758 fpu->last_ip = fxsave->rip;
9759 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009760 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009761
Christoffer Dall13931232017-12-04 21:35:34 +01009762 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009763 return 0;
9764}
9765
9766int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9767{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009768 struct fxregs_state *fxsave;
9769
9770 vcpu_load(vcpu);
9771
Marc Orrb666a4b2018-11-06 14:53:56 -08009772 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009773
Hollis Blanchardd0752062007-10-31 17:24:25 -05009774 memcpy(fxsave->st_space, fpu->fpr, 128);
9775 fxsave->cwd = fpu->fcw;
9776 fxsave->swd = fpu->fsw;
9777 fxsave->twd = fpu->ftwx;
9778 fxsave->fop = fpu->last_opcode;
9779 fxsave->rip = fpu->last_ip;
9780 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009781 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009782
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009783 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009784 return 0;
9785}
9786
Ken Hofsass01643c52018-01-31 16:03:36 -08009787static void store_regs(struct kvm_vcpu *vcpu)
9788{
9789 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9790
9791 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9792 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9793
9794 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9795 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9796
9797 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9798 kvm_vcpu_ioctl_x86_get_vcpu_events(
9799 vcpu, &vcpu->run->s.regs.events);
9800}
9801
9802static int sync_regs(struct kvm_vcpu *vcpu)
9803{
9804 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9805 return -EINVAL;
9806
9807 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9808 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9809 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9810 }
9811 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9812 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9813 return -EINVAL;
9814 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9815 }
9816 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9817 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9818 vcpu, &vcpu->run->s.regs.events))
9819 return -EINVAL;
9820 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9821 }
9822
9823 return 0;
9824}
9825
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009826static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009827{
Marc Orrb666a4b2018-11-06 14:53:56 -08009828 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009829 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009830 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009831 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009832
Dexuan Cui2acf9232010-06-10 11:27:12 +08009833 /*
9834 * Ensure guest xcr0 is valid for loading
9835 */
Dave Hansend91cab72015-09-02 16:31:26 -07009836 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009837
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009838 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009839}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009840
Sean Christopherson897cc382019-12-18 13:55:09 -08009841int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009842{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009843 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009844 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9845 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009846
Sean Christopherson897cc382019-12-18 13:55:09 -08009847 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009848}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009849
Sean Christophersone529ef62019-12-18 13:55:15 -08009850int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009851{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009852 struct page *page;
9853 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009854
Sean Christopherson95a0d012019-12-18 13:55:23 -08009855 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9856 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9857 else
9858 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9859
9860 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9861
9862 r = kvm_mmu_create(vcpu);
9863 if (r < 0)
9864 return r;
9865
9866 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009867 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9868 if (r < 0)
9869 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009870 if (kvm_apicv_activated(vcpu->kvm))
9871 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009872 } else
9873 static_key_slow_inc(&kvm_no_apic_vcpu);
9874
9875 r = -ENOMEM;
9876
9877 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9878 if (!page)
9879 goto fail_free_lapic;
9880 vcpu->arch.pio_data = page_address(page);
9881
9882 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9883 GFP_KERNEL_ACCOUNT);
9884 if (!vcpu->arch.mce_banks)
9885 goto fail_free_pio_data;
9886 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9887
9888 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9889 GFP_KERNEL_ACCOUNT))
9890 goto fail_free_mce_banks;
9891
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009892 if (!alloc_emulate_ctxt(vcpu))
9893 goto free_wbinvd_dirty_mask;
9894
Sean Christopherson95a0d012019-12-18 13:55:23 -08009895 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9896 GFP_KERNEL_ACCOUNT);
9897 if (!vcpu->arch.user_fpu) {
9898 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009899 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009900 }
9901
9902 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9903 GFP_KERNEL_ACCOUNT);
9904 if (!vcpu->arch.guest_fpu) {
9905 pr_err("kvm: failed to allocate vcpu's fpu\n");
9906 goto free_user_fpu;
9907 }
9908 fx_init(vcpu);
9909
Sean Christopherson95a0d012019-12-18 13:55:23 -08009910 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9911
9912 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9913
9914 kvm_async_pf_hash_reset(vcpu);
9915 kvm_pmu_init(vcpu);
9916
9917 vcpu->arch.pending_external_vector = -1;
9918 vcpu->arch.preempted_in_kernel = false;
9919
9920 kvm_hv_vcpu_init(vcpu);
9921
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009922 r = kvm_x86_ops.vcpu_create(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009923 if (r)
9924 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009925
Sean Christopherson0cf91352019-03-07 15:43:02 -08009926 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009927 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009928 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009929 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009930 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009931 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009932 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009933 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009934
9935free_guest_fpu:
9936 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9937free_user_fpu:
9938 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009939free_emulate_ctxt:
9940 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009941free_wbinvd_dirty_mask:
9942 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9943fail_free_mce_banks:
9944 kfree(vcpu->arch.mce_banks);
9945fail_free_pio_data:
9946 free_page((unsigned long)vcpu->arch.pio_data);
9947fail_free_lapic:
9948 kvm_free_lapic(vcpu);
9949fail_mmu_destroy:
9950 kvm_mmu_destroy(vcpu);
9951 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009952}
9953
Dominik Dingel31928aa2014-12-04 15:47:07 +01009954void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009955{
Andrew Jones332967a2014-02-28 12:52:55 +01009956 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009957
Roman Kagand3457c82017-07-14 17:13:20 +03009958 kvm_hv_vcpu_postcreate(vcpu);
9959
Christoffer Dallec7660c2017-12-04 21:35:23 +01009960 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009961 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009962 vcpu_load(vcpu);
Paolo Bonzini0c899c22020-09-24 14:45:27 +02009963 kvm_synchronize_tsc(vcpu, 0);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009964 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009965
9966 /* poll control enabled by default */
9967 vcpu->arch.msr_kvm_poll_control = 1;
9968
Christoffer Dallec7660c2017-12-04 21:35:23 +01009969 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009970
Wanpeng Lib34de572020-02-28 11:18:41 +08009971 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9972 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9973 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009974}
9975
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009976void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009977{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009978 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009979 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009980
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009981 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9982
Sean Christopherson50b143e2019-12-18 13:55:07 -08009983 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009984
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009985 kvm_x86_ops.vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009986
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009987 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009988 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9989 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9990 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009991
9992 kvm_hv_vcpu_uninit(vcpu);
9993 kvm_pmu_destroy(vcpu);
9994 kfree(vcpu->arch.mce_banks);
9995 kvm_free_lapic(vcpu);
9996 idx = srcu_read_lock(&vcpu->kvm->srcu);
9997 kvm_mmu_destroy(vcpu);
9998 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9999 free_page((unsigned long)vcpu->arch.pio_data);
Vitaly Kuznetsov255cbec2020-10-01 15:05:40 +020010000 kvfree(vcpu->arch.cpuid_entries);
Sean Christopherson95a0d012019-12-18 13:55:23 -080010001 if (!lapic_in_kernel(vcpu))
10002 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010003}
10004
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010005void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010006{
Radim Krčmářb7e31be2018-03-01 15:24:25 +010010007 kvm_lapic_reset(vcpu, init_event);
10008
Paolo Bonzinie69fab52015-06-04 10:44:44 +020010009 vcpu->arch.hflags = 0;
10010
Paolo Bonzinic43203c2016-06-01 22:26:00 +020010011 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +020010012 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +030010013 atomic_set(&vcpu->arch.nmi_queued, 0);
10014 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +020010015 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +030010016 kvm_clear_interrupt_queue(vcpu);
10017 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +020010018
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010019 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +030010020 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +030010021 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010022 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +020010023 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +010010024
Nadav Amit11190222015-04-02 03:10:38 +030010025 vcpu->arch.cr2 = 0;
10026
Avi Kivity3842d132010-07-27 12:30:24 +030010027 kvm_make_request(KVM_REQ_EVENT, vcpu);
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010028 vcpu->arch.apf.msr_en_val = 0;
10029 vcpu->arch.apf.msr_int_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -040010030 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +030010031
Glauber Costa12f9a482011-02-01 14:16:40 -050010032 kvmclock_reset(vcpu);
10033
Gleb Natapovaf585b92010-10-14 11:22:46 +020010034 kvm_clear_async_pf_completion_queue(vcpu);
10035 kvm_async_pf_hash_reset(vcpu);
10036 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010037
Wanpeng Lia554d202017-10-11 05:10:19 -070010038 if (kvm_mpx_supported()) {
10039 void *mpx_state_buffer;
10040
10041 /*
10042 * To avoid have the INIT path from kvm_apic_has_events() that be
10043 * called with loaded FPU and does not let userspace fix the state.
10044 */
Rik van Rielf775b132017-11-14 16:54:23 -050010045 if (init_event)
10046 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -080010047 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +020010048 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -070010049 if (mpx_state_buffer)
10050 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -080010051 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +020010052 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -070010053 if (mpx_state_buffer)
10054 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -050010055 if (init_event)
10056 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -070010057 }
10058
Paolo Bonzini64d60672015-05-07 11:36:11 +020010059 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +030010060 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +020010061 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -070010062
Kyle Hueydb2336a2017-03-20 01:16:28 -070010063 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -070010064
10065 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +020010066 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020010067
Julian Stecklina66f7b722012-12-05 15:26:19 +010010068 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
10069 vcpu->arch.regs_avail = ~0;
10070 vcpu->arch.regs_dirty = ~0;
10071
Wanpeng Lia554d202017-10-11 05:10:19 -070010072 vcpu->arch.ia32_xss = 0;
10073
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010074 kvm_x86_ops.vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010075}
10076
Paolo Bonzini2b4a2732014-11-24 14:35:24 +010010077void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +010010078{
10079 struct kvm_segment cs;
10080
10081 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
10082 cs.selector = vector << 8;
10083 cs.base = vector << 12;
10084 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
10085 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010086}
10087
Radim Krčmář13a34e02014-08-28 15:13:03 +020010088int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010089{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -100010090 struct kvm *kvm;
10091 struct kvm_vcpu *vcpu;
10092 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010093 int ret;
10094 u64 local_tsc;
10095 u64 max_tsc = 0;
10096 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +030010097
Sean Christopherson7e34fbd2020-09-23 11:03:55 -070010098 kvm_user_return_msr_cpu_online();
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010099 ret = kvm_x86_ops.hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010100 if (ret != 0)
10101 return ret;
10102
Andy Lutomirski4ea16362015-06-25 18:44:07 +020010103 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010010104 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010105 list_for_each_entry(kvm, &vm_list, vm_list) {
10106 kvm_for_each_vcpu(i, vcpu, kvm) {
10107 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +080010108 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010109 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
10110 backwards_tsc = true;
10111 if (vcpu->arch.last_host_tsc > max_tsc)
10112 max_tsc = vcpu->arch.last_host_tsc;
10113 }
10114 }
10115 }
10116
10117 /*
10118 * Sometimes, even reliable TSCs go backwards. This happens on
10119 * platforms that reset TSC during suspend or hibernate actions, but
10120 * maintain synchronization. We must compensate. Fortunately, we can
10121 * detect that condition here, which happens early in CPU bringup,
10122 * before any KVM threads can be running. Unfortunately, we can't
10123 * bring the TSCs fully up to date with real time, as we aren't yet far
10124 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020010125 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010126 * variables that haven't been updated yet.
10127 *
10128 * So we simply find the maximum observed TSC above, then record the
10129 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
10130 * the adjustment will be applied. Note that we accumulate
10131 * adjustments, in case multiple suspend cycles happen before some VCPU
10132 * gets a chance to run again. In the event that no KVM threads get a
10133 * chance to run, we will miss the entire elapsed period, as we'll have
10134 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
10135 * loose cycle time. This isn't too big a deal, since the loss will be
10136 * uniform across all VCPUs (not to mention the scenario is extremely
10137 * unlikely). It is possible that a second hibernate recovery happens
10138 * much faster than a first, causing the observed TSC here to be
10139 * smaller; this would require additional padding adjustment, which is
10140 * why we set last_host_tsc to the local tsc observed here.
10141 *
10142 * N.B. - this code below runs only on platforms with reliable TSC,
10143 * as that is the only way backwards_tsc is set above. Also note
10144 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
10145 * have the same delta_cyc adjustment applied if backwards_tsc
10146 * is detected. Note further, this adjustment is only done once,
10147 * as we reset last_host_tsc on all VCPUs to stop this from being
10148 * called multiple times (one for each physical CPU bringup).
10149 *
Guo Chao4a969982012-06-28 15:17:27 +080010150 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010151 * will be compensated by the logic in vcpu_load, which sets the TSC to
10152 * catchup mode. This will catchup all VCPUs to real time, but cannot
10153 * guarantee that they stay in perfect synchronization.
10154 */
10155 if (backwards_tsc) {
10156 u64 delta_cyc = max_tsc - local_tsc;
10157 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +020010158 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010159 kvm_for_each_vcpu(i, vcpu, kvm) {
10160 vcpu->arch.tsc_offset_adjustment += delta_cyc;
10161 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +080010162 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -020010163 }
10164
10165 /*
10166 * We have to disable TSC offset matching.. if you were
10167 * booting a VM while issuing an S4 host suspend....
10168 * you may have some problem. Solving this issue is
10169 * left as an exercise to the reader.
10170 */
10171 kvm->arch.last_tsc_nsec = 0;
10172 kvm->arch.last_tsc_write = 0;
10173 }
10174
10175 }
10176 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010177}
10178
Radim Krčmář13a34e02014-08-28 15:13:03 +020010179void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010180{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010181 kvm_x86_ops.hardware_disable();
Radim Krčmář13a34e02014-08-28 15:13:03 +020010182 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010183}
10184
Sean Christophersonb9904082020-03-21 13:25:55 -070010185int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010186{
Sean Christophersond008dfd2020-03-21 13:25:56 -070010187 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030010188 int r;
10189
Sean Christopherson91661982020-03-02 15:57:06 -080010190 rdmsrl_safe(MSR_EFER, &host_efer);
10191
Paolo Bonzini408e9a32020-03-05 16:11:56 +010010192 if (boot_cpu_has(X86_FEATURE_XSAVES))
10193 rdmsrl(MSR_IA32_XSS, host_xss);
10194
Sean Christophersond008dfd2020-03-21 13:25:56 -070010195 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030010196 if (r != 0)
10197 return r;
10198
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010199 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
Sean Christopherson69c6f692020-03-21 13:25:59 -070010200
Paolo Bonzini408e9a32020-03-05 16:11:56 +010010201 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
10202 supported_xss = 0;
10203
Paolo Bonzini139f7422020-05-05 09:40:46 -040010204#define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f)
10205 cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_);
10206#undef __kvm_cpu_cap_has
Sean Christophersonb11306b2019-12-10 14:44:13 -080010207
Haozhong Zhang35181e82015-10-20 15:39:03 +080010208 if (kvm_has_tsc_control) {
10209 /*
10210 * Make sure the user can only configure tsc_khz values that
10211 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -030010212 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +080010213 * be 1 on all machines.
10214 */
10215 u64 max = min(0x7fffffffULL,
10216 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
10217 kvm_max_guest_tsc_khz = max;
10218
Haozhong Zhangad7218832015-10-20 15:39:02 +080010219 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +080010220 }
Haozhong Zhangad7218832015-10-20 15:39:02 +080010221
Nadav Amit9e9c3fe2015-04-12 21:47:15 +030010222 kvm_init_msr_list();
10223 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010224}
10225
10226void kvm_arch_hardware_unsetup(void)
10227{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010228 kvm_x86_ops.hardware_unsetup();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010229}
10230
Sean Christophersonb9904082020-03-21 13:25:55 -070010231int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010232{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080010233 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -070010234 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080010235
10236 WARN_ON(!irqs_disabled());
10237
Paolo Bonzini139f7422020-05-05 09:40:46 -040010238 if (__cr4_reserved_bits(cpu_has, c) !=
10239 __cr4_reserved_bits(cpu_has, &boot_cpu_data))
Sean Christophersonf1cdecf2019-12-10 14:44:14 -080010240 return -EIO;
10241
Sean Christophersond008dfd2020-03-21 13:25:56 -070010242 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080010243}
10244
Paolo Bonzinid71ba782015-07-29 11:56:48 +020010245bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
10246{
10247 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
10248}
10249EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
10250
10251bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
10252{
10253 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
10254}
10255
Gleb Natapov54e98182012-08-05 15:58:32 +030010256struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +010010257EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +030010258
Radim Krčmáře790d9e2014-08-21 18:08:05 +020010259void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
10260{
Like Xub35e5542019-10-27 18:52:43 +080010261 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
10262
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010263 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +080010264 if (pmu->version && unlikely(pmu->event_count)) {
10265 pmu->need_cleanup = true;
10266 kvm_make_request(KVM_REQ_PMU, vcpu);
10267 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010268 kvm_x86_ops.sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +020010269}
10270
Sean Christopherson562b6b02020-01-26 16:41:13 -080010271void kvm_arch_free_vm(struct kvm *kvm)
10272{
10273 kfree(kvm->arch.hyperv.hv_pa_pg);
10274 vfree(kvm);
10275}
10276
10277
Carsten Ottee08b9632012-01-04 10:25:20 +010010278int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010279{
Carsten Ottee08b9632012-01-04 10:25:20 +010010280 if (type)
10281 return -EINVAL;
10282
Paolo Bonzini6ef768f2014-11-20 13:45:31 +010010283 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +080010284 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -070010285 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +010010286 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030010287 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010288 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010289
Sheng Yang5550af42008-10-15 20:15:06 +080010290 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
10291 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -060010292 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
10293 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
10294 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +080010295
Jan Kiszka038f8c12011-02-04 10:49:11 +010010296 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +030010297 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -020010298 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
10299
Paolo Bonzini8171cd62020-01-22 14:36:09 +010010300 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -020010301 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +010010302
Drew Schmitt6fbbde92018-08-20 10:32:15 -070010303 kvm->arch.guest_can_read_msr_platform_info = true;
10304
Andrew Jones7e44e442014-02-28 12:52:54 +010010305 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +010010306 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +010010307
Roman Kagancbc02362018-02-01 16:48:31 +030010308 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +080010309 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +080010310 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +080010311
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010312 return kvm_x86_ops.vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010313}
10314
Junaid Shahid1aa9b952019-11-04 20:26:00 +010010315int kvm_arch_post_init_vm(struct kvm *kvm)
10316{
10317 return kvm_mmu_post_init_vm(kvm);
10318}
10319
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010320static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
10321{
Christoffer Dallec7660c2017-12-04 21:35:23 +010010322 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010323 kvm_mmu_unload(vcpu);
10324 vcpu_put(vcpu);
10325}
10326
10327static void kvm_free_vcpus(struct kvm *kvm)
10328{
10329 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010330 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010331
10332 /*
10333 * Unpin any mmu pages first.
10334 */
Gleb Natapovaf585b92010-10-14 11:22:46 +020010335 kvm_for_each_vcpu(i, vcpu, kvm) {
10336 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010337 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010338 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010339 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -080010340 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010341
Gleb Natapov988a2ca2009-06-09 15:56:29 +030010342 mutex_lock(&kvm->lock);
10343 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
10344 kvm->vcpus[i] = NULL;
10345
10346 atomic_set(&kvm->online_vcpus, 0);
10347 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010348}
10349
Sheng Yangad8ba2c2009-01-06 10:03:02 +080010350void kvm_arch_sync_events(struct kvm *kvm)
10351{
Andrew Jones332967a2014-02-28 12:52:55 +010010352 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +010010353 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +080010354 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +080010355}
10356
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010357int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010358{
10359 int i, r;
Kees Cook3f649ab2020-06-03 13:09:38 -070010360 unsigned long hva, old_npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010361 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010362 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010363
10364 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010365 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
10366 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010367
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010368 slot = id_to_memslot(slots, id);
10369 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010370 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010371 return -EEXIST;
10372
10373 /*
10374 * MAP_SHARED to prevent internal slot pages from being moved
10375 * by fork()/COW.
10376 */
10377 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
10378 MAP_SHARED | MAP_ANONYMOUS, 0);
10379 if (IS_ERR((void *)hva))
10380 return PTR_ERR((void *)hva);
10381 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010382 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010383 return 0;
10384
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010385 old_npages = slot->npages;
Paolo Bonzinie0135a12020-06-11 14:01:51 -040010386 hva = 0;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010387 }
10388
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010389 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010390 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010391
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010392 m.slot = id | (i << 16);
10393 m.flags = 0;
10394 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010395 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +020010396 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010397 r = __kvm_set_memory_region(kvm, &m);
10398 if (r < 0)
10399 return r;
10400 }
10401
Eric Biggers103c7632018-01-31 17:30:21 -080010402 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -080010403 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +020010404
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +020010405 return 0;
10406}
10407EXPORT_SYMBOL_GPL(__x86_set_memory_region);
10408
Junaid Shahid1aa9b952019-11-04 20:26:00 +010010409void kvm_arch_pre_destroy_vm(struct kvm *kvm)
10410{
10411 kvm_mmu_pre_destroy_vm(kvm);
10412}
10413
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010414void kvm_arch_destroy_vm(struct kvm *kvm)
10415{
Alexander Graf1a1552542020-09-25 16:34:21 +020010416 u32 i;
10417
Andrew Honig27469d22013-04-18 09:38:14 -070010418 if (current->mm == kvm->mm) {
10419 /*
10420 * Free memory regions allocated on behalf of userspace,
10421 * unless the the memory map has changed due to process exit
10422 * or fd copying.
10423 */
Peter Xu6a3c6232020-01-09 09:57:16 -050010424 mutex_lock(&kvm->slots_lock);
10425 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
10426 0, 0);
10427 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
10428 0, 0);
10429 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
10430 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -070010431 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010432 if (kvm_x86_ops.vm_destroy)
10433 kvm_x86_ops.vm_destroy(kvm);
Alexander Graf1a1552542020-09-25 16:34:21 +020010434 for (i = 0; i < kvm->arch.msr_filter.count; i++)
10435 kfree(kvm->arch.msr_filter.ranges[i].bitmap);
Peter Xuc7611592017-03-15 16:01:19 +080010436 kvm_pic_destroy(kvm);
10437 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010438 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +020010439 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -070010440 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +080010441 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +020010442 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +030010443 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +080010444}
Zhang Xiantao0de10342007-11-20 16:25:04 +080010445
Sean Christophersone96c81e2020-02-18 13:07:27 -080010446void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010447{
10448 int i;
10449
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010450 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -080010451 kvfree(slot->arch.rmap[i]);
10452 slot->arch.rmap[i] = NULL;
10453
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010454 if (i == 0)
10455 continue;
10456
Sean Christophersone96c81e2020-02-18 13:07:27 -080010457 kvfree(slot->arch.lpage_info[i - 1]);
10458 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010459 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080010460
Sean Christophersone96c81e2020-02-18 13:07:27 -080010461 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010462}
10463
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010464static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
10465 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010466{
10467 int i;
10468
Sean Christophersonedd4fa32020-02-18 13:07:15 -080010469 /*
10470 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
10471 * old arrays will be freed by __kvm_set_memory_region() if installing
10472 * the new memslot is successful.
10473 */
10474 memset(&slot->arch, 0, sizeof(slot->arch));
10475
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010476 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010477 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010478 unsigned long ugfn;
10479 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010480 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010481
10482 lpages = gfn_to_index(slot->base_gfn + npages - 1,
10483 slot->base_gfn, level) + 1;
10484
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010485 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -070010486 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -080010487 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010488 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010489 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010490 if (i == 0)
10491 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +090010492
Ben Gardon254272c2019-02-11 11:02:50 -080010493 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010494 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010495 goto out_free;
10496
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010497 slot->arch.lpage_info[i - 1] = linfo;
10498
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010499 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010500 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010501 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010502 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010503 ugfn = slot->userspace_addr >> PAGE_SHIFT;
10504 /*
10505 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -080010506 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010507 */
Sean Christopherson600087b2020-03-02 15:57:05 -080010508 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010509 unsigned long j;
10510
10511 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +080010512 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010513 }
10514 }
10515
Xiao Guangrong21ebbed2016-02-24 17:51:09 +080010516 if (kvm_page_track_create_memslot(slot, npages))
10517 goto out_free;
10518
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010519 return 0;
10520
10521out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010522 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +010010523 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010524 slot->arch.rmap[i] = NULL;
10525 if (i == 0)
10526 continue;
10527
Thomas Huth548ef282015-02-24 21:29:25 +010010528 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010529 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010530 }
10531 return -ENOMEM;
10532}
10533
Sean Christopherson15248252019-02-05 12:54:17 -080010534void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010535{
Boris Ostrovsky91724812019-12-05 01:30:51 +000010536 struct kvm_vcpu *vcpu;
10537 int i;
10538
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +090010539 /*
10540 * memslots->generation has been incremented.
10541 * mmio generation may have reached its maximum value.
10542 */
Sean Christopherson15248252019-02-05 12:54:17 -080010543 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +000010544
10545 /* Force re-initialization of steal_time cache */
10546 kvm_for_each_vcpu(i, vcpu, kvm)
10547 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010548}
10549
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010550int kvm_arch_prepare_memory_region(struct kvm *kvm,
10551 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010552 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +090010553 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +080010554{
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010555 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
10556 return kvm_alloc_memslot_metadata(memslot,
10557 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010558 return 0;
10559}
10560
Kai Huang88178fd2015-01-28 10:54:27 +080010561static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
Anthony Yznaga37416792020-06-02 13:07:30 -070010562 struct kvm_memory_slot *old,
10563 struct kvm_memory_slot *new,
10564 enum kvm_mr_change change)
Kai Huang88178fd2015-01-28 10:54:27 +080010565{
Anthony Yznaga37416792020-06-02 13:07:30 -070010566 /*
10567 * Nothing to do for RO slots or CREATE/MOVE/DELETE of a slot.
10568 * See comments below.
10569 */
10570 if ((change != KVM_MR_FLAGS_ONLY) || (new->flags & KVM_MEM_READONLY))
Kai Huang88178fd2015-01-28 10:54:27 +080010571 return;
Kai Huang88178fd2015-01-28 10:54:27 +080010572
10573 /*
Anthony Yznaga37416792020-06-02 13:07:30 -070010574 * Dirty logging tracks sptes in 4k granularity, meaning that large
10575 * sptes have to be split. If live migration is successful, the guest
10576 * in the source machine will be destroyed and large sptes will be
10577 * created in the destination. However, if the guest continues to run
10578 * in the source machine (for example if live migration fails), small
10579 * sptes will remain around and cause bad performance.
Kai Huang88178fd2015-01-28 10:54:27 +080010580 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010581 * Scan sptes if dirty logging has been stopped, dropping those
10582 * which can be collapsed into a single large-page spte. Later
10583 * page faults will create the large-page sptes.
Kai Huang88178fd2015-01-28 10:54:27 +080010584 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010585 * There is no need to do this in any of the following cases:
10586 * CREATE: No dirty mappings will already exist.
10587 * MOVE/DELETE: The old mappings will already have been cleaned up by
10588 * kvm_arch_flush_shadow_memslot()
10589 */
10590 if ((old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10591 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10592 kvm_mmu_zap_collapsible_sptes(kvm, new);
10593
10594 /*
10595 * Enable or disable dirty logging for the slot.
Kai Huang88178fd2015-01-28 10:54:27 +080010596 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010597 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of the old
10598 * slot have been zapped so no dirty logging updates are needed for
10599 * the old slot.
10600 * For KVM_MR_CREATE and KVM_MR_MOVE, once the new slot is visible
10601 * any mappings that might be created in it will consume the
10602 * properties of the new slot and do not need to be updated here.
Kai Huang88178fd2015-01-28 10:54:27 +080010603 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010604 * When PML is enabled, the kvm_x86_ops dirty logging hooks are
10605 * called to enable/disable dirty logging.
Kai Huang88178fd2015-01-28 10:54:27 +080010606 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010607 * When disabling dirty logging with PML enabled, the D-bit is set
10608 * for sptes in the slot in order to prevent unnecessary GPA
10609 * logging in the PML buffer (and potential PML buffer full VMEXIT).
10610 * This guarantees leaving PML enabled for the guest's lifetime
10611 * won't have any additional overhead from PML when the guest is
10612 * running with dirty logging disabled.
Kai Huang88178fd2015-01-28 10:54:27 +080010613 *
Anthony Yznaga37416792020-06-02 13:07:30 -070010614 * When enabling dirty logging, large sptes are write-protected
10615 * so they can be split on first write. New large sptes cannot
10616 * be created for this slot until the end of the logging.
Kai Huang88178fd2015-01-28 10:54:27 +080010617 * See the comments in fast_page_fault().
Anthony Yznaga37416792020-06-02 13:07:30 -070010618 * For small sptes, nothing is done if the dirty log is in the
10619 * initial-all-set state. Otherwise, depending on whether pml
10620 * is enabled the D-bit or the W-bit will be cleared.
Kai Huang88178fd2015-01-28 10:54:27 +080010621 */
10622 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010623 if (kvm_x86_ops.slot_enable_log_dirty) {
10624 kvm_x86_ops.slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010625 } else {
10626 int level =
10627 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
Sean Christopherson3bae0452020-04-27 17:54:22 -070010628 PG_LEVEL_2M : PG_LEVEL_4K;
Jay Zhou3c9bd402020-02-27 09:32:27 +080010629
10630 /*
10631 * If we're with initial-all-set, we don't need
10632 * to write protect any small page because
10633 * they're reported as dirty already. However
10634 * we still need to write-protect huge pages
10635 * so that the page split can happen lazily on
10636 * the first write to the huge page.
10637 */
10638 kvm_mmu_slot_remove_write_access(kvm, new, level);
10639 }
Kai Huang88178fd2015-01-28 10:54:27 +080010640 } else {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010641 if (kvm_x86_ops.slot_disable_log_dirty)
10642 kvm_x86_ops.slot_disable_log_dirty(kvm, new);
Kai Huang88178fd2015-01-28 10:54:27 +080010643 }
10644}
10645
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010646void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010647 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010648 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010649 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010650 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010651{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010652 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010653 kvm_mmu_change_mmu_pages(kvm,
10654 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010655
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010656 /*
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010657 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010658 */
Anthony Yznaga37416792020-06-02 13:07:30 -070010659 kvm_mmu_slot_apply_flags(kvm, old, (struct kvm_memory_slot *) new, change);
Sean Christopherson21198842020-02-18 13:07:25 -080010660
10661 /* Free the arrays associated with the old memslot. */
10662 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010663 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010664}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010665
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010666void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010667{
Sean Christopherson7390de12019-02-05 13:01:31 -080010668 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010669}
10670
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010671void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10672 struct kvm_memory_slot *slot)
10673{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010674 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010675}
10676
Liran Alone6c67d82018-09-04 10:56:52 +030010677static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10678{
10679 return (is_guest_mode(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010680 kvm_x86_ops.guest_apic_has_interrupt &&
10681 kvm_x86_ops.guest_apic_has_interrupt(vcpu));
Liran Alone6c67d82018-09-04 10:56:52 +030010682}
10683
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010684static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10685{
10686 if (!list_empty_careful(&vcpu->async_pf.done))
10687 return true;
10688
10689 if (kvm_apic_has_events(vcpu))
10690 return true;
10691
10692 if (vcpu->arch.pv.pv_unhalted)
10693 return true;
10694
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010695 if (vcpu->arch.exception.pending)
10696 return true;
10697
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010698 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10699 (vcpu->arch.nmi_pending &&
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010700 kvm_x86_ops.nmi_allowed(vcpu, false)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010701 return true;
10702
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010703 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -040010704 (vcpu->arch.smi_pending &&
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010705 kvm_x86_ops.smi_allowed(vcpu, false)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010706 return true;
10707
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010708 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010709 (kvm_cpu_has_interrupt(vcpu) ||
10710 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010711 return true;
10712
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010713 if (kvm_hv_has_stimer_pending(vcpu))
10714 return true;
10715
Sean Christophersond2060bd2020-04-22 19:25:39 -070010716 if (is_guest_mode(vcpu) &&
10717 kvm_x86_ops.nested_ops->hv_timer_pending &&
10718 kvm_x86_ops.nested_ops->hv_timer_pending(vcpu))
10719 return true;
10720
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010721 return false;
10722}
10723
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010724int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10725{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010726 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010727}
Zhang Xiantao57361992007-12-17 14:21:40 +080010728
Wanpeng Li17e433b2019-08-05 10:03:19 +080010729bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10730{
10731 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10732 return true;
10733
10734 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10735 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10736 kvm_test_request(KVM_REQ_EVENT, vcpu))
10737 return true;
10738
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010739 if (vcpu->arch.apicv_active && kvm_x86_ops.dy_apicv_has_pending_interrupt(vcpu))
Wanpeng Li17e433b2019-08-05 10:03:19 +080010740 return true;
10741
10742 return false;
10743}
10744
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010745bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10746{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010747 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010748}
10749
Christoffer Dallb6d33832012-03-08 16:44:24 -050010750int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010751{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010752 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010753}
Gleb Natapov78646122009-03-23 12:12:11 +020010754
10755int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10756{
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010757 return kvm_x86_ops.interrupt_allowed(vcpu, false);
Gleb Natapov78646122009-03-23 12:12:11 +020010758}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010759
Nadav Amit82b32772014-11-02 11:54:45 +020010760unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10761{
10762 if (is_64_bit_mode(vcpu))
10763 return kvm_rip_read(vcpu);
10764 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10765 kvm_rip_read(vcpu));
10766}
10767EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10768
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010769bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10770{
Nadav Amit82b32772014-11-02 11:54:45 +020010771 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010772}
10773EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10774
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010775unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10776{
10777 unsigned long rflags;
10778
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010779 rflags = kvm_x86_ops.get_rflags(vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010780 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010781 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010782 return rflags;
10783}
10784EXPORT_SYMBOL_GPL(kvm_get_rflags);
10785
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010786static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010787{
10788 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010789 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010790 rflags |= X86_EFLAGS_TF;
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010791 kvm_x86_ops.set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010792}
10793
10794void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10795{
10796 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010797 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010798}
10799EXPORT_SYMBOL_GPL(kvm_set_rflags);
10800
Gleb Natapov56028d02010-10-17 18:13:42 +020010801void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10802{
10803 int r;
10804
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010805 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010806 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010807 return;
10808
10809 r = kvm_mmu_reload(vcpu);
10810 if (unlikely(r))
10811 return;
10812
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010813 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010814 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010815 return;
10816
Sean Christopherson7a026742020-02-06 14:14:34 -080010817 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010818}
10819
Gleb Natapovaf585b92010-10-14 11:22:46 +020010820static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10821{
Peter Xudd03bca2020-04-16 11:58:59 -040010822 BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
10823
Gleb Natapovaf585b92010-10-14 11:22:46 +020010824 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10825}
10826
10827static inline u32 kvm_async_pf_next_probe(u32 key)
10828{
Peter Xudd03bca2020-04-16 11:58:59 -040010829 return (key + 1) & (ASYNC_PF_PER_VCPU - 1);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010830}
10831
10832static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10833{
10834 u32 key = kvm_async_pf_hash_fn(gfn);
10835
10836 while (vcpu->arch.apf.gfns[key] != ~0)
10837 key = kvm_async_pf_next_probe(key);
10838
10839 vcpu->arch.apf.gfns[key] = gfn;
10840}
10841
10842static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10843{
10844 int i;
10845 u32 key = kvm_async_pf_hash_fn(gfn);
10846
Peter Xudd03bca2020-04-16 11:58:59 -040010847 for (i = 0; i < ASYNC_PF_PER_VCPU &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010848 (vcpu->arch.apf.gfns[key] != gfn &&
10849 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010850 key = kvm_async_pf_next_probe(key);
10851
10852 return key;
10853}
10854
10855bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10856{
10857 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10858}
10859
10860static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10861{
10862 u32 i, j, k;
10863
10864 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
Peter Xu0fd46042020-04-16 11:59:10 -040010865
10866 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn))
10867 return;
10868
Gleb Natapovaf585b92010-10-14 11:22:46 +020010869 while (true) {
10870 vcpu->arch.apf.gfns[i] = ~0;
10871 do {
10872 j = kvm_async_pf_next_probe(j);
10873 if (vcpu->arch.apf.gfns[j] == ~0)
10874 return;
10875 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10876 /*
10877 * k lies cyclically in ]i,j]
10878 * | i.k.j |
10879 * |....j i.k.| or |.k..j i...|
10880 */
10881 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10882 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10883 i = j;
10884 }
10885}
10886
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020010887static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020010888{
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020010889 u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT;
10890
10891 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason,
10892 sizeof(reason));
10893}
10894
10895static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token)
10896{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010897 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010898
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010899 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
10900 &token, offset, sizeof(token));
10901}
10902
10903static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu)
10904{
10905 unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token);
10906 u32 val;
10907
10908 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data,
10909 &val, offset, sizeof(val)))
10910 return false;
10911
10912 return !val;
Gleb Natapov7c907052010-10-14 11:22:53 +020010913}
10914
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010915static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10916{
10917 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10918 return false;
10919
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010920 if (!kvm_pv_async_pf_enabled(vcpu) ||
10921 (vcpu->arch.apf.send_user_only && kvm_x86_ops.get_cpl(vcpu) == 0))
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010922 return false;
10923
10924 return true;
10925}
10926
10927bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10928{
10929 if (unlikely(!lapic_in_kernel(vcpu) ||
10930 kvm_event_needs_reinjection(vcpu) ||
10931 vcpu->arch.exception.pending))
10932 return false;
10933
10934 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10935 return false;
10936
10937 /*
10938 * If interrupts are off we cannot even use an artificial
10939 * halt state.
10940 */
Paolo Bonzinic300ab92020-04-23 14:08:58 -040010941 return kvm_arch_interrupt_allowed(vcpu);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010942}
10943
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020010944bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
Gleb Natapovaf585b92010-10-14 11:22:46 +020010945 struct kvm_async_pf *work)
10946{
Avi Kivity6389ee92010-11-29 16:12:30 +020010947 struct x86_exception fault;
10948
Sean Christopherson736c2912019-12-06 15:57:14 -080010949 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010950 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010951
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010952 if (kvm_can_deliver_async_pf(vcpu) &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +020010953 !apf_put_user_notpresent(vcpu)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010954 fault.vector = PF_VECTOR;
10955 fault.error_code_valid = true;
10956 fault.error_code = 0;
10957 fault.nested_page_fault = false;
10958 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010959 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010960 kvm_inject_page_fault(vcpu, &fault);
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020010961 return true;
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010962 } else {
10963 /*
10964 * It is not possible to deliver a paravirtualized asynchronous
10965 * page fault, but putting the guest in an artificial halt state
10966 * can be beneficial nevertheless: if an interrupt arrives, we
10967 * can deliver it timely and perhaps the guest will schedule
10968 * another process. When the instruction that triggered a page
10969 * fault is retried, hopefully the page will be ready in the host.
10970 */
10971 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020010972 return false;
Gleb Natapov7c907052010-10-14 11:22:53 +020010973 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010974}
10975
10976void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10977 struct kvm_async_pf *work)
10978{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010979 struct kvm_lapic_irq irq = {
10980 .delivery_mode = APIC_DM_FIXED,
10981 .vector = vcpu->arch.apf.vec
10982 };
Avi Kivity6389ee92010-11-29 16:12:30 +020010983
chai wenf2e10662013-10-14 22:22:33 +080010984 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010985 work->arch.token = ~0; /* broadcast wakeup */
10986 else
10987 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010988 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010989
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +020010990 if ((work->wakeup_all || work->notpresent_injected) &&
10991 kvm_pv_async_pf_enabled(vcpu) &&
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020010992 !apf_put_user_ready(vcpu, work->arch.token)) {
10993 vcpu->arch.apf.pageready_pending = true;
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010994 kvm_apic_set_irq(vcpu, &irq, NULL);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020010995 }
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020010996
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010997 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010998 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010999}
11000
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020011001void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu)
11002{
11003 kvm_make_request(KVM_REQ_APF_READY, vcpu);
11004 if (!vcpu->arch.apf.pageready_pending)
11005 kvm_vcpu_kick(vcpu);
11006}
11007
Vitaly Kuznetsov7c0ade62020-05-25 16:41:18 +020011008bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
Gleb Natapov7c907052010-10-14 11:22:53 +020011009{
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011010 if (!kvm_pv_async_pf_enabled(vcpu))
Gleb Natapov7c907052010-10-14 11:22:53 +020011011 return true;
11012 else
Vitaly Kuznetsov2635b5c2020-05-25 16:41:20 +020011013 return apf_pageready_slot_free(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020011014}
11015
Paolo Bonzini5544eb92015-07-07 15:41:58 +020011016void kvm_arch_start_assignment(struct kvm *kvm)
11017{
11018 atomic_inc(&kvm->arch.assigned_device_count);
11019}
11020EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
11021
11022void kvm_arch_end_assignment(struct kvm *kvm)
11023{
11024 atomic_dec(&kvm->arch.assigned_device_count);
11025}
11026EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
11027
11028bool kvm_arch_has_assigned_device(struct kvm *kvm)
11029{
11030 return atomic_read(&kvm->arch.assigned_device_count);
11031}
11032EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
11033
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060011034void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
11035{
11036 atomic_inc(&kvm->arch.noncoherent_dma_count);
11037}
11038EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
11039
11040void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
11041{
11042 atomic_dec(&kvm->arch.noncoherent_dma_count);
11043}
11044EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
11045
11046bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
11047{
11048 return atomic_read(&kvm->arch.noncoherent_dma_count);
11049}
11050EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
11051
Alex Williamson14717e22016-05-05 11:58:35 -060011052bool kvm_arch_has_irq_bypass(void)
11053{
Sean Christopherson92735b12019-08-02 15:06:17 -070011054 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060011055}
11056
Feng Wu87276882015-09-18 22:29:40 +080011057int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
11058 struct irq_bypass_producer *prod)
11059{
11060 struct kvm_kernel_irqfd *irqfd =
11061 container_of(cons, struct kvm_kernel_irqfd, consumer);
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011062 int ret;
Feng Wu87276882015-09-18 22:29:40 +080011063
Alex Williamson14717e22016-05-05 11:58:35 -060011064 irqfd->producer = prod;
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011065 kvm_arch_start_assignment(irqfd->kvm);
11066 ret = kvm_x86_ops.update_pi_irte(irqfd->kvm,
11067 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080011068
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011069 if (ret)
11070 kvm_arch_end_assignment(irqfd->kvm);
11071
11072 return ret;
Feng Wu87276882015-09-18 22:29:40 +080011073}
11074
11075void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
11076 struct irq_bypass_producer *prod)
11077{
11078 int ret;
11079 struct kvm_kernel_irqfd *irqfd =
11080 container_of(cons, struct kvm_kernel_irqfd, consumer);
11081
Feng Wu87276882015-09-18 22:29:40 +080011082 WARN_ON(irqfd->producer != prod);
11083 irqfd->producer = NULL;
11084
11085 /*
11086 * When producer of consumer is unregistered, we change back to
11087 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020011088 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080011089 * int this case doesn't want to receive the interrupts.
11090 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -070011091 ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
Feng Wu87276882015-09-18 22:29:40 +080011092 if (ret)
11093 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
11094 " fails: %d\n", irqfd->consumer.token, ret);
Zhu Lingshan2edd9cb2020-07-31 14:55:29 +080011095
11096 kvm_arch_end_assignment(irqfd->kvm);
Feng Wu87276882015-09-18 22:29:40 +080011097}
11098
11099int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
11100 uint32_t guest_irq, bool set)
11101{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070011102 return kvm_x86_ops.update_pi_irte(kvm, host_irq, guest_irq, set);
Feng Wu87276882015-09-18 22:29:40 +080011103}
11104
Feng Wu520040142016-01-25 16:53:33 +080011105bool kvm_vector_hashing_enabled(void)
11106{
11107 return vector_hashing;
11108}
Feng Wu520040142016-01-25 16:53:33 +080011109
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030011110bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
11111{
11112 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
11113}
11114EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
11115
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011116
11117int kvm_spec_ctrl_test_value(u64 value)
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011118{
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011119 /*
11120 * test that setting IA32_SPEC_CTRL to given value
11121 * is allowed by the host processor
11122 */
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011123
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011124 u64 saved_value;
11125 unsigned long flags;
11126 int ret = 0;
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011127
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011128 local_irq_save(flags);
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011129
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011130 if (rdmsrl_safe(MSR_IA32_SPEC_CTRL, &saved_value))
11131 ret = 1;
11132 else if (wrmsrl_safe(MSR_IA32_SPEC_CTRL, value))
11133 ret = 1;
11134 else
11135 wrmsrl(MSR_IA32_SPEC_CTRL, saved_value);
11136
11137 local_irq_restore(flags);
11138
11139 return ret;
Paolo Bonzini6441fa62020-01-20 16:33:06 +010011140}
Maxim Levitsky841c2be2020-07-08 14:57:31 +030011141EXPORT_SYMBOL_GPL(kvm_spec_ctrl_test_value);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030011142
Mohammed Gamal89786142020-07-10 17:48:03 +020011143void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code)
11144{
11145 struct x86_exception fault;
Paolo Bonzini19cf4b72020-08-17 13:53:04 -040011146 u32 access = error_code &
11147 (PFERR_WRITE_MASK | PFERR_FETCH_MASK | PFERR_USER_MASK);
Mohammed Gamal89786142020-07-10 17:48:03 +020011148
11149 if (!(error_code & PFERR_PRESENT_MASK) ||
Paolo Bonzini19cf4b72020-08-17 13:53:04 -040011150 vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, &fault) != UNMAPPED_GVA) {
Mohammed Gamal89786142020-07-10 17:48:03 +020011151 /*
11152 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page
11153 * tables probably do not match the TLB. Just proceed
11154 * with the error code that the processor gave.
11155 */
11156 fault.vector = PF_VECTOR;
11157 fault.error_code_valid = true;
11158 fault.error_code = error_code;
11159 fault.nested_page_fault = false;
11160 fault.address = gva;
11161 }
11162 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault);
11163}
11164EXPORT_SYMBOL_GPL(kvm_fixup_and_inject_pf_error);
Carsten Otte043405e2007-10-10 17:16:19 +020011165
Babu Moger3f3393b2020-09-11 14:29:05 -050011166/*
11167 * Handles kvm_read/write_guest_virt*() result and either injects #PF or returns
11168 * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value
11169 * indicates whether exit to userspace is needed.
11170 */
11171int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
11172 struct x86_exception *e)
11173{
11174 if (r == X86EMUL_PROPAGATE_FAULT) {
11175 kvm_inject_emulated_page_fault(vcpu, e);
11176 return 1;
11177 }
11178
11179 /*
11180 * In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED
11181 * while handling a VMX instruction KVM could've handled the request
11182 * correctly by exiting to userspace and performing I/O but there
11183 * doesn't seem to be a real use-case behind such requests, just return
11184 * KVM_EXIT_INTERNAL_ERROR for now.
11185 */
11186 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
11187 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
11188 vcpu->run->internal.ndata = 0;
11189
11190 return 0;
11191}
11192EXPORT_SYMBOL_GPL(kvm_handle_memory_failure);
11193
Babu Moger97150922020-09-11 14:29:12 -050011194int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva)
11195{
11196 bool pcid_enabled;
11197 struct x86_exception e;
11198 unsigned i;
11199 unsigned long roots_to_free = 0;
11200 struct {
11201 u64 pcid;
11202 u64 gla;
11203 } operand;
11204 int r;
11205
11206 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
11207 if (r != X86EMUL_CONTINUE)
11208 return kvm_handle_memory_failure(vcpu, r, &e);
11209
11210 if (operand.pcid >> 12 != 0) {
11211 kvm_inject_gp(vcpu, 0);
11212 return 1;
11213 }
11214
11215 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
11216
11217 switch (type) {
11218 case INVPCID_TYPE_INDIV_ADDR:
11219 if ((!pcid_enabled && (operand.pcid != 0)) ||
11220 is_noncanonical_address(operand.gla, vcpu)) {
11221 kvm_inject_gp(vcpu, 0);
11222 return 1;
11223 }
11224 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
11225 return kvm_skip_emulated_instruction(vcpu);
11226
11227 case INVPCID_TYPE_SINGLE_CTXT:
11228 if (!pcid_enabled && (operand.pcid != 0)) {
11229 kvm_inject_gp(vcpu, 0);
11230 return 1;
11231 }
11232
11233 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
11234 kvm_mmu_sync_roots(vcpu);
11235 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
11236 }
11237
11238 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
11239 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd)
11240 == operand.pcid)
11241 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
11242
11243 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
11244 /*
11245 * If neither the current cr3 nor any of the prev_roots use the
11246 * given PCID, then nothing needs to be done here because a
11247 * resync will happen anyway before switching to any other CR3.
11248 */
11249
11250 return kvm_skip_emulated_instruction(vcpu);
11251
11252 case INVPCID_TYPE_ALL_NON_GLOBAL:
11253 /*
11254 * Currently, KVM doesn't mark global entries in the shadow
11255 * page tables, so a non-global flush just degenerates to a
11256 * global flush. If needed, we could optimize this later by
11257 * keeping track of global entries in shadow page tables.
11258 */
11259
11260 fallthrough;
11261 case INVPCID_TYPE_ALL_INCL_GLOBAL:
11262 kvm_mmu_unload(vcpu);
11263 return kvm_skip_emulated_instruction(vcpu);
11264
11265 default:
11266 BUG(); /* We have already checked above that type <= 3 */
11267 }
11268}
11269EXPORT_SYMBOL_GPL(kvm_handle_invpcid);
11270
Carsten Otte043405e2007-10-10 17:16:19 +020011271EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
11272EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
11273EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
11274EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
11275EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
11276EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
11277EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
11278EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
11279EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
11280EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070011281EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Carsten Otte043405e2007-10-10 17:16:19 +020011282EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
11283EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
11284EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
11285EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080011286EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Carsten Otte043405e2007-10-10 17:16:19 +020011287EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
11288EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
11289EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
11290EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050011291EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060011292EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);