blob: b2307c1152687275134a860e8affb91811699340 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity6aa8b732006-12-10 02:21:36 -08002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * This module enables machines with Intel VT-x extensions to run virtual
6 * machines without emulation or binary translation.
7 *
8 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Avi Kivity6aa8b732006-12-10 02:21:36 -080014 */
15
Sean Christopherson199b1182018-12-03 13:52:53 -080016#include <linux/frame.h>
17#include <linux/highmem.h>
18#include <linux/hrtimer.h>
19#include <linux/kernel.h>
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/module.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020022#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070023#include <linux/mod_devicetable.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080024#include <linux/mm.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080025#include <linux/sched.h>
Josh Poimboeufb2849092019-01-30 07:13:58 -060026#include <linux/sched/smt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040028#include <linux/tboot.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080029#include <linux/trace_events.h>
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +020030#include <linux/entry-kvm.h>
Avi Kivitye4956062007-06-28 14:15:57 -040031
Sean Christopherson199b1182018-12-03 13:52:53 -080032#include <asm/apic.h>
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020033#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080034#include <asm/cpu.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010035#include <asm/cpu_device_id.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010036#include <asm/debugreg.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080037#include <asm/desc.h>
38#include <asm/fpu/internal.h>
39#include <asm/io.h>
Feng Wuefc64402015-09-18 22:29:51 +080040#include <asm/irq_remapping.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080041#include <asm/kexec.h>
42#include <asm/perf_event.h>
43#include <asm/mce.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070044#include <asm/mmu_context.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010045#include <asm/mshyperv.h>
Benjamin Thielb10c3072020-01-23 18:29:45 +010046#include <asm/mwait.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080047#include <asm/spec-ctrl.h>
48#include <asm/virtext.h>
49#include <asm/vmx.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Sean Christopherson3077c192018-12-03 13:53:02 -080051#include "capabilities.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080052#include "cpuid.h"
Sean Christopherson4cebd742018-12-03 13:52:58 -080053#include "evmcs.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080054#include "irq.h"
55#include "kvm_cache_regs.h"
56#include "lapic.h"
57#include "mmu.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080058#include "nested.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080060#include "trace.h"
Sean Christophersoncb1d4742018-12-03 13:53:04 -080061#include "vmcs.h"
Sean Christopherson609363c2018-12-03 13:53:05 -080062#include "vmcs12.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080063#include "vmx.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080064#include "x86.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Valdis Klētnieks575b2552020-02-27 21:49:52 -050069#ifdef MODULE
Josh Triplette9bda3b2012-03-20 23:33:51 -070070static const struct x86_cpu_id vmx_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010071 X86_MATCH_FEATURE(X86_FEATURE_VMX, NULL),
Josh Triplette9bda3b2012-03-20 23:33:51 -070072 {}
73};
74MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050075#endif
Josh Triplette9bda3b2012-03-20 23:33:51 -070076
Sean Christopherson2c4fd912018-12-03 13:53:03 -080077bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020078module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080079
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010080static bool __read_mostly enable_vnmi = 1;
81module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
82
Sean Christopherson2c4fd912018-12-03 13:53:03 -080083bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020085
Sean Christopherson2c4fd912018-12-03 13:53:03 -080086bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020087module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080088
Sean Christopherson2c4fd912018-12-03 13:53:03 -080089bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070090module_param_named(unrestricted_guest,
91 enable_unrestricted_guest, bool, S_IRUGO);
92
Sean Christopherson2c4fd912018-12-03 13:53:03 -080093bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080094module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
95
Avi Kivitya27685c2012-06-12 20:30:18 +030096static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020097module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030098
Rusty Russell476bc002012-01-13 09:32:18 +103099static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +0300100module_param(fasteoi, bool, S_IRUGO);
101
Vitaly Kuznetsova4443262020-02-20 18:22:04 +0100102bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800103module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800104
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200110static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Sean Christopherson2c4fd912018-12-03 13:53:03 -0800113bool __read_mostly enable_pml = 1;
Kai Huang843e4332015-01-28 10:54:28 +0800114module_param_named(pml, enable_pml, bool, S_IRUGO);
115
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200116static bool __read_mostly dump_invalid_vmcs = 0;
117module_param(dump_invalid_vmcs, bool, 0644);
118
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100119#define MSR_BITMAP_MODE_X2APIC 1
120#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100121
Haozhong Zhang64903d62015-10-20 15:39:09 +0800122#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
123
Yunhong Jiang64672c92016-06-13 14:19:59 -0700124/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
125static int __read_mostly cpu_preemption_timer_multi;
126static bool __read_mostly enable_preemption_timer = 1;
127#ifdef CONFIG_X86_64
128module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
129#endif
130
Sean Christopherson3de63472018-07-13 08:42:30 -0700131#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800132#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
133#define KVM_VM_CR0_ALWAYS_ON \
134 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
135 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200136
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800137#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200138#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
139#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
140
Avi Kivity78ac8b42010-04-08 18:19:35 +0300141#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
142
Chao Pengbf8c55d2018-10-24 16:05:14 +0800143#define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \
144 RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \
145 RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \
146 RTIT_STATUS_BYTECNT))
147
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800148/*
149 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
150 * ple_gap: upper bound on the amount of time between two successive
151 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500152 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153 * ple_window: upper bound on the amount of time a guest is allowed to execute
154 * in a PAUSE loop. Tests indicate that most spinlocks are held for
155 * less than 2^12 cycles
156 * Time is measured based on a counter that runs at the same rate as the TSC,
157 * refer SDM volume 3b section 21.6.13 & 22.1.3.
158 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400159static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500160module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200161
Babu Moger7fbc85a2018-03-16 16:37:22 -0400162static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
163module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200165/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400166static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400167module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200168
169/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400170static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400171module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200172
173/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
175module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200176
Chao Pengf99e3da2018-10-24 16:05:10 +0800177/* Default is SYSTEM mode, 1 for host-guest mode */
178int __read_mostly pt_mode = PT_MODE_SYSTEM;
179module_param(pt_mode, int, S_IRUGO);
180
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200181static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200182static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200183static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200184
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200185/* Storage for pre module init parameter parsing */
186static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200187
188static const struct {
189 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200190 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200191} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200192 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
193 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
194 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
195 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
196 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
197 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200198};
199
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200200#define L1D_CACHE_ORDER 4
201static void *vmx_l1d_flush_pages;
202
203static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
204{
205 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200206 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200207
Waiman Long19a36d32019-08-26 15:30:23 -0400208 if (!boot_cpu_has_bug(X86_BUG_L1TF)) {
209 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
210 return 0;
211 }
212
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200213 if (!enable_ept) {
214 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
215 return 0;
216 }
217
Yi Wangd806afa2018-08-16 13:42:39 +0800218 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
219 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200220
Yi Wangd806afa2018-08-16 13:42:39 +0800221 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
222 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
223 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
224 return 0;
225 }
226 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200227
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200228 /* If set to auto use the default l1tf mitigation method */
229 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
230 switch (l1tf_mitigation) {
231 case L1TF_MITIGATION_OFF:
232 l1tf = VMENTER_L1D_FLUSH_NEVER;
233 break;
234 case L1TF_MITIGATION_FLUSH_NOWARN:
235 case L1TF_MITIGATION_FLUSH:
236 case L1TF_MITIGATION_FLUSH_NOSMT:
237 l1tf = VMENTER_L1D_FLUSH_COND;
238 break;
239 case L1TF_MITIGATION_FULL:
240 case L1TF_MITIGATION_FULL_FORCE:
241 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
242 break;
243 }
244 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
245 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
246 }
247
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200248 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
249 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Ben Gardon41836832019-02-11 11:02:52 -0800250 /*
251 * This allocation for vmx_l1d_flush_pages is not tied to a VM
252 * lifetime and so should not be charged to a memcg.
253 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200254 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
255 if (!page)
256 return -ENOMEM;
257 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200258
259 /*
260 * Initialize each page with a different pattern in
261 * order to protect against KSM in the nested
262 * virtualization case.
263 */
264 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
265 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
266 PAGE_SIZE);
267 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200268 }
269
270 l1tf_vmx_mitigation = l1tf;
271
Thomas Gleixner895ae472018-07-13 16:23:22 +0200272 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
273 static_branch_enable(&vmx_l1d_should_flush);
274 else
275 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200276
Nicolai Stange427362a2018-07-21 22:25:00 +0200277 if (l1tf == VMENTER_L1D_FLUSH_COND)
278 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200279 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200280 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200281 return 0;
282}
283
284static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200285{
286 unsigned int i;
287
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200288 if (s) {
289 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200290 if (vmentry_l1d_param[i].for_parse &&
291 sysfs_streq(s, vmentry_l1d_param[i].option))
292 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200293 }
294 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200295 return -EINVAL;
296}
297
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200298static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
299{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200300 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200301
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200302 l1tf = vmentry_l1d_flush_parse(s);
303 if (l1tf < 0)
304 return l1tf;
305
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200306 if (!boot_cpu_has(X86_BUG_L1TF))
307 return 0;
308
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200309 /*
310 * Has vmx_init() run already? If not then this is the pre init
311 * parameter parsing. In that case just store the value and let
312 * vmx_init() do the proper setup after enable_ept has been
313 * established.
314 */
315 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
316 vmentry_l1d_flush_param = l1tf;
317 return 0;
318 }
319
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200320 mutex_lock(&vmx_l1d_flush_mutex);
321 ret = vmx_setup_l1d_flush(l1tf);
322 mutex_unlock(&vmx_l1d_flush_mutex);
323 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200324}
325
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200326static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
327{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200328 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
329 return sprintf(s, "???\n");
330
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200331 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200332}
333
334static const struct kernel_param_ops vmentry_l1d_flush_ops = {
335 .set = vmentry_l1d_flush_set,
336 .get = vmentry_l1d_flush_get,
337};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200338module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200339
Gleb Natapovd99e4152012-12-20 16:57:45 +0200340static u32 vmx_segment_access_rights(struct kvm_segment *var);
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200341static __always_inline void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu,
Ashok Raj15d45072018-02-01 22:59:43 +0100342 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300343
Sean Christopherson453eafb2018-12-20 12:25:17 -0800344void vmx_vmexit(void);
345
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700346#define vmx_insn_failed(fmt...) \
347do { \
348 WARN_ONCE(1, fmt); \
349 pr_warn_ratelimited(fmt); \
350} while (0)
351
Sean Christopherson6e202092019-07-19 13:41:08 -0700352asmlinkage void vmread_error(unsigned long field, bool fault)
353{
354 if (fault)
355 kvm_spurious_fault();
356 else
357 vmx_insn_failed("kvm: vmread failed: field=%lx\n", field);
358}
359
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700360noinline void vmwrite_error(unsigned long field, unsigned long value)
361{
362 vmx_insn_failed("kvm: vmwrite failed: field=%lx val=%lx err=%d\n",
363 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
364}
365
366noinline void vmclear_error(struct vmcs *vmcs, u64 phys_addr)
367{
368 vmx_insn_failed("kvm: vmclear failed: %p/%llx\n", vmcs, phys_addr);
369}
370
371noinline void vmptrld_error(struct vmcs *vmcs, u64 phys_addr)
372{
373 vmx_insn_failed("kvm: vmptrld failed: %p/%llx\n", vmcs, phys_addr);
374}
375
376noinline void invvpid_error(unsigned long ext, u16 vpid, gva_t gva)
377{
378 vmx_insn_failed("kvm: invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n",
379 ext, vpid, gva);
380}
381
382noinline void invept_error(unsigned long ext, u64 eptp, gpa_t gpa)
383{
384 vmx_insn_failed("kvm: invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n",
385 ext, eptp, gpa);
386}
387
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800389DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300390/*
391 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
392 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
393 */
394static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395
Sheng Yang2384d2b2008-01-17 15:14:33 +0800396static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
397static DEFINE_SPINLOCK(vmx_vpid_lock);
398
Sean Christopherson3077c192018-12-03 13:53:02 -0800399struct vmcs_config vmcs_config;
400struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800401
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402#define VMX_SEGMENT_FIELD(seg) \
403 [VCPU_SREG_##seg] = { \
404 .selector = GUEST_##seg##_SELECTOR, \
405 .base = GUEST_##seg##_BASE, \
406 .limit = GUEST_##seg##_LIMIT, \
407 .ar_bytes = GUEST_##seg##_AR_BYTES, \
408 }
409
Mathias Krause772e0312012-08-30 01:30:19 +0200410static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 unsigned selector;
412 unsigned base;
413 unsigned limit;
414 unsigned ar_bytes;
415} kvm_vmx_segment_fields[] = {
416 VMX_SEGMENT_FIELD(CS),
417 VMX_SEGMENT_FIELD(DS),
418 VMX_SEGMENT_FIELD(ES),
419 VMX_SEGMENT_FIELD(FS),
420 VMX_SEGMENT_FIELD(GS),
421 VMX_SEGMENT_FIELD(SS),
422 VMX_SEGMENT_FIELD(TR),
423 VMX_SEGMENT_FIELD(LDTR),
424};
425
Sean Christophersonec0241f2020-04-15 13:34:52 -0700426static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
427{
428 vmx->segment_cache.bitmask = 0;
429}
430
Sean Christopherson23420802019-04-19 22:50:57 -0700431static unsigned long host_idt_base;
Avi Kivity26bb0982009-09-07 11:14:12 +0300432
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300433/*
Jim Mattson898a8112018-12-05 15:28:59 -0800434 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
435 * will emulate SYSCALL in legacy mode if the vendor string in guest
436 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
437 * support this emulation, IA32_STAR must always be included in
Sean Christopherson14a61b62020-09-23 11:04:08 -0700438 * vmx_uret_msrs_list[], even in i386 builds.
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300439 */
Sean Christopherson14a61b62020-09-23 11:04:08 -0700440const u32 vmx_uret_msrs_list[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800441#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300442 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400444 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Paolo Bonzinic11f83e2019-11-18 12:23:00 -0500445 MSR_IA32_TSX_CTRL,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100448#if IS_ENABLED(CONFIG_HYPERV)
449static bool __read_mostly enlightened_vmcs = true;
450module_param(enlightened_vmcs, bool, 0444);
451
Tianyu Lan877ad952018-07-19 08:40:23 +0000452/* check_ept_pointer() should be under protection of ept_pointer_lock. */
453static void check_ept_pointer_match(struct kvm *kvm)
454{
455 struct kvm_vcpu *vcpu;
456 u64 tmp_eptp = INVALID_PAGE;
457 int i;
458
459 kvm_for_each_vcpu(i, vcpu, kvm) {
460 if (!VALID_PAGE(tmp_eptp)) {
461 tmp_eptp = to_vmx(vcpu)->ept_pointer;
462 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
463 to_kvm_vmx(kvm)->ept_pointers_match
464 = EPT_POINTERS_MISMATCH;
465 return;
466 }
467 }
468
469 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
470}
471
Yi Wang8997f652019-01-21 15:27:05 +0800472static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800473 void *data)
474{
475 struct kvm_tlb_range *range = data;
476
477 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
478 range->pages);
479}
480
481static inline int __hv_remote_flush_tlb_with_range(struct kvm *kvm,
482 struct kvm_vcpu *vcpu, struct kvm_tlb_range *range)
483{
484 u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
485
486 /*
487 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs address
488 * of the base of EPT PML4 table, strip off EPT configuration
489 * information.
490 */
491 if (range)
492 return hyperv_flush_guest_mapping_range(ept_pointer & PAGE_MASK,
493 kvm_fill_hv_flush_list_func, (void *)range);
494 else
495 return hyperv_flush_guest_mapping(ept_pointer & PAGE_MASK);
496}
497
498static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
499 struct kvm_tlb_range *range)
Tianyu Lan877ad952018-07-19 08:40:23 +0000500{
Lan Tianyua5c214d2018-10-13 22:54:05 +0800501 struct kvm_vcpu *vcpu;
Lan Tianyub7c1c222019-01-04 15:20:44 +0800502 int ret = 0, i;
Tianyu Lan877ad952018-07-19 08:40:23 +0000503
504 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
505
506 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
507 check_ept_pointer_match(kvm);
508
509 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyu53963a72018-12-06 15:34:36 +0800510 kvm_for_each_vcpu(i, vcpu, kvm) {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800511 /* If ept_pointer is invalid pointer, bypass flush request. */
512 if (VALID_PAGE(to_vmx(vcpu)->ept_pointer))
513 ret |= __hv_remote_flush_tlb_with_range(
514 kvm, vcpu, range);
Lan Tianyu53963a72018-12-06 15:34:36 +0800515 }
Lan Tianyua5c214d2018-10-13 22:54:05 +0800516 } else {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800517 ret = __hv_remote_flush_tlb_with_range(kvm,
518 kvm_get_vcpu(kvm, 0), range);
Tianyu Lan877ad952018-07-19 08:40:23 +0000519 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000520
Tianyu Lan877ad952018-07-19 08:40:23 +0000521 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
522 return ret;
523}
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800524static int hv_remote_flush_tlb(struct kvm *kvm)
525{
526 return hv_remote_flush_tlb_with_range(kvm, NULL);
527}
528
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800529static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)
530{
531 struct hv_enlightened_vmcs *evmcs;
532 struct hv_partition_assist_pg **p_hv_pa_pg =
533 &vcpu->kvm->arch.hyperv.hv_pa_pg;
534 /*
535 * Synthetic VM-Exit is not enabled in current code and so All
536 * evmcs in singe VM shares same assist page.
537 */
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200538 if (!*p_hv_pa_pg)
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800539 *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200540
541 if (!*p_hv_pa_pg)
542 return -ENOMEM;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800543
544 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs;
545
546 evmcs->partition_assist_page =
547 __pa(*p_hv_pa_pg);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200548 evmcs->hv_vm_id = (unsigned long)vcpu->kvm;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800549 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;
550
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800551 return 0;
552}
553
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100554#endif /* IS_ENABLED(CONFIG_HYPERV) */
555
Yunhong Jiang64672c92016-06-13 14:19:59 -0700556/*
557 * Comment's format: document - errata name - stepping - processor name.
558 * Refer from
559 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
560 */
561static u32 vmx_preemption_cpu_tfms[] = {
562/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
5630x000206E6,
564/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
565/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
566/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
5670x00020652,
568/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
5690x00020655,
570/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
571/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
572/*
573 * 320767.pdf - AAP86 - B1 -
574 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
575 */
5760x000106E5,
577/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
5780x000106A0,
579/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
5800x000106A1,
581/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
5820x000106A4,
583 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
584 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
585 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
5860x000106A5,
Wei Huang3d82c562018-12-03 14:13:32 -0600587 /* Xeon E3-1220 V2 */
5880x000306A8,
Yunhong Jiang64672c92016-06-13 14:19:59 -0700589};
590
591static inline bool cpu_has_broken_vmx_preemption_timer(void)
592{
593 u32 eax = cpuid_eax(0x00000001), i;
594
595 /* Clear the reserved bits */
596 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000597 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700598 if (eax == vmx_preemption_cpu_tfms[i])
599 return true;
600
601 return false;
602}
603
Paolo Bonzini35754c92015-07-29 12:05:37 +0200604static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800605{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200606 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800607}
608
Sheng Yang04547152009-04-01 15:52:31 +0800609static inline bool report_flexpriority(void)
610{
611 return flexpriority_enabled;
612}
613
Sean Christopherson1e7a4832020-09-23 11:04:02 -0700614static inline int __vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800615{
616 int i;
617
Sean Christophersonfbc18002020-09-23 11:03:59 -0700618 for (i = 0; i < vmx->nr_uret_msrs; ++i)
Sean Christopherson802145c2020-09-23 11:04:09 -0700619 if (vmx_uret_msrs_list[vmx->guest_uret_msrs[i].slot] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300620 return i;
621 return -1;
622}
623
Sean Christophersond85a8032020-09-23 11:04:06 -0700624struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300625{
626 int i;
627
Sean Christopherson1e7a4832020-09-23 11:04:02 -0700628 i = __vmx_find_uret_msr(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300629 if (i >= 0)
Sean Christophersoneb3db1b2020-09-23 11:03:58 -0700630 return &vmx->guest_uret_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000631 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800632}
633
Sean Christopherson7bf662b2020-09-23 11:04:07 -0700634static int vmx_set_guest_uret_msr(struct vcpu_vmx *vmx,
635 struct vmx_uret_msr *msr, u64 data)
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500636{
637 int ret = 0;
638
639 u64 old_msr_data = msr->data;
640 msr->data = data;
Sean Christophersone9bb1ae2020-09-23 11:04:00 -0700641 if (msr - vmx->guest_uret_msrs < vmx->nr_active_uret_msrs) {
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500642 preempt_disable();
Sean Christopherson802145c2020-09-23 11:04:09 -0700643 ret = kvm_set_user_return_msr(msr->slot, msr->data, msr->mask);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500644 preempt_enable();
645 if (ret)
646 msr->data = old_msr_data;
647 }
648 return ret;
649}
650
Dave Young2965faa2015-09-09 15:38:55 -0700651#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800652static void crash_vmclear_local_loaded_vmcss(void)
653{
654 int cpu = raw_smp_processor_id();
655 struct loaded_vmcs *v;
656
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800657 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
658 loaded_vmcss_on_cpu_link)
659 vmcs_clear(v->vmcs);
660}
Dave Young2965faa2015-09-09 15:38:55 -0700661#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800662
Nadav Har'Eld462b812011-05-24 15:26:10 +0300663static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300665 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800666 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667
Nadav Har'Eld462b812011-05-24 15:26:10 +0300668 if (loaded_vmcs->cpu != cpu)
669 return; /* vcpu migration can race with cpu offline */
670 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 per_cpu(current_vmcs, cpu) = NULL;
Sean Christopherson31603d42020-03-21 12:37:49 -0700672
673 vmcs_clear(loaded_vmcs->vmcs);
674 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
675 vmcs_clear(loaded_vmcs->shadow_vmcs);
676
Nadav Har'Eld462b812011-05-24 15:26:10 +0300677 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800678
679 /*
Sean Christopherson31603d42020-03-21 12:37:49 -0700680 * Ensure all writes to loaded_vmcs, including deleting it from its
681 * current percpu list, complete before setting loaded_vmcs->vcpu to
682 * -1, otherwise a different cpu can see vcpu == -1 first and add
683 * loaded_vmcs to its percpu list before it's deleted from this cpu's
684 * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800685 */
686 smp_wmb();
687
Sean Christopherson31603d42020-03-21 12:37:49 -0700688 loaded_vmcs->cpu = -1;
689 loaded_vmcs->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690}
691
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800692void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800693{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800694 int cpu = loaded_vmcs->cpu;
695
696 if (cpu != -1)
697 smp_call_function_single(cpu,
698 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800699}
700
Avi Kivity2fb92db2011-04-27 19:42:18 +0300701static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
702 unsigned field)
703{
704 bool ret;
705 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
706
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700707 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) {
708 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS);
Avi Kivity2fb92db2011-04-27 19:42:18 +0300709 vmx->segment_cache.bitmask = 0;
710 }
711 ret = vmx->segment_cache.bitmask & mask;
712 vmx->segment_cache.bitmask |= mask;
713 return ret;
714}
715
716static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
717{
718 u16 *p = &vmx->segment_cache.seg[seg].selector;
719
720 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
721 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
722 return *p;
723}
724
725static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
726{
727 ulong *p = &vmx->segment_cache.seg[seg].base;
728
729 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
730 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
731 return *p;
732}
733
734static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
735{
736 u32 *p = &vmx->segment_cache.seg[seg].limit;
737
738 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
739 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
740 return *p;
741}
742
743static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
744{
745 u32 *p = &vmx->segment_cache.seg[seg].ar;
746
747 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
748 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
749 return *p;
750}
751
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800752void update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300753{
754 u32 eb;
755
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100756 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800757 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200758 /*
759 * Guest access to VMware backdoor ports could legitimately
760 * trigger #GP because of TSS I/O permission bitmap.
761 * We intercept those #GP and allow access to them anyway
762 * as VMware does.
763 */
764 if (enable_vmware_backdoor)
765 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100766 if ((vcpu->guest_debug &
767 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
768 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
769 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300770 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300771 eb = ~0;
Paolo Bonzinia0c13432020-07-10 17:48:08 +0200772 if (!vmx_need_pf_intercept(vcpu))
Miaohe Lin49f933d2020-02-27 11:20:54 +0800773 eb &= ~(1u << PF_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300774
775 /* When we are running a nested L2 guest and L1 specified for it a
776 * certain exception bitmap, we must trap the same exceptions and pass
777 * them to L1. When running L2, we will only handle the exceptions
778 * specified above if L1 did not want them.
779 */
780 if (is_guest_mode(vcpu))
781 eb |= get_vmcs12(vcpu)->exception_bitmap;
782
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300783 vmcs_write32(EXCEPTION_BITMAP, eb);
784}
785
Ashok Raj15d45072018-02-01 22:59:43 +0100786/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100787 * Check if MSR is intercepted for currently loaded MSR bitmap.
788 */
789static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
790{
791 unsigned long *msr_bitmap;
792 int f = sizeof(unsigned long);
793
794 if (!cpu_has_vmx_msr_bitmap())
795 return true;
796
797 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
798
799 if (msr <= 0x1fff) {
800 return !!test_bit(msr, msr_bitmap + 0x800 / f);
801 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
802 msr &= 0x1fff;
803 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
804 }
805
806 return true;
807}
808
Gleb Natapov2961e8762013-11-25 15:37:13 +0200809static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
810 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200811{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200812 vm_entry_controls_clearbit(vmx, entry);
813 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200814}
815
Sean Christophersona128a932020-09-23 11:03:57 -0700816int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr)
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400817{
818 unsigned int i;
819
820 for (i = 0; i < m->nr; ++i) {
821 if (m->val[i].index == msr)
822 return i;
823 }
824 return -ENOENT;
825}
826
Avi Kivity61d2ef22010-04-28 16:40:38 +0300827static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
828{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400829 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300830 struct msr_autoload *m = &vmx->msr_autoload;
831
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200832 switch (msr) {
833 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800834 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200835 clear_atomic_switch_msr_special(vmx,
836 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200837 VM_EXIT_LOAD_IA32_EFER);
838 return;
839 }
840 break;
841 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800842 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200843 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200844 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
845 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
846 return;
847 }
848 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200849 }
Sean Christophersona128a932020-09-23 11:03:57 -0700850 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400851 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400852 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400853 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400854 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400855 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200856
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400857skip_guest:
Sean Christophersona128a932020-09-23 11:03:57 -0700858 i = vmx_find_loadstore_msr_slot(&m->host, msr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400859 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300860 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400861
862 --m->host.nr;
863 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400864 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300865}
866
Gleb Natapov2961e8762013-11-25 15:37:13 +0200867static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
868 unsigned long entry, unsigned long exit,
869 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
870 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200871{
872 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700873 if (host_val_vmcs != HOST_IA32_EFER)
874 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200875 vm_entry_controls_setbit(vmx, entry);
876 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200877}
878
Avi Kivity61d2ef22010-04-28 16:40:38 +0300879static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400880 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300881{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400882 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300883 struct msr_autoload *m = &vmx->msr_autoload;
884
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200885 switch (msr) {
886 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800887 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200888 add_atomic_switch_msr_special(vmx,
889 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200890 VM_EXIT_LOAD_IA32_EFER,
891 GUEST_IA32_EFER,
892 HOST_IA32_EFER,
893 guest_val, host_val);
894 return;
895 }
896 break;
897 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800898 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200899 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200900 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
901 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
902 GUEST_IA32_PERF_GLOBAL_CTRL,
903 HOST_IA32_PERF_GLOBAL_CTRL,
904 guest_val, host_val);
905 return;
906 }
907 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +0100908 case MSR_IA32_PEBS_ENABLE:
909 /* PEBS needs a quiescent period after being disabled (to write
910 * a record). Disabling PEBS through VMX MSR swapping doesn't
911 * provide that period, so a CPU could write host's record into
912 * guest's memory.
913 */
914 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +0200915 }
916
Sean Christophersona128a932020-09-23 11:03:57 -0700917 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400918 if (!entry_only)
Sean Christophersona128a932020-09-23 11:03:57 -0700919 j = vmx_find_loadstore_msr_slot(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300920
Sean Christophersonce833b22020-09-23 11:03:56 -0700921 if ((i < 0 && m->guest.nr == MAX_NR_LOADSTORE_MSRS) ||
922 (j < 0 && m->host.nr == MAX_NR_LOADSTORE_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +0200923 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +0200924 "Can't add msr %x\n", msr);
925 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300926 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400927 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400928 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400929 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400930 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400931 m->guest.val[i].index = msr;
932 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300933
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400934 if (entry_only)
935 return;
936
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400937 if (j < 0) {
938 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400939 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300940 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400941 m->host.val[j].index = msr;
942 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300943}
944
Sean Christopherson86e3e492020-09-23 11:04:04 -0700945static bool update_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300946{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100947 u64 guest_efer = vmx->vcpu.arch.efer;
948 u64 ignore_bits = 0;
Sean Christopherson86e3e492020-09-23 11:04:04 -0700949 int i;
Eddie Dong2cc51562007-05-21 07:28:09 +0300950
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100951 /* Shadow paging assumes NX to be available. */
952 if (!enable_ept)
953 guest_efer |= EFER_NX;
Roel Kluin3a34a882009-08-04 02:08:45 -0700954
Avi Kivity51c6cf62007-08-29 03:48:05 +0300955 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100956 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +0300957 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100958 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300959#ifdef CONFIG_X86_64
960 ignore_bits |= EFER_LMA | EFER_LME;
961 /* SCE is meaningful only in long mode on Intel */
962 if (guest_efer & EFER_LMA)
963 ignore_bits &= ~(u64)EFER_SCE;
964#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +0300965
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800966 /*
967 * On EPT, we can't emulate NX, so we must switch EFER atomically.
968 * On CPUs that support "load IA32_EFER", always switch EFER
969 * atomically, since it's faster than switching it manually.
970 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800971 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800972 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +0300973 if (!(guest_efer & EFER_LMA))
974 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -0800975 if (guest_efer != host_efer)
976 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400977 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -0700978 else
979 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +0300980 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100981 }
Sean Christopherson86e3e492020-09-23 11:04:04 -0700982
983 i = __vmx_find_uret_msr(vmx, MSR_EFER);
984 if (i < 0)
985 return false;
986
987 clear_atomic_switch_msr(vmx, MSR_EFER);
988
989 guest_efer &= ~ignore_bits;
990 guest_efer |= host_efer & ignore_bits;
991
992 vmx->guest_uret_msrs[i].data = guest_efer;
993 vmx->guest_uret_msrs[i].mask = ~ignore_bits;
994
995 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300996}
997
Andy Lutomirskie28baea2017-02-20 08:56:11 -0800998#ifdef CONFIG_X86_32
999/*
1000 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1001 * VMCS rather than the segment table. KVM uses this helper to figure
1002 * out the current bases to poke them into the VMCS before entry.
1003 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001004static unsigned long segment_base(u16 selector)
1005{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001006 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001007 unsigned long v;
1008
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001009 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001010 return 0;
1011
Thomas Garnier45fc8752017-03-14 10:05:08 -07001012 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001013
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001014 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001015 u16 ldt_selector = kvm_read_ldt();
1016
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001017 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001018 return 0;
1019
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001020 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001021 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001022 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001023 return v;
1024}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001025#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001026
Sean Christophersone348ac72019-12-10 15:24:33 -08001027static inline bool pt_can_write_msr(struct vcpu_vmx *vmx)
1028{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001029 return vmx_pt_mode_is_host_guest() &&
Sean Christophersone348ac72019-12-10 15:24:33 -08001030 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
1031}
1032
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001033static inline bool pt_output_base_valid(struct kvm_vcpu *vcpu, u64 base)
1034{
1035 /* The base must be 128-byte aligned and a legal physical address. */
Sean Christopherson7096cbf2020-09-24 12:42:50 -07001036 return !kvm_vcpu_is_illegal_gpa(vcpu, base) && !(base & 0x7f);
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001037}
1038
Chao Peng2ef444f2018-10-24 16:05:12 +08001039static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range)
1040{
1041 u32 i;
1042
1043 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1044 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1045 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1046 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1047 for (i = 0; i < addr_range; i++) {
1048 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1049 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1050 }
1051}
1052
1053static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range)
1054{
1055 u32 i;
1056
1057 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1058 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1059 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1060 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1061 for (i = 0; i < addr_range; i++) {
1062 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1063 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1064 }
1065}
1066
1067static void pt_guest_enter(struct vcpu_vmx *vmx)
1068{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001069 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001070 return;
1071
Chao Peng2ef444f2018-10-24 16:05:12 +08001072 /*
Chao Pengb08c2892018-10-24 16:05:15 +08001073 * GUEST_IA32_RTIT_CTL is already set in the VMCS.
1074 * Save host state before VM entry.
Chao Peng2ef444f2018-10-24 16:05:12 +08001075 */
Chao Pengb08c2892018-10-24 16:05:15 +08001076 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
Chao Peng2ef444f2018-10-24 16:05:12 +08001077 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1078 wrmsrl(MSR_IA32_RTIT_CTL, 0);
1079 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1080 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1081 }
1082}
1083
1084static void pt_guest_exit(struct vcpu_vmx *vmx)
1085{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001086 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001087 return;
1088
1089 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1090 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1091 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1092 }
1093
1094 /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */
1095 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
1096}
1097
Sean Christopherson13b964a2019-05-07 09:06:31 -07001098void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1099 unsigned long fs_base, unsigned long gs_base)
1100{
1101 if (unlikely(fs_sel != host->fs_sel)) {
1102 if (!(fs_sel & 7))
1103 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1104 else
1105 vmcs_write16(HOST_FS_SELECTOR, 0);
1106 host->fs_sel = fs_sel;
1107 }
1108 if (unlikely(gs_sel != host->gs_sel)) {
1109 if (!(gs_sel & 7))
1110 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1111 else
1112 vmcs_write16(HOST_GS_SELECTOR, 0);
1113 host->gs_sel = gs_sel;
1114 }
1115 if (unlikely(fs_base != host->fs_base)) {
1116 vmcs_writel(HOST_FS_BASE, fs_base);
1117 host->fs_base = fs_base;
1118 }
1119 if (unlikely(gs_base != host->gs_base)) {
1120 vmcs_writel(HOST_GS_BASE, gs_base);
1121 host->gs_base = gs_base;
1122 }
1123}
1124
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001125void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001126{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001127 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001128 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001129#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01001130 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001131#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001132 unsigned long fs_base, gs_base;
1133 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03001134 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001135
Sean Christophersond264ee02018-08-27 15:21:12 -07001136 vmx->req_immediate_exit = false;
1137
Liran Alonf48b4712018-11-20 18:03:25 +02001138 /*
1139 * Note that guest MSRs to be saved/restored can also be changed
1140 * when guest state is loaded. This happens when guest transitions
1141 * to/from long-mode by setting MSR_EFER.LMA.
1142 */
Sean Christopherson658ece82020-09-23 11:04:01 -07001143 if (!vmx->guest_uret_msrs_loaded) {
1144 vmx->guest_uret_msrs_loaded = true;
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001145 for (i = 0; i < vmx->nr_active_uret_msrs; ++i)
Sean Christopherson802145c2020-09-23 11:04:09 -07001146 kvm_set_user_return_msr(vmx->guest_uret_msrs[i].slot,
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001147 vmx->guest_uret_msrs[i].data,
1148 vmx->guest_uret_msrs[i].mask);
Liran Alonf48b4712018-11-20 18:03:25 +02001149
1150 }
wanpeng lic9dfd3f2020-02-17 18:37:43 +08001151
1152 if (vmx->nested.need_vmcs12_to_shadow_sync)
1153 nested_sync_vmcs12_to_shadow(vcpu);
1154
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001155 if (vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001156 return;
1157
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001158 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001159
Avi Kivity33ed6322007-05-02 16:54:03 +03001160 /*
1161 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1162 * allow segment selectors with cpl > 0 or ti == 1.
1163 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07001164 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01001165
1166#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001167 savesegment(ds, host_state->ds_sel);
1168 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07001169
1170 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001171 if (likely(is_64bit_mm(current->mm))) {
Thomas Gleixner67580342020-05-28 16:13:52 -04001172 current_save_fsgs();
Sean Christophersone368b872018-07-23 12:32:41 -07001173 fs_sel = current->thread.fsindex;
1174 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001175 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07001176 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001177 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07001178 savesegment(fs, fs_sel);
1179 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001180 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07001181 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03001182 }
1183
Paolo Bonzini4679b612018-09-24 17:23:01 +02001184 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03001185#else
Sean Christophersone368b872018-07-23 12:32:41 -07001186 savesegment(fs, fs_sel);
1187 savesegment(gs, gs_sel);
1188 fs_base = segment_base(fs_sel);
1189 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001190#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001191
Sean Christopherson13b964a2019-05-07 09:06:31 -07001192 vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001193 vmx->guest_state_loaded = true;
Avi Kivity33ed6322007-05-02 16:54:03 +03001194}
1195
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001196static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001197{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001198 struct vmcs_host_state *host_state;
1199
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001200 if (!vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001201 return;
1202
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001203 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001204
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001205 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001206
Avi Kivityc8770e72010-11-11 12:37:26 +02001207#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001208 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001209#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001210 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1211 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001212#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001213 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001214#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001215 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001216#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001217 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001218 if (host_state->fs_sel & 7)
1219 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001220#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001221 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1222 loadsegment(ds, host_state->ds_sel);
1223 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001224 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001225#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001226 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001227#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001228 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001229#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001230 load_fixmap_gdt(raw_smp_processor_id());
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001231 vmx->guest_state_loaded = false;
Sean Christopherson658ece82020-09-23 11:04:01 -07001232 vmx->guest_uret_msrs_loaded = false;
Avi Kivity33ed6322007-05-02 16:54:03 +03001233}
1234
Sean Christopherson678e3152018-07-23 12:32:43 -07001235#ifdef CONFIG_X86_64
1236static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001237{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001238 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001239 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001240 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1241 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001242 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001243}
1244
Sean Christopherson678e3152018-07-23 12:32:43 -07001245static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1246{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001247 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001248 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001249 wrmsrl(MSR_KERNEL_GS_BASE, data);
1250 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001251 vmx->msr_guest_kernel_gs_base = data;
1252}
1253#endif
1254
Sean Christopherson5c911be2020-05-01 09:31:17 -07001255void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
1256 struct loaded_vmcs *buddy)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001258 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001259 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Sean Christopherson5c911be2020-05-01 09:31:17 -07001260 struct vmcs *prev;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001262 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001263 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001264 local_irq_disable();
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001265
1266 /*
Sean Christopherson31603d42020-03-21 12:37:49 -07001267 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
1268 * this cpu's percpu list, otherwise it may not yet be deleted
1269 * from its previous cpu's percpu list. Pairs with the
1270 * smb_wmb() in __loaded_vmcs_clear().
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001271 */
1272 smp_rmb();
1273
Nadav Har'Eld462b812011-05-24 15:26:10 +03001274 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1275 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001276 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001277 }
1278
Sean Christopherson5c911be2020-05-01 09:31:17 -07001279 prev = per_cpu(current_vmcs, cpu);
1280 if (prev != vmx->loaded_vmcs->vmcs) {
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001281 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1282 vmcs_load(vmx->loaded_vmcs->vmcs);
Sean Christopherson5c911be2020-05-01 09:31:17 -07001283
1284 /*
1285 * No indirect branch prediction barrier needed when switching
1286 * the active VMCS within a guest, e.g. on nested VM-Enter.
1287 * The L1 VMM can protect itself with retpolines, IBPB or IBRS.
1288 */
1289 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev))
1290 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001291 }
1292
1293 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001294 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001295 unsigned long sysenter_esp;
1296
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001297 /*
1298 * Flush all EPTP/VPID contexts, the new pCPU may have stale
1299 * TLB entries from its previous association with the vCPU.
1300 */
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001301 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001302
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303 /*
1304 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001305 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001307 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001308 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001309 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310
1311 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1312 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001313
Nadav Har'Eld462b812011-05-24 15:26:10 +03001314 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315 }
Feng Wu28b835d2015-09-18 22:29:54 +08001316
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001317 /* Setup TSC multiplier */
1318 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001319 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1320 decache_tsc_multiplier(vmx);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001321}
1322
1323/*
1324 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1325 * vcpu mutex is already taken.
1326 */
Sean Christopherson1af1bb02020-05-06 16:58:50 -07001327static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001328{
1329 struct vcpu_vmx *vmx = to_vmx(vcpu);
1330
Sean Christopherson5c911be2020-05-01 09:31:17 -07001331 vmx_vcpu_load_vmcs(vcpu, cpu, NULL);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001332
Feng Wu28b835d2015-09-18 22:29:54 +08001333 vmx_vcpu_pi_load(vcpu, cpu);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001334
Wanpeng Li74c55932017-11-29 01:31:20 -08001335 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001336}
1337
Sean Christopherson13b964a2019-05-07 09:06:31 -07001338static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339{
Feng Wu28b835d2015-09-18 22:29:54 +08001340 vmx_vcpu_pi_put(vcpu);
1341
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001342 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343}
1344
Wanpeng Lif244dee2017-07-20 01:11:54 -07001345static bool emulation_required(struct kvm_vcpu *vcpu)
1346{
Sean Christopherson2ba44932020-09-23 11:44:48 -07001347 return emulate_invalid_guest_state && !vmx_guest_state_valid(vcpu);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001348}
1349
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001350unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001352 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001353 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001354
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001355 if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) {
1356 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Avi Kivity6de12732011-03-07 12:51:22 +02001357 rflags = vmcs_readl(GUEST_RFLAGS);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001358 if (vmx->rmode.vm86_active) {
Avi Kivity6de12732011-03-07 12:51:22 +02001359 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
Sean Christophersone7bddc52019-09-27 14:45:18 -07001360 save_rflags = vmx->rmode.save_rflags;
Avi Kivity6de12732011-03-07 12:51:22 +02001361 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1362 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001363 vmx->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001364 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001365 return vmx->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366}
1367
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001368void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001370 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001371 unsigned long old_rflags;
Wanpeng Lif244dee2017-07-20 01:11:54 -07001372
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00001373 if (is_unrestricted_guest(vcpu)) {
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001374 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001375 vmx->rflags = rflags;
1376 vmcs_writel(GUEST_RFLAGS, rflags);
1377 return;
1378 }
1379
1380 old_rflags = vmx_get_rflags(vcpu);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001381 vmx->rflags = rflags;
1382 if (vmx->rmode.vm86_active) {
1383 vmx->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001384 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001385 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001387
Sean Christophersone7bddc52019-09-27 14:45:18 -07001388 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM)
1389 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390}
1391
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001392u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001393{
1394 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1395 int ret = 0;
1396
1397 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001398 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001399 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001400 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001401
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001402 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001403}
1404
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001405void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001406{
1407 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1408 u32 interruptibility = interruptibility_old;
1409
1410 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1411
Jan Kiszka48005f62010-02-19 19:38:07 +01001412 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001413 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001414 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001415 interruptibility |= GUEST_INTR_STATE_STI;
1416
1417 if ((interruptibility != interruptibility_old))
1418 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1419}
1420
Chao Pengbf8c55d2018-10-24 16:05:14 +08001421static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data)
1422{
1423 struct vcpu_vmx *vmx = to_vmx(vcpu);
1424 unsigned long value;
1425
1426 /*
1427 * Any MSR write that attempts to change bits marked reserved will
1428 * case a #GP fault.
1429 */
1430 if (data & vmx->pt_desc.ctl_bitmask)
1431 return 1;
1432
1433 /*
1434 * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will
1435 * result in a #GP unless the same write also clears TraceEn.
1436 */
1437 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) &&
1438 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN))
1439 return 1;
1440
1441 /*
1442 * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit
1443 * and FabricEn would cause #GP, if
1444 * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0
1445 */
1446 if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) &&
1447 !(data & RTIT_CTL_FABRIC_EN) &&
1448 !intel_pt_validate_cap(vmx->pt_desc.caps,
1449 PT_CAP_single_range_output))
1450 return 1;
1451
1452 /*
1453 * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that
1454 * utilize encodings marked reserved will casue a #GP fault.
1455 */
1456 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods);
1457 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) &&
1458 !test_bit((data & RTIT_CTL_MTC_RANGE) >>
1459 RTIT_CTL_MTC_RANGE_OFFSET, &value))
1460 return 1;
1461 value = intel_pt_validate_cap(vmx->pt_desc.caps,
1462 PT_CAP_cycle_thresholds);
1463 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1464 !test_bit((data & RTIT_CTL_CYC_THRESH) >>
1465 RTIT_CTL_CYC_THRESH_OFFSET, &value))
1466 return 1;
1467 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods);
1468 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1469 !test_bit((data & RTIT_CTL_PSB_FREQ) >>
1470 RTIT_CTL_PSB_FREQ_OFFSET, &value))
1471 return 1;
1472
1473 /*
1474 * If ADDRx_CFG is reserved or the encodings is >2 will
1475 * cause a #GP fault.
1476 */
1477 value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET;
1478 if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2))
1479 return 1;
1480 value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET;
1481 if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2))
1482 return 1;
1483 value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET;
1484 if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2))
1485 return 1;
1486 value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET;
1487 if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2))
1488 return 1;
1489
1490 return 0;
1491}
1492
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07001493static bool vmx_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
1494{
1495 return true;
1496}
1497
Sean Christopherson1957aa62019-08-27 14:40:39 -07001498static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499{
Paolo Bonzinifede8072020-04-27 11:55:59 -04001500 unsigned long rip, orig_rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501
Sean Christopherson1957aa62019-08-27 14:40:39 -07001502 /*
1503 * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on
1504 * undefined behavior: Intel's SDM doesn't mandate the VMCS field be
1505 * set when EPT misconfig occurs. In practice, real hardware updates
1506 * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors
1507 * (namely Hyper-V) don't set it due to it being undefined behavior,
1508 * i.e. we end up advancing IP with some random value.
1509 */
1510 if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
1511 to_vmx(vcpu)->exit_reason != EXIT_REASON_EPT_MISCONFIG) {
Paolo Bonzinifede8072020-04-27 11:55:59 -04001512 orig_rip = kvm_rip_read(vcpu);
1513 rip = orig_rip + vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
1514#ifdef CONFIG_X86_64
1515 /*
1516 * We need to mask out the high 32 bits of RIP if not in 64-bit
1517 * mode, but just finding out that we are in 64-bit mode is
1518 * quite expensive. Only do it if there was a carry.
1519 */
1520 if (unlikely(((rip ^ orig_rip) >> 31) == 3) && !is_64_bit_mode(vcpu))
1521 rip = (u32)rip;
1522#endif
Sean Christopherson1957aa62019-08-27 14:40:39 -07001523 kvm_rip_write(vcpu, rip);
1524 } else {
1525 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
1526 return 0;
1527 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528
Glauber Costa2809f5d2009-05-12 16:21:05 -04001529 /* skipping an emulated instruction also counts */
1530 vmx_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001531
Sean Christopherson60fc3d02019-08-27 14:40:38 -07001532 return 1;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001533}
1534
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02001535/*
Oliver Upton5ef8acb2020-02-07 02:36:07 -08001536 * Recognizes a pending MTF VM-exit and records the nested state for later
1537 * delivery.
1538 */
1539static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)
1540{
1541 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1542 struct vcpu_vmx *vmx = to_vmx(vcpu);
1543
1544 if (!is_guest_mode(vcpu))
1545 return;
1546
1547 /*
1548 * Per the SDM, MTF takes priority over debug-trap exceptions besides
1549 * T-bit traps. As instruction emulation is completed (i.e. at the
1550 * instruction boundary), any #DB exception pending delivery must be a
1551 * debug-trap. Record the pending MTF state to be delivered in
1552 * vmx_check_nested_events().
1553 */
1554 if (nested_cpu_has_mtf(vmcs12) &&
1555 (!vcpu->arch.exception.pending ||
1556 vcpu->arch.exception.nr == DB_VECTOR))
1557 vmx->nested.mtf_pending = true;
1558 else
1559 vmx->nested.mtf_pending = false;
1560}
1561
1562static int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu)
1563{
1564 vmx_update_emulated_instruction(vcpu);
1565 return skip_emulated_instruction(vcpu);
1566}
1567
Wanpeng Licaa057a2018-03-12 04:53:03 -07001568static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1569{
1570 /*
1571 * Ensure that we clear the HLT state in the VMCS. We don't need to
1572 * explicitly skip the instruction because if the HLT state is set,
1573 * then the instruction is already executing and RIP has already been
1574 * advanced.
1575 */
1576 if (kvm_hlt_in_guest(vcpu->kvm) &&
1577 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1578 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1579}
1580
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001581static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001582{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001583 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001584 unsigned nr = vcpu->arch.exception.nr;
1585 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001586 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001587 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001588
Jim Mattsonda998b42018-10-16 14:29:22 -07001589 kvm_deliver_exception_payload(vcpu);
1590
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001591 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001592 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001593 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1594 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001595
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001596 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001597 int inc_eip = 0;
1598 if (kvm_exception_is_soft(nr))
1599 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07001600 kvm_inject_realmode_interrupt(vcpu, nr, inc_eip);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001601 return;
1602 }
1603
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001604 WARN_ON_ONCE(vmx->emulation_required);
1605
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001606 if (kvm_exception_is_soft(nr)) {
1607 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1608 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001609 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1610 } else
1611 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1612
1613 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001614
1615 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001616}
1617
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001618static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001619{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001620 struct vmx_uret_msr tmp;
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001621 int from, to;
1622
1623 from = __vmx_find_uret_msr(vmx, msr);
1624 if (from < 0)
1625 return;
1626 to = vmx->nr_active_uret_msrs++;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001627
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001628 tmp = vmx->guest_uret_msrs[to];
1629 vmx->guest_uret_msrs[to] = vmx->guest_uret_msrs[from];
1630 vmx->guest_uret_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001631}
1632
1633/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001634 * Set up the vmcs to automatically save and restore system
1635 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1636 * mode, as fiddling with msrs is very expensive.
1637 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001638static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001639{
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001640 vmx->guest_uret_msrs_loaded = false;
1641 vmx->nr_active_uret_msrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001642#ifdef CONFIG_X86_64
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001643 /*
1644 * The SYSCALL MSRs are only needed on long mode guests, and only
1645 * when EFER.SCE is set.
1646 */
1647 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001648 vmx_setup_uret_msr(vmx, MSR_STAR);
1649 vmx_setup_uret_msr(vmx, MSR_LSTAR);
1650 vmx_setup_uret_msr(vmx, MSR_SYSCALL_MASK);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001651 }
Eddie Donga75beee2007-05-17 18:55:15 +03001652#endif
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001653 if (update_transition_efer(vmx))
1654 vmx_setup_uret_msr(vmx, MSR_EFER);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001655
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001656 if (guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
1657 vmx_setup_uret_msr(vmx, MSR_TSC_AUX);
1658
1659 vmx_setup_uret_msr(vmx, MSR_IA32_TSX_CTRL);
Avi Kivity58972972009-02-24 22:26:47 +02001660
Yang Zhang8d146952013-01-25 10:18:50 +08001661 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001662 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001663}
1664
Leonid Shatz326e7422018-11-06 12:14:25 +02001665static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001667 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1668 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001669
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001670 /*
1671 * We're here if L1 chose not to trap WRMSR to TSC. According
1672 * to the spec, this should set L1's TSC; The offset that L1
1673 * set for L2 remains unchanged, and still needs to be added
1674 * to the newly set TSC to get L2's TSC.
1675 */
1676 if (is_guest_mode(vcpu) &&
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08001677 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001678 g_tsc_offset = vmcs12->tsc_offset;
1679
1680 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1681 vcpu->arch.tsc_offset - g_tsc_offset,
1682 offset);
1683 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1684 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685}
1686
Nadav Har'El801d3422011-05-25 23:02:23 +03001687/*
1688 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1689 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1690 * all guests if the "nested" module option is off, and can also be disabled
1691 * for a single guest by disabling its VMX cpuid bit.
1692 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001693bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001694{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001695 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001696}
1697
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001698static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1699 uint64_t val)
1700{
1701 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1702
1703 return !(val & ~valid_bits);
1704}
1705
Tom Lendacky801e4592018-02-21 13:39:51 -06001706static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1707{
Paolo Bonzini13893092018-02-26 13:40:09 +01001708 switch (msr->index) {
1709 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1710 if (!nested)
1711 return 1;
1712 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
Like Xu27461da32020-05-29 15:43:45 +08001713 case MSR_IA32_PERF_CAPABILITIES:
1714 msr->data = vmx_get_perf_capabilities();
1715 return 0;
Paolo Bonzini13893092018-02-26 13:40:09 +01001716 default:
Peter Xu12bc2132020-06-22 18:04:42 -04001717 return KVM_MSR_RET_INVALID;
Paolo Bonzini13893092018-02-26 13:40:09 +01001718 }
Tom Lendacky801e4592018-02-21 13:39:51 -06001719}
1720
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001721/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 * Reads an msr value (of 'msr_index') into 'pdata'.
1723 * Returns 0 on success, non-0 otherwise.
1724 * Assumes vcpu_load() was already called.
1725 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001726static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001728 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001729 struct vmx_uret_msr *msr;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001730 u32 index;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001732 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001733#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001735 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736 break;
1737 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001738 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001740 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001741 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001742 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001743#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001745 return kvm_get_msr_common(vcpu, msr_info);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001746 case MSR_IA32_TSX_CTRL:
1747 if (!msr_info->host_initiated &&
1748 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
1749 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001750 goto find_uret_msr;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001751 case MSR_IA32_UMWAIT_CONTROL:
1752 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
1753 return 1;
1754
1755 msr_info->data = vmx->msr_ia32_umwait_control;
1756 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001757 case MSR_IA32_SPEC_CTRL:
1758 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001759 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1760 return 1;
1761
1762 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1763 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001765 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766 break;
1767 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001768 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769 break;
1770 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001771 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001773 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001774 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001775 (!msr_info->host_initiated &&
1776 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001777 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001778 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001779 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001780 case MSR_IA32_MCG_EXT_CTL:
1781 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001782 !(vmx->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001783 FEAT_CTL_LMCE_ENABLED))
Jan Kiszkacae50132014-01-04 18:47:22 +01001784 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001785 msr_info->data = vcpu->arch.mcg_ext_ctl;
1786 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001787 case MSR_IA32_FEAT_CTL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001788 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001789 break;
1790 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1791 if (!nested_vmx_allowed(vcpu))
1792 return 1;
Vitaly Kuznetsov31de3d22020-02-05 13:30:33 +01001793 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1794 &msr_info->data))
1795 return 1;
1796 /*
1797 * Enlightened VMCS v1 doesn't have certain fields, but buggy
1798 * Hyper-V versions are still trying to use corresponding
1799 * features when they are exposed. Filter out the essential
1800 * minimum.
1801 */
1802 if (!msr_info->host_initiated &&
1803 vmx->nested.enlightened_vmcs_enabled)
1804 nested_evmcs_filter_control_msr(msr_info->index,
1805 &msr_info->data);
1806 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001807 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001808 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001809 return 1;
1810 msr_info->data = vmx->pt_desc.guest.ctl;
1811 break;
1812 case MSR_IA32_RTIT_STATUS:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001813 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001814 return 1;
1815 msr_info->data = vmx->pt_desc.guest.status;
1816 break;
1817 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001818 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001819 !intel_pt_validate_cap(vmx->pt_desc.caps,
1820 PT_CAP_cr3_filtering))
1821 return 1;
1822 msr_info->data = vmx->pt_desc.guest.cr3_match;
1823 break;
1824 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001825 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001826 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1827 PT_CAP_topa_output) &&
1828 !intel_pt_validate_cap(vmx->pt_desc.caps,
1829 PT_CAP_single_range_output)))
1830 return 1;
1831 msr_info->data = vmx->pt_desc.guest.output_base;
1832 break;
1833 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001834 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001835 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1836 PT_CAP_topa_output) &&
1837 !intel_pt_validate_cap(vmx->pt_desc.caps,
1838 PT_CAP_single_range_output)))
1839 return 1;
1840 msr_info->data = vmx->pt_desc.guest.output_mask;
1841 break;
1842 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
1843 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christopherson2ef76192020-03-02 15:56:22 -08001844 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001845 (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
1846 PT_CAP_num_address_ranges)))
1847 return 1;
1848 if (index % 2)
1849 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
1850 else
1851 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
1852 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001853 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001854 if (!msr_info->host_initiated &&
1855 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001856 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001857 goto find_uret_msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001859 find_uret_msr:
Sean Christophersond85a8032020-09-23 11:04:06 -07001860 msr = vmx_find_uret_msr(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001861 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001862 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001863 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001865 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866 }
1867
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868 return 0;
1869}
1870
Sean Christopherson24085002020-04-28 16:10:24 -07001871static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu,
1872 u64 data)
1873{
1874#ifdef CONFIG_X86_64
1875 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
1876 return (u32)data;
1877#endif
1878 return (unsigned long)data;
1879}
1880
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881/*
Miaohe Lin311497e2019-12-11 14:26:25 +08001882 * Writes msr value into the appropriate "register".
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883 * Returns 0 on success, non-0 otherwise.
1884 * Assumes vcpu_load() was already called.
1885 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001886static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001888 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001889 struct vmx_uret_msr *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001890 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08001891 u32 msr_index = msr_info->index;
1892 u64 data = msr_info->data;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001893 u32 index;
Eddie Dong2cc51562007-05-21 07:28:09 +03001894
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001896 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08001897 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03001898 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001899#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001901 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902 vmcs_writel(GUEST_FS_BASE, data);
1903 break;
1904 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001905 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 vmcs_writel(GUEST_GS_BASE, data);
1907 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001908 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001909 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001910 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911#endif
1912 case MSR_IA32_SYSENTER_CS:
Sean Christophersonde70d272019-05-07 09:06:36 -07001913 if (is_guest_mode(vcpu))
1914 get_vmcs12(vcpu)->guest_sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 vmcs_write32(GUEST_SYSENTER_CS, data);
1916 break;
1917 case MSR_IA32_SYSENTER_EIP:
Sean Christopherson24085002020-04-28 16:10:24 -07001918 if (is_guest_mode(vcpu)) {
1919 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07001920 get_vmcs12(vcpu)->guest_sysenter_eip = data;
Sean Christopherson24085002020-04-28 16:10:24 -07001921 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02001922 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923 break;
1924 case MSR_IA32_SYSENTER_ESP:
Sean Christopherson24085002020-04-28 16:10:24 -07001925 if (is_guest_mode(vcpu)) {
1926 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07001927 get_vmcs12(vcpu)->guest_sysenter_esp = data;
Sean Christopherson24085002020-04-28 16:10:24 -07001928 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02001929 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930 break;
Sean Christopherson699a1ac2019-05-07 09:06:37 -07001931 case MSR_IA32_DEBUGCTLMSR:
1932 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
1933 VM_EXIT_SAVE_DEBUG_CONTROLS)
1934 get_vmcs12(vcpu)->guest_ia32_debugctl = data;
1935
1936 ret = kvm_set_msr_common(vcpu, msr_info);
1937 break;
1938
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001939 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001940 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001941 (!msr_info->host_initiated &&
1942 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001943 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001944 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07001945 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08001947 vmcs_write64(GUEST_BNDCFGS, data);
1948 break;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001949 case MSR_IA32_UMWAIT_CONTROL:
1950 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
1951 return 1;
1952
1953 /* The reserved bit 1 and non-32 bit [63:32] should be zero */
1954 if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32)))
1955 return 1;
1956
1957 vmx->msr_ia32_umwait_control = data;
1958 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001959 case MSR_IA32_SPEC_CTRL:
1960 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001961 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1962 return 1;
1963
Maxim Levitsky841c2be2020-07-08 14:57:31 +03001964 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001965 return 1;
1966
1967 vmx->spec_ctrl = data;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001968 if (!data)
1969 break;
1970
1971 /*
1972 * For non-nested:
1973 * When it's written (to non-zero) for the first time, pass
1974 * it through.
1975 *
1976 * For nested:
1977 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08001978 * nested_vmx_prepare_msr_bitmap. We should not touch the
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001979 * vmcs02.msr_bitmap here since it gets completely overwritten
1980 * in the merging. We update the vmcs01 here for L1 as well
1981 * since it will end up touching the MSR anyway now.
1982 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02001983 vmx_disable_intercept_for_msr(vcpu,
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001984 MSR_IA32_SPEC_CTRL,
1985 MSR_TYPE_RW);
1986 break;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001987 case MSR_IA32_TSX_CTRL:
1988 if (!msr_info->host_initiated &&
1989 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
1990 return 1;
1991 if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR))
1992 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001993 goto find_uret_msr;
Ashok Raj15d45072018-02-01 22:59:43 +01001994 case MSR_IA32_PRED_CMD:
1995 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01001996 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1997 return 1;
1998
1999 if (data & ~PRED_CMD_IBPB)
2000 return 1;
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002001 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL))
2002 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002003 if (!data)
2004 break;
2005
2006 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2007
2008 /*
2009 * For non-nested:
2010 * When it's written (to non-zero) for the first time, pass
2011 * it through.
2012 *
2013 * For nested:
2014 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002015 * nested_vmx_prepare_msr_bitmap. We should not touch the
Ashok Raj15d45072018-02-01 22:59:43 +01002016 * vmcs02.msr_bitmap here since it gets completely overwritten
2017 * in the merging.
2018 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002019 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W);
Ashok Raj15d45072018-02-01 22:59:43 +01002020 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002021 case MSR_IA32_CR_PAT:
Sean Christophersond28f4292019-05-07 09:06:27 -07002022 if (!kvm_pat_valid(data))
2023 return 1;
2024
Sean Christopherson142e4be2019-05-07 09:06:35 -07002025 if (is_guest_mode(vcpu) &&
2026 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
2027 get_vmcs12(vcpu)->guest_ia32_pat = data;
2028
Sheng Yang468d4722008-10-09 16:01:55 +08002029 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2030 vmcs_write64(GUEST_IA32_PAT, data);
2031 vcpu->arch.pat = data;
2032 break;
2033 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002034 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002035 break;
Will Auldba904632012-11-29 12:42:50 -08002036 case MSR_IA32_TSC_ADJUST:
2037 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002038 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002039 case MSR_IA32_MCG_EXT_CTL:
2040 if ((!msr_info->host_initiated &&
2041 !(to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002042 FEAT_CTL_LMCE_ENABLED)) ||
Ashok Rajc45dcc72016-06-22 14:59:56 +08002043 (data & ~MCG_EXT_CTL_LMCE_EN))
2044 return 1;
2045 vcpu->arch.mcg_ext_ctl = data;
2046 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002047 case MSR_IA32_FEAT_CTL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002048 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08002049 (to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002050 FEAT_CTL_LOCKED && !msr_info->host_initiated))
Jan Kiszkacae50132014-01-04 18:47:22 +01002051 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08002052 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002053 if (msr_info->host_initiated && data == 0)
2054 vmx_leave_nested(vcpu);
2055 break;
2056 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08002057 if (!msr_info->host_initiated)
2058 return 1; /* they are read-only */
2059 if (!nested_vmx_allowed(vcpu))
2060 return 1;
2061 return vmx_set_vmx_msr(vcpu, msr_index, data);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002062 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08002063 if (!vmx_pt_mode_is_host_guest() ||
Luwei Kangee85dec2018-10-24 16:05:16 +08002064 vmx_rtit_ctl_check(vcpu, data) ||
2065 vmx->nested.vmxon)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002066 return 1;
2067 vmcs_write64(GUEST_IA32_RTIT_CTL, data);
2068 vmx->pt_desc.guest.ctl = data;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002069 pt_update_intercept_for_msr(vcpu);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002070 break;
2071 case MSR_IA32_RTIT_STATUS:
Sean Christophersone348ac72019-12-10 15:24:33 -08002072 if (!pt_can_write_msr(vmx))
2073 return 1;
2074 if (data & MSR_IA32_RTIT_STATUS_MASK)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002075 return 1;
2076 vmx->pt_desc.guest.status = data;
2077 break;
2078 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christophersone348ac72019-12-10 15:24:33 -08002079 if (!pt_can_write_msr(vmx))
2080 return 1;
2081 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2082 PT_CAP_cr3_filtering))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002083 return 1;
2084 vmx->pt_desc.guest.cr3_match = data;
2085 break;
2086 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christophersone348ac72019-12-10 15:24:33 -08002087 if (!pt_can_write_msr(vmx))
2088 return 1;
2089 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2090 PT_CAP_topa_output) &&
2091 !intel_pt_validate_cap(vmx->pt_desc.caps,
2092 PT_CAP_single_range_output))
2093 return 1;
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07002094 if (!pt_output_base_valid(vcpu, data))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002095 return 1;
2096 vmx->pt_desc.guest.output_base = data;
2097 break;
2098 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christophersone348ac72019-12-10 15:24:33 -08002099 if (!pt_can_write_msr(vmx))
2100 return 1;
2101 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2102 PT_CAP_topa_output) &&
2103 !intel_pt_validate_cap(vmx->pt_desc.caps,
2104 PT_CAP_single_range_output))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002105 return 1;
2106 vmx->pt_desc.guest.output_mask = data;
2107 break;
2108 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christophersone348ac72019-12-10 15:24:33 -08002109 if (!pt_can_write_msr(vmx))
2110 return 1;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002111 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christophersone348ac72019-12-10 15:24:33 -08002112 if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
2113 PT_CAP_num_address_ranges))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002114 return 1;
Sean Christophersonfe6ed362019-12-10 15:24:32 -08002115 if (is_noncanonical_address(data, vcpu))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002116 return 1;
2117 if (index % 2)
2118 vmx->pt_desc.guest.addr_b[index / 2] = data;
2119 else
2120 vmx->pt_desc.guest.addr_a[index / 2] = data;
2121 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002122 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002123 if (!msr_info->host_initiated &&
2124 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002125 return 1;
2126 /* Check reserved bit, higher 32 bits should be zero */
2127 if ((data >> 32) != 0)
2128 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002129 goto find_uret_msr;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002130
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002132 find_uret_msr:
Sean Christophersond85a8032020-09-23 11:04:06 -07002133 msr = vmx_find_uret_msr(vmx, msr_index);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05002134 if (msr)
Sean Christopherson7bf662b2020-09-23 11:04:07 -07002135 ret = vmx_set_guest_uret_msr(vmx, msr, data);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05002136 else
2137 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138 }
2139
Eddie Dong2cc51562007-05-21 07:28:09 +03002140 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141}
2142
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002143static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144{
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002145 unsigned long guest_owned_bits;
2146
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002147 kvm_register_mark_available(vcpu, reg);
2148
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002149 switch (reg) {
2150 case VCPU_REGS_RSP:
2151 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2152 break;
2153 case VCPU_REGS_RIP:
2154 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2155 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002156 case VCPU_EXREG_PDPTR:
2157 if (enable_ept)
2158 ept_save_pdptrs(vcpu);
2159 break;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07002160 case VCPU_EXREG_CR0:
2161 guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2162
2163 vcpu->arch.cr0 &= ~guest_owned_bits;
2164 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits;
2165 break;
Sean Christopherson34059c22019-09-27 14:45:23 -07002166 case VCPU_EXREG_CR3:
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002167 if (is_unrestricted_guest(vcpu) ||
2168 (enable_ept && is_paging(vcpu)))
Sean Christopherson34059c22019-09-27 14:45:23 -07002169 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2170 break;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002171 case VCPU_EXREG_CR4:
2172 guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2173
2174 vcpu->arch.cr4 &= ~guest_owned_bits;
2175 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits;
2176 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002177 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07002178 WARN_ON_ONCE(1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002179 break;
2180 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181}
2182
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183static __init int cpu_has_kvm_support(void)
2184{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002185 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186}
2187
2188static __init int vmx_disabled_by_bios(void)
2189{
Sean Christophersona4d0b2f2019-12-20 20:45:09 -08002190 return !boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2191 !boot_cpu_has(X86_FEATURE_VMX);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192}
2193
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002194static int kvm_cpu_vmxon(u64 vmxon_pointer)
Dongxiao Xu7725b892010-05-11 18:29:38 +08002195{
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002196 u64 msr;
2197
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002198 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002199 intel_pt_handle_vmx(1);
2200
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002201 asm_volatile_goto("1: vmxon %[vmxon_pointer]\n\t"
2202 _ASM_EXTABLE(1b, %l[fault])
2203 : : [vmxon_pointer] "m"(vmxon_pointer)
2204 : : fault);
2205 return 0;
2206
2207fault:
2208 WARN_ONCE(1, "VMXON faulted, MSR_IA32_FEAT_CTL (0x3a) = 0x%llx\n",
2209 rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr) ? 0xdeadbeef : msr);
2210 intel_pt_handle_vmx(0);
2211 cr4_clear_bits(X86_CR4_VMXE);
2212
2213 return -EFAULT;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002214}
2215
Radim Krčmář13a34e02014-08-28 15:13:03 +02002216static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217{
2218 int cpu = raw_smp_processor_id();
2219 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002220 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002222 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02002223 return -EBUSY;
2224
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002225 /*
2226 * This can happen if we hot-added a CPU but failed to allocate
2227 * VP assist page for it.
2228 */
2229 if (static_branch_unlikely(&enable_evmcs) &&
2230 !hv_get_vp_assist_page(cpu))
2231 return -EFAULT;
2232
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002233 r = kvm_cpu_vmxon(phys_addr);
2234 if (r)
2235 return r;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002236
David Hildenbrandfdf288b2017-08-24 20:51:29 +02002237 if (enable_ept)
2238 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02002239
2240 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241}
2242
Nadav Har'Eld462b812011-05-24 15:26:10 +03002243static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002244{
2245 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002246 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002247
Nadav Har'Eld462b812011-05-24 15:26:10 +03002248 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2249 loaded_vmcss_on_cpu_link)
2250 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002251}
2252
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002253
2254/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2255 * tricks.
2256 */
2257static void kvm_cpu_vmxoff(void)
2258{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002259 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002260
2261 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002262 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002263}
2264
Radim Krčmář13a34e02014-08-28 15:13:03 +02002265static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002267 vmclear_local_loaded_vmcss();
2268 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269}
2270
Sean Christopherson7a57c092020-03-12 11:04:16 -07002271/*
2272 * There is no X86_FEATURE for SGX yet, but anyway we need to query CPUID
2273 * directly instead of going through cpu_has(), to ensure KVM is trapping
2274 * ENCLS whenever it's supported in hardware. It does not matter whether
2275 * the host OS supports or has enabled SGX.
2276 */
2277static bool cpu_has_sgx(void)
2278{
2279 return cpuid_eax(0) >= 0x12 && (cpuid_eax(0x12) & BIT(0));
2280}
2281
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002282static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002283 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284{
2285 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002286 u32 ctl = ctl_min | ctl_opt;
2287
2288 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2289
2290 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2291 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2292
2293 /* Ensure minimum (required) set of control bits are supported. */
2294 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002295 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002296
2297 *result = ctl;
2298 return 0;
2299}
2300
Sean Christopherson7caaa712018-12-03 13:53:01 -08002301static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
2302 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002303{
2304 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002305 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002306 u32 _pin_based_exec_control = 0;
2307 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002308 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002309 u32 _vmexit_control = 0;
2310 u32 _vmentry_control = 0;
2311
Paolo Bonzini13893092018-02-26 13:40:09 +01002312 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05302313 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002314#ifdef CONFIG_X86_64
2315 CPU_BASED_CR8_LOAD_EXITING |
2316 CPU_BASED_CR8_STORE_EXITING |
2317#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002318 CPU_BASED_CR3_LOAD_EXITING |
2319 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08002320 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002321 CPU_BASED_MOV_DR_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08002322 CPU_BASED_USE_TSC_OFFSETTING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002323 CPU_BASED_MWAIT_EXITING |
2324 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002325 CPU_BASED_INVLPG_EXITING |
2326 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002327
Sheng Yangf78e0e22007-10-29 09:40:42 +08002328 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002329 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002330 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002331 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2332 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002333 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002334#ifdef CONFIG_X86_64
2335 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2336 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2337 ~CPU_BASED_CR8_STORE_EXITING;
2338#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002339 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002340 min2 = 0;
2341 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002342 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002343 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002344 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002345 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002346 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002347 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02002348 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002349 SECONDARY_EXEC_ENABLE_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002350 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002351 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002352 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08002353 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08002354 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02002355 SECONDARY_EXEC_RDSEED_EXITING |
2356 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08002357 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04002358 SECONDARY_EXEC_TSC_SCALING |
Tao Xue69e72fa2019-07-16 14:55:49 +08002359 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Chao Pengf99e3da2018-10-24 16:05:10 +08002360 SECONDARY_EXEC_PT_USE_GPA |
2361 SECONDARY_EXEC_PT_CONCEAL_VMX |
Sean Christopherson7a57c092020-03-12 11:04:16 -07002362 SECONDARY_EXEC_ENABLE_VMFUNC;
2363 if (cpu_has_sgx())
2364 opt2 |= SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08002365 if (adjust_vmx_controls(min2, opt2,
2366 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002367 &_cpu_based_2nd_exec_control) < 0)
2368 return -EIO;
2369 }
2370#ifndef CONFIG_X86_64
2371 if (!(_cpu_based_2nd_exec_control &
2372 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2373 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2374#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002375
2376 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2377 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002378 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002379 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2380 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002381
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002382 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08002383 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002384
Sheng Yangd56f5462008-04-25 10:13:16 +08002385 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002386 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2387 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002388 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2389 CPU_BASED_CR3_STORE_EXITING |
2390 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08002391 } else if (vmx_cap->ept) {
2392 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002393 pr_warn_once("EPT CAP should not exist if not support "
2394 "1-setting enable EPT VM-execution control\n");
2395 }
2396 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002397 vmx_cap->vpid) {
2398 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002399 pr_warn_once("VPID CAP should not exist if not support "
2400 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002401 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002402
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002403 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002404#ifdef CONFIG_X86_64
2405 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2406#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002407 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002408 VM_EXIT_LOAD_IA32_PAT |
2409 VM_EXIT_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002410 VM_EXIT_CLEAR_BNDCFGS |
2411 VM_EXIT_PT_CONCEAL_PIP |
2412 VM_EXIT_CLEAR_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002413 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2414 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002415 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002416
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002417 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2418 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2419 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002420 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2421 &_pin_based_exec_control) < 0)
2422 return -EIO;
2423
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002424 if (cpu_has_broken_vmx_preemption_timer())
2425 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002426 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002427 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002428 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2429
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002430 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002431 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2432 VM_ENTRY_LOAD_IA32_PAT |
2433 VM_ENTRY_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002434 VM_ENTRY_LOAD_BNDCFGS |
2435 VM_ENTRY_PT_CONCEAL_PIP |
2436 VM_ENTRY_LOAD_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002437 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2438 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002439 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002441 /*
2442 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2443 * can't be used due to an errata where VM Exit may incorrectly clear
2444 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2445 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2446 */
2447 if (boot_cpu_data.x86 == 0x6) {
2448 switch (boot_cpu_data.x86_model) {
2449 case 26: /* AAK155 */
2450 case 30: /* AAP115 */
2451 case 37: /* AAT100 */
2452 case 44: /* BC86,AAY89,BD102 */
2453 case 46: /* BA97 */
Sean Christopherson85ba2b12019-01-14 12:12:02 -08002454 _vmentry_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002455 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2456 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2457 "does not work properly. Using workaround\n");
2458 break;
2459 default:
2460 break;
2461 }
2462 }
2463
2464
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002465 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002466
2467 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2468 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002469 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002470
2471#ifdef CONFIG_X86_64
2472 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2473 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002474 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002475#endif
2476
2477 /* Require Write-Back (WB) memory type for VMCS accesses. */
2478 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002479 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002480
Yang, Sheng002c7f72007-07-31 14:23:01 +03002481 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002482 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002483 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002484
Liran Alon2307af12018-06-29 22:59:04 +03002485 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002486
Yang, Sheng002c7f72007-07-31 14:23:01 +03002487 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2488 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002489 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002490 vmcs_conf->vmexit_ctrl = _vmexit_control;
2491 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002492
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002493 if (static_branch_unlikely(&enable_evmcs))
2494 evmcs_sanitize_exec_ctrls(vmcs_conf);
2495
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002496 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002497}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498
Ben Gardon41836832019-02-11 11:02:52 -08002499struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500{
2501 int node = cpu_to_node(cpu);
2502 struct page *pages;
2503 struct vmcs *vmcs;
2504
Ben Gardon41836832019-02-11 11:02:52 -08002505 pages = __alloc_pages_node(node, flags, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506 if (!pages)
2507 return NULL;
2508 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002509 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002510
2511 /* KVM supports Enlightened VMCS v1 only */
2512 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002513 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002514 else
Liran Alon392b2f22018-06-23 02:35:01 +03002515 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002516
Liran Alon491a6032018-06-23 02:35:12 +03002517 if (shadow)
2518 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 return vmcs;
2520}
2521
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002522void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002524 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525}
2526
Nadav Har'Eld462b812011-05-24 15:26:10 +03002527/*
2528 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2529 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002530void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002531{
2532 if (!loaded_vmcs->vmcs)
2533 return;
2534 loaded_vmcs_clear(loaded_vmcs);
2535 free_vmcs(loaded_vmcs->vmcs);
2536 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002537 if (loaded_vmcs->msr_bitmap)
2538 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002539 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002540}
2541
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002542int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002543{
Liran Alon491a6032018-06-23 02:35:12 +03002544 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002545 if (!loaded_vmcs->vmcs)
2546 return -ENOMEM;
2547
Sean Christophersond260f9e2020-03-21 12:37:50 -07002548 vmcs_clear(loaded_vmcs->vmcs);
2549
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002550 loaded_vmcs->shadow_vmcs = NULL;
Sean Christopherson804939e2019-05-07 12:18:05 -07002551 loaded_vmcs->hv_timer_soft_disabled = false;
Sean Christophersond260f9e2020-03-21 12:37:50 -07002552 loaded_vmcs->cpu = -1;
2553 loaded_vmcs->launched = 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002554
2555 if (cpu_has_vmx_msr_bitmap()) {
Ben Gardon41836832019-02-11 11:02:52 -08002556 loaded_vmcs->msr_bitmap = (unsigned long *)
2557 __get_free_page(GFP_KERNEL_ACCOUNT);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002558 if (!loaded_vmcs->msr_bitmap)
2559 goto out_vmcs;
2560 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002561
Arnd Bergmann1f008e12018-05-25 17:36:17 +02002562 if (IS_ENABLED(CONFIG_HYPERV) &&
2563 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002564 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2565 struct hv_enlightened_vmcs *evmcs =
2566 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2567
2568 evmcs->hv_enlightenments_control.msr_bitmap = 1;
2569 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002570 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002571
2572 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002573 memset(&loaded_vmcs->controls_shadow, 0,
2574 sizeof(struct vmcs_controls_shadow));
Sean Christophersond7ee0392018-07-23 12:32:47 -07002575
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002576 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002577
2578out_vmcs:
2579 free_loaded_vmcs(loaded_vmcs);
2580 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002581}
2582
Sam Ravnborg39959582007-06-01 00:47:13 -07002583static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584{
2585 int cpu;
2586
Zachary Amsden3230bb42009-09-29 11:38:37 -10002587 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002589 per_cpu(vmxarea, cpu) = NULL;
2590 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591}
2592
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593static __init int alloc_kvm_area(void)
2594{
2595 int cpu;
2596
Zachary Amsden3230bb42009-09-29 11:38:37 -10002597 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 struct vmcs *vmcs;
2599
Ben Gardon41836832019-02-11 11:02:52 -08002600 vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601 if (!vmcs) {
2602 free_kvm_area();
2603 return -ENOMEM;
2604 }
2605
Liran Alon2307af12018-06-29 22:59:04 +03002606 /*
2607 * When eVMCS is enabled, alloc_vmcs_cpu() sets
2608 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
2609 * revision_id reported by MSR_IA32_VMX_BASIC.
2610 *
Linus Torvalds312a4662018-12-26 17:03:51 -08002611 * However, even though not explicitly documented by
Liran Alon2307af12018-06-29 22:59:04 +03002612 * TLFS, VMXArea passed as VMXON argument should
2613 * still be marked with revision_id reported by
2614 * physical CPU.
2615 */
2616 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002617 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002618
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619 per_cpu(vmxarea, cpu) = vmcs;
2620 }
2621 return 0;
2622}
2623
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002624static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002625 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002627 if (!emulate_invalid_guest_state) {
2628 /*
2629 * CS and SS RPL should be equal during guest entry according
2630 * to VMX spec, but in reality it is not always so. Since vcpu
2631 * is in the middle of the transition from real mode to
2632 * protected mode it is safe to assume that RPL 0 is a good
2633 * default value.
2634 */
2635 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03002636 save->selector &= ~SEGMENT_RPL_MASK;
2637 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02002638 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002640 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641}
2642
2643static void enter_pmode(struct kvm_vcpu *vcpu)
2644{
2645 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002646 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647
Gleb Natapovd99e4152012-12-20 16:57:45 +02002648 /*
2649 * Update real mode segment cache. It may be not up-to-date if sement
2650 * register was written while vcpu was in a guest mode.
2651 */
2652 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2653 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2654 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2655 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2656 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2657 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2658
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002659 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002661 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662
2663 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002664 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2665 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002666 vmcs_writel(GUEST_RFLAGS, flags);
2667
Rusty Russell66aee912007-07-17 23:34:16 +10002668 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2669 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670
2671 update_exception_bitmap(vcpu);
2672
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002673 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2674 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2675 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2676 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2677 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2678 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679}
2680
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002681static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682{
Mathias Krause772e0312012-08-30 01:30:19 +02002683 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002684 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685
Gleb Natapovd99e4152012-12-20 16:57:45 +02002686 var.dpl = 0x3;
2687 if (seg == VCPU_SREG_CS)
2688 var.type = 0x3;
2689
2690 if (!emulate_invalid_guest_state) {
2691 var.selector = var.base >> 4;
2692 var.base = var.base & 0xffff0;
2693 var.limit = 0xffff;
2694 var.g = 0;
2695 var.db = 0;
2696 var.present = 1;
2697 var.s = 1;
2698 var.l = 0;
2699 var.unusable = 0;
2700 var.type = 0x3;
2701 var.avl = 0;
2702 if (save->base & 0xf)
2703 printk_once(KERN_WARNING "kvm: segment base is not "
2704 "paragraph aligned when entering "
2705 "protected mode (seg=%d)", seg);
2706 }
2707
2708 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05002709 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002710 vmcs_write32(sf->limit, var.limit);
2711 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712}
2713
2714static void enter_rmode(struct kvm_vcpu *vcpu)
2715{
2716 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002717 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002718 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002720 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2721 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2722 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2723 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2724 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002725 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2726 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002727
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002728 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729
Gleb Natapov776e58e2011-03-13 12:34:27 +02002730 /*
2731 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002732 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002733 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002734 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002735 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2736 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002737
Avi Kivity2fb92db2011-04-27 19:42:18 +03002738 vmx_segment_cache_clear(vmx);
2739
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002740 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2743
2744 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002745 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002747 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748
2749 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002750 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 update_exception_bitmap(vcpu);
2752
Gleb Natapovd99e4152012-12-20 16:57:45 +02002753 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2754 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2755 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2756 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2757 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2758 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002759
Eddie Dong8668a3c2007-10-10 14:26:45 +08002760 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761}
2762
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002763void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Amit Shah401d10d2009-02-20 22:53:37 +05302764{
2765 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond85a8032020-09-23 11:04:06 -07002766 struct vmx_uret_msr *msr = vmx_find_uret_msr(vmx, MSR_EFER);
Avi Kivity26bb0982009-09-07 11:14:12 +03002767
2768 if (!msr)
2769 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302770
Avi Kivityf6801df2010-01-21 15:31:50 +02002771 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302772 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002773 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302774 msr->data = efer;
2775 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002776 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302777
2778 msr->data = efer & ~EFER_LME;
2779 }
2780 setup_msrs(vmx);
2781}
2782
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002783#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784
2785static void enter_lmode(struct kvm_vcpu *vcpu)
2786{
2787 u32 guest_tr_ar;
2788
Avi Kivity2fb92db2011-04-27 19:42:18 +03002789 vmx_segment_cache_clear(to_vmx(vcpu));
2790
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002792 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002793 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2794 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002796 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
2797 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798 }
Avi Kivityda38f432010-07-06 11:30:49 +03002799 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800}
2801
2802static void exit_lmode(struct kvm_vcpu *vcpu)
2803{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002804 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002805 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806}
2807
2808#endif
2809
Sean Christopherson77809382020-03-20 14:28:18 -07002810static void vmx_flush_tlb_all(struct kvm_vcpu *vcpu)
Sean Christopherson5058b692020-03-20 14:28:14 -07002811{
2812 struct vcpu_vmx *vmx = to_vmx(vcpu);
2813
2814 /*
Sean Christopherson77809382020-03-20 14:28:18 -07002815 * INVEPT must be issued when EPT is enabled, irrespective of VPID, as
2816 * the CPU is not required to invalidate guest-physical mappings on
2817 * VM-Entry, even if VPID is disabled. Guest-physical mappings are
2818 * associated with the root EPT structure and not any particular VPID
2819 * (INVVPID also isn't required to invalidate guest-physical mappings).
Sean Christopherson5058b692020-03-20 14:28:14 -07002820 */
2821 if (enable_ept) {
2822 ept_sync_global();
2823 } else if (enable_vpid) {
2824 if (cpu_has_vmx_invvpid_global()) {
2825 vpid_sync_vcpu_global();
2826 } else {
2827 vpid_sync_vcpu_single(vmx->vpid);
2828 vpid_sync_vcpu_single(vmx->nested.vpid02);
2829 }
2830 }
2831}
2832
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002833static void vmx_flush_tlb_current(struct kvm_vcpu *vcpu)
2834{
Sean Christopherson2a40b902020-07-15 20:41:18 -07002835 struct kvm_mmu *mmu = vcpu->arch.mmu;
2836 u64 root_hpa = mmu->root_hpa;
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002837
2838 /* No flush required if the current context is invalid. */
2839 if (!VALID_PAGE(root_hpa))
2840 return;
2841
2842 if (enable_ept)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002843 ept_sync_context(construct_eptp(vcpu, root_hpa,
2844 mmu->shadow_root_level));
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002845 else if (!is_guest_mode(vcpu))
2846 vpid_sync_context(to_vmx(vcpu)->vpid);
2847 else
2848 vpid_sync_context(nested_get_vpid02(vcpu));
2849}
2850
Junaid Shahidfaff8752018-06-29 13:10:05 -07002851static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
2852{
Junaid Shahidfaff8752018-06-29 13:10:05 -07002853 /*
Sean Christophersonad104b52020-03-20 14:28:11 -07002854 * vpid_sync_vcpu_addr() is a nop if vmx->vpid==0, see the comment in
2855 * vmx_flush_tlb_guest() for an explanation of why this is ok.
Junaid Shahidfaff8752018-06-29 13:10:05 -07002856 */
Sean Christophersonad104b52020-03-20 14:28:11 -07002857 vpid_sync_vcpu_addr(to_vmx(vcpu)->vpid, addr);
Junaid Shahidfaff8752018-06-29 13:10:05 -07002858}
2859
Sean Christophersone64419d2020-03-20 14:28:10 -07002860static void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu)
2861{
2862 /*
2863 * vpid_sync_context() is a nop if vmx->vpid==0, e.g. if enable_vpid==0
2864 * or a vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit
2865 * are required to flush GVA->{G,H}PA mappings from the TLB if vpid is
2866 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed),
2867 * i.e. no explicit INVVPID is necessary.
2868 */
2869 vpid_sync_context(to_vmx(vcpu)->vpid);
2870}
2871
Peter Shier43fea4e2020-08-20 16:05:45 -07002872void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08002873{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002874 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2875
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002876 if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002877 return;
2878
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02002879 if (is_pae_paging(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002880 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
2881 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
2882 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
2883 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002884 }
2885}
2886
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002887void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03002888{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002889 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2890
Sean Christopherson9932b492020-04-15 13:34:50 -07002891 if (WARN_ON_ONCE(!is_pae_paging(vcpu)))
2892 return;
2893
2894 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2895 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2896 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2897 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002898
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002899 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002900}
2901
Sheng Yang14394422008-04-28 12:24:45 +08002902static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2903 unsigned long cr0,
2904 struct kvm_vcpu *vcpu)
2905{
Sean Christopherson2183f562019-05-07 12:17:56 -07002906 struct vcpu_vmx *vmx = to_vmx(vcpu);
2907
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002908 if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
Sean Christopherson34059c22019-09-27 14:45:23 -07002909 vmx_cache_reg(vcpu, VCPU_EXREG_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08002910 if (!(cr0 & X86_CR0_PG)) {
2911 /* From paging/starting to nonpaging */
Sean Christopherson2183f562019-05-07 12:17:56 -07002912 exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
2913 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08002914 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002915 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002916 } else if (!is_paging(vcpu)) {
2917 /* From nonpaging to paging */
Sean Christopherson2183f562019-05-07 12:17:56 -07002918 exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
2919 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08002920 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002921 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002922 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002923
2924 if (!(cr0 & X86_CR0_WP))
2925 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002926}
2927
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002928void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002930 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002931 unsigned long hw_cr0;
2932
Sean Christopherson3de63472018-07-13 08:42:30 -07002933 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002934 if (is_unrestricted_guest(vcpu))
Gleb Natapov50378782013-02-04 16:00:28 +02002935 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02002936 else {
Gleb Natapov50378782013-02-04 16:00:28 +02002937 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002938
Gleb Natapov218e7632013-01-21 15:36:45 +02002939 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
2940 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941
Gleb Natapov218e7632013-01-21 15:36:45 +02002942 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
2943 enter_rmode(vcpu);
2944 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002946#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002947 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002948 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10002950 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951 exit_lmode(vcpu);
2952 }
2953#endif
2954
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002955 if (enable_ept && !is_unrestricted_guest(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08002956 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2957
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002959 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002960 vcpu->arch.cr0 = cr0;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07002961 kvm_register_mark_available(vcpu, VCPU_EXREG_CR0);
Gleb Natapov14168782013-01-21 15:36:49 +02002962
2963 /* depends on vcpu->arch.cr0 to be set to a new value */
2964 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965}
2966
Sean Christophersond468d942020-07-15 20:41:20 -07002967static int vmx_get_max_tdp_level(void)
Sean Christopherson0047fca2020-05-01 21:32:33 -07002968{
Sean Christophersond468d942020-07-15 20:41:20 -07002969 if (cpu_has_vmx_ept_5levels())
Sean Christopherson0047fca2020-05-01 21:32:33 -07002970 return 5;
2971 return 4;
2972}
2973
Sean Christopherson2a40b902020-07-15 20:41:18 -07002974u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,
2975 int root_level)
Sheng Yang14394422008-04-28 12:24:45 +08002976{
Yu Zhang855feb62017-08-24 20:27:55 +08002977 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08002978
Sean Christopherson2a40b902020-07-15 20:41:18 -07002979 eptp |= (root_level == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08002980
Peter Feiner995f00a2017-06-30 17:26:32 -07002981 if (enable_ept_ad_bits &&
2982 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02002983 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08002984 eptp |= (root_hpa & PAGE_MASK);
2985
2986 return eptp;
2987}
2988
Sean Christopherson2a40b902020-07-15 20:41:18 -07002989static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,
2990 int pgd_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991{
Tianyu Lan877ad952018-07-19 08:40:23 +00002992 struct kvm *kvm = vcpu->kvm;
Sean Christopherson04f11ef2019-09-27 14:45:16 -07002993 bool update_guest_cr3 = true;
Sheng Yang14394422008-04-28 12:24:45 +08002994 unsigned long guest_cr3;
2995 u64 eptp;
2996
Avi Kivity089d0342009-03-23 18:26:32 +02002997 if (enable_ept) {
Sean Christopherson2a40b902020-07-15 20:41:18 -07002998 eptp = construct_eptp(vcpu, pgd, pgd_level);
Sheng Yang14394422008-04-28 12:24:45 +08002999 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00003000
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003001 if (kvm_x86_ops.tlb_remote_flush) {
Tianyu Lan877ad952018-07-19 08:40:23 +00003002 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3003 to_vmx(vcpu)->ept_pointer = eptp;
3004 to_kvm_vmx(kvm)->ept_pointers_match
3005 = EPT_POINTERS_CHECK;
3006 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3007 }
3008
Paolo Bonzinidf7e0682020-05-20 08:37:37 -04003009 if (!enable_unrestricted_guest && !is_paging(vcpu))
Tianyu Lan877ad952018-07-19 08:40:23 +00003010 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Sean Christophersonb17b7432019-09-27 14:45:17 -07003011 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3012 guest_cr3 = vcpu->arch.cr3;
3013 else /* vmcs01.GUEST_CR3 is already up-to-date. */
3014 update_guest_cr3 = false;
Peter Shier43fea4e2020-08-20 16:05:45 -07003015 vmx_ept_load_pdptrs(vcpu);
Sean Christophersonbe100ef2020-03-20 14:28:33 -07003016 } else {
3017 guest_cr3 = pgd;
Sheng Yang14394422008-04-28 12:24:45 +08003018 }
3019
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003020 if (update_guest_cr3)
3021 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022}
3023
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003024int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003026 struct vcpu_vmx *vmx = to_vmx(vcpu);
Ben Serebrin085e68e2015-04-16 11:58:05 -07003027 /*
3028 * Pass through host's Machine Check Enable value to hw_cr4, which
3029 * is in force while we are in guest mode. Do not let guests control
3030 * this bit, even if host CR4.MCE == 0.
3031 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003032 unsigned long hw_cr4;
3033
3034 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003035 if (is_unrestricted_guest(vcpu))
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003036 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003037 else if (vmx->rmode.vm86_active)
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003038 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3039 else
3040 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003041
Sean Christopherson64f7a112018-04-30 10:01:06 -07003042 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3043 if (cr4 & X86_CR4_UMIP) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003044 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07003045 hw_cr4 &= ~X86_CR4_UMIP;
3046 } else if (!is_guest_mode(vcpu) ||
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003047 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
3048 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
3049 }
Sean Christopherson64f7a112018-04-30 10:01:06 -07003050 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02003051
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003052 if (cr4 & X86_CR4_VMXE) {
3053 /*
3054 * To use VMXON (and later other VMX instructions), a guest
3055 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3056 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003057 * is here. We operate under the default treatment of SMM,
3058 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003059 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003060 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003061 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003062 }
David Matlack38991522016-11-29 18:14:08 -08003063
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003064 if (vmx->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003065 return 1;
3066
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003067 vcpu->arch.cr4 = cr4;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07003068 kvm_register_mark_available(vcpu, VCPU_EXREG_CR4);
Sheng Yang14394422008-04-28 12:24:45 +08003069
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003070 if (!is_unrestricted_guest(vcpu)) {
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003071 if (enable_ept) {
3072 if (!is_paging(vcpu)) {
3073 hw_cr4 &= ~X86_CR4_PAE;
3074 hw_cr4 |= X86_CR4_PSE;
3075 } else if (!(cr4 & X86_CR4_PAE)) {
3076 hw_cr4 &= ~X86_CR4_PAE;
3077 }
3078 }
3079
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003080 /*
Huaitong Handdba2622016-03-22 16:51:15 +08003081 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3082 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3083 * to be manually disabled when guest switches to non-paging
3084 * mode.
3085 *
3086 * If !enable_unrestricted_guest, the CPU is always running
3087 * with CR0.PG=1 and CR4 needs to be modified.
3088 * If enable_unrestricted_guest, the CPU automatically
3089 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003090 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003091 if (!is_paging(vcpu))
3092 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3093 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003094
Sheng Yang14394422008-04-28 12:24:45 +08003095 vmcs_writel(CR4_READ_SHADOW, cr4);
3096 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003097 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098}
3099
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003100void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101{
Avi Kivitya9179492011-01-03 14:28:52 +02003102 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103 u32 ar;
3104
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003105 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003106 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003107 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003108 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003109 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003110 var->base = vmx_read_guest_seg_base(vmx, seg);
3111 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3112 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003113 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003114 var->base = vmx_read_guest_seg_base(vmx, seg);
3115 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3116 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3117 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003118 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119 var->type = ar & 15;
3120 var->s = (ar >> 4) & 1;
3121 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003122 /*
3123 * Some userspaces do not preserve unusable property. Since usable
3124 * segment has to be present according to VMX spec we can use present
3125 * property to amend userspace bug by making unusable segment always
3126 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3127 * segment as unusable.
3128 */
3129 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130 var->avl = (ar >> 12) & 1;
3131 var->l = (ar >> 13) & 1;
3132 var->db = (ar >> 14) & 1;
3133 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134}
3135
Avi Kivitya9179492011-01-03 14:28:52 +02003136static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3137{
Avi Kivitya9179492011-01-03 14:28:52 +02003138 struct kvm_segment s;
3139
3140 if (to_vmx(vcpu)->rmode.vm86_active) {
3141 vmx_get_segment(vcpu, &s, seg);
3142 return s.base;
3143 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003144 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003145}
3146
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003147int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003148{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003149 struct vcpu_vmx *vmx = to_vmx(vcpu);
3150
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003151 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003152 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003153 else {
3154 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003155 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003156 }
Avi Kivity69c73022011-03-07 15:26:44 +02003157}
3158
Avi Kivity653e3102007-05-07 10:55:37 +03003159static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 u32 ar;
3162
Avi Kivityf0495f92012-06-07 17:06:10 +03003163 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164 ar = 1 << 16;
3165 else {
3166 ar = var->type & 15;
3167 ar |= (var->s & 1) << 4;
3168 ar |= (var->dpl & 3) << 5;
3169 ar |= (var->present & 1) << 7;
3170 ar |= (var->avl & 1) << 12;
3171 ar |= (var->l & 1) << 13;
3172 ar |= (var->db & 1) << 14;
3173 ar |= (var->g & 1) << 15;
3174 }
Avi Kivity653e3102007-05-07 10:55:37 +03003175
3176 return ar;
3177}
3178
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003179void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03003180{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003181 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003182 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003183
Avi Kivity2fb92db2011-04-27 19:42:18 +03003184 vmx_segment_cache_clear(vmx);
3185
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003186 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3187 vmx->rmode.segs[seg] = *var;
3188 if (seg == VCPU_SREG_TR)
3189 vmcs_write16(sf->selector, var->selector);
3190 else if (var->s)
3191 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003192 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003193 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003194
Avi Kivity653e3102007-05-07 10:55:37 +03003195 vmcs_writel(sf->base, var->base);
3196 vmcs_write32(sf->limit, var->limit);
3197 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003198
3199 /*
3200 * Fix the "Accessed" bit in AR field of segment registers for older
3201 * qemu binaries.
3202 * IA32 arch specifies that at the time of processor reset the
3203 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003204 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003205 * state vmexit when "unrestricted guest" mode is turned on.
3206 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3207 * tree. Newer qemu binaries with that qemu fix would not need this
3208 * kvm hack.
3209 */
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003210 if (is_unrestricted_guest(vcpu) && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003211 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003212
Gleb Natapovf924d662012-12-12 19:10:55 +02003213 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003214
3215out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003216 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217}
3218
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3220{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003221 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222
3223 *db = (ar >> 14) & 1;
3224 *l = (ar >> 13) & 1;
3225}
3226
Gleb Natapov89a27f42010-02-16 10:51:48 +02003227static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003229 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3230 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231}
3232
Gleb Natapov89a27f42010-02-16 10:51:48 +02003233static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003235 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3236 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237}
3238
Gleb Natapov89a27f42010-02-16 10:51:48 +02003239static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003241 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3242 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243}
3244
Gleb Natapov89a27f42010-02-16 10:51:48 +02003245static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003247 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3248 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249}
3250
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003251static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3252{
3253 struct kvm_segment var;
3254 u32 ar;
3255
3256 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003257 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003258 if (seg == VCPU_SREG_CS)
3259 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003260 ar = vmx_segment_access_rights(&var);
3261
3262 if (var.base != (var.selector << 4))
3263 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003264 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003265 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003266 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003267 return false;
3268
3269 return true;
3270}
3271
3272static bool code_segment_valid(struct kvm_vcpu *vcpu)
3273{
3274 struct kvm_segment cs;
3275 unsigned int cs_rpl;
3276
3277 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003278 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003279
Avi Kivity1872a3f2009-01-04 23:26:52 +02003280 if (cs.unusable)
3281 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003282 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003283 return false;
3284 if (!cs.s)
3285 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003286 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003287 if (cs.dpl > cs_rpl)
3288 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003289 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003290 if (cs.dpl != cs_rpl)
3291 return false;
3292 }
3293 if (!cs.present)
3294 return false;
3295
3296 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3297 return true;
3298}
3299
3300static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3301{
3302 struct kvm_segment ss;
3303 unsigned int ss_rpl;
3304
3305 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003306 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003307
Avi Kivity1872a3f2009-01-04 23:26:52 +02003308 if (ss.unusable)
3309 return true;
3310 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003311 return false;
3312 if (!ss.s)
3313 return false;
3314 if (ss.dpl != ss_rpl) /* DPL != RPL */
3315 return false;
3316 if (!ss.present)
3317 return false;
3318
3319 return true;
3320}
3321
3322static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3323{
3324 struct kvm_segment var;
3325 unsigned int rpl;
3326
3327 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03003328 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003329
Avi Kivity1872a3f2009-01-04 23:26:52 +02003330 if (var.unusable)
3331 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003332 if (!var.s)
3333 return false;
3334 if (!var.present)
3335 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003336 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003337 if (var.dpl < rpl) /* DPL < RPL */
3338 return false;
3339 }
3340
3341 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3342 * rights flags
3343 */
3344 return true;
3345}
3346
3347static bool tr_valid(struct kvm_vcpu *vcpu)
3348{
3349 struct kvm_segment tr;
3350
3351 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3352
Avi Kivity1872a3f2009-01-04 23:26:52 +02003353 if (tr.unusable)
3354 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03003355 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003356 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003357 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003358 return false;
3359 if (!tr.present)
3360 return false;
3361
3362 return true;
3363}
3364
3365static bool ldtr_valid(struct kvm_vcpu *vcpu)
3366{
3367 struct kvm_segment ldtr;
3368
3369 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3370
Avi Kivity1872a3f2009-01-04 23:26:52 +02003371 if (ldtr.unusable)
3372 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03003373 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003374 return false;
3375 if (ldtr.type != 2)
3376 return false;
3377 if (!ldtr.present)
3378 return false;
3379
3380 return true;
3381}
3382
3383static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3384{
3385 struct kvm_segment cs, ss;
3386
3387 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3388 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3389
Nadav Amitb32a9912015-03-29 16:33:04 +03003390 return ((cs.selector & SEGMENT_RPL_MASK) ==
3391 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003392}
3393
3394/*
3395 * Check if guest state is valid. Returns true if valid, false if
3396 * not.
3397 * We assume that registers are always usable
3398 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07003399bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003400{
3401 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003402 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003403 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3404 return false;
3405 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3406 return false;
3407 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3408 return false;
3409 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3410 return false;
3411 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3412 return false;
3413 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3414 return false;
3415 } else {
3416 /* protected mode guest state checks */
3417 if (!cs_ss_rpl_check(vcpu))
3418 return false;
3419 if (!code_segment_valid(vcpu))
3420 return false;
3421 if (!stack_segment_valid(vcpu))
3422 return false;
3423 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3424 return false;
3425 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3426 return false;
3427 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3428 return false;
3429 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3430 return false;
3431 if (!tr_valid(vcpu))
3432 return false;
3433 if (!ldtr_valid(vcpu))
3434 return false;
3435 }
3436 /* TODO:
3437 * - Add checks on RIP
3438 * - Add checks on RFLAGS
3439 */
3440
3441 return true;
3442}
3443
Mike Dayd77c26f2007-10-08 09:02:08 -04003444static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003446 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003447 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003448 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003450 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003451 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003452 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3453 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003454 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003455 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003456 r = kvm_write_guest_page(kvm, fn++, &data,
3457 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003458 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003459 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003460 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3461 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003462 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003463 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3464 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003465 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003466 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003467 r = kvm_write_guest_page(kvm, fn, &data,
3468 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3469 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003470out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003471 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003472 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003473}
3474
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003475static int init_rmode_identity_map(struct kvm *kvm)
3476{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003477 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Peter Xu2a5755b2020-01-09 09:57:14 -05003478 int i, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08003479 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003480 u32 tmp;
3481
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003482 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003483 mutex_lock(&kvm->slots_lock);
3484
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003485 if (likely(kvm_vmx->ept_identity_pagetable_done))
Peter Xu2a5755b2020-01-09 09:57:14 -05003486 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003487
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003488 if (!kvm_vmx->ept_identity_map_addr)
3489 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
3490 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08003491
David Hildenbrandd8a6e362017-08-24 20:51:34 +02003492 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003493 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08003494 if (r < 0)
Peter Xu2a5755b2020-01-09 09:57:14 -05003495 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003496
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003497 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3498 if (r < 0)
3499 goto out;
3500 /* Set up identity-mapping pagetable for EPT in real mode */
3501 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3502 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3503 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3504 r = kvm_write_guest_page(kvm, identity_map_pfn,
3505 &tmp, i * sizeof(tmp), sizeof(tmp));
3506 if (r < 0)
3507 goto out;
3508 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003509 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003510
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003511out:
Tang Chena255d472014-09-16 18:41:58 +08003512 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003513 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003514}
3515
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516static void seg_setup(int seg)
3517{
Mathias Krause772e0312012-08-30 01:30:19 +02003518 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003519 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520
3521 vmcs_write16(sf->selector, 0);
3522 vmcs_writel(sf->base, 0);
3523 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003524 ar = 0x93;
3525 if (seg == VCPU_SREG_CS)
3526 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003527
3528 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529}
3530
Sheng Yangf78e0e22007-10-29 09:40:42 +08003531static int alloc_apic_access_page(struct kvm *kvm)
3532{
Xiao Guangrong44841412012-09-07 14:14:20 +08003533 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003534 int r = 0;
3535
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003536 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003537 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003538 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02003539 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3540 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003541 if (r)
3542 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003543
Tang Chen73a6d942014-09-11 13:38:00 +08003544 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003545 if (is_error_page(page)) {
3546 r = -EFAULT;
3547 goto out;
3548 }
3549
Tang Chenc24ae0d2014-09-24 15:57:58 +08003550 /*
3551 * Do not pin the page in memory, so that memory hot-unplug
3552 * is able to migrate it.
3553 */
3554 put_page(page);
3555 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003556out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003557 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003558 return r;
3559}
3560
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003561int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003562{
3563 int vpid;
3564
Avi Kivity919818a2009-03-23 18:01:29 +02003565 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003566 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003567 spin_lock(&vmx_vpid_lock);
3568 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003569 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003570 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003571 else
3572 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003573 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003574 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003575}
3576
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003577void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003578{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003579 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003580 return;
3581 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003582 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003583 spin_unlock(&vmx_vpid_lock);
3584}
3585
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003586static __always_inline void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003587 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003588{
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003589 struct vcpu_vmx *vmx = to_vmx(vcpu);
3590 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003591 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003592
3593 if (!cpu_has_vmx_msr_bitmap())
3594 return;
3595
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003596 if (static_branch_unlikely(&enable_evmcs))
3597 evmcs_touch_msr_bitmap();
3598
Sheng Yang25c5f222008-03-28 13:18:56 +08003599 /*
3600 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3601 * have the write-low and read-high bitmap offsets the wrong way round.
3602 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3603 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003604 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003605 if (type & MSR_TYPE_R)
3606 /* read-low */
3607 __clear_bit(msr, msr_bitmap + 0x000 / f);
3608
3609 if (type & MSR_TYPE_W)
3610 /* write-low */
3611 __clear_bit(msr, msr_bitmap + 0x800 / f);
3612
Sheng Yang25c5f222008-03-28 13:18:56 +08003613 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3614 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003615 if (type & MSR_TYPE_R)
3616 /* read-high */
3617 __clear_bit(msr, msr_bitmap + 0x400 / f);
3618
3619 if (type & MSR_TYPE_W)
3620 /* write-high */
3621 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3622
3623 }
3624}
3625
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003626static __always_inline void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003627 u32 msr, int type)
3628{
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003629 struct vcpu_vmx *vmx = to_vmx(vcpu);
3630 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003631 int f = sizeof(unsigned long);
3632
3633 if (!cpu_has_vmx_msr_bitmap())
3634 return;
3635
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003636 if (static_branch_unlikely(&enable_evmcs))
3637 evmcs_touch_msr_bitmap();
3638
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003639 /*
3640 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3641 * have the write-low and read-high bitmap offsets the wrong way round.
3642 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3643 */
3644 if (msr <= 0x1fff) {
3645 if (type & MSR_TYPE_R)
3646 /* read-low */
3647 __set_bit(msr, msr_bitmap + 0x000 / f);
3648
3649 if (type & MSR_TYPE_W)
3650 /* write-low */
3651 __set_bit(msr, msr_bitmap + 0x800 / f);
3652
3653 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3654 msr &= 0x1fff;
3655 if (type & MSR_TYPE_R)
3656 /* read-high */
3657 __set_bit(msr, msr_bitmap + 0x400 / f);
3658
3659 if (type & MSR_TYPE_W)
3660 /* write-high */
3661 __set_bit(msr, msr_bitmap + 0xc00 / f);
3662
3663 }
3664}
3665
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003666static __always_inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu,
3667 u32 msr, int type, bool value)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003668{
3669 if (value)
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003670 vmx_enable_intercept_for_msr(vcpu, msr, type);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003671 else
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003672 vmx_disable_intercept_for_msr(vcpu, msr, type);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003673}
3674
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003675static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003676{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003677 u8 mode = 0;
3678
3679 if (cpu_has_secondary_exec_ctrls() &&
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003680 (secondary_exec_controls_get(to_vmx(vcpu)) &
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003681 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3682 mode |= MSR_BITMAP_MODE_X2APIC;
3683 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3684 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3685 }
3686
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003687 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003688}
3689
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003690static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu,
3691 unsigned long *msr_bitmap, u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003692{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003693 int msr;
3694
3695 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3696 unsigned word = msr / BITS_PER_LONG;
3697 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
3698 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003699 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003700
3701 if (mode & MSR_BITMAP_MODE_X2APIC) {
3702 /*
3703 * TPR reads and writes can be virtualized even if virtual interrupt
3704 * delivery is not in use.
3705 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003706 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003707 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003708 vmx_enable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_RW);
3709 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3710 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003711 }
3712 }
3713}
3714
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003715void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003716{
3717 struct vcpu_vmx *vmx = to_vmx(vcpu);
3718 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3719 u8 mode = vmx_msr_bitmap_mode(vcpu);
3720 u8 changed = mode ^ vmx->msr_bitmap_mode;
3721
3722 if (!changed)
3723 return;
3724
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003725 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003726 vmx_update_msr_bitmap_x2apic(vcpu, msr_bitmap, mode);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003727
3728 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003729}
3730
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003731void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu)
Chao Pengb08c2892018-10-24 16:05:15 +08003732{
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003733 struct vcpu_vmx *vmx = to_vmx(vcpu);
Chao Pengb08c2892018-10-24 16:05:15 +08003734 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
3735 u32 i;
3736
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003737 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_STATUS, MSR_TYPE_RW, flag);
3738 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_BASE, MSR_TYPE_RW, flag);
3739 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_MASK, MSR_TYPE_RW, flag);
3740 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_CR3_MATCH, MSR_TYPE_RW, flag);
Chao Pengb08c2892018-10-24 16:05:15 +08003741 for (i = 0; i < vmx->pt_desc.addr_range; i++) {
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003742 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
3743 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
Chao Pengb08c2892018-10-24 16:05:15 +08003744 }
3745}
3746
Liran Alone6c67d82018-09-04 10:56:52 +03003747static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3748{
3749 struct vcpu_vmx *vmx = to_vmx(vcpu);
3750 void *vapic_page;
3751 u32 vppr;
3752 int rvi;
3753
3754 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3755 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003756 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
Liran Alone6c67d82018-09-04 10:56:52 +03003757 return false;
3758
Paolo Bonzini7e712682018-10-03 13:44:26 +02003759 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003760
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003761 vapic_page = vmx->nested.virtual_apic_map.hva;
Liran Alone6c67d82018-09-04 10:56:52 +03003762 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
Liran Alone6c67d82018-09-04 10:56:52 +03003763
3764 return ((rvi & 0xf0) > (vppr & 0xf0));
3765}
3766
Wincy Van06a55242017-04-28 13:13:59 +08003767static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
3768 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003769{
3770#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08003771 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
3772
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003773 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08003774 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003775 * The vector of interrupt to be delivered to vcpu had
3776 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08003777 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003778 * Following cases will be reached in this block, and
3779 * we always send a notification event in all cases as
3780 * explained below.
3781 *
3782 * Case 1: vcpu keeps in non-root mode. Sending a
3783 * notification event posts the interrupt to vcpu.
3784 *
3785 * Case 2: vcpu exits to root mode and is still
3786 * runnable. PIR will be synced to vIRR before the
3787 * next vcpu entry. Sending a notification event in
3788 * this case has no effect, as vcpu is not in root
3789 * mode.
3790 *
3791 * Case 3: vcpu exits to root mode and is blocked.
3792 * vcpu_block() has already synced PIR to vIRR and
3793 * never blocks vcpu if vIRR is not cleared. Therefore,
3794 * a blocked vcpu here does not wait for any requested
3795 * interrupts in PIR, and sending a notification event
3796 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08003797 */
Feng Wu28b835d2015-09-18 22:29:54 +08003798
Wincy Van06a55242017-04-28 13:13:59 +08003799 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003800 return true;
3801 }
3802#endif
3803 return false;
3804}
3805
Wincy Van705699a2015-02-03 23:58:17 +08003806static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
3807 int vector)
3808{
3809 struct vcpu_vmx *vmx = to_vmx(vcpu);
3810
3811 if (is_guest_mode(vcpu) &&
3812 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08003813 /*
3814 * If a posted intr is not recognized by hardware,
3815 * we will accomplish it in the next vmentry.
3816 */
3817 vmx->nested.pi_pending = true;
3818 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02003819 /* the PIR and ON have been set by L1. */
3820 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
3821 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08003822 return 0;
3823 }
3824 return -1;
3825}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826/*
Yang Zhanga20ed542013-04-11 19:25:15 +08003827 * Send interrupt to vcpu via posted interrupt way.
3828 * 1. If target vcpu is running(non-root mode), send posted interrupt
3829 * notification to vcpu and hardware will sync PIR to vIRR atomically.
3830 * 2. If target vcpu isn't running(root mode), kick it to pick up the
3831 * interrupt from PIR in next vmentry.
3832 */
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003833static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
Yang Zhanga20ed542013-04-11 19:25:15 +08003834{
3835 struct vcpu_vmx *vmx = to_vmx(vcpu);
3836 int r;
3837
Wincy Van705699a2015-02-03 23:58:17 +08003838 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
3839 if (!r)
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003840 return 0;
3841
3842 if (!vcpu->arch.apicv_active)
3843 return -1;
Wincy Van705699a2015-02-03 23:58:17 +08003844
Yang Zhanga20ed542013-04-11 19:25:15 +08003845 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003846 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003847
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003848 /* If a previous notification has sent the IPI, nothing to do. */
3849 if (pi_test_and_set_on(&vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003850 return 0;
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003851
Wanpeng Li379a3c82020-04-28 14:23:27 +08003852 if (vcpu != kvm_get_running_vcpu() &&
3853 !kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08003854 kvm_vcpu_kick(vcpu);
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003855
3856 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003857}
3858
Avi Kivity6aa8b732006-12-10 02:21:36 -08003859/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003860 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3861 * will not change in the lifetime of the guest.
3862 * Note that host-state that does change is set elsewhere. E.g., host-state
3863 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3864 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003865void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003866{
3867 u32 low32, high32;
3868 unsigned long tmpl;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003869 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003870
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07003871 cr0 = read_cr0();
3872 WARN_ON(cr0 & X86_CR0_TS);
3873 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003874
3875 /*
3876 * Save the most likely value for this task's CR3 in the VMCS.
3877 * We can't use __get_current_cr3_fast() because we're not atomic.
3878 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07003879 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003880 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003881 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003882
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003883 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003884 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003885 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003886 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003887
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003888 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003889#ifdef CONFIG_X86_64
3890 /*
3891 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003892 * vmx_prepare_switch_to_host(), in case userspace uses
3893 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03003894 */
3895 vmcs_write16(HOST_DS_SELECTOR, 0);
3896 vmcs_write16(HOST_ES_SELECTOR, 0);
3897#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003898 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3899 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003900#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003901 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3902 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3903
Sean Christopherson23420802019-04-19 22:50:57 -07003904 vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003905
Sean Christopherson453eafb2018-12-20 12:25:17 -08003906 vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003907
3908 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
3909 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
3910 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
3911 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
3912
3913 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3914 rdmsr(MSR_IA32_CR_PAT, low32, high32);
3915 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
3916 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003917
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003918 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003919 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003920}
3921
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003922void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003923{
Sean Christophersonfa71e952020-07-02 21:04:22 -07003924 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS;
3925 if (!enable_ept)
3926 vmx->vcpu.arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003927 if (is_guest_mode(&vmx->vcpu))
3928 vmx->vcpu.arch.cr4_guest_owned_bits &=
3929 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003930 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
3931}
3932
Sean Christophersonc075c3e2019-05-07 12:17:53 -07003933u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
Yang Zhang01e439b2013-04-11 19:25:12 +08003934{
3935 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
3936
Andrey Smetanind62caab2015-11-10 15:36:33 +03003937 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08003938 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003939
3940 if (!enable_vnmi)
3941 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
3942
Sean Christopherson804939e2019-05-07 12:18:05 -07003943 if (!enable_preemption_timer)
3944 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
3945
Yang Zhang01e439b2013-04-11 19:25:12 +08003946 return pin_based_exec_ctrl;
3947}
3948
Andrey Smetanind62caab2015-11-10 15:36:33 +03003949static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
3950{
3951 struct vcpu_vmx *vmx = to_vmx(vcpu);
3952
Sean Christophersonc5f2c762019-05-07 12:17:55 -07003953 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03003954 if (cpu_has_secondary_exec_ctrls()) {
3955 if (kvm_vcpu_apicv_active(vcpu))
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003956 secondary_exec_controls_setbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03003957 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3958 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3959 else
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003960 secondary_exec_controls_clearbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03003961 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3962 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3963 }
3964
3965 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003966 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003967}
3968
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08003969u32 vmx_exec_control(struct vcpu_vmx *vmx)
3970{
3971 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
3972
3973 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
3974 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
3975
3976 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
3977 exec_control &= ~CPU_BASED_TPR_SHADOW;
3978#ifdef CONFIG_X86_64
3979 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3980 CPU_BASED_CR8_LOAD_EXITING;
3981#endif
3982 }
3983 if (!enable_ept)
3984 exec_control |= CPU_BASED_CR3_STORE_EXITING |
3985 CPU_BASED_CR3_LOAD_EXITING |
3986 CPU_BASED_INVLPG_EXITING;
3987 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
3988 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
3989 CPU_BASED_MONITOR_EXITING);
3990 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
3991 exec_control &= ~CPU_BASED_HLT_EXITING;
3992 return exec_control;
3993}
3994
Sean Christopherson8b50b922020-09-24 17:30:11 -07003995/*
3996 * Adjust a single secondary execution control bit to intercept/allow an
3997 * instruction in the guest. This is usually done based on whether or not a
3998 * feature has been exposed to the guest in order to correctly emulate faults.
3999 */
4000static inline void
4001vmx_adjust_secondary_exec_control(struct vcpu_vmx *vmx, u32 *exec_control,
4002 u32 control, bool enabled, bool exiting)
4003{
4004 /*
4005 * If the control is for an opt-in feature, clear the control if the
4006 * feature is not exposed to the guest, i.e. not enabled. If the
4007 * control is opt-out, i.e. an exiting control, clear the control if
4008 * the feature _is_ exposed to the guest, i.e. exiting/interception is
4009 * disabled for the associated instruction. Note, the caller is
4010 * responsible presetting exec_control to set all supported bits.
4011 */
4012 if (enabled == exiting)
4013 *exec_control &= ~control;
4014
4015 /*
4016 * Update the nested MSR settings so that a nested VMM can/can't set
4017 * controls for features that are/aren't exposed to the guest.
4018 */
4019 if (nested) {
4020 if (enabled)
4021 vmx->nested.msrs.secondary_ctls_high |= control;
4022 else
4023 vmx->nested.msrs.secondary_ctls_high &= ~control;
4024 }
4025}
4026
4027/*
4028 * Wrapper macro for the common case of adjusting a secondary execution control
4029 * based on a single guest CPUID bit, with a dedicated feature bit. This also
4030 * verifies that the control is actually supported by KVM and hardware.
4031 */
4032#define vmx_adjust_sec_exec_control(vmx, exec_control, name, feat_name, ctrl_name, exiting) \
4033({ \
4034 bool __enabled; \
4035 \
4036 if (cpu_has_vmx_##name()) { \
4037 __enabled = guest_cpuid_has(&(vmx)->vcpu, \
4038 X86_FEATURE_##feat_name); \
4039 vmx_adjust_secondary_exec_control(vmx, exec_control, \
4040 SECONDARY_EXEC_##ctrl_name, __enabled, exiting); \
4041 } \
4042})
4043
4044/* More macro magic for ENABLE_/opt-in versus _EXITING/opt-out controls. */
4045#define vmx_adjust_sec_exec_feature(vmx, exec_control, lname, uname) \
4046 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, ENABLE_##uname, false)
4047
4048#define vmx_adjust_sec_exec_exiting(vmx, exec_control, lname, uname) \
4049 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, uname##_EXITING, true)
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004050
Paolo Bonzini80154d72017-08-24 13:55:35 +02004051static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004052{
Paolo Bonzini80154d72017-08-24 13:55:35 +02004053 struct kvm_vcpu *vcpu = &vmx->vcpu;
4054
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004055 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004056
Sean Christopherson2ef76192020-03-02 15:56:22 -08004057 if (vmx_pt_mode_is_system())
Chao Pengf99e3da2018-10-24 16:05:10 +08004058 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004059 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004060 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4061 if (vmx->vpid == 0)
4062 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4063 if (!enable_ept) {
4064 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4065 enable_unrestricted_guest = 0;
4066 }
4067 if (!enable_unrestricted_guest)
4068 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004069 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004070 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02004071 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004072 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4073 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004074 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004075
4076 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4077 * in vmx_set_cr4. */
4078 exec_control &= ~SECONDARY_EXEC_DESC;
4079
Abel Gordonabc4fc52013-04-18 14:35:25 +03004080 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4081 (handle_vmptrld).
4082 We can NOT enable shadow_vmcs here because we don't have yet
4083 a current VMCS12
4084 */
4085 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004086
4087 if (!enable_pml)
4088 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004089
Sean Christophersonbecdad82020-09-23 09:50:45 -07004090 if (cpu_has_vmx_xsaves()) {
Paolo Bonzini3db13482017-08-24 14:48:03 +02004091 /* Exposing XSAVES only when XSAVE is exposed */
4092 bool xsaves_enabled =
Sean Christopherson96be4e02019-12-10 14:44:15 -08004093 boot_cpu_has(X86_FEATURE_XSAVE) &&
Paolo Bonzini3db13482017-08-24 14:48:03 +02004094 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4095 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4096
Aaron Lewis72041602019-10-21 16:30:20 -07004097 vcpu->arch.xsaves_enabled = xsaves_enabled;
4098
Sean Christopherson8b50b922020-09-24 17:30:11 -07004099 vmx_adjust_secondary_exec_control(vmx, &exec_control,
4100 SECONDARY_EXEC_XSAVES,
4101 xsaves_enabled, false);
Paolo Bonzini3db13482017-08-24 14:48:03 +02004102 }
4103
Sean Christopherson8b50b922020-09-24 17:30:11 -07004104 vmx_adjust_sec_exec_feature(vmx, &exec_control, rdtscp, RDTSCP);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004105
Sean Christophersonb936d3e2020-09-23 09:50:46 -07004106 /*
4107 * Expose INVPCID if and only if PCID is also exposed to the guest.
4108 * INVPCID takes a #UD when it's disabled in the VMCS, but a #GP or #PF
4109 * if CR4.PCIDE=0. Enumerating CPUID.INVPCID=1 would lead to incorrect
4110 * behavior from the guest perspective (it would expect #GP or #PF).
4111 */
4112 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
4113 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
Sean Christopherson8b50b922020-09-24 17:30:11 -07004114 vmx_adjust_sec_exec_feature(vmx, &exec_control, invpcid, INVPCID);
Sean Christophersonb936d3e2020-09-23 09:50:46 -07004115
Paolo Bonzini80154d72017-08-24 13:55:35 +02004116
Sean Christopherson8b50b922020-09-24 17:30:11 -07004117 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdrand, RDRAND);
4118 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdseed, RDSEED);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004119
Sean Christopherson8b50b922020-09-24 17:30:11 -07004120 vmx_adjust_sec_exec_control(vmx, &exec_control, waitpkg, WAITPKG,
4121 ENABLE_USR_WAIT_PAUSE, false);
Tao Xue69e72fa2019-07-16 14:55:49 +08004122
Paolo Bonzini80154d72017-08-24 13:55:35 +02004123 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004124}
4125
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004126static void ept_set_mmio_spte_mask(void)
4127{
4128 /*
4129 * EPT Misconfigurations can be generated if the value of bits 2:0
4130 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004131 */
Paolo Bonzinie7581ca2020-05-19 05:04:49 -04004132 kvm_mmu_set_mmio_spte_mask(VMX_EPT_MISCONFIG_WX_VALUE, 0);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004133}
4134
Wanpeng Lif53cd632014-12-02 19:14:58 +08004135#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136
Sean Christopherson944c3462018-12-03 13:53:09 -08004137/*
Xiaoyao Li1b842922019-10-20 17:11:01 +08004138 * Noting that the initialization of Guest-state Area of VMCS is in
4139 * vmx_vcpu_reset().
Sean Christopherson944c3462018-12-03 13:53:09 -08004140 */
Xiaoyao Li1b842922019-10-20 17:11:01 +08004141static void init_vmcs(struct vcpu_vmx *vmx)
Sean Christopherson944c3462018-12-03 13:53:09 -08004142{
Sean Christopherson944c3462018-12-03 13:53:09 -08004143 if (nested)
Xiaoyao Li1b842922019-10-20 17:11:01 +08004144 nested_vmx_set_vmcs_shadowing_bitmap();
Sean Christopherson944c3462018-12-03 13:53:09 -08004145
Sheng Yang25c5f222008-03-28 13:18:56 +08004146 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004147 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08004148
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4150
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151 /* Control */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004152 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004153
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004154 exec_controls_set(vmx, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155
Dan Williamsdfa169b2016-06-02 11:17:24 -07004156 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004157 vmx_compute_secondary_exec_control(vmx);
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004158 secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07004159 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004160
Andrey Smetanind62caab2015-11-10 15:36:33 +03004161 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004162 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4163 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4164 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4165 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4166
4167 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004168
Li RongQing0bcf2612015-12-03 13:29:34 +08004169 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004170 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004171 }
4172
Wanpeng Lib31c1142018-03-12 04:53:04 -07004173 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004174 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004175 vmx->ple_window = ple_window;
4176 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004177 }
4178
Xiao Guangrongc3707952011-07-12 03:28:04 +08004179 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4180 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4182
Avi Kivity9581d442010-10-19 16:46:55 +02004183 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4184 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004185 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4187 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188
Bandan Das2a499e42017-08-03 15:54:41 -04004189 if (cpu_has_vmx_vmfunc())
4190 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4191
Eddie Dong2cc51562007-05-21 07:28:09 +03004192 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4193 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004194 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03004195 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004196 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197
Radim Krčmář74545702015-04-27 15:11:25 +02004198 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4199 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004200
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004201 vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08004202
4203 /* 22.2.1, 20.8.1 */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004204 vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004205
Sean Christophersonfa71e952020-07-02 21:04:22 -07004206 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4207 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004208
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004209 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004210
Xiaoyao Li35fbe0d2019-10-20 17:10:58 +08004211 if (vmx->vpid != 0)
4212 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4213
Sean Christophersonbecdad82020-09-23 09:50:45 -07004214 if (cpu_has_vmx_xsaves())
Wanpeng Lif53cd632014-12-02 19:14:58 +08004215 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4216
Peter Feiner4e595162016-07-07 14:49:58 -07004217 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07004218 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4219 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4220 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07004221
4222 if (cpu_has_vmx_encls_vmexit())
4223 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Chao Peng2ef444f2018-10-24 16:05:12 +08004224
Sean Christopherson2ef76192020-03-02 15:56:22 -08004225 if (vmx_pt_mode_is_host_guest()) {
Chao Peng2ef444f2018-10-24 16:05:12 +08004226 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
4227 /* Bit[6~0] are forced to 1, writes are ignored. */
4228 vmx->pt_desc.guest.output_mask = 0x7F;
4229 vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
4230 }
Paolo Bonzini8c4182b2020-07-10 17:48:10 +02004231
4232 /*
4233 * If EPT is enabled, #PF is only trapped if MAXPHYADDR is mismatched
4234 * between guest and host. In that case we only care about present
4235 * faults.
4236 */
4237 if (enable_ept) {
4238 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, PFERR_PRESENT_MASK);
4239 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, PFERR_PRESENT_MASK);
4240 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004241}
4242
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004243static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004244{
4245 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004246 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004247 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004248
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004249 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004250 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004251
Tao Xu6e3ba4a2019-07-16 14:55:50 +08004252 vmx->msr_ia32_umwait_control = 0;
4253
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004254 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Wanpeng Li95c06542019-09-05 14:26:28 +08004255 vmx->hv_deadline_tsc = -1;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004256 kvm_set_cr8(vcpu, 0);
4257
4258 if (!init_event) {
4259 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4260 MSR_IA32_APICBASE_ENABLE;
4261 if (kvm_vcpu_is_reset_bsp(vcpu))
4262 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4263 apic_base_msr.host_initiated = true;
4264 kvm_set_apic_base(vcpu, &apic_base_msr);
4265 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004266
Avi Kivity2fb92db2011-04-27 19:42:18 +03004267 vmx_segment_cache_clear(vmx);
4268
Avi Kivity5706be02008-08-20 15:07:31 +03004269 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004270 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004271 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004272
4273 seg_setup(VCPU_SREG_DS);
4274 seg_setup(VCPU_SREG_ES);
4275 seg_setup(VCPU_SREG_FS);
4276 seg_setup(VCPU_SREG_GS);
4277 seg_setup(VCPU_SREG_SS);
4278
4279 vmcs_write16(GUEST_TR_SELECTOR, 0);
4280 vmcs_writel(GUEST_TR_BASE, 0);
4281 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4282 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4283
4284 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4285 vmcs_writel(GUEST_LDTR_BASE, 0);
4286 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4287 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4288
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004289 if (!init_event) {
4290 vmcs_write32(GUEST_SYSENTER_CS, 0);
4291 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4292 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4293 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4294 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004295
Wanpeng Lic37c2872017-11-20 14:52:21 -08004296 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01004297 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004298
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004299 vmcs_writel(GUEST_GDTR_BASE, 0);
4300 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4301
4302 vmcs_writel(GUEST_IDTR_BASE, 0);
4303 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4304
Anthony Liguori443381a2010-12-06 10:53:38 -06004305 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004306 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004307 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07004308 if (kvm_mpx_supported())
4309 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004310
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004311 setup_msrs(vmx);
4312
Avi Kivity6aa8b732006-12-10 02:21:36 -08004313 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4314
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004315 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004316 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004317 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004318 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004319 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004320 vmcs_write32(TPR_THRESHOLD, 0);
4321 }
4322
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004323 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004325 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004326 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06004327 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004328 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004329 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004330
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004331 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004332
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004333 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004334 if (init_event)
4335 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336}
4337
Jan Kiszkac9a79532014-03-07 20:03:15 +01004338static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004339{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08004340 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004341}
4342
Jan Kiszkac9a79532014-03-07 20:03:15 +01004343static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004344{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004345 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004346 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01004347 enable_irq_window(vcpu);
4348 return;
4349 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004350
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08004351 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004352}
4353
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004354static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004355{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004356 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004357 uint32_t intr;
4358 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004359
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004360 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004361
Avi Kivityfa89a812008-09-01 15:57:51 +03004362 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004363 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004364 int inc_eip = 0;
4365 if (vcpu->arch.interrupt.soft)
4366 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004367 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
Eddie Dong85f455f2007-07-06 12:20:49 +03004368 return;
4369 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004370 intr = irq | INTR_INFO_VALID_MASK;
4371 if (vcpu->arch.interrupt.soft) {
4372 intr |= INTR_TYPE_SOFT_INTR;
4373 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4374 vmx->vcpu.arch.event_exit_inst_len);
4375 } else
4376 intr |= INTR_TYPE_EXT_INTR;
4377 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004378
4379 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004380}
4381
Sheng Yangf08864b2008-05-15 18:23:25 +08004382static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4383{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004384 struct vcpu_vmx *vmx = to_vmx(vcpu);
4385
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004386 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004387 /*
4388 * Tracking the NMI-blocked state in software is built upon
4389 * finding the next open IRQ window. This, in turn, depends on
4390 * well-behaving guests: They have to keep IRQs disabled at
4391 * least as long as the NMI handler runs. Otherwise we may
4392 * cause NMI nesting, maybe breaking the guest. But as this is
4393 * highly unlikely, we can live with the residual risk.
4394 */
4395 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
4396 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4397 }
4398
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004399 ++vcpu->stat.nmi_injections;
4400 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004401
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004402 if (vmx->rmode.vm86_active) {
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004403 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004404 return;
4405 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08004406
Sheng Yangf08864b2008-05-15 18:23:25 +08004407 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4408 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004409
4410 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004411}
4412
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004413bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004414{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004415 struct vcpu_vmx *vmx = to_vmx(vcpu);
4416 bool masked;
4417
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004418 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004419 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004420 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02004421 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004422 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4423 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4424 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004425}
4426
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004427void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004428{
4429 struct vcpu_vmx *vmx = to_vmx(vcpu);
4430
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004431 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004432 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4433 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4434 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4435 }
4436 } else {
4437 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4438 if (masked)
4439 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4440 GUEST_INTR_STATE_NMI);
4441 else
4442 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4443 GUEST_INTR_STATE_NMI);
4444 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004445}
4446
Sean Christopherson1b660b62020-04-22 19:25:44 -07004447bool vmx_nmi_blocked(struct kvm_vcpu *vcpu)
4448{
4449 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
4450 return false;
4451
4452 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4453 return true;
4454
4455 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4456 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
4457 GUEST_INTR_STATE_NMI));
4458}
4459
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004460static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Jan Kiszka2505dc92013-04-14 12:12:47 +02004461{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004462 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004463 return -EBUSY;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004464
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004465 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
4466 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004467 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004468
Sean Christopherson1b660b62020-04-22 19:25:44 -07004469 return !vmx_nmi_blocked(vcpu);
4470}
Sean Christopherson429ab572020-04-22 19:25:42 -07004471
Sean Christopherson1b660b62020-04-22 19:25:44 -07004472bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)
4473{
4474 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Sean Christopherson88c604b2020-04-22 19:25:41 -07004475 return false;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004476
Sean Christopherson7ab0abd2020-04-22 19:25:50 -07004477 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) ||
Sean Christopherson1b660b62020-04-22 19:25:44 -07004478 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4479 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Jan Kiszka2505dc92013-04-14 12:12:47 +02004480}
4481
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004482static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Gleb Natapov78646122009-03-23 12:12:11 +02004483{
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004484 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004485 return -EBUSY;
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004486
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004487 /*
4488 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
4489 * e.g. if the IRQ arrived asynchronously after checking nested events.
4490 */
4491 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004492 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004493
Sean Christopherson1b660b62020-04-22 19:25:44 -07004494 return !vmx_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02004495}
4496
Izik Eiduscbc94022007-10-25 00:29:55 +02004497static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4498{
4499 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004500
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004501 if (enable_unrestricted_guest)
4502 return 0;
4503
Peter Xu6a3c6232020-01-09 09:57:16 -05004504 mutex_lock(&kvm->slots_lock);
4505 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4506 PAGE_SIZE * 3);
4507 mutex_unlock(&kvm->slots_lock);
4508
Izik Eiduscbc94022007-10-25 00:29:55 +02004509 if (ret)
4510 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004511 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004512 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004513}
4514
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004515static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4516{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004517 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004518 return 0;
4519}
4520
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004521static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004522{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004523 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004524 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004525 /*
4526 * Update instruction length as we may reinject the exception
4527 * from user space while in guest debugging mode.
4528 */
4529 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4530 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004531 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004532 return false;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004533 fallthrough;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004534 case DB_VECTOR:
Miaohe Lina8cfbae2020-02-19 10:45:48 +08004535 return !(vcpu->guest_debug &
4536 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP));
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004537 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004538 case OF_VECTOR:
4539 case BR_VECTOR:
4540 case UD_VECTOR:
4541 case DF_VECTOR:
4542 case SS_VECTOR:
4543 case GP_VECTOR:
4544 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004545 return true;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004546 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004547 return false;
4548}
4549
4550static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4551 int vec, u32 err_code)
4552{
4553 /*
4554 * Instruction with address size override prefix opcode 0x67
4555 * Cause the #SS fault with 0 error code in VM86 mode.
4556 */
4557 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004558 if (kvm_emulate_instruction(vcpu, 0)) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004559 if (vcpu->arch.halt_request) {
4560 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004561 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004562 }
4563 return 1;
4564 }
4565 return 0;
4566 }
4567
4568 /*
4569 * Forward all other exceptions that are valid in real mode.
4570 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4571 * the required debugging infrastructure rework.
4572 */
4573 kvm_queue_exception(vcpu, vec);
4574 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004575}
4576
Andi Kleena0861c02009-06-08 17:37:09 +08004577/*
4578 * Trigger machine check on the host. We assume all the MSRs are already set up
4579 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4580 * We pass a fake environment to the machine check handler because we want
4581 * the guest to be always treated like user space, no matter what context
4582 * it used internally.
4583 */
4584static void kvm_machine_check(void)
4585{
Uros Bizjakfb56baa2020-04-14 09:14:14 +02004586#if defined(CONFIG_X86_MCE)
Andi Kleena0861c02009-06-08 17:37:09 +08004587 struct pt_regs regs = {
4588 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4589 .flags = X86_EFLAGS_IF,
4590 };
4591
Thomas Gleixner8cd501c2020-02-25 23:33:23 +01004592 do_machine_check(&regs);
Andi Kleena0861c02009-06-08 17:37:09 +08004593#endif
4594}
4595
Avi Kivity851ba692009-08-24 11:10:17 +03004596static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004597{
Sean Christopherson95b5a482019-04-19 22:50:59 -07004598 /* handled by vmx_vcpu_run() */
Andi Kleena0861c02009-06-08 17:37:09 +08004599 return 1;
4600}
4601
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004602/*
4603 * If the host has split lock detection disabled, then #AC is
4604 * unconditionally injected into the guest, which is the pre split lock
4605 * detection behaviour.
4606 *
4607 * If the host has split lock detection enabled then #AC is
4608 * only injected into the guest when:
4609 * - Guest CPL == 3 (user mode)
4610 * - Guest has #AC detection enabled in CR0
4611 * - Guest EFLAGS has AC bit set
4612 */
4613static inline bool guest_inject_ac(struct kvm_vcpu *vcpu)
4614{
4615 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
4616 return true;
4617
4618 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) &&
4619 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
4620}
4621
Sean Christopherson95b5a482019-04-19 22:50:59 -07004622static int handle_exception_nmi(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004623{
Avi Kivity1155f762007-11-22 11:30:47 +02004624 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004625 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004626 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004627 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004628 u32 vect_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004629
Avi Kivity1155f762007-11-22 11:30:47 +02004630 vect_info = vmx->idt_vectoring_info;
Sean Christophersonf27ad732020-04-27 10:18:37 -07004631 intr_info = vmx_get_intr_info(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004632
Paolo Bonzini2ea72032019-06-06 14:57:25 +02004633 if (is_machine_check(intr_info) || is_nmi(intr_info))
Sean Christopherson95b5a482019-04-19 22:50:59 -07004634 return 1; /* handled by handle_exception_nmi_irqoff() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004635
Wanpeng Li082d06e2018-04-03 16:28:48 -07004636 if (is_invalid_opcode(intr_info))
4637 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004638
Avi Kivity6aa8b732006-12-10 02:21:36 -08004639 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004640 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004641 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004642
Liran Alon9e869482018-03-12 13:12:51 +02004643 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4644 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christophersona6c6ed12019-08-27 14:40:30 -07004645
4646 /*
4647 * VMware backdoor emulation on #GP interception only handles
4648 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
4649 * error code on #GP.
4650 */
4651 if (error_code) {
4652 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4653 return 1;
4654 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004655 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon9e869482018-03-12 13:12:51 +02004656 }
4657
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004658 /*
4659 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4660 * MMIO, it is better to report an internal error.
4661 * See the comments in vmx_handle_exit.
4662 */
4663 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4664 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4665 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4666 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Jim Mattson1aa561b2020-06-03 16:56:21 -07004667 vcpu->run->internal.ndata = 4;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004668 vcpu->run->internal.data[0] = vect_info;
4669 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004670 vcpu->run->internal.data[2] = error_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07004671 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004672 return 0;
4673 }
4674
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675 if (is_page_fault(intr_info)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07004676 cr2 = vmx_get_exit_qual(vcpu);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004677 if (enable_ept && !vcpu->arch.apf.host_apf_flags) {
4678 /*
4679 * EPT will cause page fault only if we need to
4680 * detect illegal GPAs.
4681 */
4682 kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
4683 return 1;
4684 } else
4685 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686 }
4687
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004688 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004689
4690 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4691 return handle_rmode_exception(vcpu, ex_no, error_code);
4692
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004693 switch (ex_no) {
4694 case DB_VECTOR:
Sean Christopherson5addc232020-04-15 13:34:53 -07004695 dr6 = vmx_get_exit_qual(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004696 if (!(vcpu->guest_debug &
4697 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004698 if (is_icebp(intr_info))
Sean Christopherson1957aa62019-08-27 14:40:39 -07004699 WARN_ON(!skip_emulated_instruction(vcpu));
Huw Daviesfd2a4452014-04-16 10:02:51 +01004700
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004701 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004702 return 1;
4703 }
Peter Xu13196632020-05-05 16:49:58 -04004704 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004705 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004706 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004707 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004708 /*
4709 * Update instruction length as we may reinject #BP from
4710 * user space while in guest debugging mode. Reading it for
4711 * #DB as well causes no harm, it is not used in that case.
4712 */
4713 vmx->vcpu.arch.event_exit_inst_len =
4714 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004716 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004717 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4718 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004719 break;
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004720 case AC_VECTOR:
4721 if (guest_inject_ac(vcpu)) {
4722 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4723 return 1;
4724 }
4725
4726 /*
4727 * Handle split lock. Depending on detection mode this will
4728 * either warn and disable split lock detection for this
4729 * task or force SIGBUS on it.
4730 */
4731 if (handle_guest_split_lock(kvm_rip_read(vcpu)))
4732 return 1;
4733 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004734 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004735 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4736 kvm_run->ex.exception = ex_no;
4737 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004738 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740 return 0;
4741}
4742
Andrea Arcangelif399e602019-11-04 17:59:58 -05004743static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004744{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004745 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746 return 1;
4747}
4748
Avi Kivity851ba692009-08-24 11:10:17 +03004749static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004750{
Avi Kivity851ba692009-08-24 11:10:17 +03004751 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004752 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004753 return 0;
4754}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004755
Avi Kivity851ba692009-08-24 11:10:17 +03004756static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757{
He, Qingbfdaab02007-09-12 14:18:28 +08004758 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004759 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004760 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761
Sean Christopherson5addc232020-04-15 13:34:53 -07004762 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02004763 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004764
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004765 ++vcpu->stat.io_exits;
4766
Sean Christopherson432baf62018-03-08 08:57:26 -08004767 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004768 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004769
4770 port = exit_qualification >> 16;
4771 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004772 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004773
Sean Christophersondca7f122018-03-08 08:57:27 -08004774 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004775}
4776
Ingo Molnar102d8322007-02-19 14:37:47 +02004777static void
4778vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4779{
4780 /*
4781 * Patch in the VMCALL instruction:
4782 */
4783 hypercall[0] = 0x0f;
4784 hypercall[1] = 0x01;
4785 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004786}
4787
Guo Chao0fa06072012-06-28 15:16:19 +08004788/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004789static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4790{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004791 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004792 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4793 unsigned long orig_val = val;
4794
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004795 /*
4796 * We get here when L2 changed cr0 in a way that did not change
4797 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004798 * but did change L0 shadowed bits. So we first calculate the
4799 * effective cr0 value that L1 would like to write into the
4800 * hardware. It consists of the L2-owned bits from the new
4801 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004802 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004803 val = (val & ~vmcs12->cr0_guest_host_mask) |
4804 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4805
David Matlack38991522016-11-29 18:14:08 -08004806 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004807 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004808
4809 if (kvm_set_cr0(vcpu, val))
4810 return 1;
4811 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004812 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004813 } else {
4814 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08004815 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004816 return 1;
David Matlack38991522016-11-29 18:14:08 -08004817
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004818 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004819 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004820}
4821
4822static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4823{
4824 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004825 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4826 unsigned long orig_val = val;
4827
4828 /* analogously to handle_set_cr0 */
4829 val = (val & ~vmcs12->cr4_guest_host_mask) |
4830 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4831 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004832 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004833 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004834 return 0;
4835 } else
4836 return kvm_set_cr4(vcpu, val);
4837}
4838
Paolo Bonzini0367f202016-07-12 10:44:55 +02004839static int handle_desc(struct kvm_vcpu *vcpu)
4840{
4841 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004842 return kvm_emulate_instruction(vcpu, 0);
Paolo Bonzini0367f202016-07-12 10:44:55 +02004843}
4844
Avi Kivity851ba692009-08-24 11:10:17 +03004845static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004846{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004847 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004848 int cr;
4849 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004850 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004851 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004852
Sean Christopherson5addc232020-04-15 13:34:53 -07004853 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004854 cr = exit_qualification & 15;
4855 reg = (exit_qualification >> 8) & 15;
4856 switch ((exit_qualification >> 4) & 3) {
4857 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03004858 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004859 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860 switch (cr) {
4861 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004862 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004863 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004864 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004865 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03004866 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004867 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004869 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004870 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004871 case 8: {
4872 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03004873 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004874 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004875 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004876 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08004877 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004878 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08004879 return ret;
4880 /*
4881 * TODO: we might be squashing a
4882 * KVM_GUESTDBG_SINGLESTEP-triggered
4883 * KVM_EXIT_DEBUG here.
4884 */
Avi Kivity851ba692009-08-24 11:10:17 +03004885 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004886 return 0;
4887 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02004888 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004889 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03004890 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004891 WARN_ONCE(1, "Guest should always own CR0.TS");
4892 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02004893 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08004894 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004895 case 1: /*mov from cr*/
4896 switch (cr) {
4897 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004898 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02004899 val = kvm_read_cr3(vcpu);
4900 kvm_register_write(vcpu, reg, val);
4901 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004902 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004903 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004904 val = kvm_get_cr8(vcpu);
4905 kvm_register_write(vcpu, reg, val);
4906 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004907 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004908 }
4909 break;
4910 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02004911 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004912 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02004913 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004914
Kyle Huey6affcbe2016-11-29 12:40:40 -08004915 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004916 default:
4917 break;
4918 }
Avi Kivity851ba692009-08-24 11:10:17 +03004919 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03004920 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08004921 (int)(exit_qualification >> 4) & 3, cr);
4922 return 0;
4923}
4924
Avi Kivity851ba692009-08-24 11:10:17 +03004925static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004926{
He, Qingbfdaab02007-09-12 14:18:28 +08004927 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004928 int dr, dr7, reg;
4929
Sean Christopherson5addc232020-04-15 13:34:53 -07004930 exit_qualification = vmx_get_exit_qual(vcpu);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004931 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4932
4933 /* First, if DR does not exist, trigger UD */
4934 if (!kvm_require_dr(vcpu, dr))
4935 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004936
Jan Kiszkaf2483412010-01-20 18:20:20 +01004937 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03004938 if (!kvm_require_cpl(vcpu, 0))
4939 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004940 dr7 = vmcs_readl(GUEST_DR7);
4941 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004942 /*
4943 * As the vm-exit takes precedence over the debug trap, we
4944 * need to emulate the latter, either for the host or the
4945 * guest debugging itself.
4946 */
4947 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Paolo Bonzini45981de2020-05-06 05:59:39 -04004948 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_RTM | DR6_FIXED_1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004949 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02004950 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004951 vcpu->run->debug.arch.exception = DB_VECTOR;
4952 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004953 return 0;
4954 } else {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004955 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004956 return 1;
4957 }
4958 }
4959
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004960 if (vcpu->guest_debug == 0) {
Sean Christopherson2183f562019-05-07 12:17:56 -07004961 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004962
4963 /*
4964 * No more DR vmexits; force a reload of the debug registers
4965 * and reenter on this instruction. The next vmexit will
4966 * retrieve the full state of the debug registers.
4967 */
4968 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
4969 return 1;
4970 }
4971
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004972 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4973 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03004974 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004975
4976 if (kvm_get_dr(vcpu, dr, &val))
4977 return 1;
4978 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03004979 } else
Nadav Amit57773922014-06-18 17:19:23 +03004980 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004981 return 1;
4982
Kyle Huey6affcbe2016-11-29 12:40:40 -08004983 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984}
4985
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004986static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
4987{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004988 get_debugreg(vcpu->arch.db[0], 0);
4989 get_debugreg(vcpu->arch.db[1], 1);
4990 get_debugreg(vcpu->arch.db[2], 2);
4991 get_debugreg(vcpu->arch.db[3], 3);
4992 get_debugreg(vcpu->arch.dr6, 6);
4993 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
4994
4995 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Sean Christopherson2183f562019-05-07 12:17:56 -07004996 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004997}
4998
Gleb Natapov020df072010-04-13 10:05:23 +03004999static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5000{
5001 vmcs_writel(GUEST_DR7, val);
5002}
5003
Avi Kivity851ba692009-08-24 11:10:17 +03005004static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005005{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01005006 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005007 return 1;
5008}
5009
Avi Kivity851ba692009-08-24 11:10:17 +03005010static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005011{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005012 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005013
Avi Kivity3842d132010-07-27 12:30:24 +03005014 kvm_make_request(KVM_REQ_EVENT, vcpu);
5015
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005016 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005017 return 1;
5018}
5019
Avi Kivity851ba692009-08-24 11:10:17 +03005020static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005021{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005022 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005023}
5024
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005025static int handle_invd(struct kvm_vcpu *vcpu)
5026{
Tom Lendacky28e2b2f2020-09-24 13:41:58 -05005027 /* Treat an INVD instruction as a NOP and just skip it. */
5028 return kvm_skip_emulated_instruction(vcpu);
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005029}
5030
Avi Kivity851ba692009-08-24 11:10:17 +03005031static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005032{
Sean Christopherson5addc232020-04-15 13:34:53 -07005033 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005034
5035 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005036 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005037}
5038
Avi Kivityfee84b02011-11-10 14:57:25 +02005039static int handle_rdpmc(struct kvm_vcpu *vcpu)
5040{
5041 int err;
5042
5043 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005044 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02005045}
5046
Avi Kivity851ba692009-08-24 11:10:17 +03005047static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005048{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005049 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005050}
5051
Dexuan Cui2acf9232010-06-10 11:27:12 +08005052static int handle_xsetbv(struct kvm_vcpu *vcpu)
5053{
5054 u64 new_bv = kvm_read_edx_eax(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07005055 u32 index = kvm_rcx_read(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005056
5057 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005058 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005059 return 1;
5060}
5061
Avi Kivity851ba692009-08-24 11:10:17 +03005062static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005063{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005064 if (likely(fasteoi)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07005065 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005066 int access_type, offset;
5067
5068 access_type = exit_qualification & APIC_ACCESS_TYPE;
5069 offset = exit_qualification & APIC_ACCESS_OFFSET;
5070 /*
5071 * Sane guest uses MOV to write EOI, with written value
5072 * not cared. So make a short-circuit here by avoiding
5073 * heavy instruction emulation.
5074 */
5075 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5076 (offset == APIC_EOI)) {
5077 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005078 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005079 }
5080 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005081 return kvm_emulate_instruction(vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005082}
5083
Yang Zhangc7c9c562013-01-25 10:18:51 +08005084static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5085{
Sean Christopherson5addc232020-04-15 13:34:53 -07005086 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005087 int vector = exit_qualification & 0xff;
5088
5089 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5090 kvm_apic_set_eoi_accelerated(vcpu, vector);
5091 return 1;
5092}
5093
Yang Zhang83d4c282013-01-25 10:18:49 +08005094static int handle_apic_write(struct kvm_vcpu *vcpu)
5095{
Sean Christopherson5addc232020-04-15 13:34:53 -07005096 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhang83d4c282013-01-25 10:18:49 +08005097 u32 offset = exit_qualification & 0xfff;
5098
5099 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5100 kvm_apic_write_nodecode(vcpu, offset);
5101 return 1;
5102}
5103
Avi Kivity851ba692009-08-24 11:10:17 +03005104static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005105{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005106 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005107 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005108 bool has_error_code = false;
5109 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005110 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005111 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005112
5113 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005114 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005115 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005116
Sean Christopherson5addc232020-04-15 13:34:53 -07005117 exit_qualification = vmx_get_exit_qual(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005118
5119 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005120 if (reason == TASK_SWITCH_GATE && idt_v) {
5121 switch (type) {
5122 case INTR_TYPE_NMI_INTR:
5123 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005124 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005125 break;
5126 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005127 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005128 kvm_clear_interrupt_queue(vcpu);
5129 break;
5130 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005131 if (vmx->idt_vectoring_info &
5132 VECTORING_INFO_DELIVER_CODE_MASK) {
5133 has_error_code = true;
5134 error_code =
5135 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5136 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005137 fallthrough;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005138 case INTR_TYPE_SOFT_EXCEPTION:
5139 kvm_clear_exception_queue(vcpu);
5140 break;
5141 default:
5142 break;
5143 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005144 }
Izik Eidus37817f22008-03-24 23:14:53 +02005145 tss_selector = exit_qualification;
5146
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005147 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5148 type != INTR_TYPE_EXT_INTR &&
5149 type != INTR_TYPE_NMI_INTR))
Sean Christopherson1957aa62019-08-27 14:40:39 -07005150 WARN_ON(!skip_emulated_instruction(vcpu));
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005151
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005152 /*
5153 * TODO: What about debug traps on tss switch?
5154 * Are we supposed to inject them and update dr6?
5155 */
Sean Christopherson10517782019-08-27 14:40:35 -07005156 return kvm_task_switch(vcpu, tss_selector,
5157 type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005158 reason, has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02005159}
5160
Avi Kivity851ba692009-08-24 11:10:17 +03005161static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005162{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005163 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005164 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01005165 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005166
Sean Christopherson5addc232020-04-15 13:34:53 -07005167 exit_qualification = vmx_get_exit_qual(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005168
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005169 /*
5170 * EPT violation happened while executing iret from NMI,
5171 * "blocked by NMI" bit has to be set before next VM entry.
5172 * There are errata that may cause this bit to not be set:
5173 * AAK134, BY25.
5174 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005175 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005176 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005177 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005178 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5179
Sheng Yang14394422008-04-28 12:24:45 +08005180 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005181 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005182
Junaid Shahid27959a42016-12-06 16:46:10 -08005183 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005184 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08005185 ? PFERR_USER_MASK : 0;
5186 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005187 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08005188 ? PFERR_WRITE_MASK : 0;
5189 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005190 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08005191 ? PFERR_FETCH_MASK : 0;
5192 /* ept page table entry is present? */
5193 error_code |= (exit_qualification &
5194 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5195 EPT_VIOLATION_EXECUTABLE))
5196 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005197
Paolo Bonzinieebed242016-11-28 14:39:58 +01005198 error_code |= (exit_qualification & 0x100) != 0 ?
5199 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005200
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005201 vcpu->arch.exit_qualification = exit_qualification;
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005202
5203 /*
5204 * Check that the GPA doesn't exceed physical memory limits, as that is
5205 * a guest page fault. We have to emulate the instruction here, because
5206 * if the illegal address is that of a paging structure, then
5207 * EPT_VIOLATION_ACC_WRITE bit is set. Alternatively, if supported we
5208 * would also use advanced VM-exit information for EPT violations to
5209 * reconstruct the page fault error code.
5210 */
Sean Christophersondc465152020-09-24 12:42:49 -07005211 if (unlikely(kvm_vcpu_is_illegal_gpa(vcpu, gpa)))
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005212 return kvm_emulate_instruction(vcpu, 0);
5213
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005214 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005215}
5216
Avi Kivity851ba692009-08-24 11:10:17 +03005217static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005218{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005219 gpa_t gpa;
5220
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005221 /*
5222 * A nested guest cannot optimize MMIO vmexits, because we have an
5223 * nGPA here instead of the required GPA.
5224 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005225 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005226 if (!is_guest_mode(vcpu) &&
5227 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08005228 trace_kvm_fast_mmio(gpa);
Sean Christopherson1957aa62019-08-27 14:40:39 -07005229 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005230 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005231
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07005232 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005233}
5234
Avi Kivity851ba692009-08-24 11:10:17 +03005235static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005236{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005237 WARN_ON_ONCE(!enable_vnmi);
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005238 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Sheng Yangf08864b2008-05-15 18:23:25 +08005239 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005240 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005241
5242 return 1;
5243}
5244
Mohammed Gamal80ced182009-09-01 12:48:18 +02005245static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005246{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005247 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity49e9d552010-09-19 14:34:08 +02005248 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005249 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005250
Sean Christopherson2183f562019-05-07 12:17:56 -07005251 intr_window_requested = exec_controls_get(vmx) &
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005252 CPU_BASED_INTR_WINDOW_EXITING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005253
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005254 while (vmx->emulation_required && count-- != 0) {
Sean Christophersondb438592020-04-22 19:25:48 -07005255 if (intr_window_requested && !vmx_interrupt_blocked(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005256 return handle_interrupt_window(&vmx->vcpu);
5257
Radim Krčmář72875d82017-04-26 22:32:19 +02005258 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005259 return 1;
5260
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005261 if (!kvm_emulate_instruction(vcpu, 0))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005262 return 0;
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005263
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005264 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
Sean Christopherson8fff2712019-08-27 14:40:37 -07005265 vcpu->arch.exception.pending) {
5266 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5267 vcpu->run->internal.suberror =
5268 KVM_INTERNAL_ERROR_EMULATION;
5269 vcpu->run->internal.ndata = 0;
5270 return 0;
5271 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005272
Gleb Natapov8d76c492013-05-08 18:38:44 +03005273 if (vcpu->arch.halt_request) {
5274 vcpu->arch.halt_request = 0;
Sean Christopherson8fff2712019-08-27 14:40:37 -07005275 return kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005276 }
5277
Sean Christopherson8fff2712019-08-27 14:40:37 -07005278 /*
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005279 * Note, return 1 and not 0, vcpu_run() will invoke
5280 * xfer_to_guest_mode() which will create a proper return
5281 * code.
Sean Christopherson8fff2712019-08-27 14:40:37 -07005282 */
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005283 if (__xfer_to_guest_mode_work_pending())
Sean Christopherson8fff2712019-08-27 14:40:37 -07005284 return 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005285 }
5286
Sean Christopherson8fff2712019-08-27 14:40:37 -07005287 return 1;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005288}
5289
5290static void grow_ple_window(struct kvm_vcpu *vcpu)
5291{
5292 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005293 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005294
Babu Mogerc8e88712018-03-16 16:37:24 -04005295 vmx->ple_window = __grow_ple_window(old, ple_window,
5296 ple_window_grow,
5297 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005298
Peter Xu4f75bcc2019-09-06 10:17:22 +08005299 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005300 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005301 trace_kvm_ple_window_update(vcpu->vcpu_id,
5302 vmx->ple_window, old);
5303 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005304}
5305
5306static void shrink_ple_window(struct kvm_vcpu *vcpu)
5307{
5308 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005309 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005310
Babu Mogerc8e88712018-03-16 16:37:24 -04005311 vmx->ple_window = __shrink_ple_window(old, ple_window,
5312 ple_window_shrink,
5313 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005314
Peter Xu4f75bcc2019-09-06 10:17:22 +08005315 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005316 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005317 trace_kvm_ple_window_update(vcpu->vcpu_id,
5318 vmx->ple_window, old);
5319 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005320}
5321
Peng Haoe01bca22018-04-07 05:47:32 +08005322static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005323{
5324 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
5325 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
5326 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
5327 0ull, VMX_EPT_EXECUTABLE_MASK,
5328 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005329 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005330
5331 ept_set_mmio_spte_mask();
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005332}
5333
Avi Kivity6aa8b732006-12-10 02:21:36 -08005334/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005335 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5336 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5337 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005338static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005339{
Wanpeng Lib31c1142018-03-12 04:53:04 -07005340 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005341 grow_ple_window(vcpu);
5342
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08005343 /*
5344 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
5345 * VM-execution control is ignored if CPL > 0. OTOH, KVM
5346 * never set PAUSE_EXITING and just set PLE if supported,
5347 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
5348 */
5349 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005350 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005351}
5352
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005353static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08005354{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005355 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08005356}
5357
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005358static int handle_mwait(struct kvm_vcpu *vcpu)
5359{
5360 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
5361 return handle_nop(vcpu);
5362}
5363
Jim Mattson45ec3682017-08-23 16:32:04 -07005364static int handle_invalid_op(struct kvm_vcpu *vcpu)
5365{
5366 kvm_queue_exception(vcpu, UD_VECTOR);
5367 return 1;
5368}
5369
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005370static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5371{
5372 return 1;
5373}
5374
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005375static int handle_monitor(struct kvm_vcpu *vcpu)
5376{
5377 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
5378 return handle_nop(vcpu);
5379}
5380
Junaid Shahideb4b2482018-06-27 14:59:14 -07005381static int handle_invpcid(struct kvm_vcpu *vcpu)
5382{
5383 u32 vmx_instruction_info;
5384 unsigned long type;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005385 gva_t gva;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005386 struct {
5387 u64 pcid;
5388 u64 gla;
5389 } operand;
5390
5391 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5392 kvm_queue_exception(vcpu, UD_VECTOR);
5393 return 1;
5394 }
5395
5396 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5397 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5398
5399 if (type > 3) {
5400 kvm_inject_gp(vcpu, 0);
5401 return 1;
5402 }
5403
5404 /* According to the Intel instruction reference, the memory operand
5405 * is read even if it isn't needed (e.g., for type==all)
5406 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005407 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005408 vmx_instruction_info, false,
5409 sizeof(operand), &gva))
Junaid Shahideb4b2482018-06-27 14:59:14 -07005410 return 1;
5411
Babu Moger97150922020-09-11 14:29:12 -05005412 return kvm_handle_invpcid(vcpu, type, gva);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005413}
5414
Kai Huang843e4332015-01-28 10:54:28 +08005415static int handle_pml_full(struct kvm_vcpu *vcpu)
5416{
5417 unsigned long exit_qualification;
5418
5419 trace_kvm_pml_full(vcpu->vcpu_id);
5420
Sean Christopherson5addc232020-04-15 13:34:53 -07005421 exit_qualification = vmx_get_exit_qual(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005422
5423 /*
5424 * PML buffer FULL happened while executing iret from NMI,
5425 * "blocked by NMI" bit has to be set before next VM entry.
5426 */
5427 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005428 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005429 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5430 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5431 GUEST_INTR_STATE_NMI);
5432
5433 /*
5434 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5435 * here.., and there's no userspace involvement needed for PML.
5436 */
5437 return 1;
5438}
5439
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005440static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07005441{
Sean Christopherson804939e2019-05-07 12:18:05 -07005442 struct vcpu_vmx *vmx = to_vmx(vcpu);
5443
5444 if (!vmx->req_immediate_exit &&
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005445 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) {
Sean Christophersond264ee02018-08-27 15:21:12 -07005446 kvm_lapic_expired_hv_timer(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005447 return EXIT_FASTPATH_REENTER_GUEST;
5448 }
Sean Christopherson804939e2019-05-07 12:18:05 -07005449
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005450 return EXIT_FASTPATH_NONE;
5451}
5452
5453static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5454{
5455 handle_fastpath_preemption_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005456 return 1;
5457}
5458
Sean Christophersone4027cf2018-12-03 13:53:12 -08005459/*
5460 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5461 * are overwritten by nested_vmx_setup() when nested=1.
5462 */
5463static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5464{
5465 kvm_queue_exception(vcpu, UD_VECTOR);
5466 return 1;
5467}
5468
Sean Christopherson0b665d32018-08-14 09:33:34 -07005469static int handle_encls(struct kvm_vcpu *vcpu)
5470{
5471 /*
5472 * SGX virtualization is not yet supported. There is no software
5473 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
5474 * to prevent the guest from executing ENCLS.
5475 */
5476 kvm_queue_exception(vcpu, UD_VECTOR);
5477 return 1;
5478}
5479
Nadav Har'El0140cae2011-05-25 23:06:28 +03005480/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005481 * The exit handlers return 1 if the exit was handled fully and guest execution
5482 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5483 * to be done to userspace and return 0.
5484 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005485static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Sean Christopherson95b5a482019-04-19 22:50:59 -07005486 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005487 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005488 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005489 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005490 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005491 [EXIT_REASON_CR_ACCESS] = handle_cr,
5492 [EXIT_REASON_DR_ACCESS] = handle_dr,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005493 [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
5494 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
5495 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005496 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005497 [EXIT_REASON_HLT] = kvm_emulate_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005498 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005499 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005500 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005501 [EXIT_REASON_VMCALL] = handle_vmcall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005502 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5503 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5504 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5505 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5506 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5507 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5508 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5509 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5510 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005511 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5512 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005513 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005514 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005515 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005516 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005517 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005518 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005519 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5520 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005521 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5522 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005523 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005524 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005525 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005526 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005527 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5528 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Jim Mattson45ec3682017-08-23 16:32:04 -07005529 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07005530 [EXIT_REASON_RDSEED] = handle_invalid_op,
Kai Huang843e4332015-01-28 10:54:28 +08005531 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005532 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005533 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005534 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005535 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005536};
5537
5538static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005539 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005540
Sean Christopherson235ba742020-09-23 13:13:46 -07005541static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
5542 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02005543{
Sean Christopherson235ba742020-09-23 13:13:46 -07005544 struct vcpu_vmx *vmx = to_vmx(vcpu);
5545
Sean Christopherson5addc232020-04-15 13:34:53 -07005546 *info1 = vmx_get_exit_qual(vcpu);
Sean Christopherson235ba742020-09-23 13:13:46 -07005547 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
5548 *info2 = vmx->idt_vectoring_info;
5549 *intr_info = vmx_get_intr_info(vcpu);
5550 if (is_exception_with_error_code(*intr_info))
5551 *error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5552 else
5553 *error_code = 0;
5554 } else {
5555 *info2 = 0;
5556 *intr_info = 0;
5557 *error_code = 0;
5558 }
Avi Kivity586f9602010-11-18 13:09:54 +02005559}
5560
Kai Huanga3eaa862015-11-04 13:46:05 +08005561static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005562{
Kai Huanga3eaa862015-11-04 13:46:05 +08005563 if (vmx->pml_pg) {
5564 __free_page(vmx->pml_pg);
5565 vmx->pml_pg = NULL;
5566 }
Kai Huang843e4332015-01-28 10:54:28 +08005567}
5568
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005569static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005570{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005571 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005572 u64 *pml_buf;
5573 u16 pml_idx;
5574
5575 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5576
5577 /* Do nothing if PML buffer is empty */
5578 if (pml_idx == (PML_ENTITY_NUM - 1))
5579 return;
5580
5581 /* PML index always points to next available PML buffer entity */
5582 if (pml_idx >= PML_ENTITY_NUM)
5583 pml_idx = 0;
5584 else
5585 pml_idx++;
5586
5587 pml_buf = page_address(vmx->pml_pg);
5588 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5589 u64 gpa;
5590
5591 gpa = pml_buf[pml_idx];
5592 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005593 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005594 }
5595
5596 /* reset PML index */
5597 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5598}
5599
5600/*
5601 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
5602 * Called before reporting dirty_bitmap to userspace.
5603 */
5604static void kvm_flush_pml_buffers(struct kvm *kvm)
5605{
5606 int i;
5607 struct kvm_vcpu *vcpu;
5608 /*
5609 * We only need to kick vcpu out of guest mode here, as PML buffer
5610 * is flushed at beginning of all VMEXITs, and it's obvious that only
5611 * vcpus running in guest are possible to have unflushed GPAs in PML
5612 * buffer.
5613 */
5614 kvm_for_each_vcpu(i, vcpu, kvm)
5615 kvm_vcpu_kick(vcpu);
5616}
5617
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005618static void vmx_dump_sel(char *name, uint32_t sel)
5619{
5620 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005621 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005622 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5623 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5624 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5625}
5626
5627static void vmx_dump_dtsel(char *name, uint32_t limit)
5628{
5629 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5630 name, vmcs_read32(limit),
5631 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5632}
5633
Paolo Bonzini69090812019-04-15 15:16:17 +02005634void dump_vmcs(void)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005635{
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005636 u32 vmentry_ctl, vmexit_ctl;
5637 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
5638 unsigned long cr4;
5639 u64 efer;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005640
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005641 if (!dump_invalid_vmcs) {
5642 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
5643 return;
5644 }
5645
5646 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5647 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5648 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5649 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5650 cr4 = vmcs_readl(GUEST_CR4);
5651 efer = vmcs_read64(GUEST_IA32_EFER);
5652 secondary_exec_control = 0;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005653 if (cpu_has_secondary_exec_ctrls())
5654 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5655
5656 pr_err("*** Guest State ***\n");
5657 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5658 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5659 vmcs_readl(CR0_GUEST_HOST_MASK));
5660 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5661 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5662 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
5663 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
5664 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
5665 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005666 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5667 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5668 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5669 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005670 }
5671 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5672 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5673 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5674 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5675 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5676 vmcs_readl(GUEST_SYSENTER_ESP),
5677 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5678 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5679 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5680 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5681 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5682 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5683 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5684 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5685 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5686 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5687 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
5688 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
5689 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005690 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5691 efer, vmcs_read64(GUEST_IA32_PAT));
5692 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5693 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005694 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005695 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005696 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005697 pr_err("PerfGlobCtl = 0x%016llx\n",
5698 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005699 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005700 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005701 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5702 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5703 vmcs_read32(GUEST_ACTIVITY_STATE));
5704 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5705 pr_err("InterruptStatus = %04x\n",
5706 vmcs_read16(GUEST_INTR_STATUS));
5707
5708 pr_err("*** Host State ***\n");
5709 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5710 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5711 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5712 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5713 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5714 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5715 vmcs_read16(HOST_TR_SELECTOR));
5716 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5717 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5718 vmcs_readl(HOST_TR_BASE));
5719 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5720 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5721 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5722 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5723 vmcs_readl(HOST_CR4));
5724 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5725 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5726 vmcs_read32(HOST_IA32_SYSENTER_CS),
5727 vmcs_readl(HOST_IA32_SYSENTER_EIP));
5728 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005729 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5730 vmcs_read64(HOST_IA32_EFER),
5731 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005732 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005733 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005734 pr_err("PerfGlobCtl = 0x%016llx\n",
5735 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005736
5737 pr_err("*** Control State ***\n");
5738 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5739 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5740 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5741 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5742 vmcs_read32(EXCEPTION_BITMAP),
5743 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5744 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5745 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5746 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5747 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5748 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5749 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5750 vmcs_read32(VM_EXIT_INTR_INFO),
5751 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5752 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5753 pr_err(" reason=%08x qualification=%016lx\n",
5754 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5755 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5756 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5757 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005758 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005759 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005760 pr_err("TSC Multiplier = 0x%016llx\n",
5761 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005762 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
5763 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
5764 u16 status = vmcs_read16(GUEST_INTR_STATUS);
5765 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
5766 }
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005767 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005768 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
5769 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005770 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005771 }
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005772 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5773 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5774 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005775 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005776 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5777 pr_err("PLE Gap=%08x Window=%08x\n",
5778 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5779 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5780 pr_err("Virtual processor ID = 0x%04x\n",
5781 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5782}
5783
Avi Kivity6aa8b732006-12-10 02:21:36 -08005784/*
5785 * The guest has exited. See if we can fix it or if we need userspace
5786 * assistance.
5787 */
Wanpeng Li404d5d72020-04-28 14:23:25 +08005788static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005789{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005790 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005791 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005792 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005793
Kai Huang843e4332015-01-28 10:54:28 +08005794 /*
5795 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5796 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5797 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5798 * mode as if vcpus is in root mode, the PML buffer must has been
5799 * flushed already.
5800 */
5801 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005802 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005803
Sean Christophersondb438592020-04-22 19:25:48 -07005804 /*
5805 * We should never reach this point with a pending nested VM-Enter, and
5806 * more specifically emulation of L2 due to invalid guest state (see
5807 * below) should never happen as that means we incorrectly allowed a
5808 * nested VM-Enter with an invalid vmcs12.
5809 */
5810 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5811
Mohammed Gamal80ced182009-09-01 12:48:18 +02005812 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02005813 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02005814 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005815
Paolo Bonzini96b100c2020-03-17 18:32:50 +01005816 if (is_guest_mode(vcpu)) {
5817 /*
5818 * The host physical addresses of some pages of guest memory
5819 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
5820 * Page). The CPU may write to these pages via their host
5821 * physical address while L2 is running, bypassing any
5822 * address-translation-based dirty tracking (e.g. EPT write
5823 * protection).
5824 *
5825 * Mark them dirty on every exit from L2 to prevent them from
5826 * getting out of sync with dirty tracking.
5827 */
5828 nested_mark_vmcs12_pages_dirty(vcpu);
5829
Sean Christophersonf47baae2020-04-15 10:55:16 -07005830 if (nested_vmx_reflect_vmexit(vcpu))
Sean Christopherson789afc52020-04-15 10:55:10 -07005831 return 1;
Paolo Bonzini96b100c2020-03-17 18:32:50 +01005832 }
Nadav Har'El644d7112011-05-25 23:12:35 +03005833
Mohammed Gamal51207022010-05-31 22:40:54 +03005834 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005835 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03005836 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5837 vcpu->run->fail_entry.hardware_entry_failure_reason
5838 = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07005839 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Mohammed Gamal51207022010-05-31 22:40:54 +03005840 return 0;
5841 }
5842
Avi Kivity29bd8a72007-09-10 17:27:03 +03005843 if (unlikely(vmx->fail)) {
Paolo Bonzini3b20e032019-07-19 18:15:08 +02005844 dump_vmcs();
Avi Kivity851ba692009-08-24 11:10:17 +03005845 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5846 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03005847 = vmcs_read32(VM_INSTRUCTION_ERROR);
Jim Mattson8a14fe42020-06-03 16:56:22 -07005848 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005849 return 0;
5850 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005851
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005852 /*
5853 * Note:
5854 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
5855 * delivery event since it indicates guest is accessing MMIO.
5856 * The vm-exit can be triggered again after return to guest that
5857 * will cause infinite loop.
5858 */
Mike Dayd77c26f2007-10-08 09:02:08 -04005859 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08005860 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02005861 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00005862 exit_reason != EXIT_REASON_PML_FULL &&
Wanpeng Li99b82a12020-08-19 16:55:27 +08005863 exit_reason != EXIT_REASON_APIC_ACCESS &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005864 exit_reason != EXIT_REASON_TASK_SWITCH)) {
5865 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5866 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005867 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005868 vcpu->run->internal.data[0] = vectoring_info;
5869 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005870 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
5871 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
5872 vcpu->run->internal.ndata++;
5873 vcpu->run->internal.data[3] =
5874 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5875 }
Jim Mattson1aa561b2020-06-03 16:56:21 -07005876 vcpu->run->internal.data[vcpu->run->internal.ndata++] =
Jim Mattson8a14fe42020-06-03 16:56:22 -07005877 vcpu->arch.last_vmentry_cpu;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005878 return 0;
5879 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005880
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005881 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005882 vmx->loaded_vmcs->soft_vnmi_blocked)) {
Sean Christophersondb438592020-04-22 19:25:48 -07005883 if (!vmx_interrupt_blocked(vcpu)) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005884 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5885 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
5886 vcpu->arch.nmi_pending) {
5887 /*
5888 * This CPU don't support us in finding the end of an
5889 * NMI-blocked window if the guest runs with IRQs
5890 * disabled. So we pull the trigger after 1 s of
5891 * futile waiting, but inform the user about this.
5892 */
5893 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
5894 "state on VCPU %d after 1 s timeout\n",
5895 __func__, vcpu->vcpu_id);
5896 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5897 }
5898 }
5899
Wanpeng Li404d5d72020-04-28 14:23:25 +08005900 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08005901 return 1;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08005902
5903 if (exit_reason >= kvm_vmx_max_exit_handlers)
5904 goto unexpected_vmexit;
5905#ifdef CONFIG_RETPOLINE
5906 if (exit_reason == EXIT_REASON_MSR_WRITE)
5907 return kvm_emulate_wrmsr(vcpu);
5908 else if (exit_reason == EXIT_REASON_PREEMPTION_TIMER)
5909 return handle_preemption_timer(vcpu);
5910 else if (exit_reason == EXIT_REASON_INTERRUPT_WINDOW)
5911 return handle_interrupt_window(vcpu);
5912 else if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
5913 return handle_external_interrupt(vcpu);
5914 else if (exit_reason == EXIT_REASON_HLT)
5915 return kvm_emulate_halt(vcpu);
5916 else if (exit_reason == EXIT_REASON_EPT_MISCONFIG)
5917 return handle_ept_misconfig(vcpu);
5918#endif
5919
5920 exit_reason = array_index_nospec(exit_reason,
5921 kvm_vmx_max_exit_handlers);
5922 if (!kvm_vmx_exit_handlers[exit_reason])
5923 goto unexpected_vmexit;
5924
5925 return kvm_vmx_exit_handlers[exit_reason](vcpu);
5926
5927unexpected_vmexit:
5928 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", exit_reason);
5929 dump_vmcs();
5930 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5931 vcpu->run->internal.suberror =
5932 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
Jim Mattson1aa561b2020-06-03 16:56:21 -07005933 vcpu->run->internal.ndata = 2;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08005934 vcpu->run->internal.data[0] = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07005935 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08005936 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005937}
5938
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005939/*
5940 * Software based L1D cache flush which is used when microcode providing
5941 * the cache control MSR is not loaded.
5942 *
5943 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
5944 * flush it is required to read in 64 KiB because the replacement algorithm
5945 * is not exactly LRU. This could be sized at runtime via topology
5946 * information but as all relevant affected CPUs have 32KiB L1D cache size
5947 * there is no point in doing so.
5948 */
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02005949static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005950{
5951 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005952
5953 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02005954 * This code is only executed when the the flush mode is 'cond' or
5955 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005956 */
Nicolai Stange427362a2018-07-21 22:25:00 +02005957 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02005958 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005959
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005960 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02005961 * Clear the per-vcpu flush bit, it gets set again
5962 * either from vcpu_run() or from one of the unsafe
5963 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005964 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02005965 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02005966 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02005967
5968 /*
5969 * Clear the per-cpu flush bit, it gets set again from
5970 * the interrupt handlers.
5971 */
5972 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
5973 kvm_clear_cpu_l1tf_flush_l1d();
5974
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005975 if (!flush_l1d)
5976 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005977 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005978
5979 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005980
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02005981 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02005982 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02005983 return;
5984 }
5985
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005986 asm volatile(
5987 /* First ensure the pages are in the TLB */
5988 "xorl %%eax, %%eax\n"
5989 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02005990 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005991 "addl $4096, %%eax\n\t"
5992 "cmpl %%eax, %[size]\n\t"
5993 "jne .Lpopulate_tlb\n\t"
5994 "xorl %%eax, %%eax\n\t"
5995 "cpuid\n\t"
5996 /* Now fill the cache */
5997 "xorl %%eax, %%eax\n"
5998 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02005999 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006000 "addl $64, %%eax\n\t"
6001 "cmpl %%eax, %[size]\n\t"
6002 "jne .Lfill_cache\n\t"
6003 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006004 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006005 [size] "r" (size)
6006 : "eax", "ebx", "ecx", "edx");
6007}
6008
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006009static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006010{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006011 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Liran Alon132f4f72019-11-11 14:30:54 +02006012 int tpr_threshold;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006013
6014 if (is_guest_mode(vcpu) &&
6015 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6016 return;
6017
Liran Alon132f4f72019-11-11 14:30:54 +02006018 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
Liran Alon02d496cf2019-11-11 14:30:55 +02006019 if (is_guest_mode(vcpu))
6020 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6021 else
6022 vmcs_write32(TPR_THRESHOLD, tpr_threshold);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006023}
6024
Sean Christopherson97b7ead2018-12-03 13:53:16 -08006025void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08006026{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006027 struct vcpu_vmx *vmx = to_vmx(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006028 u32 sec_exec_control;
6029
Jim Mattson8d860bb2018-05-09 16:56:05 -04006030 if (!lapic_in_kernel(vcpu))
6031 return;
6032
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07006033 if (!flexpriority_enabled &&
6034 !cpu_has_vmx_virtualize_x2apic_mode())
6035 return;
6036
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006037 /* Postpone execution until vmcs01 is the current VMCS. */
6038 if (is_guest_mode(vcpu)) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006039 vmx->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006040 return;
6041 }
6042
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006043 sec_exec_control = secondary_exec_controls_get(vmx);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006044 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6045 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08006046
Jim Mattson8d860bb2018-05-09 16:56:05 -04006047 switch (kvm_get_apic_mode(vcpu)) {
6048 case LAPIC_MODE_INVALID:
6049 WARN_ONCE(true, "Invalid local APIC state");
6050 case LAPIC_MODE_DISABLED:
6051 break;
6052 case LAPIC_MODE_XAPIC:
6053 if (flexpriority_enabled) {
6054 sec_exec_control |=
6055 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006056 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6057
6058 /*
6059 * Flush the TLB, reloading the APIC access page will
6060 * only do so if its physical address has changed, but
6061 * the guest may have inserted a non-APIC mapping into
6062 * the TLB while the APIC access page was disabled.
6063 */
6064 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006065 }
6066 break;
6067 case LAPIC_MODE_X2APIC:
6068 if (cpu_has_vmx_virtualize_x2apic_mode())
6069 sec_exec_control |=
6070 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6071 break;
Yang Zhang8d146952013-01-25 10:18:50 +08006072 }
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006073 secondary_exec_controls_set(vmx, sec_exec_control);
Yang Zhang8d146952013-01-25 10:18:50 +08006074
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006075 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006076}
6077
Sean Christophersona4148b72020-03-20 14:28:24 -07006078static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
Tang Chen38b99172014-09-24 15:57:54 +08006079{
Sean Christophersona4148b72020-03-20 14:28:24 -07006080 struct page *page;
6081
Sean Christopherson1196cb92020-03-20 14:28:23 -07006082 /* Defer reload until vmcs01 is the current VMCS. */
6083 if (is_guest_mode(vcpu)) {
6084 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true;
6085 return;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07006086 }
Sean Christopherson1196cb92020-03-20 14:28:23 -07006087
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006088 if (!(secondary_exec_controls_get(to_vmx(vcpu)) &
6089 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
6090 return;
6091
Sean Christophersona4148b72020-03-20 14:28:24 -07006092 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
6093 if (is_error_page(page))
6094 return;
6095
6096 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
Sean Christopherson1196cb92020-03-20 14:28:23 -07006097 vmx_flush_tlb_current(vcpu);
Sean Christophersona4148b72020-03-20 14:28:24 -07006098
6099 /*
6100 * Do not pin apic access page in memory, the MMU notifier
6101 * will call us again if it is migrated or swapped out.
6102 */
6103 put_page(page);
Tang Chen38b99172014-09-24 15:57:54 +08006104}
6105
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006106static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006107{
6108 u16 status;
6109 u8 old;
6110
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006111 if (max_isr == -1)
6112 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006113
6114 status = vmcs_read16(GUEST_INTR_STATUS);
6115 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006116 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006117 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006118 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006119 vmcs_write16(GUEST_INTR_STATUS, status);
6120 }
6121}
6122
6123static void vmx_set_rvi(int vector)
6124{
6125 u16 status;
6126 u8 old;
6127
Wei Wang4114c272014-11-05 10:53:43 +08006128 if (vector == -1)
6129 vector = 0;
6130
Yang Zhangc7c9c562013-01-25 10:18:51 +08006131 status = vmcs_read16(GUEST_INTR_STATUS);
6132 old = (u8)status & 0xff;
6133 if ((u8)vector != old) {
6134 status &= ~0xff;
6135 status |= (u8)vector;
6136 vmcs_write16(GUEST_INTR_STATUS, status);
6137 }
6138}
6139
6140static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6141{
Liran Alon851c1a182017-12-24 18:12:56 +02006142 /*
6143 * When running L2, updating RVI is only relevant when
6144 * vmcs12 virtual-interrupt-delivery enabled.
6145 * However, it can be enabled only when L1 also
6146 * intercepts external-interrupts and in that case
6147 * we should not update vmcs02 RVI but instead intercept
6148 * interrupt. Therefore, do nothing when running L2.
6149 */
6150 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08006151 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006152}
6153
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006154static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006155{
6156 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006157 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02006158 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006159
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006160 WARN_ON(!vcpu->arch.apicv_active);
6161 if (pi_test_on(&vmx->pi_desc)) {
6162 pi_clear_on(&vmx->pi_desc);
6163 /*
Liran Alond9ff2742019-11-11 14:25:25 +02006164 * IOMMU can write to PID.ON, so the barrier matters even on UP.
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006165 * But on x86 this is just a compiler barrier anyway.
6166 */
6167 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02006168 max_irr_updated =
6169 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6170
6171 /*
6172 * If we are running L2 and L1 has a new pending interrupt
6173 * which can be injected, we should re-evaluate
6174 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02006175 * If L1 intercepts external-interrupts, we should
6176 * exit from L2 to L1. Otherwise, interrupt should be
6177 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02006178 */
Liran Alon851c1a182017-12-24 18:12:56 +02006179 if (is_guest_mode(vcpu) && max_irr_updated) {
6180 if (nested_exit_on_intr(vcpu))
6181 kvm_vcpu_exiting_guest_mode(vcpu);
6182 else
6183 kvm_make_request(KVM_REQ_EVENT, vcpu);
6184 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006185 } else {
6186 max_irr = kvm_lapic_find_highest_irr(vcpu);
6187 }
6188 vmx_hwapic_irr_update(vcpu, max_irr);
6189 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006190}
6191
Andrey Smetanin63086302015-11-10 15:36:32 +03006192static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006193{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006194 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08006195 return;
6196
Yang Zhangc7c9c562013-01-25 10:18:51 +08006197 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6198 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6199 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6200 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6201}
6202
Paolo Bonzini967235d2016-12-19 14:03:45 +01006203static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
6204{
6205 struct vcpu_vmx *vmx = to_vmx(vcpu);
6206
6207 pi_clear_on(&vmx->pi_desc);
6208 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
6209}
6210
Sean Christopherson535f7ef2020-09-15 12:15:04 -07006211void vmx_do_interrupt_nmi_irqoff(unsigned long entry);
6212
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006213static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, u32 intr_info)
6214{
6215 unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK;
6216 gate_desc *desc = (gate_desc *)host_idt_base + vector;
6217
6218 kvm_before_interrupt(vcpu);
6219 vmx_do_interrupt_nmi_irqoff(gate_offset(desc));
6220 kvm_after_interrupt(vcpu);
6221}
6222
Sean Christopherson95b5a482019-04-19 22:50:59 -07006223static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006224{
Sean Christopherson87915852020-04-15 13:34:54 -07006225 u32 intr_info = vmx_get_intr_info(&vmx->vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006226
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006227 /* if exit due to PF check for async PF */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006228 if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02006229 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags();
Andi Kleena0861c02009-06-08 17:37:09 +08006230 /* Handle machine checks before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006231 else if (is_machine_check(intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08006232 kvm_machine_check();
Gleb Natapov20f65982009-05-11 13:35:55 +03006233 /* We need to handle NMIs before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006234 else if (is_nmi(intr_info))
6235 handle_interrupt_nmi_irqoff(&vmx->vcpu, intr_info);
Avi Kivity51aa01d2010-07-20 14:31:20 +03006236}
Gleb Natapov20f65982009-05-11 13:35:55 +03006237
Sean Christopherson95b5a482019-04-19 22:50:59 -07006238static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006239{
Sean Christopherson87915852020-04-15 13:34:54 -07006240 u32 intr_info = vmx_get_intr_info(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006241
Sean Christopherson49def502019-04-19 22:50:56 -07006242 if (WARN_ONCE(!is_external_intr(intr_info),
6243 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
6244 return;
6245
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006246 handle_interrupt_nmi_irqoff(vcpu, intr_info);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006247}
Sean Christopherson95b5a482019-04-19 22:50:59 -07006248
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006249static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006250{
6251 struct vcpu_vmx *vmx = to_vmx(vcpu);
6252
6253 if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6254 handle_external_interrupt_irqoff(vcpu);
6255 else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)
6256 handle_exception_nmi_irqoff(vmx);
6257}
Yang Zhanga547c6d2013-04-11 19:25:10 +08006258
Sean Christophersoncb97c2d2020-02-18 15:40:11 -08006259static bool vmx_has_emulated_msr(u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006260{
Tom Lendackybc226f02018-05-10 22:06:39 +02006261 switch (index) {
6262 case MSR_IA32_SMBASE:
6263 /*
6264 * We cannot do SMM unless we can run the guest in big
6265 * real mode.
6266 */
6267 return enable_unrestricted_guest || emulate_invalid_guest_state;
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02006268 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
6269 return nested;
Tom Lendackybc226f02018-05-10 22:06:39 +02006270 case MSR_AMD64_VIRT_SPEC_CTRL:
6271 /* This is AMD only. */
6272 return false;
6273 default:
6274 return true;
6275 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006276}
6277
Avi Kivity51aa01d2010-07-20 14:31:20 +03006278static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6279{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006280 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006281 bool unblock_nmi;
6282 u8 vector;
6283 bool idtv_info_valid;
6284
6285 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006286
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006287 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006288 if (vmx->loaded_vmcs->nmi_known_unmasked)
6289 return;
Sean Christopherson87915852020-04-15 13:34:54 -07006290
6291 exit_intr_info = vmx_get_intr_info(&vmx->vcpu);
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006292 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6293 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6294 /*
6295 * SDM 3: 27.7.1.2 (September 2008)
6296 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6297 * a guest IRET fault.
6298 * SDM 3: 23.2.2 (September 2008)
6299 * Bit 12 is undefined in any of the following cases:
6300 * If the VM exit sets the valid bit in the IDT-vectoring
6301 * information field.
6302 * If the VM exit is due to a double fault.
6303 */
6304 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6305 vector != DF_VECTOR && !idtv_info_valid)
6306 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6307 GUEST_INTR_STATE_NMI);
6308 else
6309 vmx->loaded_vmcs->nmi_known_unmasked =
6310 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6311 & GUEST_INTR_STATE_NMI);
6312 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
6313 vmx->loaded_vmcs->vnmi_blocked_time +=
6314 ktime_to_ns(ktime_sub(ktime_get(),
6315 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006316}
6317
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006318static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006319 u32 idt_vectoring_info,
6320 int instr_len_field,
6321 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006322{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006323 u8 vector;
6324 int type;
6325 bool idtv_info_valid;
6326
6327 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006328
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006329 vcpu->arch.nmi_injected = false;
6330 kvm_clear_exception_queue(vcpu);
6331 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006332
6333 if (!idtv_info_valid)
6334 return;
6335
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006336 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006337
Avi Kivity668f6122008-07-02 09:28:55 +03006338 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6339 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006340
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006341 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006342 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006343 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006344 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006345 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006346 * Clear bit "block by NMI" before VM entry if a NMI
6347 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006348 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006349 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006350 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006351 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006352 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006353 fallthrough;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006354 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006355 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006356 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03006357 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006358 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03006359 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006360 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006361 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006362 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006363 fallthrough;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006364 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006365 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006366 break;
6367 default:
6368 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006369 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006370}
6371
Avi Kivity83422e12010-07-20 14:43:23 +03006372static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6373{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006374 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006375 VM_EXIT_INSTRUCTION_LEN,
6376 IDT_VECTORING_ERROR_CODE);
6377}
6378
Avi Kivityb463a6f2010-07-20 15:06:17 +03006379static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6380{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006381 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006382 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6383 VM_ENTRY_INSTRUCTION_LEN,
6384 VM_ENTRY_EXCEPTION_ERROR_CODE);
6385
6386 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6387}
6388
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006389static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6390{
6391 int i, nr_msrs;
6392 struct perf_guest_switch_msr *msrs;
6393
6394 msrs = perf_guest_get_msrs(&nr_msrs);
6395
6396 if (!msrs)
6397 return;
6398
6399 for (i = 0; i < nr_msrs; i++)
6400 if (msrs[i].host == msrs[i].guest)
6401 clear_atomic_switch_msr(vmx, msrs[i].msr);
6402 else
6403 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006404 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006405}
6406
Sean Christophersonf459a702018-08-27 15:21:11 -07006407static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006408{
6409 struct vcpu_vmx *vmx = to_vmx(vcpu);
6410 u64 tscl;
6411 u32 delta_tsc;
6412
Sean Christophersond264ee02018-08-27 15:21:12 -07006413 if (vmx->req_immediate_exit) {
Sean Christopherson804939e2019-05-07 12:18:05 -07006414 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
6415 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6416 } else if (vmx->hv_deadline_tsc != -1) {
Sean Christophersonf459a702018-08-27 15:21:11 -07006417 tscl = rdtsc();
6418 if (vmx->hv_deadline_tsc > tscl)
6419 /* set_hv_timer ensures the delta fits in 32-bits */
6420 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6421 cpu_preemption_timer_multi);
6422 else
6423 delta_tsc = 0;
6424
Sean Christopherson804939e2019-05-07 12:18:05 -07006425 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
6426 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6427 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
6428 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
6429 vmx->loaded_vmcs->hv_timer_soft_disabled = true;
Sean Christophersonf459a702018-08-27 15:21:11 -07006430 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006431}
6432
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006433void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006434{
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006435 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
6436 vmx->loaded_vmcs->host_state.rsp = host_rsp;
6437 vmcs_writel(HOST_RSP, host_rsp);
6438 }
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006439}
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006440
Wanpeng Li404d5d72020-04-28 14:23:25 +08006441static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006442{
6443 switch (to_vmx(vcpu)->exit_reason) {
6444 case EXIT_REASON_MSR_WRITE:
6445 return handle_fastpath_set_msr_irqoff(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04006446 case EXIT_REASON_PREEMPTION_TIMER:
6447 return handle_fastpath_preemption_timer(vcpu);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006448 default:
6449 return EXIT_FASTPATH_NONE;
6450 }
6451}
6452
Sean Christophersonfc2ba5a2019-01-25 07:41:19 -08006453bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006454
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006455static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
6456 struct vcpu_vmx *vmx)
6457{
6458 /*
6459 * VMENTER enables interrupts (host state), but the kernel state is
6460 * interrupts disabled when this is invoked. Also tell RCU about
6461 * it. This is the same logic as for exit_to_user_mode().
6462 *
6463 * This ensures that e.g. latency analysis on the host observes
6464 * guest mode as interrupt enabled.
6465 *
6466 * guest_enter_irqoff() informs context tracking about the
6467 * transition to guest mode and if enabled adjusts RCU state
6468 * accordingly.
6469 */
6470 instrumentation_begin();
6471 trace_hardirqs_on_prepare();
6472 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
6473 instrumentation_end();
6474
6475 guest_enter_irqoff();
6476 lockdep_hardirqs_on(CALLER_ADDR0);
6477
6478 /* L1D Flush includes CPU buffer clear to mitigate MDS */
6479 if (static_branch_unlikely(&vmx_l1d_should_flush))
6480 vmx_l1d_flush(vcpu);
6481 else if (static_branch_unlikely(&mds_user_clear))
6482 mds_clear_cpu_buffers();
6483
Thomas Gleixner2245d392020-07-08 21:52:00 +02006484 if (vcpu->arch.cr2 != native_read_cr2())
6485 native_write_cr2(vcpu->arch.cr2);
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006486
6487 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
6488 vmx->loaded_vmcs->launched);
6489
Thomas Gleixner2245d392020-07-08 21:52:00 +02006490 vcpu->arch.cr2 = native_read_cr2();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006491
6492 /*
6493 * VMEXIT disables interrupts (host state), but tracing and lockdep
6494 * have them in state 'on' as recorded before entering guest mode.
6495 * Same as enter_from_user_mode().
6496 *
6497 * guest_exit_irqoff() restores host context and reinstates RCU if
6498 * enabled and required.
6499 *
6500 * This needs to be done before the below as native_read_msr()
6501 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
6502 * into world and some more.
6503 */
6504 lockdep_hardirqs_off(CALLER_ADDR0);
6505 guest_exit_irqoff();
6506
6507 instrumentation_begin();
6508 trace_hardirqs_off_finish();
6509 instrumentation_end();
6510}
6511
Wanpeng Li404d5d72020-04-28 14:23:25 +08006512static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006513{
Wanpeng Li404d5d72020-04-28 14:23:25 +08006514 fastpath_t exit_fastpath;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006515 struct vcpu_vmx *vmx = to_vmx(vcpu);
6516 unsigned long cr3, cr4;
6517
Wanpeng Li404d5d72020-04-28 14:23:25 +08006518reenter_guest:
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006519 /* Record the guest's net vcpu time for enforced NMI injections. */
6520 if (unlikely(!enable_vnmi &&
6521 vmx->loaded_vmcs->soft_vnmi_blocked))
6522 vmx->loaded_vmcs->entry_time = ktime_get();
6523
6524 /* Don't enter VMX if guest state is invalid, let the exit handler
6525 start emulation until we arrive back to a valid state */
6526 if (vmx->emulation_required)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006527 return EXIT_FASTPATH_NONE;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006528
6529 if (vmx->ple_window_dirty) {
6530 vmx->ple_window_dirty = false;
6531 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6532 }
6533
wanpeng lic9dfd3f2020-02-17 18:37:43 +08006534 /*
6535 * We did this in prepare_switch_to_guest, because it needs to
6536 * be within srcu_read_lock.
6537 */
6538 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006539
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006540 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006541 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006542 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006543 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6544
6545 cr3 = __get_current_cr3_fast();
6546 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6547 vmcs_writel(HOST_CR3, cr3);
6548 vmx->loaded_vmcs->host_state.cr3 = cr3;
6549 }
6550
6551 cr4 = cr4_read_shadow();
6552 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
6553 vmcs_writel(HOST_CR4, cr4);
6554 vmx->loaded_vmcs->host_state.cr4 = cr4;
6555 }
6556
6557 /* When single-stepping over STI and MOV SS, we must clear the
6558 * corresponding interruptibility bits in the guest state. Otherwise
6559 * vmentry fails as it then expects bit 14 (BS) in pending debug
6560 * exceptions being set, but that's not correct for the guest debugging
6561 * case. */
6562 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6563 vmx_set_interrupt_shadow(vcpu, 0);
6564
Aaron Lewis139a12c2019-10-21 16:30:25 -07006565 kvm_load_guest_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006566
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006567 pt_guest_enter(vmx);
6568
Vitaly Kuznetsov49097762020-06-19 11:40:46 +02006569 atomic_switch_perf_msrs(vmx);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006570
Sean Christopherson804939e2019-05-07 12:18:05 -07006571 if (enable_preemption_timer)
6572 vmx_update_hv_timer(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006573
Wanpeng Li010fd372020-09-10 17:50:41 +08006574 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08006575
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006576 /*
6577 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6578 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6579 * is no need to worry about the conditional branch over the wrmsr
6580 * being speculatively taken.
6581 */
6582 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
6583
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006584 /* The actual VMENTER/EXIT is in the .noinstr.text section. */
6585 vmx_vcpu_enter_exit(vcpu, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006586
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006587 /*
6588 * We do not use IBRS in the kernel. If this vCPU has used the
6589 * SPEC_CTRL MSR it may have left it on; save the value and
6590 * turn it off. This is much more efficient than blindly adding
6591 * it to the atomic save/restore list. Especially as the former
6592 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6593 *
6594 * For non-nested case:
6595 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6596 * save it.
6597 *
6598 * For nested case:
6599 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6600 * save it.
6601 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006602 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006603 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006604
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006605 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006606
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006607 /* All fields are clean at this point */
6608 if (static_branch_unlikely(&enable_evmcs))
6609 current_evmcs->hv_clean_fields |=
6610 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6611
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08006612 if (static_branch_unlikely(&enable_evmcs))
6613 current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index;
6614
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006615 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006616 if (vmx->host_debugctlmsr)
6617 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006618
Avi Kivityaa67f602012-08-01 16:48:03 +03006619#ifndef CONFIG_X86_64
6620 /*
6621 * The sysexit path does not restore ds/es, so we must set them to
6622 * a reasonable value ourselves.
6623 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006624 * We can't defer this to vmx_prepare_switch_to_host() since that
6625 * function may be executed in interrupt context, which saves and
6626 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006627 */
6628 loadsegment(ds, __USER_DS);
6629 loadsegment(es, __USER_DS);
6630#endif
6631
Sean Christophersone5d03de2020-04-15 13:34:51 -07006632 vmx_register_cache_reset(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006633
Chao Peng2ef444f2018-10-24 16:05:12 +08006634 pt_guest_exit(vmx);
6635
Aaron Lewis139a12c2019-10-21 16:30:25 -07006636 kvm_load_host_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006637
Gleb Natapove0b890d2013-09-25 12:51:33 +03006638 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006639 vmx->idt_vectoring_info = 0;
6640
Sean Christopherson873e1da2020-04-10 10:47:02 -07006641 if (unlikely(vmx->fail)) {
6642 vmx->exit_reason = 0xdead;
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006643 return EXIT_FASTPATH_NONE;
Sean Christopherson873e1da2020-04-10 10:47:02 -07006644 }
6645
6646 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
6647 if (unlikely((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY))
Sean Christophersonbeb8d932019-04-19 22:50:55 -07006648 kvm_machine_check();
6649
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006650 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
6651
Sean Christopherson873e1da2020-04-10 10:47:02 -07006652 if (unlikely(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006653 return EXIT_FASTPATH_NONE;
6654
Jim Mattsonb060ca32017-09-14 16:31:42 -07006655 vmx->loaded_vmcs->launched = 1;
6656 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03006657
Avi Kivity51aa01d2010-07-20 14:31:20 +03006658 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006659 vmx_complete_interrupts(vmx);
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006660
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006661 if (is_guest_mode(vcpu))
6662 return EXIT_FASTPATH_NONE;
6663
6664 exit_fastpath = vmx_exit_handlers_fastpath(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006665 if (exit_fastpath == EXIT_FASTPATH_REENTER_GUEST) {
6666 if (!kvm_vcpu_exit_request(vcpu)) {
6667 /*
6668 * FIXME: this goto should be a loop in vcpu_enter_guest,
6669 * but it would incur the cost of a retpoline for now.
6670 * Revisit once static calls are available.
6671 */
Wanpeng Li379a3c82020-04-28 14:23:27 +08006672 if (vcpu->arch.apicv_active)
6673 vmx_sync_pir_to_irr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006674 goto reenter_guest;
6675 }
6676 exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
6677 }
6678
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006679 return exit_fastpath;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006680}
6681
Avi Kivity6aa8b732006-12-10 02:21:36 -08006682static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6683{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006684 struct vcpu_vmx *vmx = to_vmx(vcpu);
6685
Kai Huang843e4332015-01-28 10:54:28 +08006686 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006687 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006688 free_vpid(vmx->vpid);
Sean Christopherson55d23752018-12-03 13:53:18 -08006689 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006690 free_loaded_vmcs(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006691}
6692
Sean Christopherson987b2592019-12-18 13:54:55 -08006693static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006694{
Ben Gardon41836832019-02-11 11:02:52 -08006695 struct vcpu_vmx *vmx;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006696 unsigned long *msr_bitmap;
Sean Christopherson34109c02019-12-18 13:54:50 -08006697 int i, cpu, err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006698
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006699 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0);
6700 vmx = to_vmx(vcpu);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006701
Peter Feiner4e595162016-07-07 14:49:58 -07006702 err = -ENOMEM;
6703
Sean Christopherson034d8e22019-12-18 13:54:49 -08006704 vmx->vpid = allocate_vpid();
6705
Peter Feiner4e595162016-07-07 14:49:58 -07006706 /*
6707 * If PML is turned on, failure on enabling PML just results in failure
6708 * of creating the vcpu, therefore we can simplify PML logic (by
6709 * avoiding dealing with cases, such as enabling PML partially on vcpus
Miaohe Lin67b0ae42019-12-11 14:26:22 +08006710 * for the guest), etc.
Peter Feiner4e595162016-07-07 14:49:58 -07006711 */
6712 if (enable_pml) {
Ben Gardon41836832019-02-11 11:02:52 -08006713 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Peter Feiner4e595162016-07-07 14:49:58 -07006714 if (!vmx->pml_pg)
Sean Christopherson987b2592019-12-18 13:54:55 -08006715 goto free_vpid;
Peter Feiner4e595162016-07-07 14:49:58 -07006716 }
6717
Sean Christopherson14a61b62020-09-23 11:04:08 -07006718 BUILD_BUG_ON(ARRAY_SIZE(vmx_uret_msrs_list) != MAX_NR_USER_RETURN_MSRS);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006719
Sean Christopherson14a61b62020-09-23 11:04:08 -07006720 for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i) {
6721 u32 index = vmx_uret_msrs_list[i];
Xiaoyao Li4be53412019-10-20 17:11:00 +08006722 u32 data_low, data_high;
Sean Christophersonfbc18002020-09-23 11:03:59 -07006723 int j = vmx->nr_uret_msrs;
Xiaoyao Li4be53412019-10-20 17:11:00 +08006724
6725 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6726 continue;
6727 if (wrmsr_safe(index, data_low, data_high) < 0)
6728 continue;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006729
Sean Christopherson802145c2020-09-23 11:04:09 -07006730 vmx->guest_uret_msrs[j].slot = i;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006731 vmx->guest_uret_msrs[j].data = 0;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006732 switch (index) {
6733 case MSR_IA32_TSX_CTRL:
6734 /*
6735 * No need to pass TSX_CTRL_CPUID_CLEAR through, so
6736 * let's avoid changing CPUID bits under the host
6737 * kernel's feet.
6738 */
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006739 vmx->guest_uret_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006740 break;
6741 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006742 vmx->guest_uret_msrs[j].mask = -1ull;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006743 break;
6744 }
Sean Christophersonfbc18002020-09-23 11:03:59 -07006745 ++vmx->nr_uret_msrs;
Xiaoyao Li4be53412019-10-20 17:11:00 +08006746 }
6747
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006748 err = alloc_loaded_vmcs(&vmx->vmcs01);
6749 if (err < 0)
Jim Mattson7d737102019-12-03 16:24:42 -08006750 goto free_pml;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006751
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006752 msr_bitmap = vmx->vmcs01.msr_bitmap;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006753 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_TSC, MSR_TYPE_R);
6754 vmx_disable_intercept_for_msr(vcpu, MSR_FS_BASE, MSR_TYPE_RW);
6755 vmx_disable_intercept_for_msr(vcpu, MSR_GS_BASE, MSR_TYPE_RW);
6756 vmx_disable_intercept_for_msr(vcpu, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
6757 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6758 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6759 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
Sean Christopherson987b2592019-12-18 13:54:55 -08006760 if (kvm_cstate_in_guest(vcpu->kvm)) {
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006761 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C1_RES, MSR_TYPE_R);
6762 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
6763 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
6764 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
Wanpeng Lib5170062019-05-21 14:06:53 +08006765 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006766 vmx->msr_bitmap_mode = 0;
6767
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006768 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03006769 cpu = get_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006770 vmx_vcpu_load(vcpu, cpu);
6771 vcpu->cpu = cpu;
Xiaoyao Li1b842922019-10-20 17:11:01 +08006772 init_vmcs(vmx);
Sean Christopherson34109c02019-12-18 13:54:50 -08006773 vmx_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006774 put_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006775 if (cpu_need_virtualize_apic_accesses(vcpu)) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006776 err = alloc_apic_access_page(vcpu->kvm);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006777 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006778 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02006779 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006780
Sean Christophersone90008d2018-03-05 12:04:37 -08006781 if (enable_ept && !enable_unrestricted_guest) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006782 err = init_rmode_identity_map(vcpu->kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08006783 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02006784 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006785 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006786
Roman Kagan63aff652018-07-19 21:59:07 +03006787 if (nested)
Chenyi Qiangb9757a42020-08-28 16:56:22 +08006788 memcpy(&vmx->nested.msrs, &vmcs_config.nested, sizeof(vmx->nested.msrs));
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006789 else
6790 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08006791
Wincy Van705699a2015-02-03 23:58:17 +08006792 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006793 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006794
Paolo Bonzinibab0c312020-02-11 18:40:58 +01006795 vcpu->arch.microcode_version = 0x100000000ULL;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08006796 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006797
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02006798 /*
6799 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
6800 * or POSTED_INTR_WAKEUP_VECTOR.
6801 */
6802 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
6803 vmx->pi_desc.sn = 1;
6804
Lan Tianyu53963a72018-12-06 15:34:36 +08006805 vmx->ept_pointer = INVALID_PAGE;
6806
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006807 return 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006808
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006809free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006810 free_loaded_vmcs(vmx->loaded_vmcs);
Peter Feiner4e595162016-07-07 14:49:58 -07006811free_pml:
6812 vmx_destroy_pml_buffer(vmx);
Sean Christopherson987b2592019-12-18 13:54:55 -08006813free_vpid:
Wanpeng Li991e7a02015-09-16 17:30:05 +08006814 free_vpid(vmx->vpid);
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006815 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006816}
6817
Thomas Gleixner65fd4cb2019-02-19 11:10:49 +01006818#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
6819#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006820
Wanpeng Lib31c1142018-03-12 04:53:04 -07006821static int vmx_vm_init(struct kvm *kvm)
6822{
Tianyu Lan877ad952018-07-19 08:40:23 +00006823 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
6824
Wanpeng Lib31c1142018-03-12 04:53:04 -07006825 if (!ple_gap)
6826 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006827
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006828 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
6829 switch (l1tf_mitigation) {
6830 case L1TF_MITIGATION_OFF:
6831 case L1TF_MITIGATION_FLUSH_NOWARN:
6832 /* 'I explicitly don't care' is set */
6833 break;
6834 case L1TF_MITIGATION_FLUSH:
6835 case L1TF_MITIGATION_FLUSH_NOSMT:
6836 case L1TF_MITIGATION_FULL:
6837 /*
6838 * Warn upon starting the first VM in a potentially
6839 * insecure environment.
6840 */
Josh Poimboeufb2849092019-01-30 07:13:58 -06006841 if (sched_smt_active())
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006842 pr_warn_once(L1TF_MSG_SMT);
6843 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
6844 pr_warn_once(L1TF_MSG_L1D);
6845 break;
6846 case L1TF_MITIGATION_FULL_FORCE:
6847 /* Flush is enforced */
6848 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006849 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006850 }
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06006851 kvm_apicv_init(kvm, enable_apicv);
Wanpeng Lib31c1142018-03-12 04:53:04 -07006852 return 0;
6853}
6854
Sean Christophersonf257d6d2019-04-19 22:18:17 -07006855static int __init vmx_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03006856{
6857 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08006858 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03006859
Sean Christophersonff10e222019-12-20 20:45:10 -08006860 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
6861 !this_cpu_has(X86_FEATURE_VMX)) {
6862 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id());
6863 return -EIO;
6864 }
6865
Sean Christopherson7caaa712018-12-03 13:53:01 -08006866 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07006867 return -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006868 if (nested)
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006869 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept);
Yang, Sheng002c7f72007-07-31 14:23:01 +03006870 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6871 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6872 smp_processor_id());
Sean Christophersonf257d6d2019-04-19 22:18:17 -07006873 return -EIO;
Yang, Sheng002c7f72007-07-31 14:23:01 +03006874 }
Sean Christophersonf257d6d2019-04-19 22:18:17 -07006875 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03006876}
6877
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006878static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08006879{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006880 u8 cache;
6881 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006882
Chia-I Wu222f06e2020-02-13 13:30:34 -08006883 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in
6884 * memory aliases with conflicting memory types and sometimes MCEs.
6885 * We have to be careful as to what are honored and when.
6886 *
6887 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to
6888 * UC. The effective memory type is UC or WC depending on guest PAT.
6889 * This was historically the source of MCEs and we want to be
6890 * conservative.
6891 *
6892 * When there is no need to deal with noncoherent DMA (e.g., no VT-d
6893 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The
6894 * EPT memory type is set to WB. The effective memory type is forced
6895 * WB.
6896 *
6897 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The
6898 * EPT memory type is used to emulate guest CD/MTRR.
Sheng Yang522c68c2009-04-27 20:35:43 +08006899 */
Chia-I Wu222f06e2020-02-13 13:30:34 -08006900
Paolo Bonzini606decd2015-10-01 13:12:47 +02006901 if (is_mmio) {
6902 cache = MTRR_TYPE_UNCACHABLE;
6903 goto exit;
6904 }
6905
6906 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006907 ipat = VMX_EPT_IPAT_BIT;
6908 cache = MTRR_TYPE_WRBACK;
6909 goto exit;
6910 }
6911
6912 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
6913 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02006914 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08006915 cache = MTRR_TYPE_WRBACK;
6916 else
6917 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006918 goto exit;
6919 }
6920
Xiao Guangrongff536042015-06-15 16:55:22 +08006921 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006922
6923exit:
6924 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08006925}
6926
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006927static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006928{
6929 /*
6930 * These bits in the secondary execution controls field
6931 * are dynamic, the others are mostly based on the hypervisor
6932 * architecture and the guest's CPUID. Do not touch the
6933 * dynamic bits.
6934 */
6935 u32 mask =
6936 SECONDARY_EXEC_SHADOW_VMCS |
6937 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02006938 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6939 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006940
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006941 u32 new_ctl = vmx->secondary_exec_control;
6942 u32 cur_ctl = secondary_exec_controls_get(vmx);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006943
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006944 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006945}
6946
David Matlack8322ebb2016-11-29 18:14:09 -08006947/*
6948 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
6949 * (indicating "allowed-1") if they are supported in the guest's CPUID.
6950 */
6951static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
6952{
6953 struct vcpu_vmx *vmx = to_vmx(vcpu);
6954 struct kvm_cpuid_entry2 *entry;
6955
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006956 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
6957 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08006958
6959#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
6960 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006961 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08006962} while (0)
6963
6964 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08006965 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME));
6966 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME));
6967 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC));
6968 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE));
6969 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE));
6970 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE));
6971 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE));
6972 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE));
6973 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR));
6974 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM));
6975 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX));
6976 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX));
6977 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID));
6978 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE));
David Matlack8322ebb2016-11-29 18:14:09 -08006979
6980 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08006981 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE));
6982 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP));
6983 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP));
6984 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU));
6985 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP));
6986 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57));
David Matlack8322ebb2016-11-29 18:14:09 -08006987
6988#undef cr4_fixed1_update
6989}
6990
Liran Alon5f76f6f2018-09-14 03:25:52 +03006991static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
6992{
6993 struct vcpu_vmx *vmx = to_vmx(vcpu);
6994
6995 if (kvm_mpx_supported()) {
6996 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
6997
6998 if (mpx_enabled) {
6999 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7000 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7001 } else {
7002 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7003 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7004 }
7005 }
7006}
7007
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007008static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7009{
7010 struct vcpu_vmx *vmx = to_vmx(vcpu);
7011 struct kvm_cpuid_entry2 *best = NULL;
7012 int i;
7013
7014 for (i = 0; i < PT_CPUID_LEAVES; i++) {
7015 best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7016 if (!best)
7017 return;
7018 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7019 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7020 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7021 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7022 }
7023
7024 /* Get the number of configurable Address Ranges for filtering */
7025 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7026 PT_CAP_num_address_ranges);
7027
7028 /* Initialize and clear the no dependency bits */
7029 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7030 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7031
7032 /*
7033 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7034 * will inject an #GP
7035 */
7036 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7037 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7038
7039 /*
7040 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7041 * PSBFreq can be set
7042 */
7043 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7044 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7045 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7046
7047 /*
7048 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7049 * MTCFreq can be set
7050 */
7051 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7052 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7053 RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7054
7055 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7056 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7057 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7058 RTIT_CTL_PTW_EN);
7059
7060 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7061 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7062 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7063
7064 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7065 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7066 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7067
7068 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */
7069 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7070 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7071
7072 /* unmask address range configure area */
7073 for (i = 0; i < vmx->pt_desc.addr_range; i++)
Gustavo A. R. Silvad14eff12018-12-26 14:40:59 -06007074 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007075}
7076
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007077static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08007078{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007079 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007080
Aaron Lewis72041602019-10-21 16:30:20 -07007081 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7082 vcpu->arch.xsaves_enabled = false;
7083
Paolo Bonzini80154d72017-08-24 13:55:35 +02007084 if (cpu_has_secondary_exec_ctrls()) {
7085 vmx_compute_secondary_exec_control(vmx);
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007086 vmcs_set_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007087 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007088
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007089 if (nested_vmx_allowed(vcpu))
7090 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007091 FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7092 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007093 else
7094 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007095 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7096 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX);
David Matlack8322ebb2016-11-29 18:14:09 -08007097
Liran Alon5f76f6f2018-09-14 03:25:52 +03007098 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08007099 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03007100 nested_vmx_entry_exit_ctls_update(vcpu);
7101 }
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007102
7103 if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7104 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7105 update_intel_pt_cfg(vcpu);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007106
7107 if (boot_cpu_has(X86_FEATURE_RTM)) {
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07007108 struct vmx_uret_msr *msr;
Sean Christophersond85a8032020-09-23 11:04:06 -07007109 msr = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007110 if (msr) {
7111 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
Sean Christopherson7bf662b2020-09-23 11:04:07 -07007112 vmx_set_guest_uret_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007113 }
7114 }
Sheng Yang0e851882009-12-18 16:48:46 +08007115}
7116
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007117static __init void vmx_set_cpu_caps(void)
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007118{
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007119 kvm_set_cpu_caps();
7120
7121 /* CPUID 0x1 */
7122 if (nested)
7123 kvm_cpu_cap_set(X86_FEATURE_VMX);
7124
7125 /* CPUID 0x7 */
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007126 if (kvm_mpx_supported())
7127 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX);
7128 if (cpu_has_vmx_invpcid())
7129 kvm_cpu_cap_check_and_set(X86_FEATURE_INVPCID);
7130 if (vmx_pt_mode_is_host_guest())
7131 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT);
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007132
Sean Christopherson90d2f602020-03-02 15:56:47 -08007133 if (vmx_umip_emulated())
7134 kvm_cpu_cap_set(X86_FEATURE_UMIP);
7135
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007136 /* CPUID 0xD.1 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01007137 supported_xss = 0;
Sean Christophersonbecdad82020-09-23 09:50:45 -07007138 if (!cpu_has_vmx_xsaves())
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007139 kvm_cpu_cap_clear(X86_FEATURE_XSAVES);
7140
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007141 /* CPUID 0x80000001 */
7142 if (!cpu_has_vmx_rdtscp())
7143 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007144
Sean Christophersonbecdad82020-09-23 09:50:45 -07007145 if (cpu_has_vmx_waitpkg())
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007146 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007147}
7148
Sean Christophersond264ee02018-08-27 15:21:12 -07007149static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7150{
7151 to_vmx(vcpu)->req_immediate_exit = true;
7152}
7153
Oliver Upton35a57132020-02-04 15:26:31 -08007154static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
7155 struct x86_instruction_info *info)
7156{
7157 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7158 unsigned short port;
7159 bool intercept;
7160 int size;
7161
7162 if (info->intercept == x86_intercept_in ||
7163 info->intercept == x86_intercept_ins) {
7164 port = info->src_val;
7165 size = info->dst_bytes;
7166 } else {
7167 port = info->dst_val;
7168 size = info->src_bytes;
7169 }
7170
7171 /*
7172 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
7173 * VM-exits depend on the 'unconditional IO exiting' VM-execution
7174 * control.
7175 *
7176 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
7177 */
7178 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7179 intercept = nested_cpu_has(vmcs12,
7180 CPU_BASED_UNCOND_IO_EXITING);
7181 else
7182 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
7183
Oliver Upton86f7e902020-02-29 11:30:14 -08007184 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
Oliver Upton35a57132020-02-04 15:26:31 -08007185 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
7186}
7187
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007188static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7189 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007190 enum x86_intercept_stage stage,
7191 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007192{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007193 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007194
Oliver Upton35a57132020-02-04 15:26:31 -08007195 switch (info->intercept) {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007196 /*
7197 * RDPID causes #UD if disabled through secondary execution controls.
7198 * Because it is marked as EmulateOnUD, we need to intercept it here.
7199 */
Oliver Upton35a57132020-02-04 15:26:31 -08007200 case x86_intercept_rdtscp:
Sean Christopherson7f3603b2020-09-23 09:50:47 -07007201 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_RDTSCP)) {
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007202 exception->vector = UD_VECTOR;
7203 exception->error_code_valid = false;
Oliver Upton35a57132020-02-04 15:26:31 -08007204 return X86EMUL_PROPAGATE_FAULT;
7205 }
7206 break;
7207
7208 case x86_intercept_in:
7209 case x86_intercept_ins:
7210 case x86_intercept_out:
7211 case x86_intercept_outs:
7212 return vmx_check_intercept_io(vcpu, info);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007213
Oliver Upton86f7e902020-02-29 11:30:14 -08007214 case x86_intercept_lgdt:
7215 case x86_intercept_lidt:
7216 case x86_intercept_lldt:
7217 case x86_intercept_ltr:
7218 case x86_intercept_sgdt:
7219 case x86_intercept_sidt:
7220 case x86_intercept_sldt:
7221 case x86_intercept_str:
7222 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
7223 return X86EMUL_CONTINUE;
7224
7225 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
7226 break;
7227
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007228 /* TODO: check more intercepts... */
Oliver Upton35a57132020-02-04 15:26:31 -08007229 default:
7230 break;
7231 }
7232
Paolo Bonzini07721fe2020-02-04 15:26:29 -08007233 return X86EMUL_UNHANDLEABLE;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007234}
7235
Yunhong Jiang64672c92016-06-13 14:19:59 -07007236#ifdef CONFIG_X86_64
7237/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
7238static inline int u64_shl_div_u64(u64 a, unsigned int shift,
7239 u64 divisor, u64 *result)
7240{
7241 u64 low = a << shift, high = a >> (64 - shift);
7242
7243 /* To avoid the overflow on divq */
7244 if (high >= divisor)
7245 return 1;
7246
7247 /* Low hold the result, high hold rem which is discarded */
7248 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
7249 "rm" (divisor), "0" (low), "1" (high));
7250 *result = low;
7251
7252 return 0;
7253}
7254
Sean Christophersonf9927982019-04-16 13:32:46 -07007255static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
7256 bool *expired)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007257{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007258 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007259 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
Sean Christopherson39497d72019-04-17 10:15:32 -07007260 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007261
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007262 vmx = to_vmx(vcpu);
7263 tscl = rdtsc();
7264 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
7265 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Sean Christopherson39497d72019-04-17 10:15:32 -07007266 lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
7267 ktimer->timer_advance_ns);
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007268
7269 if (delta_tsc > lapic_timer_advance_cycles)
7270 delta_tsc -= lapic_timer_advance_cycles;
7271 else
7272 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007273
7274 /* Convert to host delta tsc if tsc scaling is enabled */
7275 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
Sean Christopherson0967fa12019-04-16 13:32:48 -07007276 delta_tsc && u64_shl_div_u64(delta_tsc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007277 kvm_tsc_scaling_ratio_frac_bits,
Sean Christopherson0967fa12019-04-16 13:32:48 -07007278 vcpu->arch.tsc_scaling_ratio, &delta_tsc))
Yunhong Jiang64672c92016-06-13 14:19:59 -07007279 return -ERANGE;
7280
7281 /*
7282 * If the delta tsc can't fit in the 32 bit after the multi shift,
7283 * we can't use the preemption timer.
7284 * It's possible that it fits on later vmentries, but checking
7285 * on every vmentry is costly so we just use an hrtimer.
7286 */
7287 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
7288 return -ERANGE;
7289
7290 vmx->hv_deadline_tsc = tscl + delta_tsc;
Sean Christophersonf9927982019-04-16 13:32:46 -07007291 *expired = !delta_tsc;
7292 return 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007293}
7294
7295static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
7296{
Sean Christophersonf459a702018-08-27 15:21:11 -07007297 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007298}
7299#endif
7300
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007301static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007302{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007303 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007304 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007305}
7306
Kai Huang843e4332015-01-28 10:54:28 +08007307static void vmx_slot_enable_log_dirty(struct kvm *kvm,
7308 struct kvm_memory_slot *slot)
7309{
Jay Zhou3c9bd402020-02-27 09:32:27 +08007310 if (!kvm_dirty_log_manual_protect_and_init_set(kvm))
7311 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
Kai Huang843e4332015-01-28 10:54:28 +08007312 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
7313}
7314
7315static void vmx_slot_disable_log_dirty(struct kvm *kvm,
7316 struct kvm_memory_slot *slot)
7317{
7318 kvm_mmu_slot_set_dirty(kvm, slot);
7319}
7320
7321static void vmx_flush_log_dirty(struct kvm *kvm)
7322{
7323 kvm_flush_pml_buffers(kvm);
7324}
7325
7326static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
7327 struct kvm_memory_slot *memslot,
7328 gfn_t offset, unsigned long mask)
7329{
7330 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
7331}
7332
Yunhong Jiangbc225122016-06-13 14:19:58 -07007333static int vmx_pre_block(struct kvm_vcpu *vcpu)
7334{
7335 if (pi_pre_block(vcpu))
7336 return 1;
7337
Yunhong Jiang64672c92016-06-13 14:19:59 -07007338 if (kvm_lapic_hv_timer_in_use(vcpu))
7339 kvm_lapic_switch_to_sw_timer(vcpu);
7340
Yunhong Jiangbc225122016-06-13 14:19:58 -07007341 return 0;
7342}
7343
Yunhong Jiangbc225122016-06-13 14:19:58 -07007344static void vmx_post_block(struct kvm_vcpu *vcpu)
7345{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007346 if (kvm_x86_ops.set_hv_timer)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007347 kvm_lapic_switch_to_hv_timer(vcpu);
7348
Yunhong Jiangbc225122016-06-13 14:19:58 -07007349 pi_post_block(vcpu);
7350}
7351
Ashok Rajc45dcc72016-06-22 14:59:56 +08007352static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7353{
7354 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7355 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007356 FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007357 else
7358 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007359 ~FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007360}
7361
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007362static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Ladi Prosek72d7b372017-10-11 16:54:41 +02007363{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007364 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7365 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007366 return -EBUSY;
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -04007367 return !is_smm(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02007368}
7369
Ladi Prosek0234bf82017-10-11 16:54:40 +02007370static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7371{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007372 struct vcpu_vmx *vmx = to_vmx(vcpu);
7373
7374 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7375 if (vmx->nested.smm.guest_mode)
7376 nested_vmx_vmexit(vcpu, -1, 0, 0);
7377
7378 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7379 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007380 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007381 return 0;
7382}
7383
Sean Christophersoned193212019-04-02 08:03:09 -07007384static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007385{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007386 struct vcpu_vmx *vmx = to_vmx(vcpu);
7387 int ret;
7388
7389 if (vmx->nested.smm.vmxon) {
7390 vmx->nested.vmxon = true;
7391 vmx->nested.smm.vmxon = false;
7392 }
7393
7394 if (vmx->nested.smm.guest_mode) {
Sean Christophersona633e412018-09-26 09:23:47 -07007395 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007396 if (ret)
7397 return ret;
7398
7399 vmx->nested.smm.guest_mode = false;
7400 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007401 return 0;
7402}
7403
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007404static void enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007405{
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007406 /* RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007407}
7408
Liran Alon4b9852f2019-08-26 13:24:49 +03007409static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7410{
7411 return to_vmx(vcpu)->nested.vmxon;
7412}
7413
Jim Mattson93dff2f2020-05-08 13:36:43 -07007414static void vmx_migrate_timers(struct kvm_vcpu *vcpu)
7415{
7416 if (is_guest_mode(vcpu)) {
7417 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer;
7418
7419 if (hrtimer_try_to_cancel(timer) == 1)
7420 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
7421 }
7422}
7423
Sean Christopherson6e4fd062020-03-21 13:26:01 -07007424static void hardware_unsetup(void)
Sean Christophersona3203382018-12-03 13:53:11 -08007425{
7426 if (nested)
7427 nested_vmx_hardware_unsetup();
7428
7429 free_kvm_area();
7430}
7431
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007432static bool vmx_check_apicv_inhibit_reasons(ulong bit)
7433{
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -06007434 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
7435 BIT(APICV_INHIBIT_REASON_HYPERV);
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007436
7437 return supported & BIT(bit);
7438}
7439
Sean Christophersone286ac02020-03-21 13:26:02 -07007440static struct kvm_x86_ops vmx_x86_ops __initdata = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007441 .hardware_unsetup = hardware_unsetup,
Sean Christopherson484014f2020-03-21 13:25:57 -07007442
Avi Kivity6aa8b732006-12-10 02:21:36 -08007443 .hardware_enable = hardware_enable,
7444 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007445 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007446 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007447
Sean Christopherson484014f2020-03-21 13:25:57 -07007448 .vm_size = sizeof(struct kvm_vmx),
Wanpeng Lib31c1142018-03-12 04:53:04 -07007449 .vm_init = vmx_vm_init,
7450
Avi Kivity6aa8b732006-12-10 02:21:36 -08007451 .vcpu_create = vmx_create_vcpu,
7452 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007453 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007454
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007455 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007456 .vcpu_load = vmx_vcpu_load,
7457 .vcpu_put = vmx_vcpu_put,
7458
Paolo Bonzini69869822020-07-10 17:48:06 +02007459 .update_exception_bitmap = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007460 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007461 .get_msr = vmx_get_msr,
7462 .set_msr = vmx_set_msr,
7463 .get_segment_base = vmx_get_segment_base,
7464 .get_segment = vmx_get_segment,
7465 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007466 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007467 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
7468 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007469 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007470 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007471 .get_idt = vmx_get_idt,
7472 .set_idt = vmx_set_idt,
7473 .get_gdt = vmx_get_gdt,
7474 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007475 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007476 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007477 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007478 .get_rflags = vmx_get_rflags,
7479 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007480
Sean Christopherson77809382020-03-20 14:28:18 -07007481 .tlb_flush_all = vmx_flush_tlb_all,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07007482 .tlb_flush_current = vmx_flush_tlb_current,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007483 .tlb_flush_gva = vmx_flush_tlb_gva,
Sean Christophersone64419d2020-03-20 14:28:10 -07007484 .tlb_flush_guest = vmx_flush_tlb_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007485
Avi Kivity6aa8b732006-12-10 02:21:36 -08007486 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007487 .handle_exit = vmx_handle_exit,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08007488 .skip_emulated_instruction = vmx_skip_emulated_instruction,
7489 .update_emulated_instruction = vmx_update_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007490 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7491 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007492 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007493 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007494 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007495 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007496 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007497 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007498 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007499 .get_nmi_mask = vmx_get_nmi_mask,
7500 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007501 .enable_nmi_window = enable_nmi_window,
7502 .enable_irq_window = enable_irq_window,
7503 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007504 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007505 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007506 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007507 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007508 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007509 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007510 .hwapic_irr_update = vmx_hwapic_irr_update,
7511 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007512 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007513 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7514 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007515 .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007516
Izik Eiduscbc94022007-10-25 00:29:55 +02007517 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007518 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007519 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007520
Avi Kivity586f9602010-11-18 13:09:54 +02007521 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007522
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007523 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007524
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007525 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007526
Leonid Shatz326e7422018-11-06 12:14:25 +02007527 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007528
Sean Christopherson484014f2020-03-21 13:25:57 -07007529 .load_mmu_pgd = vmx_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007530
7531 .check_intercept = vmx_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007532 .handle_exit_irqoff = vmx_handle_exit_irqoff,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007533
Sean Christophersond264ee02018-08-27 15:21:12 -07007534 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007535
7536 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007537
7538 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
7539 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
7540 .flush_log_dirty = vmx_flush_log_dirty,
7541 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +02007542
Feng Wubf9f6ac2015-09-18 22:29:55 +08007543 .pre_block = vmx_pre_block,
7544 .post_block = vmx_post_block,
7545
Wei Huang25462f72015-06-19 15:45:05 +02007546 .pmu_ops = &intel_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04007547 .nested_ops = &vmx_nested_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007548
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007549 .update_pi_irte = pi_update_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007550
7551#ifdef CONFIG_X86_64
7552 .set_hv_timer = vmx_set_hv_timer,
7553 .cancel_hv_timer = vmx_cancel_hv_timer,
7554#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007555
7556 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007557
Ladi Prosek72d7b372017-10-11 16:54:41 +02007558 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007559 .pre_enter_smm = vmx_pre_enter_smm,
7560 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007561 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007562
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007563 .can_emulate_instruction = vmx_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03007564 .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
Jim Mattson93dff2f2020-05-08 13:36:43 -07007565 .migrate_timers = vmx_migrate_timers,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007566};
7567
Avi Kivity6aa8b732006-12-10 02:21:36 -08007568static __init int hardware_setup(void)
7569{
7570 unsigned long host_bndcfgs;
7571 struct desc_ptr dt;
Sean Christopherson703c3352020-03-02 15:57:03 -08007572 int r, i, ept_lpage_level;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007573
Avi Kivity6aa8b732006-12-10 02:21:36 -08007574 store_idt(&dt);
7575 host_idt_base = dt.address;
7576
Sean Christopherson14a61b62020-09-23 11:04:08 -07007577 for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i)
7578 kvm_define_user_return_msr(i, vmx_uret_msrs_list[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007579
7580 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
7581 return -EIO;
7582
7583 if (boot_cpu_has(X86_FEATURE_NX))
7584 kvm_enable_efer_bits(EFER_NX);
7585
7586 if (boot_cpu_has(X86_FEATURE_MPX)) {
7587 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7588 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7589 }
7590
Sean Christopherson7f5581f2020-03-02 15:56:24 -08007591 if (!cpu_has_vmx_mpx())
Sean Christophersoncfc48182020-03-02 15:56:23 -08007592 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
7593 XFEATURE_MASK_BNDCSR);
7594
Avi Kivity6aa8b732006-12-10 02:21:36 -08007595 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7596 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7597 enable_vpid = 0;
7598
7599 if (!cpu_has_vmx_ept() ||
7600 !cpu_has_vmx_ept_4levels() ||
7601 !cpu_has_vmx_ept_mt_wb() ||
7602 !cpu_has_vmx_invept_global())
7603 enable_ept = 0;
7604
7605 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7606 enable_ept_ad_bits = 0;
7607
7608 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Avi Kivity873a7c42006-12-13 00:34:14 -08007609 enable_unrestricted_guest = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007610
7611 if (!cpu_has_vmx_flexpriority())
7612 flexpriority_enabled = 0;
7613
7614 if (!cpu_has_virtual_nmis())
7615 enable_vnmi = 0;
7616
7617 /*
7618 * set_apic_access_page_addr() is used to reload apic access
7619 * page upon invalidation. No need to do anything if not
7620 * using the APIC_ACCESS_ADDR VMCS field.
7621 */
7622 if (!flexpriority_enabled)
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007623 vmx_x86_ops.set_apic_access_page_addr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007624
7625 if (!cpu_has_vmx_tpr_shadow())
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007626 vmx_x86_ops.update_cr8_intercept = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007627
Avi Kivity6aa8b732006-12-10 02:21:36 -08007628#if IS_ENABLED(CONFIG_HYPERV)
7629 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7630 && enable_ept) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007631 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
7632 vmx_x86_ops.tlb_remote_flush_with_range =
Avi Kivity6aa8b732006-12-10 02:21:36 -08007633 hv_remote_flush_tlb_with_range;
7634 }
7635#endif
7636
7637 if (!cpu_has_vmx_ple()) {
7638 ple_gap = 0;
7639 ple_window = 0;
7640 ple_window_grow = 0;
7641 ple_window_max = 0;
7642 ple_window_shrink = 0;
7643 }
7644
7645 if (!cpu_has_vmx_apicv()) {
7646 enable_apicv = 0;
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007647 vmx_x86_ops.sync_pir_to_irr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007648 }
7649
7650 if (cpu_has_vmx_tsc_scaling()) {
7651 kvm_has_tsc_control = true;
7652 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7653 kvm_tsc_scaling_ratio_frac_bits = 48;
7654 }
7655
7656 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7657
7658 if (enable_ept)
7659 vmx_enable_tdp();
Sean Christopherson703c3352020-03-02 15:57:03 -08007660
7661 if (!enable_ept)
7662 ept_lpage_level = 0;
7663 else if (cpu_has_vmx_ept_1g_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007664 ept_lpage_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08007665 else if (cpu_has_vmx_ept_2m_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007666 ept_lpage_level = PG_LEVEL_2M;
Sean Christopherson703c3352020-03-02 15:57:03 -08007667 else
Sean Christopherson3bae0452020-04-27 17:54:22 -07007668 ept_lpage_level = PG_LEVEL_4K;
Sean Christopherson83013052020-07-15 20:41:22 -07007669 kvm_configure_mmu(enable_ept, vmx_get_max_tdp_level(), ept_lpage_level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007670
7671 /*
7672 * Only enable PML when hardware supports PML feature, and both EPT
7673 * and EPT A/D bit features are enabled -- PML depends on them to work.
7674 */
7675 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7676 enable_pml = 0;
7677
7678 if (!enable_pml) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007679 vmx_x86_ops.slot_enable_log_dirty = NULL;
7680 vmx_x86_ops.slot_disable_log_dirty = NULL;
7681 vmx_x86_ops.flush_log_dirty = NULL;
7682 vmx_x86_ops.enable_log_dirty_pt_masked = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007683 }
7684
7685 if (!cpu_has_vmx_preemption_timer())
7686 enable_preemption_timer = false;
7687
7688 if (enable_preemption_timer) {
7689 u64 use_timer_freq = 5000ULL * 1000 * 1000;
7690 u64 vmx_msr;
7691
7692 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7693 cpu_preemption_timer_multi =
7694 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7695
7696 if (tsc_khz)
7697 use_timer_freq = (u64)tsc_khz * 1000;
7698 use_timer_freq >>= cpu_preemption_timer_multi;
7699
7700 /*
7701 * KVM "disables" the preemption timer by setting it to its max
7702 * value. Don't use the timer if it might cause spurious exits
7703 * at a rate faster than 0.1 Hz (of uninterrupted guest time).
7704 */
7705 if (use_timer_freq > 0xffffffffu / 10)
7706 enable_preemption_timer = false;
7707 }
7708
7709 if (!enable_preemption_timer) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007710 vmx_x86_ops.set_hv_timer = NULL;
7711 vmx_x86_ops.cancel_hv_timer = NULL;
7712 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007713 }
7714
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007715 kvm_set_posted_intr_wakeup_handler(pi_wakeup_handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007716
7717 kvm_mce_cap_supported |= MCG_LMCE_P;
7718
7719 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
7720 return -EINVAL;
7721 if (!enable_ept || !cpu_has_vmx_intel_pt())
7722 pt_mode = PT_MODE_SYSTEM;
7723
7724 if (nested) {
7725 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
7726 vmx_capability.ept);
7727
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07007728 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007729 if (r)
7730 return r;
7731 }
7732
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007733 vmx_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08007734
Avi Kivity6aa8b732006-12-10 02:21:36 -08007735 r = alloc_kvm_area();
7736 if (r)
7737 nested_vmx_hardware_unsetup();
7738 return r;
7739}
7740
Sean Christophersond008dfd2020-03-21 13:25:56 -07007741static struct kvm_x86_init_ops vmx_init_ops __initdata = {
7742 .cpu_has_kvm_support = cpu_has_kvm_support,
7743 .disabled_by_bios = vmx_disabled_by_bios,
7744 .check_processor_compatibility = vmx_check_processor_compat,
7745 .hardware_setup = hardware_setup,
7746
7747 .runtime_ops = &vmx_x86_ops,
7748};
7749
Avi Kivity6aa8b732006-12-10 02:21:36 -08007750static void vmx_cleanup_l1d_flush(void)
7751{
7752 if (vmx_l1d_flush_pages) {
7753 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
7754 vmx_l1d_flush_pages = NULL;
7755 }
7756 /* Restore state so sysfs ignores VMX */
7757 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
7758}
7759
7760static void vmx_exit(void)
7761{
7762#ifdef CONFIG_KEXEC_CORE
7763 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
7764 synchronize_rcu();
7765#endif
7766
7767 kvm_exit();
7768
7769#if IS_ENABLED(CONFIG_HYPERV)
7770 if (static_branch_unlikely(&enable_evmcs)) {
7771 int cpu;
7772 struct hv_vp_assist_page *vp_ap;
7773 /*
7774 * Reset everything to support using non-enlightened VMCS
7775 * access later (e.g. when we reload the module with
7776 * enlightened_vmcs=0)
7777 */
7778 for_each_online_cpu(cpu) {
7779 vp_ap = hv_get_vp_assist_page(cpu);
7780
7781 if (!vp_ap)
7782 continue;
7783
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08007784 vp_ap->nested_control.features.directhypercall = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007785 vp_ap->current_nested_vmcs = 0;
7786 vp_ap->enlighten_vmentry = 0;
7787 }
7788
7789 static_branch_disable(&enable_evmcs);
7790 }
7791#endif
7792 vmx_cleanup_l1d_flush();
7793}
7794module_exit(vmx_exit);
7795
7796static int __init vmx_init(void)
7797{
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02007798 int r, cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007799
7800#if IS_ENABLED(CONFIG_HYPERV)
7801 /*
7802 * Enlightened VMCS usage should be recommended and the host needs
7803 * to support eVMCS v1 or above. We can also disable eVMCS support
7804 * with module parameter.
7805 */
7806 if (enlightened_vmcs &&
7807 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
7808 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
7809 KVM_EVMCS_VERSION) {
7810 int cpu;
7811
7812 /* Check that we have assist pages on all online CPUs */
7813 for_each_online_cpu(cpu) {
7814 if (!hv_get_vp_assist_page(cpu)) {
7815 enlightened_vmcs = false;
7816 break;
7817 }
7818 }
7819
7820 if (enlightened_vmcs) {
7821 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
7822 static_branch_enable(&enable_evmcs);
7823 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08007824
7825 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
7826 vmx_x86_ops.enable_direct_tlbflush
7827 = hv_enable_direct_tlbflush;
7828
Avi Kivity6aa8b732006-12-10 02:21:36 -08007829 } else {
7830 enlightened_vmcs = false;
7831 }
7832#endif
7833
Sean Christophersond008dfd2020-03-21 13:25:56 -07007834 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
Avi Kivity6aa8b732006-12-10 02:21:36 -08007835 __alignof__(struct vcpu_vmx), THIS_MODULE);
7836 if (r)
7837 return r;
7838
7839 /*
7840 * Must be called after kvm_init() so enable_ept is properly set
7841 * up. Hand the parameter mitigation value in which was stored in
7842 * the pre module init parser. If no parameter was given, it will
7843 * contain 'auto' which will be turned into the default 'cond'
7844 * mitigation mode.
7845 */
Waiman Long19a36d32019-08-26 15:30:23 -04007846 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
7847 if (r) {
7848 vmx_exit();
7849 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007850 }
7851
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02007852 for_each_possible_cpu(cpu) {
7853 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007854
7855 pi_init(cpu);
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02007856 }
7857
Avi Kivity6aa8b732006-12-10 02:21:36 -08007858#ifdef CONFIG_KEXEC_CORE
7859 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
7860 crash_vmclear_local_loaded_vmcss);
7861#endif
7862 vmx_check_vmcs12_offsets();
7863
Mohammed Gamal3edd6832020-07-10 17:48:11 +02007864 /*
7865 * Intel processors don't have problems with
7866 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR so enable
7867 * it for VMX by default
7868 */
7869 allow_smaller_maxphyaddr = true;
7870
Avi Kivity6aa8b732006-12-10 02:21:36 -08007871 return 0;
7872}
7873module_init(vmx_init);