blob: 2ce2c73645bf9bcaa235ad5e5972ca21ddd719ea [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/highmem.h>
17#include <linux/hrtimer.h>
18#include <linux/kernel.h>
Avi Kivityedf88412007-12-16 11:02:48 +020019#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080020#include <linux/module.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020021#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070022#include <linux/mod_devicetable.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080023#include <linux/mm.h>
Julien Thierry00089c02020-09-04 16:30:25 +010024#include <linux/objtool.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>
Lai Jiangshana217a652021-05-04 21:50:14 +020039#include <asm/idtentry.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080040#include <asm/io.h>
Feng Wuefc64402015-09-18 22:29:51 +080041#include <asm/irq_remapping.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080042#include <asm/kexec.h>
43#include <asm/perf_event.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"
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +010054#include "hyperv.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080055#include "irq.h"
56#include "kvm_cache_regs.h"
57#include "lapic.h"
58#include "mmu.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080059#include "nested.h"
Wei Huang25462f72015-06-19 15:45:05 +020060#include "pmu.h"
Sean Christopherson9798adb2021-04-12 16:21:38 +120061#include "sgx.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080062#include "trace.h"
Sean Christophersoncb1d4742018-12-03 13:53:04 -080063#include "vmcs.h"
Sean Christopherson609363c2018-12-03 13:53:05 -080064#include "vmcs12.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080065#include "vmx.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080066#include "x86.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030067
Avi Kivity6aa8b732006-12-10 02:21:36 -080068MODULE_AUTHOR("Qumranet");
69MODULE_LICENSE("GPL");
70
Valdis Klētnieks575b2552020-02-27 21:49:52 -050071#ifdef MODULE
Josh Triplette9bda3b2012-03-20 23:33:51 -070072static const struct x86_cpu_id vmx_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010073 X86_MATCH_FEATURE(X86_FEATURE_VMX, NULL),
Josh Triplette9bda3b2012-03-20 23:33:51 -070074 {}
75};
76MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050077#endif
Josh Triplette9bda3b2012-03-20 23:33:51 -070078
Sean Christopherson2c4fd912018-12-03 13:53:03 -080079bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020080module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080081
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010082static bool __read_mostly enable_vnmi = 1;
83module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
84
Sean Christopherson2c4fd912018-12-03 13:53:03 -080085bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020086module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020087
Sean Christopherson2c4fd912018-12-03 13:53:03 -080088bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020089module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080090
Sean Christopherson2c4fd912018-12-03 13:53:03 -080091bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070092module_param_named(unrestricted_guest,
93 enable_unrestricted_guest, bool, S_IRUGO);
94
Sean Christopherson2c4fd912018-12-03 13:53:03 -080095bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080096module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
97
Avi Kivitya27685c2012-06-12 20:30:18 +030098static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020099module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300100
Rusty Russell476bc002012-01-13 09:32:18 +1030101static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +0300102module_param(fasteoi, bool, S_IRUGO);
103
Vitaly Kuznetsova4443262020-02-20 18:22:04 +0100104bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800105module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800106
Nadav Har'El801d3422011-05-25 23:02:23 +0300107/*
108 * If nested=1, nested virtualization is supported, i.e., guests may use
109 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
110 * use VMX instructions.
111 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200112static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300113module_param(nested, bool, S_IRUGO);
114
Sean Christopherson2c4fd912018-12-03 13:53:03 -0800115bool __read_mostly enable_pml = 1;
Kai Huang843e4332015-01-28 10:54:28 +0800116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200118static bool __read_mostly dump_invalid_vmcs = 0;
119module_param(dump_invalid_vmcs, bool, 0644);
120
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100123
Haozhong Zhang64903d62015-10-20 15:39:09 +0800124#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
125
Yunhong Jiang64672c92016-06-13 14:19:59 -0700126/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
127static int __read_mostly cpu_preemption_timer_multi;
128static bool __read_mostly enable_preemption_timer = 1;
129#ifdef CONFIG_X86_64
130module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
131#endif
132
Mohammed Gamalb96e6502020-09-03 16:11:22 +0200133extern bool __read_mostly allow_smaller_maxphyaddr;
134module_param(allow_smaller_maxphyaddr, bool, S_IRUGO);
135
Sean Christopherson3de63472018-07-13 08:42:30 -0700136#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800137#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
138#define KVM_VM_CR0_ALWAYS_ON \
139 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
140 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200141
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800142#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200143#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
144#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
145
Avi Kivity78ac8b42010-04-08 18:19:35 +0300146#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
147
Chao Pengbf8c55d2018-10-24 16:05:14 +0800148#define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \
149 RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \
150 RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \
151 RTIT_STATUS_BYTECNT))
152
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153/*
Alexander Graf3eb90012020-09-25 16:34:20 +0200154 * List of MSRs that can be directly passed to the guest.
155 * In addition to these x2apic and PT MSRs are handled specially.
156 */
157static u32 vmx_possible_passthrough_msrs[MAX_POSSIBLE_PASSTHROUGH_MSRS] = {
158 MSR_IA32_SPEC_CTRL,
159 MSR_IA32_PRED_CMD,
160 MSR_IA32_TSC,
Sean Christophersondbdd0962021-04-21 19:38:31 -0700161#ifdef CONFIG_X86_64
Alexander Graf3eb90012020-09-25 16:34:20 +0200162 MSR_FS_BASE,
163 MSR_GS_BASE,
164 MSR_KERNEL_GS_BASE,
Sean Christophersondbdd0962021-04-21 19:38:31 -0700165#endif
Alexander Graf3eb90012020-09-25 16:34:20 +0200166 MSR_IA32_SYSENTER_CS,
167 MSR_IA32_SYSENTER_ESP,
168 MSR_IA32_SYSENTER_EIP,
169 MSR_CORE_C1_RES,
170 MSR_CORE_C3_RESIDENCY,
171 MSR_CORE_C6_RESIDENCY,
172 MSR_CORE_C7_RESIDENCY,
173};
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800174
175/*
176 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
177 * ple_gap: upper bound on the amount of time between two successive
178 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500179 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800180 * ple_window: upper bound on the amount of time a guest is allowed to execute
181 * in a PAUSE loop. Tests indicate that most spinlocks are held for
182 * less than 2^12 cycles
183 * Time is measured based on a counter that runs at the same rate as the TSC,
184 * refer SDM volume 3b section 21.6.13 & 22.1.3.
185 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400186static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500187module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Babu Moger7fbc85a2018-03-16 16:37:22 -0400189static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
190module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800191
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200192/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400193static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400194module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200195
196/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400197static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400198module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200199
200/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400201static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
202module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200203
Chao Pengf99e3da2018-10-24 16:05:10 +0800204/* Default is SYSTEM mode, 1 for host-guest mode */
205int __read_mostly pt_mode = PT_MODE_SYSTEM;
206module_param(pt_mode, int, S_IRUGO);
207
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200208static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200209static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200210static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200211
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200212/* Storage for pre module init parameter parsing */
213static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200214
215static const struct {
216 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200217 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200218} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200219 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
220 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
221 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
222 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
223 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
224 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200225};
226
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200227#define L1D_CACHE_ORDER 4
228static void *vmx_l1d_flush_pages;
229
230static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
231{
232 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200233 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200234
Waiman Long19a36d32019-08-26 15:30:23 -0400235 if (!boot_cpu_has_bug(X86_BUG_L1TF)) {
236 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
237 return 0;
238 }
239
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200240 if (!enable_ept) {
241 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
242 return 0;
243 }
244
Yi Wangd806afa2018-08-16 13:42:39 +0800245 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
246 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200247
Yi Wangd806afa2018-08-16 13:42:39 +0800248 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
249 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
250 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
251 return 0;
252 }
253 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200254
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200255 /* If set to auto use the default l1tf mitigation method */
256 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
257 switch (l1tf_mitigation) {
258 case L1TF_MITIGATION_OFF:
259 l1tf = VMENTER_L1D_FLUSH_NEVER;
260 break;
261 case L1TF_MITIGATION_FLUSH_NOWARN:
262 case L1TF_MITIGATION_FLUSH:
263 case L1TF_MITIGATION_FLUSH_NOSMT:
264 l1tf = VMENTER_L1D_FLUSH_COND;
265 break;
266 case L1TF_MITIGATION_FULL:
267 case L1TF_MITIGATION_FULL_FORCE:
268 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
269 break;
270 }
271 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
272 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
273 }
274
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200275 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
276 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Ben Gardon41836832019-02-11 11:02:52 -0800277 /*
278 * This allocation for vmx_l1d_flush_pages is not tied to a VM
279 * lifetime and so should not be charged to a memcg.
280 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200281 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
282 if (!page)
283 return -ENOMEM;
284 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200285
286 /*
287 * Initialize each page with a different pattern in
288 * order to protect against KSM in the nested
289 * virtualization case.
290 */
291 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
292 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
293 PAGE_SIZE);
294 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200295 }
296
297 l1tf_vmx_mitigation = l1tf;
298
Thomas Gleixner895ae472018-07-13 16:23:22 +0200299 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
300 static_branch_enable(&vmx_l1d_should_flush);
301 else
302 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200303
Nicolai Stange427362a2018-07-21 22:25:00 +0200304 if (l1tf == VMENTER_L1D_FLUSH_COND)
305 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200306 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200307 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200308 return 0;
309}
310
311static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200312{
313 unsigned int i;
314
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200315 if (s) {
316 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200317 if (vmentry_l1d_param[i].for_parse &&
318 sysfs_streq(s, vmentry_l1d_param[i].option))
319 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200320 }
321 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200322 return -EINVAL;
323}
324
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200325static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
326{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200327 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200328
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200329 l1tf = vmentry_l1d_flush_parse(s);
330 if (l1tf < 0)
331 return l1tf;
332
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200333 if (!boot_cpu_has(X86_BUG_L1TF))
334 return 0;
335
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200336 /*
337 * Has vmx_init() run already? If not then this is the pre init
338 * parameter parsing. In that case just store the value and let
339 * vmx_init() do the proper setup after enable_ept has been
340 * established.
341 */
342 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
343 vmentry_l1d_flush_param = l1tf;
344 return 0;
345 }
346
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200347 mutex_lock(&vmx_l1d_flush_mutex);
348 ret = vmx_setup_l1d_flush(l1tf);
349 mutex_unlock(&vmx_l1d_flush_mutex);
350 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200351}
352
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200353static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
354{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200355 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
356 return sprintf(s, "???\n");
357
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200358 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200359}
360
361static const struct kernel_param_ops vmentry_l1d_flush_ops = {
362 .set = vmentry_l1d_flush_set,
363 .get = vmentry_l1d_flush_get,
364};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200365module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200366
Gleb Natapovd99e4152012-12-20 16:57:45 +0200367static u32 vmx_segment_access_rights(struct kvm_segment *var);
Avi Kivity75880a02007-06-20 11:20:04 +0300368
Sean Christopherson453eafb2018-12-20 12:25:17 -0800369void vmx_vmexit(void);
370
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700371#define vmx_insn_failed(fmt...) \
372do { \
373 WARN_ONCE(1, fmt); \
374 pr_warn_ratelimited(fmt); \
375} while (0)
376
Sean Christopherson6e202092019-07-19 13:41:08 -0700377asmlinkage void vmread_error(unsigned long field, bool fault)
378{
379 if (fault)
380 kvm_spurious_fault();
381 else
382 vmx_insn_failed("kvm: vmread failed: field=%lx\n", field);
383}
384
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700385noinline void vmwrite_error(unsigned long field, unsigned long value)
386{
387 vmx_insn_failed("kvm: vmwrite failed: field=%lx val=%lx err=%d\n",
388 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
389}
390
391noinline void vmclear_error(struct vmcs *vmcs, u64 phys_addr)
392{
393 vmx_insn_failed("kvm: vmclear failed: %p/%llx\n", vmcs, phys_addr);
394}
395
396noinline void vmptrld_error(struct vmcs *vmcs, u64 phys_addr)
397{
398 vmx_insn_failed("kvm: vmptrld failed: %p/%llx\n", vmcs, phys_addr);
399}
400
401noinline void invvpid_error(unsigned long ext, u16 vpid, gva_t gva)
402{
403 vmx_insn_failed("kvm: invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n",
404 ext, vpid, gva);
405}
406
407noinline void invept_error(unsigned long ext, u64 eptp, gpa_t gpa)
408{
409 vmx_insn_failed("kvm: invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n",
410 ext, eptp, gpa);
411}
412
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800414DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300415/*
416 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
417 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
418 */
419static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420
Sheng Yang2384d2b2008-01-17 15:14:33 +0800421static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
422static DEFINE_SPINLOCK(vmx_vpid_lock);
423
Sean Christopherson3077c192018-12-03 13:53:02 -0800424struct vmcs_config vmcs_config;
425struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800426
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427#define VMX_SEGMENT_FIELD(seg) \
428 [VCPU_SREG_##seg] = { \
429 .selector = GUEST_##seg##_SELECTOR, \
430 .base = GUEST_##seg##_BASE, \
431 .limit = GUEST_##seg##_LIMIT, \
432 .ar_bytes = GUEST_##seg##_AR_BYTES, \
433 }
434
Mathias Krause772e0312012-08-30 01:30:19 +0200435static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 unsigned selector;
437 unsigned base;
438 unsigned limit;
439 unsigned ar_bytes;
440} kvm_vmx_segment_fields[] = {
441 VMX_SEGMENT_FIELD(CS),
442 VMX_SEGMENT_FIELD(DS),
443 VMX_SEGMENT_FIELD(ES),
444 VMX_SEGMENT_FIELD(FS),
445 VMX_SEGMENT_FIELD(GS),
446 VMX_SEGMENT_FIELD(SS),
447 VMX_SEGMENT_FIELD(TR),
448 VMX_SEGMENT_FIELD(LDTR),
449};
450
Sean Christophersonec0241f2020-04-15 13:34:52 -0700451static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
452{
453 vmx->segment_cache.bitmask = 0;
454}
455
Sean Christopherson23420802019-04-19 22:50:57 -0700456static unsigned long host_idt_base;
Avi Kivity26bb0982009-09-07 11:14:12 +0300457
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100458#if IS_ENABLED(CONFIG_HYPERV)
459static bool __read_mostly enlightened_vmcs = true;
460module_param(enlightened_vmcs, bool, 0444);
461
Yi Wang8997f652019-01-21 15:27:05 +0800462static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800463 void *data)
464{
465 struct kvm_tlb_range *range = data;
466
467 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
468 range->pages);
469}
470
Sean Christopherson978c8342021-03-05 10:31:23 -0800471static inline int hv_remote_flush_root_ept(hpa_t root_ept,
472 struct kvm_tlb_range *range)
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800473{
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800474 if (range)
Sean Christopherson978c8342021-03-05 10:31:23 -0800475 return hyperv_flush_guest_mapping_range(root_ept,
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800476 kvm_fill_hv_flush_list_func, (void *)range);
477 else
Sean Christopherson978c8342021-03-05 10:31:23 -0800478 return hyperv_flush_guest_mapping(root_ept);
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800479}
480
481static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
482 struct kvm_tlb_range *range)
Tianyu Lan877ad952018-07-19 08:40:23 +0000483{
Sean Christophersonb68aa15c2021-03-05 10:31:15 -0800484 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Lan Tianyua5c214d2018-10-13 22:54:05 +0800485 struct kvm_vcpu *vcpu;
Sean Christopherson978c8342021-03-05 10:31:23 -0800486 int ret = 0, i, nr_unique_valid_roots;
487 hpa_t root;
Tianyu Lan877ad952018-07-19 08:40:23 +0000488
Sean Christopherson978c8342021-03-05 10:31:23 -0800489 spin_lock(&kvm_vmx->hv_root_ept_lock);
Tianyu Lan877ad952018-07-19 08:40:23 +0000490
Sean Christopherson978c8342021-03-05 10:31:23 -0800491 if (!VALID_PAGE(kvm_vmx->hv_root_ept)) {
492 nr_unique_valid_roots = 0;
Tianyu Lan877ad952018-07-19 08:40:23 +0000493
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800494 /*
Sean Christopherson978c8342021-03-05 10:31:23 -0800495 * Flush all valid roots, and see if all vCPUs have converged
496 * on a common root, in which case future flushes can skip the
497 * loop and flush the common root.
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800498 */
Lan Tianyu53963a72018-12-06 15:34:36 +0800499 kvm_for_each_vcpu(i, vcpu, kvm) {
Sean Christopherson978c8342021-03-05 10:31:23 -0800500 root = to_vmx(vcpu)->hv_root_ept;
501 if (!VALID_PAGE(root) || root == kvm_vmx->hv_root_ept)
Sean Christopherson288bee22021-03-05 10:31:16 -0800502 continue;
503
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800504 /*
Sean Christopherson978c8342021-03-05 10:31:23 -0800505 * Set the tracked root to the first valid root. Keep
506 * this root for the entirety of the loop even if more
507 * roots are encountered as a low effort optimization
508 * to avoid flushing the same (first) root again.
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800509 */
Sean Christopherson978c8342021-03-05 10:31:23 -0800510 if (++nr_unique_valid_roots == 1)
511 kvm_vmx->hv_root_ept = root;
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +0200512
Sean Christopherson14072e52021-03-05 10:31:22 -0800513 if (!ret)
Sean Christopherson978c8342021-03-05 10:31:23 -0800514 ret = hv_remote_flush_root_ept(root, range);
Sean Christopherson14072e52021-03-05 10:31:22 -0800515
516 /*
Sean Christopherson978c8342021-03-05 10:31:23 -0800517 * Stop processing roots if a failure occurred and
518 * multiple valid roots have already been detected.
Sean Christopherson14072e52021-03-05 10:31:22 -0800519 */
Sean Christopherson978c8342021-03-05 10:31:23 -0800520 if (ret && nr_unique_valid_roots > 1)
Sean Christopherson14072e52021-03-05 10:31:22 -0800521 break;
Lan Tianyu53963a72018-12-06 15:34:36 +0800522 }
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800523
524 /*
Sean Christopherson978c8342021-03-05 10:31:23 -0800525 * The optimized flush of a single root can't be used if there
526 * are multiple valid roots (obviously).
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800527 */
Sean Christopherson978c8342021-03-05 10:31:23 -0800528 if (nr_unique_valid_roots > 1)
529 kvm_vmx->hv_root_ept = INVALID_PAGE;
Lan Tianyua5c214d2018-10-13 22:54:05 +0800530 } else {
Sean Christopherson978c8342021-03-05 10:31:23 -0800531 ret = hv_remote_flush_root_ept(kvm_vmx->hv_root_ept, range);
Tianyu Lan877ad952018-07-19 08:40:23 +0000532 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000533
Sean Christopherson978c8342021-03-05 10:31:23 -0800534 spin_unlock(&kvm_vmx->hv_root_ept_lock);
Tianyu Lan877ad952018-07-19 08:40:23 +0000535 return ret;
536}
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800537static int hv_remote_flush_tlb(struct kvm *kvm)
538{
539 return hv_remote_flush_tlb_with_range(kvm, NULL);
540}
541
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800542static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)
543{
544 struct hv_enlightened_vmcs *evmcs;
545 struct hv_partition_assist_pg **p_hv_pa_pg =
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100546 &to_kvm_hv(vcpu->kvm)->hv_pa_pg;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800547 /*
548 * Synthetic VM-Exit is not enabled in current code and so All
549 * evmcs in singe VM shares same assist page.
550 */
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200551 if (!*p_hv_pa_pg)
Sean Christophersoneba04b22021-03-30 19:30:25 -0700552 *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL_ACCOUNT);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200553
554 if (!*p_hv_pa_pg)
555 return -ENOMEM;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800556
557 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs;
558
559 evmcs->partition_assist_page =
560 __pa(*p_hv_pa_pg);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200561 evmcs->hv_vm_id = (unsigned long)vcpu->kvm;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800562 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;
563
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800564 return 0;
565}
566
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100567#endif /* IS_ENABLED(CONFIG_HYPERV) */
568
Sean Christopherson978c8342021-03-05 10:31:23 -0800569static void hv_track_root_ept(struct kvm_vcpu *vcpu, hpa_t root_ept)
Sean Christophersonc82f1b62021-03-05 10:31:20 -0800570{
571#if IS_ENABLED(CONFIG_HYPERV)
572 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
573
574 if (kvm_x86_ops.tlb_remote_flush == hv_remote_flush_tlb) {
Sean Christopherson978c8342021-03-05 10:31:23 -0800575 spin_lock(&kvm_vmx->hv_root_ept_lock);
576 to_vmx(vcpu)->hv_root_ept = root_ept;
577 if (root_ept != kvm_vmx->hv_root_ept)
578 kvm_vmx->hv_root_ept = INVALID_PAGE;
579 spin_unlock(&kvm_vmx->hv_root_ept_lock);
Sean Christophersonc82f1b62021-03-05 10:31:20 -0800580 }
581#endif
582}
583
Yunhong Jiang64672c92016-06-13 14:19:59 -0700584/*
585 * Comment's format: document - errata name - stepping - processor name.
586 * Refer from
587 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
588 */
589static u32 vmx_preemption_cpu_tfms[] = {
590/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
5910x000206E6,
592/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
593/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
594/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
5950x00020652,
596/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
5970x00020655,
598/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
599/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
600/*
601 * 320767.pdf - AAP86 - B1 -
602 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
603 */
6040x000106E5,
605/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
6060x000106A0,
607/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
6080x000106A1,
609/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
6100x000106A4,
611 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
612 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
613 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
6140x000106A5,
Wei Huang3d82c562018-12-03 14:13:32 -0600615 /* Xeon E3-1220 V2 */
6160x000306A8,
Yunhong Jiang64672c92016-06-13 14:19:59 -0700617};
618
619static inline bool cpu_has_broken_vmx_preemption_timer(void)
620{
621 u32 eax = cpuid_eax(0x00000001), i;
622
623 /* Clear the reserved bits */
624 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000625 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700626 if (eax == vmx_preemption_cpu_tfms[i])
627 return true;
628
629 return false;
630}
631
Paolo Bonzini35754c92015-07-29 12:05:37 +0200632static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800633{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200634 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800635}
636
Sheng Yang04547152009-04-01 15:52:31 +0800637static inline bool report_flexpriority(void)
638{
639 return flexpriority_enabled;
640}
641
Alexander Graf3eb90012020-09-25 16:34:20 +0200642static int possible_passthrough_msr_slot(u32 msr)
643{
644 u32 i;
645
646 for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++)
647 if (vmx_possible_passthrough_msrs[i] == msr)
648 return i;
649
650 return -ENOENT;
651}
652
653static bool is_valid_passthrough_msr(u32 msr)
654{
655 bool r;
656
657 switch (msr) {
658 case 0x800 ... 0x8ff:
659 /* x2APIC MSRs. These are handled in vmx_update_msr_bitmap_x2apic() */
660 return true;
661 case MSR_IA32_RTIT_STATUS:
662 case MSR_IA32_RTIT_OUTPUT_BASE:
663 case MSR_IA32_RTIT_OUTPUT_MASK:
664 case MSR_IA32_RTIT_CR3_MATCH:
665 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
666 /* PT MSRs. These are handled in pt_update_intercept_for_msr() */
Like Xu1b5ac3222021-02-01 13:10:34 +0800667 case MSR_LBR_SELECT:
668 case MSR_LBR_TOS:
669 case MSR_LBR_INFO_0 ... MSR_LBR_INFO_0 + 31:
670 case MSR_LBR_NHM_FROM ... MSR_LBR_NHM_FROM + 31:
671 case MSR_LBR_NHM_TO ... MSR_LBR_NHM_TO + 31:
672 case MSR_LBR_CORE_FROM ... MSR_LBR_CORE_FROM + 8:
673 case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8:
674 /* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */
Alexander Graf3eb90012020-09-25 16:34:20 +0200675 return true;
676 }
677
678 r = possible_passthrough_msr_slot(msr) != -ENOENT;
679
680 WARN(!r, "Invalid MSR %x, please adapt vmx_possible_passthrough_msrs[]", msr);
681
682 return r;
683}
684
Sean Christophersond85a8032020-09-23 11:04:06 -0700685struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300686{
687 int i;
688
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -0700689 i = kvm_find_user_return_msr(msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300690 if (i >= 0)
Sean Christophersoneb3db1b2020-09-23 11:03:58 -0700691 return &vmx->guest_uret_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000692 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800693}
694
Sean Christopherson7bf662b2020-09-23 11:04:07 -0700695static int vmx_set_guest_uret_msr(struct vcpu_vmx *vmx,
696 struct vmx_uret_msr *msr, u64 data)
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500697{
Sean Christophersonee9d22e2021-05-04 10:17:28 -0700698 unsigned int slot = msr - vmx->guest_uret_msrs;
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500699 int ret = 0;
700
701 u64 old_msr_data = msr->data;
702 msr->data = data;
Sean Christophersonee9d22e2021-05-04 10:17:28 -0700703 if (msr->load_into_hardware) {
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500704 preempt_disable();
Sean Christophersonee9d22e2021-05-04 10:17:28 -0700705 ret = kvm_set_user_return_msr(slot, msr->data, msr->mask);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500706 preempt_enable();
707 if (ret)
708 msr->data = old_msr_data;
709 }
710 return ret;
711}
712
Dave Young2965faa2015-09-09 15:38:55 -0700713#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800714static void crash_vmclear_local_loaded_vmcss(void)
715{
716 int cpu = raw_smp_processor_id();
717 struct loaded_vmcs *v;
718
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800719 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
720 loaded_vmcss_on_cpu_link)
721 vmcs_clear(v->vmcs);
722}
Dave Young2965faa2015-09-09 15:38:55 -0700723#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800724
Nadav Har'Eld462b812011-05-24 15:26:10 +0300725static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300727 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800728 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729
Nadav Har'Eld462b812011-05-24 15:26:10 +0300730 if (loaded_vmcs->cpu != cpu)
731 return; /* vcpu migration can race with cpu offline */
732 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733 per_cpu(current_vmcs, cpu) = NULL;
Sean Christopherson31603d42020-03-21 12:37:49 -0700734
735 vmcs_clear(loaded_vmcs->vmcs);
736 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
737 vmcs_clear(loaded_vmcs->shadow_vmcs);
738
Nadav Har'Eld462b812011-05-24 15:26:10 +0300739 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800740
741 /*
Sean Christopherson31603d42020-03-21 12:37:49 -0700742 * Ensure all writes to loaded_vmcs, including deleting it from its
743 * current percpu list, complete before setting loaded_vmcs->vcpu to
744 * -1, otherwise a different cpu can see vcpu == -1 first and add
745 * loaded_vmcs to its percpu list before it's deleted from this cpu's
746 * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800747 */
748 smp_wmb();
749
Sean Christopherson31603d42020-03-21 12:37:49 -0700750 loaded_vmcs->cpu = -1;
751 loaded_vmcs->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752}
753
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800754void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800755{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800756 int cpu = loaded_vmcs->cpu;
757
758 if (cpu != -1)
759 smp_call_function_single(cpu,
760 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800761}
762
Avi Kivity2fb92db2011-04-27 19:42:18 +0300763static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
764 unsigned field)
765{
766 bool ret;
767 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
768
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700769 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) {
770 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS);
Avi Kivity2fb92db2011-04-27 19:42:18 +0300771 vmx->segment_cache.bitmask = 0;
772 }
773 ret = vmx->segment_cache.bitmask & mask;
774 vmx->segment_cache.bitmask |= mask;
775 return ret;
776}
777
778static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
779{
780 u16 *p = &vmx->segment_cache.seg[seg].selector;
781
782 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
783 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
784 return *p;
785}
786
787static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
788{
789 ulong *p = &vmx->segment_cache.seg[seg].base;
790
791 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
792 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
793 return *p;
794}
795
796static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
797{
798 u32 *p = &vmx->segment_cache.seg[seg].limit;
799
800 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
801 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
802 return *p;
803}
804
805static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
806{
807 u32 *p = &vmx->segment_cache.seg[seg].ar;
808
809 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
810 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
811 return *p;
812}
813
Jason Baronb6a7cc32021-01-14 22:27:54 -0500814void vmx_update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300815{
816 u32 eb;
817
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100818 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800819 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200820 /*
821 * Guest access to VMware backdoor ports could legitimately
822 * trigger #GP because of TSS I/O permission bitmap.
823 * We intercept those #GP and allow access to them anyway
824 * as VMware does.
825 */
826 if (enable_vmware_backdoor)
827 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100828 if ((vcpu->guest_debug &
829 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
830 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
831 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300832 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300833 eb = ~0;
Paolo Bonzinia0c13432020-07-10 17:48:08 +0200834 if (!vmx_need_pf_intercept(vcpu))
Miaohe Lin49f933d2020-02-27 11:20:54 +0800835 eb &= ~(1u << PF_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300836
837 /* When we are running a nested L2 guest and L1 specified for it a
838 * certain exception bitmap, we must trap the same exceptions and pass
839 * them to L1. When running L2, we will only handle the exceptions
840 * specified above if L1 did not want them.
841 */
842 if (is_guest_mode(vcpu))
843 eb |= get_vmcs12(vcpu)->exception_bitmap;
Paolo Bonzinib502e6e2020-09-29 08:31:32 -0400844 else {
845 /*
846 * If EPT is enabled, #PF is only trapped if MAXPHYADDR is mismatched
847 * between guest and host. In that case we only care about present
848 * faults. For vmcs02, however, PFEC_MASK and PFEC_MATCH are set in
849 * prepare_vmcs02_rare.
850 */
851 bool selective_pf_trap = enable_ept && (eb & (1u << PF_VECTOR));
852 int mask = selective_pf_trap ? PFERR_PRESENT_MASK : 0;
853 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, mask);
854 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, mask);
855 }
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300856
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300857 vmcs_write32(EXCEPTION_BITMAP, eb);
858}
859
Ashok Raj15d45072018-02-01 22:59:43 +0100860/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100861 * Check if MSR is intercepted for currently loaded MSR bitmap.
862 */
863static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
864{
865 unsigned long *msr_bitmap;
866 int f = sizeof(unsigned long);
867
868 if (!cpu_has_vmx_msr_bitmap())
869 return true;
870
871 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
872
873 if (msr <= 0x1fff) {
874 return !!test_bit(msr, msr_bitmap + 0x800 / f);
875 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
876 msr &= 0x1fff;
877 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
878 }
879
880 return true;
881}
882
Gleb Natapov2961e8762013-11-25 15:37:13 +0200883static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
884 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200885{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200886 vm_entry_controls_clearbit(vmx, entry);
887 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200888}
889
Sean Christophersona128a932020-09-23 11:03:57 -0700890int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr)
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400891{
892 unsigned int i;
893
894 for (i = 0; i < m->nr; ++i) {
895 if (m->val[i].index == msr)
896 return i;
897 }
898 return -ENOENT;
899}
900
Avi Kivity61d2ef22010-04-28 16:40:38 +0300901static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
902{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400903 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300904 struct msr_autoload *m = &vmx->msr_autoload;
905
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200906 switch (msr) {
907 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800908 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200909 clear_atomic_switch_msr_special(vmx,
910 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200911 VM_EXIT_LOAD_IA32_EFER);
912 return;
913 }
914 break;
915 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800916 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200917 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200918 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
919 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
920 return;
921 }
922 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200923 }
Sean Christophersona128a932020-09-23 11:03:57 -0700924 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400925 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400926 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400927 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400928 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400929 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200930
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400931skip_guest:
Sean Christophersona128a932020-09-23 11:03:57 -0700932 i = vmx_find_loadstore_msr_slot(&m->host, msr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400933 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300934 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400935
936 --m->host.nr;
937 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400938 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300939}
940
Gleb Natapov2961e8762013-11-25 15:37:13 +0200941static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
942 unsigned long entry, unsigned long exit,
943 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
944 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200945{
946 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700947 if (host_val_vmcs != HOST_IA32_EFER)
948 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200949 vm_entry_controls_setbit(vmx, entry);
950 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200951}
952
Avi Kivity61d2ef22010-04-28 16:40:38 +0300953static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400954 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300955{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400956 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300957 struct msr_autoload *m = &vmx->msr_autoload;
958
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200959 switch (msr) {
960 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800961 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200962 add_atomic_switch_msr_special(vmx,
963 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200964 VM_EXIT_LOAD_IA32_EFER,
965 GUEST_IA32_EFER,
966 HOST_IA32_EFER,
967 guest_val, host_val);
968 return;
969 }
970 break;
971 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800972 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200973 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200974 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
975 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
976 GUEST_IA32_PERF_GLOBAL_CTRL,
977 HOST_IA32_PERF_GLOBAL_CTRL,
978 guest_val, host_val);
979 return;
980 }
981 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +0100982 case MSR_IA32_PEBS_ENABLE:
983 /* PEBS needs a quiescent period after being disabled (to write
984 * a record). Disabling PEBS through VMX MSR swapping doesn't
985 * provide that period, so a CPU could write host's record into
986 * guest's memory.
987 */
988 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +0200989 }
990
Sean Christophersona128a932020-09-23 11:03:57 -0700991 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400992 if (!entry_only)
Sean Christophersona128a932020-09-23 11:03:57 -0700993 j = vmx_find_loadstore_msr_slot(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300994
Sean Christophersonce833b22020-09-23 11:03:56 -0700995 if ((i < 0 && m->guest.nr == MAX_NR_LOADSTORE_MSRS) ||
996 (j < 0 && m->host.nr == MAX_NR_LOADSTORE_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +0200997 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +0200998 "Can't add msr %x\n", msr);
999 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001000 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001001 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04001002 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001003 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001004 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001005 m->guest.val[i].index = msr;
1006 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001007
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001008 if (entry_only)
1009 return;
1010
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001011 if (j < 0) {
1012 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001013 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03001014 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001015 m->host.val[j].index = msr;
1016 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001017}
1018
Sean Christopherson86e3e492020-09-23 11:04:04 -07001019static bool update_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +03001020{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001021 u64 guest_efer = vmx->vcpu.arch.efer;
1022 u64 ignore_bits = 0;
Sean Christopherson86e3e492020-09-23 11:04:04 -07001023 int i;
Eddie Dong2cc51562007-05-21 07:28:09 +03001024
Paolo Bonzini9167ab72019-10-27 16:23:23 +01001025 /* Shadow paging assumes NX to be available. */
1026 if (!enable_ept)
1027 guest_efer |= EFER_NX;
Roel Kluin3a34a882009-08-04 02:08:45 -07001028
Avi Kivity51c6cf62007-08-29 03:48:05 +03001029 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001030 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03001031 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001032 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001033#ifdef CONFIG_X86_64
1034 ignore_bits |= EFER_LMA | EFER_LME;
1035 /* SCE is meaningful only in long mode on Intel */
1036 if (guest_efer & EFER_LMA)
1037 ignore_bits &= ~(u64)EFER_SCE;
1038#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03001039
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001040 /*
1041 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1042 * On CPUs that support "load IA32_EFER", always switch EFER
1043 * atomically, since it's faster than switching it manually.
1044 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -08001045 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001046 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03001047 if (!(guest_efer & EFER_LMA))
1048 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001049 if (guest_efer != host_efer)
1050 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001051 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07001052 else
1053 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03001054 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001055 }
Sean Christopherson86e3e492020-09-23 11:04:04 -07001056
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -07001057 i = kvm_find_user_return_msr(MSR_EFER);
Sean Christopherson86e3e492020-09-23 11:04:04 -07001058 if (i < 0)
1059 return false;
1060
1061 clear_atomic_switch_msr(vmx, MSR_EFER);
1062
1063 guest_efer &= ~ignore_bits;
1064 guest_efer |= host_efer & ignore_bits;
1065
1066 vmx->guest_uret_msrs[i].data = guest_efer;
1067 vmx->guest_uret_msrs[i].mask = ~ignore_bits;
1068
1069 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001070}
1071
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001072#ifdef CONFIG_X86_32
1073/*
1074 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1075 * VMCS rather than the segment table. KVM uses this helper to figure
1076 * out the current bases to poke them into the VMCS before entry.
1077 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001078static unsigned long segment_base(u16 selector)
1079{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001080 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001081 unsigned long v;
1082
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001083 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001084 return 0;
1085
Thomas Garnier45fc8752017-03-14 10:05:08 -07001086 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001087
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001088 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001089 u16 ldt_selector = kvm_read_ldt();
1090
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001091 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001092 return 0;
1093
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001094 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001095 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001096 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001097 return v;
1098}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001099#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001100
Sean Christophersone348ac72019-12-10 15:24:33 -08001101static inline bool pt_can_write_msr(struct vcpu_vmx *vmx)
1102{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001103 return vmx_pt_mode_is_host_guest() &&
Sean Christophersone348ac72019-12-10 15:24:33 -08001104 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
1105}
1106
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001107static inline bool pt_output_base_valid(struct kvm_vcpu *vcpu, u64 base)
1108{
1109 /* The base must be 128-byte aligned and a legal physical address. */
Sean Christopherson636e8b72021-02-03 16:01:10 -08001110 return kvm_vcpu_is_legal_aligned_gpa(vcpu, base, 128);
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001111}
1112
Chao Peng2ef444f2018-10-24 16:05:12 +08001113static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range)
1114{
1115 u32 i;
1116
1117 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1118 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1119 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1120 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1121 for (i = 0; i < addr_range; i++) {
1122 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1123 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1124 }
1125}
1126
1127static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range)
1128{
1129 u32 i;
1130
1131 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1132 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1133 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1134 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1135 for (i = 0; i < addr_range; i++) {
1136 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1137 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1138 }
1139}
1140
1141static void pt_guest_enter(struct vcpu_vmx *vmx)
1142{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001143 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001144 return;
1145
Chao Peng2ef444f2018-10-24 16:05:12 +08001146 /*
Chao Pengb08c2892018-10-24 16:05:15 +08001147 * GUEST_IA32_RTIT_CTL is already set in the VMCS.
1148 * Save host state before VM entry.
Chao Peng2ef444f2018-10-24 16:05:12 +08001149 */
Chao Pengb08c2892018-10-24 16:05:15 +08001150 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
Chao Peng2ef444f2018-10-24 16:05:12 +08001151 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1152 wrmsrl(MSR_IA32_RTIT_CTL, 0);
1153 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1154 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1155 }
1156}
1157
1158static void pt_guest_exit(struct vcpu_vmx *vmx)
1159{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001160 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001161 return;
1162
1163 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1164 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1165 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1166 }
1167
1168 /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */
1169 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
1170}
1171
Sean Christopherson13b964a2019-05-07 09:06:31 -07001172void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1173 unsigned long fs_base, unsigned long gs_base)
1174{
1175 if (unlikely(fs_sel != host->fs_sel)) {
1176 if (!(fs_sel & 7))
1177 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1178 else
1179 vmcs_write16(HOST_FS_SELECTOR, 0);
1180 host->fs_sel = fs_sel;
1181 }
1182 if (unlikely(gs_sel != host->gs_sel)) {
1183 if (!(gs_sel & 7))
1184 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1185 else
1186 vmcs_write16(HOST_GS_SELECTOR, 0);
1187 host->gs_sel = gs_sel;
1188 }
1189 if (unlikely(fs_base != host->fs_base)) {
1190 vmcs_writel(HOST_FS_BASE, fs_base);
1191 host->fs_base = fs_base;
1192 }
1193 if (unlikely(gs_base != host->gs_base)) {
1194 vmcs_writel(HOST_GS_BASE, gs_base);
1195 host->gs_base = gs_base;
1196 }
1197}
1198
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001199void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001200{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001201 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001202 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001203#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01001204 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001205#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001206 unsigned long fs_base, gs_base;
1207 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03001208 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001209
Sean Christophersond264ee02018-08-27 15:21:12 -07001210 vmx->req_immediate_exit = false;
1211
Liran Alonf48b4712018-11-20 18:03:25 +02001212 /*
1213 * Note that guest MSRs to be saved/restored can also be changed
1214 * when guest state is loaded. This happens when guest transitions
1215 * to/from long-mode by setting MSR_EFER.LMA.
1216 */
Sean Christopherson658ece82020-09-23 11:04:01 -07001217 if (!vmx->guest_uret_msrs_loaded) {
1218 vmx->guest_uret_msrs_loaded = true;
Sean Christophersone5fda4b2021-05-04 10:17:32 -07001219 for (i = 0; i < kvm_nr_uret_msrs; ++i) {
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001220 if (!vmx->guest_uret_msrs[i].load_into_hardware)
1221 continue;
1222
1223 kvm_set_user_return_msr(i,
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001224 vmx->guest_uret_msrs[i].data,
1225 vmx->guest_uret_msrs[i].mask);
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001226 }
Liran Alonf48b4712018-11-20 18:03:25 +02001227 }
wanpeng lic9dfd3f2020-02-17 18:37:43 +08001228
1229 if (vmx->nested.need_vmcs12_to_shadow_sync)
1230 nested_sync_vmcs12_to_shadow(vcpu);
1231
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001232 if (vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001233 return;
1234
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001235 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001236
Avi Kivity33ed6322007-05-02 16:54:03 +03001237 /*
1238 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1239 * allow segment selectors with cpl > 0 or ti == 1.
1240 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07001241 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01001242
1243#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001244 savesegment(ds, host_state->ds_sel);
1245 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07001246
1247 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001248 if (likely(is_64bit_mm(current->mm))) {
Thomas Gleixner67580342020-05-28 16:13:52 -04001249 current_save_fsgs();
Sean Christophersone368b872018-07-23 12:32:41 -07001250 fs_sel = current->thread.fsindex;
1251 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001252 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07001253 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001254 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07001255 savesegment(fs, fs_sel);
1256 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001257 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07001258 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03001259 }
1260
Paolo Bonzini4679b612018-09-24 17:23:01 +02001261 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03001262#else
Sean Christophersone368b872018-07-23 12:32:41 -07001263 savesegment(fs, fs_sel);
1264 savesegment(gs, gs_sel);
1265 fs_base = segment_base(fs_sel);
1266 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001267#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001268
Sean Christopherson13b964a2019-05-07 09:06:31 -07001269 vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001270 vmx->guest_state_loaded = true;
Avi Kivity33ed6322007-05-02 16:54:03 +03001271}
1272
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001273static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001274{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001275 struct vmcs_host_state *host_state;
1276
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001277 if (!vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001278 return;
1279
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001280 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001281
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001282 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001283
Avi Kivityc8770e72010-11-11 12:37:26 +02001284#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001285 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001286#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001287 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1288 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001289#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001290 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001291#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001292 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001293#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001294 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001295 if (host_state->fs_sel & 7)
1296 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001297#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001298 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1299 loadsegment(ds, host_state->ds_sel);
1300 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001301 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001302#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001303 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001304#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001305 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001306#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001307 load_fixmap_gdt(raw_smp_processor_id());
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001308 vmx->guest_state_loaded = false;
Sean Christopherson658ece82020-09-23 11:04:01 -07001309 vmx->guest_uret_msrs_loaded = false;
Avi Kivity33ed6322007-05-02 16:54:03 +03001310}
1311
Sean Christopherson678e3152018-07-23 12:32:43 -07001312#ifdef CONFIG_X86_64
1313static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001314{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001315 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001316 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001317 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1318 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001319 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001320}
1321
Sean Christopherson678e3152018-07-23 12:32:43 -07001322static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1323{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001324 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001325 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001326 wrmsrl(MSR_KERNEL_GS_BASE, data);
1327 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001328 vmx->msr_guest_kernel_gs_base = data;
1329}
1330#endif
1331
Sean Christopherson5c911be2020-05-01 09:31:17 -07001332void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
1333 struct loaded_vmcs *buddy)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001335 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001336 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Sean Christopherson5c911be2020-05-01 09:31:17 -07001337 struct vmcs *prev;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001339 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001340 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001341 local_irq_disable();
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001342
1343 /*
Sean Christopherson31603d42020-03-21 12:37:49 -07001344 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
1345 * this cpu's percpu list, otherwise it may not yet be deleted
1346 * from its previous cpu's percpu list. Pairs with the
1347 * smb_wmb() in __loaded_vmcs_clear().
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001348 */
1349 smp_rmb();
1350
Nadav Har'Eld462b812011-05-24 15:26:10 +03001351 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1352 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001353 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001354 }
1355
Sean Christopherson5c911be2020-05-01 09:31:17 -07001356 prev = per_cpu(current_vmcs, cpu);
1357 if (prev != vmx->loaded_vmcs->vmcs) {
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001358 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1359 vmcs_load(vmx->loaded_vmcs->vmcs);
Sean Christopherson5c911be2020-05-01 09:31:17 -07001360
1361 /*
1362 * No indirect branch prediction barrier needed when switching
1363 * the active VMCS within a guest, e.g. on nested VM-Enter.
1364 * The L1 VMM can protect itself with retpolines, IBPB or IBRS.
1365 */
1366 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev))
1367 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001368 }
1369
1370 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001371 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001372 unsigned long sysenter_esp;
1373
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001374 /*
1375 * Flush all EPTP/VPID contexts, the new pCPU may have stale
1376 * TLB entries from its previous association with the vCPU.
1377 */
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001378 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001379
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380 /*
1381 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001382 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001384 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001385 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001386 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387
1388 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1389 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001390
Nadav Har'Eld462b812011-05-24 15:26:10 +03001391 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392 }
Feng Wu28b835d2015-09-18 22:29:54 +08001393
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001394 /* Setup TSC multiplier */
1395 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001396 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1397 decache_tsc_multiplier(vmx);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001398}
1399
1400/*
1401 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1402 * vcpu mutex is already taken.
1403 */
Sean Christopherson1af1bb02020-05-06 16:58:50 -07001404static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001405{
1406 struct vcpu_vmx *vmx = to_vmx(vcpu);
1407
Sean Christopherson5c911be2020-05-01 09:31:17 -07001408 vmx_vcpu_load_vmcs(vcpu, cpu, NULL);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001409
Feng Wu28b835d2015-09-18 22:29:54 +08001410 vmx_vcpu_pi_load(vcpu, cpu);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001411
Wanpeng Li74c55932017-11-29 01:31:20 -08001412 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001413}
1414
Sean Christopherson13b964a2019-05-07 09:06:31 -07001415static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416{
Feng Wu28b835d2015-09-18 22:29:54 +08001417 vmx_vcpu_pi_put(vcpu);
1418
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001419 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420}
1421
Wanpeng Lif244dee2017-07-20 01:11:54 -07001422static bool emulation_required(struct kvm_vcpu *vcpu)
1423{
Sean Christopherson2ba44932020-09-23 11:44:48 -07001424 return emulate_invalid_guest_state && !vmx_guest_state_valid(vcpu);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001425}
1426
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001427unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001429 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001430 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001431
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001432 if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) {
1433 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Avi Kivity6de12732011-03-07 12:51:22 +02001434 rflags = vmcs_readl(GUEST_RFLAGS);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001435 if (vmx->rmode.vm86_active) {
Avi Kivity6de12732011-03-07 12:51:22 +02001436 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
Sean Christophersone7bddc52019-09-27 14:45:18 -07001437 save_rflags = vmx->rmode.save_rflags;
Avi Kivity6de12732011-03-07 12:51:22 +02001438 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1439 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001440 vmx->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001441 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001442 return vmx->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443}
1444
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001445void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001447 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001448 unsigned long old_rflags;
Wanpeng Lif244dee2017-07-20 01:11:54 -07001449
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00001450 if (is_unrestricted_guest(vcpu)) {
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001451 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001452 vmx->rflags = rflags;
1453 vmcs_writel(GUEST_RFLAGS, rflags);
1454 return;
1455 }
1456
1457 old_rflags = vmx_get_rflags(vcpu);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001458 vmx->rflags = rflags;
1459 if (vmx->rmode.vm86_active) {
1460 vmx->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001461 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001462 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001464
Sean Christophersone7bddc52019-09-27 14:45:18 -07001465 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM)
1466 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467}
1468
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001469u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001470{
1471 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1472 int ret = 0;
1473
1474 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001475 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001476 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001477 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001478
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001479 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001480}
1481
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001482void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001483{
1484 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1485 u32 interruptibility = interruptibility_old;
1486
1487 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1488
Jan Kiszka48005f62010-02-19 19:38:07 +01001489 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001490 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001491 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001492 interruptibility |= GUEST_INTR_STATE_STI;
1493
1494 if ((interruptibility != interruptibility_old))
1495 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1496}
1497
Chao Pengbf8c55d2018-10-24 16:05:14 +08001498static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data)
1499{
1500 struct vcpu_vmx *vmx = to_vmx(vcpu);
1501 unsigned long value;
1502
1503 /*
1504 * Any MSR write that attempts to change bits marked reserved will
1505 * case a #GP fault.
1506 */
1507 if (data & vmx->pt_desc.ctl_bitmask)
1508 return 1;
1509
1510 /*
1511 * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will
1512 * result in a #GP unless the same write also clears TraceEn.
1513 */
1514 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) &&
1515 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN))
1516 return 1;
1517
1518 /*
1519 * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit
1520 * and FabricEn would cause #GP, if
1521 * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0
1522 */
1523 if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) &&
1524 !(data & RTIT_CTL_FABRIC_EN) &&
1525 !intel_pt_validate_cap(vmx->pt_desc.caps,
1526 PT_CAP_single_range_output))
1527 return 1;
1528
1529 /*
1530 * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that
Ingo Molnard9f6e122021-03-18 15:28:01 +01001531 * utilize encodings marked reserved will cause a #GP fault.
Chao Pengbf8c55d2018-10-24 16:05:14 +08001532 */
1533 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods);
1534 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) &&
1535 !test_bit((data & RTIT_CTL_MTC_RANGE) >>
1536 RTIT_CTL_MTC_RANGE_OFFSET, &value))
1537 return 1;
1538 value = intel_pt_validate_cap(vmx->pt_desc.caps,
1539 PT_CAP_cycle_thresholds);
1540 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1541 !test_bit((data & RTIT_CTL_CYC_THRESH) >>
1542 RTIT_CTL_CYC_THRESH_OFFSET, &value))
1543 return 1;
1544 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods);
1545 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1546 !test_bit((data & RTIT_CTL_PSB_FREQ) >>
1547 RTIT_CTL_PSB_FREQ_OFFSET, &value))
1548 return 1;
1549
1550 /*
1551 * If ADDRx_CFG is reserved or the encodings is >2 will
1552 * cause a #GP fault.
1553 */
1554 value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET;
1555 if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2))
1556 return 1;
1557 value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET;
1558 if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2))
1559 return 1;
1560 value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET;
1561 if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2))
1562 return 1;
1563 value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET;
1564 if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2))
1565 return 1;
1566
1567 return 0;
1568}
1569
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07001570static bool vmx_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
1571{
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001572 /*
1573 * Emulation of instructions in SGX enclaves is impossible as RIP does
1574 * not point tthe failing instruction, and even if it did, the code
1575 * stream is inaccessible. Inject #UD instead of exiting to userspace
1576 * so that guest userspace can't DoS the guest simply by triggering
1577 * emulation (enclaves are CPL3 only).
1578 */
1579 if (to_vmx(vcpu)->exit_reason.enclave_mode) {
1580 kvm_queue_exception(vcpu, UD_VECTOR);
1581 return false;
1582 }
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07001583 return true;
1584}
1585
Sean Christopherson1957aa62019-08-27 14:40:39 -07001586static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587{
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001588 union vmx_exit_reason exit_reason = to_vmx(vcpu)->exit_reason;
Paolo Bonzinifede8072020-04-27 11:55:59 -04001589 unsigned long rip, orig_rip;
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001590 u32 instr_len;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591
Sean Christopherson1957aa62019-08-27 14:40:39 -07001592 /*
1593 * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on
1594 * undefined behavior: Intel's SDM doesn't mandate the VMCS field be
1595 * set when EPT misconfig occurs. In practice, real hardware updates
1596 * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors
1597 * (namely Hyper-V) don't set it due to it being undefined behavior,
1598 * i.e. we end up advancing IP with some random value.
1599 */
1600 if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001601 exit_reason.basic != EXIT_REASON_EPT_MISCONFIG) {
1602 instr_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
1603
1604 /*
1605 * Emulating an enclave's instructions isn't supported as KVM
1606 * cannot access the enclave's memory or its true RIP, e.g. the
1607 * vmcs.GUEST_RIP points at the exit point of the enclave, not
1608 * the RIP that actually triggered the VM-Exit. But, because
1609 * most instructions that cause VM-Exit will #UD in an enclave,
1610 * most instruction-based VM-Exits simply do not occur.
1611 *
1612 * There are a few exceptions, notably the debug instructions
1613 * INT1ICEBRK and INT3, as they are allowed in debug enclaves
1614 * and generate #DB/#BP as expected, which KVM might intercept.
1615 * But again, the CPU does the dirty work and saves an instr
1616 * length of zero so VMMs don't shoot themselves in the foot.
1617 * WARN if KVM tries to skip a non-zero length instruction on
1618 * a VM-Exit from an enclave.
1619 */
1620 if (!instr_len)
1621 goto rip_updated;
1622
1623 WARN(exit_reason.enclave_mode,
1624 "KVM: skipping instruction after SGX enclave VM-Exit");
1625
Paolo Bonzinifede8072020-04-27 11:55:59 -04001626 orig_rip = kvm_rip_read(vcpu);
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001627 rip = orig_rip + instr_len;
Paolo Bonzinifede8072020-04-27 11:55:59 -04001628#ifdef CONFIG_X86_64
1629 /*
1630 * We need to mask out the high 32 bits of RIP if not in 64-bit
1631 * mode, but just finding out that we are in 64-bit mode is
1632 * quite expensive. Only do it if there was a carry.
1633 */
1634 if (unlikely(((rip ^ orig_rip) >> 31) == 3) && !is_64_bit_mode(vcpu))
1635 rip = (u32)rip;
1636#endif
Sean Christopherson1957aa62019-08-27 14:40:39 -07001637 kvm_rip_write(vcpu, rip);
1638 } else {
1639 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
1640 return 0;
1641 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001643rip_updated:
Glauber Costa2809f5d2009-05-12 16:21:05 -04001644 /* skipping an emulated instruction also counts */
1645 vmx_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001646
Sean Christopherson60fc3d02019-08-27 14:40:38 -07001647 return 1;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001648}
1649
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02001650/*
Oliver Upton5ef8acb2020-02-07 02:36:07 -08001651 * Recognizes a pending MTF VM-exit and records the nested state for later
1652 * delivery.
1653 */
1654static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)
1655{
1656 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1657 struct vcpu_vmx *vmx = to_vmx(vcpu);
1658
1659 if (!is_guest_mode(vcpu))
1660 return;
1661
1662 /*
1663 * Per the SDM, MTF takes priority over debug-trap exceptions besides
1664 * T-bit traps. As instruction emulation is completed (i.e. at the
1665 * instruction boundary), any #DB exception pending delivery must be a
1666 * debug-trap. Record the pending MTF state to be delivered in
1667 * vmx_check_nested_events().
1668 */
1669 if (nested_cpu_has_mtf(vmcs12) &&
1670 (!vcpu->arch.exception.pending ||
1671 vcpu->arch.exception.nr == DB_VECTOR))
1672 vmx->nested.mtf_pending = true;
1673 else
1674 vmx->nested.mtf_pending = false;
1675}
1676
1677static int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu)
1678{
1679 vmx_update_emulated_instruction(vcpu);
1680 return skip_emulated_instruction(vcpu);
1681}
1682
Wanpeng Licaa057a2018-03-12 04:53:03 -07001683static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1684{
1685 /*
1686 * Ensure that we clear the HLT state in the VMCS. We don't need to
1687 * explicitly skip the instruction because if the HLT state is set,
1688 * then the instruction is already executing and RIP has already been
1689 * advanced.
1690 */
1691 if (kvm_hlt_in_guest(vcpu->kvm) &&
1692 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1693 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1694}
1695
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001696static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001697{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001698 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001699 unsigned nr = vcpu->arch.exception.nr;
1700 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001701 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001702 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001703
Jim Mattsonda998b42018-10-16 14:29:22 -07001704 kvm_deliver_exception_payload(vcpu);
1705
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001706 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001707 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001708 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1709 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001710
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001711 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001712 int inc_eip = 0;
1713 if (kvm_exception_is_soft(nr))
1714 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07001715 kvm_inject_realmode_interrupt(vcpu, nr, inc_eip);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001716 return;
1717 }
1718
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001719 WARN_ON_ONCE(vmx->emulation_required);
1720
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001721 if (kvm_exception_is_soft(nr)) {
1722 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1723 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001724 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1725 } else
1726 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1727
1728 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001729
1730 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001731}
1732
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001733static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr,
1734 bool load_into_hardware)
Eddie Donga75beee2007-05-17 18:55:15 +03001735{
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001736 struct vmx_uret_msr *uret_msr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001737
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001738 uret_msr = vmx_find_uret_msr(vmx, msr);
1739 if (!uret_msr)
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001740 return;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001741
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001742 uret_msr->load_into_hardware = load_into_hardware;
Eddie Donga75beee2007-05-17 18:55:15 +03001743}
1744
1745/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001746 * Set up the vmcs to automatically save and restore system
1747 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1748 * mode, as fiddling with msrs is very expensive.
1749 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001750static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001751{
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001752#ifdef CONFIG_X86_64
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001753 bool load_syscall_msrs;
1754
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001755 /*
1756 * The SYSCALL MSRs are only needed on long mode guests, and only
1757 * when EFER.SCE is set.
1758 */
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001759 load_syscall_msrs = is_long_mode(&vmx->vcpu) &&
1760 (vmx->vcpu.arch.efer & EFER_SCE);
1761
1762 vmx_setup_uret_msr(vmx, MSR_STAR, load_syscall_msrs);
1763 vmx_setup_uret_msr(vmx, MSR_LSTAR, load_syscall_msrs);
1764 vmx_setup_uret_msr(vmx, MSR_SYSCALL_MASK, load_syscall_msrs);
Eddie Donga75beee2007-05-17 18:55:15 +03001765#endif
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001766 vmx_setup_uret_msr(vmx, MSR_EFER, update_transition_efer(vmx));
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001767
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001768 vmx_setup_uret_msr(vmx, MSR_TSC_AUX,
1769 guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP) ||
1770 guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDPID));
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001771
Sean Christopherson5e17c622021-05-04 10:17:30 -07001772 /*
1773 * hle=0, rtm=0, tsx_ctrl=1 can be found with some combinations of new
1774 * kernel and old userspace. If those guests run on a tsx=off host, do
1775 * allow guests to use TSX_CTRL, but don't change the value in hardware
1776 * so that TSX remains always disabled.
1777 */
1778 vmx_setup_uret_msr(vmx, MSR_IA32_TSX_CTRL, boot_cpu_has(X86_FEATURE_RTM));
Avi Kivity58972972009-02-24 22:26:47 +02001779
Yang Zhang8d146952013-01-25 10:18:50 +08001780 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001781 vmx_update_msr_bitmap(&vmx->vcpu);
Sean Christophersonee9d22e2021-05-04 10:17:28 -07001782
1783 /*
1784 * The set of MSRs to load may have changed, reload MSRs before the
1785 * next VM-Enter.
1786 */
1787 vmx->guest_uret_msrs_loaded = false;
Avi Kivitye38aea32007-04-19 13:22:48 +03001788}
1789
Ilias Stamatis307a94c2021-05-26 19:44:13 +01001790u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu)
1791{
1792 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1793
1794 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING))
1795 return vmcs12->tsc_offset;
1796
1797 return 0;
1798}
1799
1800u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)
1801{
1802 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1803
1804 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING) &&
1805 nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))
1806 return vmcs12->tsc_multiplier;
1807
1808 return kvm_default_tsc_scaling_ratio;
1809}
1810
Leonid Shatz326e7422018-11-06 12:14:25 +02001811static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001812{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001813 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1814 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001815
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001816 /*
1817 * We're here if L1 chose not to trap WRMSR to TSC. According
1818 * to the spec, this should set L1's TSC; The offset that L1
1819 * set for L2 remains unchanged, and still needs to be added
1820 * to the newly set TSC to get L2's TSC.
1821 */
1822 if (is_guest_mode(vcpu) &&
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08001823 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001824 g_tsc_offset = vmcs12->tsc_offset;
1825
1826 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1827 vcpu->arch.tsc_offset - g_tsc_offset,
1828 offset);
1829 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1830 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831}
1832
Nadav Har'El801d3422011-05-25 23:02:23 +03001833/*
1834 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1835 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1836 * all guests if the "nested" module option is off, and can also be disabled
1837 * for a single guest by disabling its VMX cpuid bit.
1838 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001839bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001840{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001841 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001842}
1843
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001844static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1845 uint64_t val)
1846{
1847 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1848
1849 return !(val & ~valid_bits);
1850}
1851
Tom Lendacky801e4592018-02-21 13:39:51 -06001852static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1853{
Paolo Bonzini13893092018-02-26 13:40:09 +01001854 switch (msr->index) {
1855 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1856 if (!nested)
1857 return 1;
1858 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
Like Xu27461da32020-05-29 15:43:45 +08001859 case MSR_IA32_PERF_CAPABILITIES:
1860 msr->data = vmx_get_perf_capabilities();
1861 return 0;
Paolo Bonzini13893092018-02-26 13:40:09 +01001862 default:
Peter Xu12bc2132020-06-22 18:04:42 -04001863 return KVM_MSR_RET_INVALID;
Paolo Bonzini13893092018-02-26 13:40:09 +01001864 }
Tom Lendacky801e4592018-02-21 13:39:51 -06001865}
1866
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001867/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868 * Reads an msr value (of 'msr_index') into 'pdata'.
1869 * Returns 0 on success, non-0 otherwise.
1870 * Assumes vcpu_load() was already called.
1871 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001872static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001874 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001875 struct vmx_uret_msr *msr;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001876 u32 index;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001878 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001879#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001881 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882 break;
1883 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001884 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001886 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001887 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001888 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001889#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001891 return kvm_get_msr_common(vcpu, msr_info);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001892 case MSR_IA32_TSX_CTRL:
1893 if (!msr_info->host_initiated &&
1894 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
1895 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001896 goto find_uret_msr;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001897 case MSR_IA32_UMWAIT_CONTROL:
1898 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
1899 return 1;
1900
1901 msr_info->data = vmx->msr_ia32_umwait_control;
1902 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001903 case MSR_IA32_SPEC_CTRL:
1904 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05001905 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001906 return 1;
1907
1908 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1909 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001911 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912 break;
1913 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001914 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 break;
1916 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001917 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001919 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001920 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001921 (!msr_info->host_initiated &&
1922 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001923 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001924 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001925 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001926 case MSR_IA32_MCG_EXT_CTL:
1927 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001928 !(vmx->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001929 FEAT_CTL_LMCE_ENABLED))
Jan Kiszkacae50132014-01-04 18:47:22 +01001930 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001931 msr_info->data = vcpu->arch.mcg_ext_ctl;
1932 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001933 case MSR_IA32_FEAT_CTL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001934 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001935 break;
Sean Christopherson8f102442021-04-12 16:21:40 +12001936 case MSR_IA32_SGXLEPUBKEYHASH0 ... MSR_IA32_SGXLEPUBKEYHASH3:
1937 if (!msr_info->host_initiated &&
1938 !guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC))
1939 return 1;
1940 msr_info->data = to_vmx(vcpu)->msr_ia32_sgxlepubkeyhash
1941 [msr_info->index - MSR_IA32_SGXLEPUBKEYHASH0];
1942 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01001943 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1944 if (!nested_vmx_allowed(vcpu))
1945 return 1;
Vitaly Kuznetsov31de3d22020-02-05 13:30:33 +01001946 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1947 &msr_info->data))
1948 return 1;
1949 /*
1950 * Enlightened VMCS v1 doesn't have certain fields, but buggy
1951 * Hyper-V versions are still trying to use corresponding
1952 * features when they are exposed. Filter out the essential
1953 * minimum.
1954 */
1955 if (!msr_info->host_initiated &&
1956 vmx->nested.enlightened_vmcs_enabled)
1957 nested_evmcs_filter_control_msr(msr_info->index,
1958 &msr_info->data);
1959 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001960 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001961 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001962 return 1;
1963 msr_info->data = vmx->pt_desc.guest.ctl;
1964 break;
1965 case MSR_IA32_RTIT_STATUS:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001966 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001967 return 1;
1968 msr_info->data = vmx->pt_desc.guest.status;
1969 break;
1970 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001971 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001972 !intel_pt_validate_cap(vmx->pt_desc.caps,
1973 PT_CAP_cr3_filtering))
1974 return 1;
1975 msr_info->data = vmx->pt_desc.guest.cr3_match;
1976 break;
1977 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001978 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001979 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1980 PT_CAP_topa_output) &&
1981 !intel_pt_validate_cap(vmx->pt_desc.caps,
1982 PT_CAP_single_range_output)))
1983 return 1;
1984 msr_info->data = vmx->pt_desc.guest.output_base;
1985 break;
1986 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001987 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001988 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1989 PT_CAP_topa_output) &&
1990 !intel_pt_validate_cap(vmx->pt_desc.caps,
1991 PT_CAP_single_range_output)))
1992 return 1;
1993 msr_info->data = vmx->pt_desc.guest.output_mask;
1994 break;
1995 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
1996 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christopherson2ef76192020-03-02 15:56:22 -08001997 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001998 (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
1999 PT_CAP_num_address_ranges)))
2000 return 1;
2001 if (index % 2)
2002 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
2003 else
2004 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
2005 break;
Like Xud8550662021-01-08 09:36:55 +08002006 case MSR_IA32_DEBUGCTLMSR:
2007 msr_info->data = vmcs_read64(GUEST_IA32_DEBUGCTL);
2008 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002009 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002010 find_uret_msr:
Sean Christophersond85a8032020-09-23 11:04:06 -07002011 msr = vmx_find_uret_msr(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002012 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002013 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08002014 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002016 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017 }
2018
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019 return 0;
2020}
2021
Sean Christopherson24085002020-04-28 16:10:24 -07002022static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu,
2023 u64 data)
2024{
2025#ifdef CONFIG_X86_64
2026 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
2027 return (u32)data;
2028#endif
2029 return (unsigned long)data;
2030}
2031
Like Xuc6462362021-02-01 13:10:31 +08002032static u64 vcpu_supported_debugctl(struct kvm_vcpu *vcpu)
2033{
2034 u64 debugctl = vmx_supported_debugctl();
2035
2036 if (!intel_pmu_lbr_is_enabled(vcpu))
Like Xue6209a32021-02-01 13:10:36 +08002037 debugctl &= ~DEBUGCTLMSR_LBR_MASK;
Like Xuc6462362021-02-01 13:10:31 +08002038
Paolo Bonzini76ea4382021-05-06 06:30:04 -04002039 if (!guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))
2040 debugctl &= ~DEBUGCTLMSR_BUS_LOCK_DETECT;
2041
Like Xuc6462362021-02-01 13:10:31 +08002042 return debugctl;
2043}
2044
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045/*
Miaohe Lin311497e2019-12-11 14:26:25 +08002046 * Writes msr value into the appropriate "register".
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047 * Returns 0 on success, non-0 otherwise.
2048 * Assumes vcpu_load() was already called.
2049 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002050static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002052 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002053 struct vmx_uret_msr *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002054 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002055 u32 msr_index = msr_info->index;
2056 u64 data = msr_info->data;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002057 u32 index;
Eddie Dong2cc51562007-05-21 07:28:09 +03002058
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002060 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002061 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002062 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002063#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002065 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066 vmcs_writel(GUEST_FS_BASE, data);
2067 break;
2068 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002069 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070 vmcs_writel(GUEST_GS_BASE, data);
2071 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002072 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07002073 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002074 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075#endif
2076 case MSR_IA32_SYSENTER_CS:
Sean Christophersonde70d272019-05-07 09:06:36 -07002077 if (is_guest_mode(vcpu))
2078 get_vmcs12(vcpu)->guest_sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 vmcs_write32(GUEST_SYSENTER_CS, data);
2080 break;
2081 case MSR_IA32_SYSENTER_EIP:
Sean Christopherson24085002020-04-28 16:10:24 -07002082 if (is_guest_mode(vcpu)) {
2083 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002084 get_vmcs12(vcpu)->guest_sysenter_eip = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002085 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002086 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087 break;
2088 case MSR_IA32_SYSENTER_ESP:
Sean Christopherson24085002020-04-28 16:10:24 -07002089 if (is_guest_mode(vcpu)) {
2090 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002091 get_vmcs12(vcpu)->guest_sysenter_esp = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002092 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002093 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 break;
Like Xud8550662021-01-08 09:36:55 +08002095 case MSR_IA32_DEBUGCTLMSR: {
Like Xuc6462362021-02-01 13:10:31 +08002096 u64 invalid = data & ~vcpu_supported_debugctl(vcpu);
Like Xud8550662021-01-08 09:36:55 +08002097 if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) {
2098 if (report_ignored_msrs)
2099 vcpu_unimpl(vcpu, "%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n",
2100 __func__, data);
2101 data &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR);
2102 invalid &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR);
2103 }
2104
2105 if (invalid)
2106 return 1;
2107
Sean Christopherson699a1ac2019-05-07 09:06:37 -07002108 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
2109 VM_EXIT_SAVE_DEBUG_CONTROLS)
2110 get_vmcs12(vcpu)->guest_ia32_debugctl = data;
2111
Like Xud8550662021-01-08 09:36:55 +08002112 vmcs_write64(GUEST_IA32_DEBUGCTL, data);
Like Xu8e129112021-02-01 13:10:33 +08002113 if (intel_pmu_lbr_is_enabled(vcpu) && !to_vmx(vcpu)->lbr_desc.event &&
2114 (data & DEBUGCTLMSR_LBR))
2115 intel_pmu_create_guest_lbr_event(vcpu);
Like Xud8550662021-01-08 09:36:55 +08002116 return 0;
2117 }
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002118 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08002119 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02002120 (!msr_info->host_initiated &&
2121 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002122 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002123 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07002124 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002126 vmcs_write64(GUEST_BNDCFGS, data);
2127 break;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08002128 case MSR_IA32_UMWAIT_CONTROL:
2129 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
2130 return 1;
2131
2132 /* The reserved bit 1 and non-32 bit [63:32] should be zero */
2133 if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32)))
2134 return 1;
2135
2136 vmx->msr_ia32_umwait_control = data;
2137 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002138 case MSR_IA32_SPEC_CTRL:
2139 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002140 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002141 return 1;
2142
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002143 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002144 return 1;
2145
2146 vmx->spec_ctrl = data;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002147 if (!data)
2148 break;
2149
2150 /*
2151 * For non-nested:
2152 * When it's written (to non-zero) for the first time, pass
2153 * it through.
2154 *
2155 * For nested:
2156 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002157 * nested_vmx_prepare_msr_bitmap. We should not touch the
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002158 * vmcs02.msr_bitmap here since it gets completely overwritten
2159 * in the merging. We update the vmcs01 here for L1 as well
2160 * since it will end up touching the MSR anyway now.
2161 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002162 vmx_disable_intercept_for_msr(vcpu,
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002163 MSR_IA32_SPEC_CTRL,
2164 MSR_TYPE_RW);
2165 break;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002166 case MSR_IA32_TSX_CTRL:
2167 if (!msr_info->host_initiated &&
2168 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
2169 return 1;
2170 if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR))
2171 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002172 goto find_uret_msr;
Ashok Raj15d45072018-02-01 22:59:43 +01002173 case MSR_IA32_PRED_CMD:
2174 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002175 !guest_has_pred_cmd_msr(vcpu))
Ashok Raj15d45072018-02-01 22:59:43 +01002176 return 1;
2177
2178 if (data & ~PRED_CMD_IBPB)
2179 return 1;
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002180 if (!boot_cpu_has(X86_FEATURE_IBPB))
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002181 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002182 if (!data)
2183 break;
2184
2185 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2186
2187 /*
2188 * For non-nested:
2189 * When it's written (to non-zero) for the first time, pass
2190 * it through.
2191 *
2192 * For nested:
2193 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002194 * nested_vmx_prepare_msr_bitmap. We should not touch the
Ashok Raj15d45072018-02-01 22:59:43 +01002195 * vmcs02.msr_bitmap here since it gets completely overwritten
2196 * in the merging.
2197 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002198 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W);
Ashok Raj15d45072018-02-01 22:59:43 +01002199 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002200 case MSR_IA32_CR_PAT:
Sean Christophersond28f4292019-05-07 09:06:27 -07002201 if (!kvm_pat_valid(data))
2202 return 1;
2203
Sean Christopherson142e4be2019-05-07 09:06:35 -07002204 if (is_guest_mode(vcpu) &&
2205 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
2206 get_vmcs12(vcpu)->guest_ia32_pat = data;
2207
Sheng Yang468d4722008-10-09 16:01:55 +08002208 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2209 vmcs_write64(GUEST_IA32_PAT, data);
2210 vcpu->arch.pat = data;
2211 break;
2212 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002213 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002214 break;
Will Auldba904632012-11-29 12:42:50 -08002215 case MSR_IA32_TSC_ADJUST:
2216 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002217 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002218 case MSR_IA32_MCG_EXT_CTL:
2219 if ((!msr_info->host_initiated &&
2220 !(to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002221 FEAT_CTL_LMCE_ENABLED)) ||
Ashok Rajc45dcc72016-06-22 14:59:56 +08002222 (data & ~MCG_EXT_CTL_LMCE_EN))
2223 return 1;
2224 vcpu->arch.mcg_ext_ctl = data;
2225 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002226 case MSR_IA32_FEAT_CTL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002227 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08002228 (to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002229 FEAT_CTL_LOCKED && !msr_info->host_initiated))
Jan Kiszkacae50132014-01-04 18:47:22 +01002230 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08002231 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002232 if (msr_info->host_initiated && data == 0)
2233 vmx_leave_nested(vcpu);
Sean Christopherson72add912021-04-12 16:21:42 +12002234
2235 /* SGX may be enabled/disabled by guest's firmware */
2236 vmx_write_encls_bitmap(vcpu, NULL);
Jan Kiszkacae50132014-01-04 18:47:22 +01002237 break;
Sean Christopherson8f102442021-04-12 16:21:40 +12002238 case MSR_IA32_SGXLEPUBKEYHASH0 ... MSR_IA32_SGXLEPUBKEYHASH3:
2239 /*
2240 * On real hardware, the LE hash MSRs are writable before
2241 * the firmware sets bit 0 in MSR 0x7a ("activating" SGX),
2242 * at which point SGX related bits in IA32_FEATURE_CONTROL
2243 * become writable.
2244 *
2245 * KVM does not emulate SGX activation for simplicity, so
2246 * allow writes to the LE hash MSRs if IA32_FEATURE_CONTROL
2247 * is unlocked. This is technically not architectural
2248 * behavior, but it's close enough.
2249 */
2250 if (!msr_info->host_initiated &&
2251 (!guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC) ||
2252 ((vmx->msr_ia32_feature_control & FEAT_CTL_LOCKED) &&
2253 !(vmx->msr_ia32_feature_control & FEAT_CTL_SGX_LC_ENABLED))))
2254 return 1;
2255 vmx->msr_ia32_sgxlepubkeyhash
2256 [msr_index - MSR_IA32_SGXLEPUBKEYHASH0] = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002257 break;
2258 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08002259 if (!msr_info->host_initiated)
2260 return 1; /* they are read-only */
2261 if (!nested_vmx_allowed(vcpu))
2262 return 1;
2263 return vmx_set_vmx_msr(vcpu, msr_index, data);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002264 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08002265 if (!vmx_pt_mode_is_host_guest() ||
Luwei Kangee85dec2018-10-24 16:05:16 +08002266 vmx_rtit_ctl_check(vcpu, data) ||
2267 vmx->nested.vmxon)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002268 return 1;
2269 vmcs_write64(GUEST_IA32_RTIT_CTL, data);
2270 vmx->pt_desc.guest.ctl = data;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002271 pt_update_intercept_for_msr(vcpu);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002272 break;
2273 case MSR_IA32_RTIT_STATUS:
Sean Christophersone348ac72019-12-10 15:24:33 -08002274 if (!pt_can_write_msr(vmx))
2275 return 1;
2276 if (data & MSR_IA32_RTIT_STATUS_MASK)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002277 return 1;
2278 vmx->pt_desc.guest.status = data;
2279 break;
2280 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christophersone348ac72019-12-10 15:24:33 -08002281 if (!pt_can_write_msr(vmx))
2282 return 1;
2283 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2284 PT_CAP_cr3_filtering))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002285 return 1;
2286 vmx->pt_desc.guest.cr3_match = data;
2287 break;
2288 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christophersone348ac72019-12-10 15:24:33 -08002289 if (!pt_can_write_msr(vmx))
2290 return 1;
2291 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2292 PT_CAP_topa_output) &&
2293 !intel_pt_validate_cap(vmx->pt_desc.caps,
2294 PT_CAP_single_range_output))
2295 return 1;
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07002296 if (!pt_output_base_valid(vcpu, data))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002297 return 1;
2298 vmx->pt_desc.guest.output_base = data;
2299 break;
2300 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christophersone348ac72019-12-10 15:24:33 -08002301 if (!pt_can_write_msr(vmx))
2302 return 1;
2303 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2304 PT_CAP_topa_output) &&
2305 !intel_pt_validate_cap(vmx->pt_desc.caps,
2306 PT_CAP_single_range_output))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002307 return 1;
2308 vmx->pt_desc.guest.output_mask = data;
2309 break;
2310 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christophersone348ac72019-12-10 15:24:33 -08002311 if (!pt_can_write_msr(vmx))
2312 return 1;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002313 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christophersone348ac72019-12-10 15:24:33 -08002314 if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
2315 PT_CAP_num_address_ranges))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002316 return 1;
Sean Christophersonfe6ed362019-12-10 15:24:32 -08002317 if (is_noncanonical_address(data, vcpu))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002318 return 1;
2319 if (index % 2)
2320 vmx->pt_desc.guest.addr_b[index / 2] = data;
2321 else
2322 vmx->pt_desc.guest.addr_a[index / 2] = data;
2323 break;
Paolo Bonzini9c9520c2021-02-02 09:36:08 -05002324 case MSR_IA32_PERF_CAPABILITIES:
2325 if (data && !vcpu_to_pmu(vcpu)->version)
2326 return 1;
2327 if (data & PMU_CAP_LBR_FMT) {
2328 if ((data & PMU_CAP_LBR_FMT) !=
2329 (vmx_get_perf_capabilities() & PMU_CAP_LBR_FMT))
2330 return 1;
2331 if (!intel_pmu_lbr_is_compatible(vcpu))
2332 return 1;
2333 }
2334 ret = kvm_set_msr_common(vcpu, msr_info);
2335 break;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002336
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002338 find_uret_msr:
Sean Christophersond85a8032020-09-23 11:04:06 -07002339 msr = vmx_find_uret_msr(vmx, msr_index);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05002340 if (msr)
Sean Christopherson7bf662b2020-09-23 11:04:07 -07002341 ret = vmx_set_guest_uret_msr(vmx, msr, data);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05002342 else
2343 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344 }
2345
Eddie Dong2cc51562007-05-21 07:28:09 +03002346 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347}
2348
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002349static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350{
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002351 unsigned long guest_owned_bits;
2352
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002353 kvm_register_mark_available(vcpu, reg);
2354
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002355 switch (reg) {
2356 case VCPU_REGS_RSP:
2357 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2358 break;
2359 case VCPU_REGS_RIP:
2360 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2361 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002362 case VCPU_EXREG_PDPTR:
2363 if (enable_ept)
2364 ept_save_pdptrs(vcpu);
2365 break;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07002366 case VCPU_EXREG_CR0:
2367 guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2368
2369 vcpu->arch.cr0 &= ~guest_owned_bits;
2370 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits;
2371 break;
Sean Christopherson34059c22019-09-27 14:45:23 -07002372 case VCPU_EXREG_CR3:
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002373 if (is_unrestricted_guest(vcpu) ||
2374 (enable_ept && is_paging(vcpu)))
Sean Christopherson34059c22019-09-27 14:45:23 -07002375 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2376 break;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002377 case VCPU_EXREG_CR4:
2378 guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2379
2380 vcpu->arch.cr4 &= ~guest_owned_bits;
2381 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits;
2382 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002383 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07002384 WARN_ON_ONCE(1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002385 break;
2386 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387}
2388
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389static __init int cpu_has_kvm_support(void)
2390{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002391 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392}
2393
2394static __init int vmx_disabled_by_bios(void)
2395{
Sean Christophersona4d0b2f2019-12-20 20:45:09 -08002396 return !boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2397 !boot_cpu_has(X86_FEATURE_VMX);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398}
2399
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002400static int kvm_cpu_vmxon(u64 vmxon_pointer)
Dongxiao Xu7725b892010-05-11 18:29:38 +08002401{
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002402 u64 msr;
2403
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002404 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002405
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002406 asm_volatile_goto("1: vmxon %[vmxon_pointer]\n\t"
2407 _ASM_EXTABLE(1b, %l[fault])
2408 : : [vmxon_pointer] "m"(vmxon_pointer)
2409 : : fault);
2410 return 0;
2411
2412fault:
2413 WARN_ONCE(1, "VMXON faulted, MSR_IA32_FEAT_CTL (0x3a) = 0x%llx\n",
2414 rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr) ? 0xdeadbeef : msr);
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002415 cr4_clear_bits(X86_CR4_VMXE);
2416
2417 return -EFAULT;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002418}
2419
Radim Krčmář13a34e02014-08-28 15:13:03 +02002420static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421{
2422 int cpu = raw_smp_processor_id();
2423 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002424 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002426 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02002427 return -EBUSY;
2428
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002429 /*
2430 * This can happen if we hot-added a CPU but failed to allocate
2431 * VP assist page for it.
2432 */
2433 if (static_branch_unlikely(&enable_evmcs) &&
2434 !hv_get_vp_assist_page(cpu))
2435 return -EFAULT;
2436
Sean Christopherson5ef940b2020-12-30 16:26:58 -08002437 intel_pt_handle_vmx(1);
2438
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002439 r = kvm_cpu_vmxon(phys_addr);
Sean Christopherson5ef940b2020-12-30 16:26:58 -08002440 if (r) {
2441 intel_pt_handle_vmx(0);
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002442 return r;
Sean Christopherson5ef940b2020-12-30 16:26:58 -08002443 }
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002444
David Hildenbrandfdf288b2017-08-24 20:51:29 +02002445 if (enable_ept)
2446 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02002447
2448 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449}
2450
Nadav Har'Eld462b812011-05-24 15:26:10 +03002451static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002452{
2453 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002454 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002455
Nadav Har'Eld462b812011-05-24 15:26:10 +03002456 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2457 loaded_vmcss_on_cpu_link)
2458 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002459}
2460
Radim Krčmář13a34e02014-08-28 15:13:03 +02002461static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002463 vmclear_local_loaded_vmcss();
Sean Christopherson6a289132020-12-30 16:26:59 -08002464
2465 if (cpu_vmxoff())
2466 kvm_spurious_fault();
Sean Christopherson5ef940b2020-12-30 16:26:58 -08002467
2468 intel_pt_handle_vmx(0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469}
2470
Sean Christopherson7a57c092020-03-12 11:04:16 -07002471/*
2472 * There is no X86_FEATURE for SGX yet, but anyway we need to query CPUID
2473 * directly instead of going through cpu_has(), to ensure KVM is trapping
2474 * ENCLS whenever it's supported in hardware. It does not matter whether
2475 * the host OS supports or has enabled SGX.
2476 */
2477static bool cpu_has_sgx(void)
2478{
2479 return cpuid_eax(0) >= 0x12 && (cpuid_eax(0x12) & BIT(0));
2480}
2481
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002482static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002483 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484{
2485 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002486 u32 ctl = ctl_min | ctl_opt;
2487
2488 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2489
2490 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2491 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2492
2493 /* Ensure minimum (required) set of control bits are supported. */
2494 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002495 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002496
2497 *result = ctl;
2498 return 0;
2499}
2500
Sean Christopherson7caaa712018-12-03 13:53:01 -08002501static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
2502 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002503{
2504 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002505 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002506 u32 _pin_based_exec_control = 0;
2507 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002508 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002509 u32 _vmexit_control = 0;
2510 u32 _vmentry_control = 0;
2511
Paolo Bonzini13893092018-02-26 13:40:09 +01002512 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05302513 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002514#ifdef CONFIG_X86_64
2515 CPU_BASED_CR8_LOAD_EXITING |
2516 CPU_BASED_CR8_STORE_EXITING |
2517#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002518 CPU_BASED_CR3_LOAD_EXITING |
2519 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08002520 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002521 CPU_BASED_MOV_DR_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08002522 CPU_BASED_USE_TSC_OFFSETTING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002523 CPU_BASED_MWAIT_EXITING |
2524 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002525 CPU_BASED_INVLPG_EXITING |
2526 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002527
Sheng Yangf78e0e22007-10-29 09:40:42 +08002528 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002529 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002530 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002531 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2532 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002533 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002534#ifdef CONFIG_X86_64
2535 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2536 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2537 ~CPU_BASED_CR8_STORE_EXITING;
2538#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002539 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002540 min2 = 0;
2541 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002542 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002543 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002544 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002545 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002546 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002547 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02002548 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002549 SECONDARY_EXEC_ENABLE_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002550 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002551 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002552 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08002553 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08002554 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02002555 SECONDARY_EXEC_RDSEED_EXITING |
2556 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08002557 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04002558 SECONDARY_EXEC_TSC_SCALING |
Tao Xue69e72fa2019-07-16 14:55:49 +08002559 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Chao Pengf99e3da2018-10-24 16:05:10 +08002560 SECONDARY_EXEC_PT_USE_GPA |
2561 SECONDARY_EXEC_PT_CONCEAL_VMX |
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08002562 SECONDARY_EXEC_ENABLE_VMFUNC |
2563 SECONDARY_EXEC_BUS_LOCK_DETECTION;
Sean Christopherson7a57c092020-03-12 11:04:16 -07002564 if (cpu_has_sgx())
2565 opt2 |= SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08002566 if (adjust_vmx_controls(min2, opt2,
2567 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002568 &_cpu_based_2nd_exec_control) < 0)
2569 return -EIO;
2570 }
2571#ifndef CONFIG_X86_64
2572 if (!(_cpu_based_2nd_exec_control &
2573 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2574 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2575#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002576
2577 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2578 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002579 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002580 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2581 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002582
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002583 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08002584 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002585
Sheng Yangd56f5462008-04-25 10:13:16 +08002586 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002587 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2588 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002589 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2590 CPU_BASED_CR3_STORE_EXITING |
2591 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08002592 } else if (vmx_cap->ept) {
2593 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002594 pr_warn_once("EPT CAP should not exist if not support "
2595 "1-setting enable EPT VM-execution control\n");
2596 }
2597 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002598 vmx_cap->vpid) {
2599 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002600 pr_warn_once("VPID CAP should not exist if not support "
2601 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002602 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002603
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002604 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002605#ifdef CONFIG_X86_64
2606 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2607#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002608 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002609 VM_EXIT_LOAD_IA32_PAT |
2610 VM_EXIT_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002611 VM_EXIT_CLEAR_BNDCFGS |
2612 VM_EXIT_PT_CONCEAL_PIP |
2613 VM_EXIT_CLEAR_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002614 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2615 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002616 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002617
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002618 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2619 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2620 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002621 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2622 &_pin_based_exec_control) < 0)
2623 return -EIO;
2624
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002625 if (cpu_has_broken_vmx_preemption_timer())
2626 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002627 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002628 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002629 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2630
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002631 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002632 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2633 VM_ENTRY_LOAD_IA32_PAT |
2634 VM_ENTRY_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002635 VM_ENTRY_LOAD_BNDCFGS |
2636 VM_ENTRY_PT_CONCEAL_PIP |
2637 VM_ENTRY_LOAD_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002638 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2639 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002640 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002642 /*
2643 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2644 * can't be used due to an errata where VM Exit may incorrectly clear
2645 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2646 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2647 */
2648 if (boot_cpu_data.x86 == 0x6) {
2649 switch (boot_cpu_data.x86_model) {
2650 case 26: /* AAK155 */
2651 case 30: /* AAP115 */
2652 case 37: /* AAT100 */
2653 case 44: /* BC86,AAY89,BD102 */
2654 case 46: /* BA97 */
Sean Christopherson85ba2b12019-01-14 12:12:02 -08002655 _vmentry_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002656 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2657 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2658 "does not work properly. Using workaround\n");
2659 break;
2660 default:
2661 break;
2662 }
2663 }
2664
2665
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002666 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002667
2668 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2669 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002670 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002671
2672#ifdef CONFIG_X86_64
2673 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2674 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002675 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002676#endif
2677
2678 /* Require Write-Back (WB) memory type for VMCS accesses. */
2679 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002680 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002681
Yang, Sheng002c7f72007-07-31 14:23:01 +03002682 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002683 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002684 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002685
Liran Alon2307af12018-06-29 22:59:04 +03002686 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002687
Yang, Sheng002c7f72007-07-31 14:23:01 +03002688 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2689 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002690 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002691 vmcs_conf->vmexit_ctrl = _vmexit_control;
2692 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002693
Vitaly Kuznetsov064eedf2020-10-14 16:33:46 +02002694#if IS_ENABLED(CONFIG_HYPERV)
2695 if (enlightened_vmcs)
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002696 evmcs_sanitize_exec_ctrls(vmcs_conf);
Vitaly Kuznetsov064eedf2020-10-14 16:33:46 +02002697#endif
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002698
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002699 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002700}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701
Ben Gardon41836832019-02-11 11:02:52 -08002702struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703{
2704 int node = cpu_to_node(cpu);
2705 struct page *pages;
2706 struct vmcs *vmcs;
2707
Ben Gardon41836832019-02-11 11:02:52 -08002708 pages = __alloc_pages_node(node, flags, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709 if (!pages)
2710 return NULL;
2711 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002712 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002713
2714 /* KVM supports Enlightened VMCS v1 only */
2715 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002716 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002717 else
Liran Alon392b2f22018-06-23 02:35:01 +03002718 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002719
Liran Alon491a6032018-06-23 02:35:12 +03002720 if (shadow)
2721 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722 return vmcs;
2723}
2724
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002725void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002727 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728}
2729
Nadav Har'Eld462b812011-05-24 15:26:10 +03002730/*
2731 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2732 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002733void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002734{
2735 if (!loaded_vmcs->vmcs)
2736 return;
2737 loaded_vmcs_clear(loaded_vmcs);
2738 free_vmcs(loaded_vmcs->vmcs);
2739 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002740 if (loaded_vmcs->msr_bitmap)
2741 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002742 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002743}
2744
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002745int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002746{
Liran Alon491a6032018-06-23 02:35:12 +03002747 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002748 if (!loaded_vmcs->vmcs)
2749 return -ENOMEM;
2750
Sean Christophersond260f9e2020-03-21 12:37:50 -07002751 vmcs_clear(loaded_vmcs->vmcs);
2752
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002753 loaded_vmcs->shadow_vmcs = NULL;
Sean Christopherson804939e2019-05-07 12:18:05 -07002754 loaded_vmcs->hv_timer_soft_disabled = false;
Sean Christophersond260f9e2020-03-21 12:37:50 -07002755 loaded_vmcs->cpu = -1;
2756 loaded_vmcs->launched = 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002757
2758 if (cpu_has_vmx_msr_bitmap()) {
Ben Gardon41836832019-02-11 11:02:52 -08002759 loaded_vmcs->msr_bitmap = (unsigned long *)
2760 __get_free_page(GFP_KERNEL_ACCOUNT);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002761 if (!loaded_vmcs->msr_bitmap)
2762 goto out_vmcs;
2763 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002764
Arnd Bergmann1f008e12018-05-25 17:36:17 +02002765 if (IS_ENABLED(CONFIG_HYPERV) &&
2766 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002767 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2768 struct hv_enlightened_vmcs *evmcs =
2769 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2770
2771 evmcs->hv_enlightenments_control.msr_bitmap = 1;
2772 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002773 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002774
2775 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002776 memset(&loaded_vmcs->controls_shadow, 0,
2777 sizeof(struct vmcs_controls_shadow));
Sean Christophersond7ee0392018-07-23 12:32:47 -07002778
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002779 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002780
2781out_vmcs:
2782 free_loaded_vmcs(loaded_vmcs);
2783 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002784}
2785
Sam Ravnborg39959582007-06-01 00:47:13 -07002786static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787{
2788 int cpu;
2789
Zachary Amsden3230bb42009-09-29 11:38:37 -10002790 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002792 per_cpu(vmxarea, cpu) = NULL;
2793 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794}
2795
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796static __init int alloc_kvm_area(void)
2797{
2798 int cpu;
2799
Zachary Amsden3230bb42009-09-29 11:38:37 -10002800 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 struct vmcs *vmcs;
2802
Ben Gardon41836832019-02-11 11:02:52 -08002803 vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 if (!vmcs) {
2805 free_kvm_area();
2806 return -ENOMEM;
2807 }
2808
Liran Alon2307af12018-06-29 22:59:04 +03002809 /*
2810 * When eVMCS is enabled, alloc_vmcs_cpu() sets
2811 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
2812 * revision_id reported by MSR_IA32_VMX_BASIC.
2813 *
Linus Torvalds312a4662018-12-26 17:03:51 -08002814 * However, even though not explicitly documented by
Liran Alon2307af12018-06-29 22:59:04 +03002815 * TLFS, VMXArea passed as VMXON argument should
2816 * still be marked with revision_id reported by
2817 * physical CPU.
2818 */
2819 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002820 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002821
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 per_cpu(vmxarea, cpu) = vmcs;
2823 }
2824 return 0;
2825}
2826
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002827static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002828 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002830 if (!emulate_invalid_guest_state) {
2831 /*
2832 * CS and SS RPL should be equal during guest entry according
2833 * to VMX spec, but in reality it is not always so. Since vcpu
2834 * is in the middle of the transition from real mode to
2835 * protected mode it is safe to assume that RPL 0 is a good
2836 * default value.
2837 */
2838 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03002839 save->selector &= ~SEGMENT_RPL_MASK;
2840 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02002841 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002843 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844}
2845
2846static void enter_pmode(struct kvm_vcpu *vcpu)
2847{
2848 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002849 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850
Gleb Natapovd99e4152012-12-20 16:57:45 +02002851 /*
Ingo Molnard9f6e122021-03-18 15:28:01 +01002852 * Update real mode segment cache. It may be not up-to-date if segment
Gleb Natapovd99e4152012-12-20 16:57:45 +02002853 * register was written while vcpu was in a guest mode.
2854 */
2855 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2856 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2857 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2858 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2859 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2860 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2861
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002862 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002864 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865
2866 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002867 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2868 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 vmcs_writel(GUEST_RFLAGS, flags);
2870
Rusty Russell66aee912007-07-17 23:34:16 +10002871 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2872 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873
Jason Baronb6a7cc32021-01-14 22:27:54 -05002874 vmx_update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002876 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2877 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2878 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2879 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2880 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2881 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882}
2883
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002884static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885{
Mathias Krause772e0312012-08-30 01:30:19 +02002886 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002887 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888
Gleb Natapovd99e4152012-12-20 16:57:45 +02002889 var.dpl = 0x3;
2890 if (seg == VCPU_SREG_CS)
2891 var.type = 0x3;
2892
2893 if (!emulate_invalid_guest_state) {
2894 var.selector = var.base >> 4;
2895 var.base = var.base & 0xffff0;
2896 var.limit = 0xffff;
2897 var.g = 0;
2898 var.db = 0;
2899 var.present = 1;
2900 var.s = 1;
2901 var.l = 0;
2902 var.unusable = 0;
2903 var.type = 0x3;
2904 var.avl = 0;
2905 if (save->base & 0xf)
2906 printk_once(KERN_WARNING "kvm: segment base is not "
2907 "paragraph aligned when entering "
2908 "protected mode (seg=%d)", seg);
2909 }
2910
2911 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05002912 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002913 vmcs_write32(sf->limit, var.limit);
2914 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915}
2916
2917static void enter_rmode(struct kvm_vcpu *vcpu)
2918{
2919 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002920 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002921 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002923 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2924 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2925 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2926 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2927 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002928 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2929 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002930
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002931 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932
Gleb Natapov776e58e2011-03-13 12:34:27 +02002933 /*
2934 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002935 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002936 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002937 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002938 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2939 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002940
Avi Kivity2fb92db2011-04-27 19:42:18 +03002941 vmx_segment_cache_clear(vmx);
2942
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002943 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2946
2947 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002948 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002950 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951
2952 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002953 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Jason Baronb6a7cc32021-01-14 22:27:54 -05002954 vmx_update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955
Gleb Natapovd99e4152012-12-20 16:57:45 +02002956 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2957 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2958 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2959 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2960 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2961 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002962
Eddie Dong8668a3c2007-10-10 14:26:45 +08002963 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964}
2965
Maxim Levitsky72f211e2020-10-01 14:29:53 +03002966int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Amit Shah401d10d2009-02-20 22:53:37 +05302967{
2968 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond85a8032020-09-23 11:04:06 -07002969 struct vmx_uret_msr *msr = vmx_find_uret_msr(vmx, MSR_EFER);
Avi Kivity26bb0982009-09-07 11:14:12 +03002970
Maxim Levitsky72f211e2020-10-01 14:29:53 +03002971 /* Nothing to do if hardware doesn't support EFER. */
Avi Kivity26bb0982009-09-07 11:14:12 +03002972 if (!msr)
Maxim Levitsky72f211e2020-10-01 14:29:53 +03002973 return 0;
Amit Shah401d10d2009-02-20 22:53:37 +05302974
Avi Kivityf6801df2010-01-21 15:31:50 +02002975 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302976 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002977 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302978 msr->data = efer;
2979 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002980 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302981
2982 msr->data = efer & ~EFER_LME;
2983 }
2984 setup_msrs(vmx);
Maxim Levitsky72f211e2020-10-01 14:29:53 +03002985 return 0;
Amit Shah401d10d2009-02-20 22:53:37 +05302986}
2987
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002988#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989
2990static void enter_lmode(struct kvm_vcpu *vcpu)
2991{
2992 u32 guest_tr_ar;
2993
Avi Kivity2fb92db2011-04-27 19:42:18 +03002994 vmx_segment_cache_clear(to_vmx(vcpu));
2995
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002997 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002998 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2999 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003000 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003001 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3002 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003 }
Avi Kivityda38f432010-07-06 11:30:49 +03003004 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005}
3006
3007static void exit_lmode(struct kvm_vcpu *vcpu)
3008{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003009 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003010 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011}
3012
3013#endif
3014
Sean Christopherson77809382020-03-20 14:28:18 -07003015static void vmx_flush_tlb_all(struct kvm_vcpu *vcpu)
Sean Christopherson5058b692020-03-20 14:28:14 -07003016{
3017 struct vcpu_vmx *vmx = to_vmx(vcpu);
3018
3019 /*
Sean Christopherson77809382020-03-20 14:28:18 -07003020 * INVEPT must be issued when EPT is enabled, irrespective of VPID, as
3021 * the CPU is not required to invalidate guest-physical mappings on
3022 * VM-Entry, even if VPID is disabled. Guest-physical mappings are
3023 * associated with the root EPT structure and not any particular VPID
3024 * (INVVPID also isn't required to invalidate guest-physical mappings).
Sean Christopherson5058b692020-03-20 14:28:14 -07003025 */
3026 if (enable_ept) {
3027 ept_sync_global();
3028 } else if (enable_vpid) {
3029 if (cpu_has_vmx_invvpid_global()) {
3030 vpid_sync_vcpu_global();
3031 } else {
3032 vpid_sync_vcpu_single(vmx->vpid);
3033 vpid_sync_vcpu_single(vmx->nested.vpid02);
3034 }
3035 }
3036}
3037
Sean Christopherson33d19ec2020-03-20 14:28:16 -07003038static void vmx_flush_tlb_current(struct kvm_vcpu *vcpu)
3039{
Sean Christopherson2a40b902020-07-15 20:41:18 -07003040 struct kvm_mmu *mmu = vcpu->arch.mmu;
3041 u64 root_hpa = mmu->root_hpa;
Sean Christopherson33d19ec2020-03-20 14:28:16 -07003042
3043 /* No flush required if the current context is invalid. */
3044 if (!VALID_PAGE(root_hpa))
3045 return;
3046
3047 if (enable_ept)
Sean Christopherson2a40b902020-07-15 20:41:18 -07003048 ept_sync_context(construct_eptp(vcpu, root_hpa,
3049 mmu->shadow_root_level));
Sean Christopherson33d19ec2020-03-20 14:28:16 -07003050 else if (!is_guest_mode(vcpu))
3051 vpid_sync_context(to_vmx(vcpu)->vpid);
3052 else
3053 vpid_sync_context(nested_get_vpid02(vcpu));
3054}
3055
Junaid Shahidfaff8752018-06-29 13:10:05 -07003056static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
3057{
Junaid Shahidfaff8752018-06-29 13:10:05 -07003058 /*
Sean Christophersonad104b52020-03-20 14:28:11 -07003059 * vpid_sync_vcpu_addr() is a nop if vmx->vpid==0, see the comment in
3060 * vmx_flush_tlb_guest() for an explanation of why this is ok.
Junaid Shahidfaff8752018-06-29 13:10:05 -07003061 */
Sean Christophersonad104b52020-03-20 14:28:11 -07003062 vpid_sync_vcpu_addr(to_vmx(vcpu)->vpid, addr);
Junaid Shahidfaff8752018-06-29 13:10:05 -07003063}
3064
Sean Christophersone64419d2020-03-20 14:28:10 -07003065static void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu)
3066{
3067 /*
3068 * vpid_sync_context() is a nop if vmx->vpid==0, e.g. if enable_vpid==0
3069 * or a vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit
3070 * are required to flush GVA->{G,H}PA mappings from the TLB if vpid is
3071 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed),
3072 * i.e. no explicit INVVPID is necessary.
3073 */
3074 vpid_sync_context(to_vmx(vcpu)->vpid);
3075}
3076
Peter Shier43fea4e2020-08-20 16:05:45 -07003077void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003078{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003079 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3080
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003081 if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003082 return;
3083
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02003084 if (is_pae_paging(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003085 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3086 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3087 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3088 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003089 }
3090}
3091
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003092void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03003093{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003094 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3095
Sean Christopherson9932b492020-04-15 13:34:50 -07003096 if (WARN_ON_ONCE(!is_pae_paging(vcpu)))
3097 return;
3098
3099 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3100 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3101 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3102 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003103
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003104 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003105}
3106
Sheng Yang14394422008-04-28 12:24:45 +08003107static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3108 unsigned long cr0,
3109 struct kvm_vcpu *vcpu)
3110{
Sean Christopherson2183f562019-05-07 12:17:56 -07003111 struct vcpu_vmx *vmx = to_vmx(vcpu);
3112
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003113 if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
Sean Christopherson34059c22019-09-27 14:45:23 -07003114 vmx_cache_reg(vcpu, VCPU_EXREG_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003115 if (!(cr0 & X86_CR0_PG)) {
3116 /* From paging/starting to nonpaging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003117 exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3118 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08003119 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003120 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003121 } else if (!is_paging(vcpu)) {
3122 /* From nonpaging to paging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003123 exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3124 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08003125 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003126 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003127 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003128
3129 if (!(cr0 & X86_CR0_WP))
3130 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003131}
3132
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003133void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003135 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003136 unsigned long hw_cr0;
3137
Sean Christopherson3de63472018-07-13 08:42:30 -07003138 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003139 if (is_unrestricted_guest(vcpu))
Gleb Natapov50378782013-02-04 16:00:28 +02003140 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003141 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003142 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003143
Gleb Natapov218e7632013-01-21 15:36:45 +02003144 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3145 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146
Gleb Natapov218e7632013-01-21 15:36:45 +02003147 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3148 enter_rmode(vcpu);
3149 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003151#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003152 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003153 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003155 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 exit_lmode(vcpu);
3157 }
3158#endif
3159
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003160 if (enable_ept && !is_unrestricted_guest(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003161 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3162
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003164 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003165 vcpu->arch.cr0 = cr0;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07003166 kvm_register_mark_available(vcpu, VCPU_EXREG_CR0);
Gleb Natapov14168782013-01-21 15:36:49 +02003167
3168 /* depends on vcpu->arch.cr0 to be set to a new value */
3169 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170}
3171
Sean Christophersond468d942020-07-15 20:41:20 -07003172static int vmx_get_max_tdp_level(void)
Sean Christopherson0047fca2020-05-01 21:32:33 -07003173{
Sean Christophersond468d942020-07-15 20:41:20 -07003174 if (cpu_has_vmx_ept_5levels())
Sean Christopherson0047fca2020-05-01 21:32:33 -07003175 return 5;
3176 return 4;
3177}
3178
Sean Christophersone83bc092021-03-05 10:31:13 -08003179u64 construct_eptp(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level)
Sheng Yang14394422008-04-28 12:24:45 +08003180{
Yu Zhang855feb62017-08-24 20:27:55 +08003181 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08003182
Sean Christopherson2a40b902020-07-15 20:41:18 -07003183 eptp |= (root_level == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08003184
Peter Feiner995f00a2017-06-30 17:26:32 -07003185 if (enable_ept_ad_bits &&
3186 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02003187 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sean Christophersone83bc092021-03-05 10:31:13 -08003188 eptp |= root_hpa;
Sheng Yang14394422008-04-28 12:24:45 +08003189
3190 return eptp;
3191}
3192
Sean Christophersone83bc092021-03-05 10:31:13 -08003193static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
3194 int root_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195{
Tianyu Lan877ad952018-07-19 08:40:23 +00003196 struct kvm *kvm = vcpu->kvm;
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003197 bool update_guest_cr3 = true;
Sheng Yang14394422008-04-28 12:24:45 +08003198 unsigned long guest_cr3;
3199 u64 eptp;
3200
Avi Kivity089d0342009-03-23 18:26:32 +02003201 if (enable_ept) {
Sean Christophersone83bc092021-03-05 10:31:13 -08003202 eptp = construct_eptp(vcpu, root_hpa, root_level);
Sheng Yang14394422008-04-28 12:24:45 +08003203 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00003204
Sean Christopherson978c8342021-03-05 10:31:23 -08003205 hv_track_root_ept(vcpu, root_hpa);
Tianyu Lan877ad952018-07-19 08:40:23 +00003206
Paolo Bonzinidf7e0682020-05-20 08:37:37 -04003207 if (!enable_unrestricted_guest && !is_paging(vcpu))
Tianyu Lan877ad952018-07-19 08:40:23 +00003208 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Sean Christophersonb17b7432019-09-27 14:45:17 -07003209 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3210 guest_cr3 = vcpu->arch.cr3;
3211 else /* vmcs01.GUEST_CR3 is already up-to-date. */
3212 update_guest_cr3 = false;
Peter Shier43fea4e2020-08-20 16:05:45 -07003213 vmx_ept_load_pdptrs(vcpu);
Sean Christophersonbe100ef2020-03-20 14:28:33 -07003214 } else {
Sean Christophersone83bc092021-03-05 10:31:13 -08003215 guest_cr3 = root_hpa | kvm_get_active_pcid(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003216 }
3217
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003218 if (update_guest_cr3)
3219 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220}
3221
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07003222static bool vmx_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
3223{
3224 /*
3225 * We operate under the default treatment of SMM, so VMX cannot be
3226 * enabled under SMM. Note, whether or not VMXE is allowed at all is
Sean Christophersonee69c922020-10-06 18:44:16 -07003227 * handled by kvm_is_valid_cr4().
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07003228 */
3229 if ((cr4 & X86_CR4_VMXE) && is_smm(vcpu))
3230 return false;
3231
3232 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
3233 return false;
3234
3235 return true;
3236}
3237
3238void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239{
Jim Mattson2259c172020-10-29 10:06:48 -07003240 unsigned long old_cr4 = vcpu->arch.cr4;
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003241 struct vcpu_vmx *vmx = to_vmx(vcpu);
Ben Serebrin085e68e2015-04-16 11:58:05 -07003242 /*
3243 * Pass through host's Machine Check Enable value to hw_cr4, which
3244 * is in force while we are in guest mode. Do not let guests control
3245 * this bit, even if host CR4.MCE == 0.
3246 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003247 unsigned long hw_cr4;
3248
3249 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003250 if (is_unrestricted_guest(vcpu))
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003251 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003252 else if (vmx->rmode.vm86_active)
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003253 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3254 else
3255 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003256
Sean Christopherson64f7a112018-04-30 10:01:06 -07003257 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3258 if (cr4 & X86_CR4_UMIP) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003259 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07003260 hw_cr4 &= ~X86_CR4_UMIP;
3261 } else if (!is_guest_mode(vcpu) ||
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003262 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
3263 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
3264 }
Sean Christopherson64f7a112018-04-30 10:01:06 -07003265 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02003266
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003267 vcpu->arch.cr4 = cr4;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07003268 kvm_register_mark_available(vcpu, VCPU_EXREG_CR4);
Sheng Yang14394422008-04-28 12:24:45 +08003269
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003270 if (!is_unrestricted_guest(vcpu)) {
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003271 if (enable_ept) {
3272 if (!is_paging(vcpu)) {
3273 hw_cr4 &= ~X86_CR4_PAE;
3274 hw_cr4 |= X86_CR4_PSE;
3275 } else if (!(cr4 & X86_CR4_PAE)) {
3276 hw_cr4 &= ~X86_CR4_PAE;
3277 }
3278 }
3279
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003280 /*
Huaitong Handdba2622016-03-22 16:51:15 +08003281 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3282 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3283 * to be manually disabled when guest switches to non-paging
3284 * mode.
3285 *
3286 * If !enable_unrestricted_guest, the CPU is always running
3287 * with CR0.PG=1 and CR4 needs to be modified.
3288 * If enable_unrestricted_guest, the CPU automatically
3289 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003290 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003291 if (!is_paging(vcpu))
3292 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3293 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003294
Sheng Yang14394422008-04-28 12:24:45 +08003295 vmcs_writel(CR4_READ_SHADOW, cr4);
3296 vmcs_writel(GUEST_CR4, hw_cr4);
Jim Mattson2259c172020-10-29 10:06:48 -07003297
3298 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
3299 kvm_update_cpuid_runtime(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003300}
3301
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003302void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303{
Avi Kivitya9179492011-01-03 14:28:52 +02003304 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305 u32 ar;
3306
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003307 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003308 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003309 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003310 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003311 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003312 var->base = vmx_read_guest_seg_base(vmx, seg);
3313 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3314 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003315 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003316 var->base = vmx_read_guest_seg_base(vmx, seg);
3317 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3318 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3319 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003320 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 var->type = ar & 15;
3322 var->s = (ar >> 4) & 1;
3323 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003324 /*
3325 * Some userspaces do not preserve unusable property. Since usable
3326 * segment has to be present according to VMX spec we can use present
3327 * property to amend userspace bug by making unusable segment always
3328 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3329 * segment as unusable.
3330 */
3331 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332 var->avl = (ar >> 12) & 1;
3333 var->l = (ar >> 13) & 1;
3334 var->db = (ar >> 14) & 1;
3335 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003336}
3337
Avi Kivitya9179492011-01-03 14:28:52 +02003338static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3339{
Avi Kivitya9179492011-01-03 14:28:52 +02003340 struct kvm_segment s;
3341
3342 if (to_vmx(vcpu)->rmode.vm86_active) {
3343 vmx_get_segment(vcpu, &s, seg);
3344 return s.base;
3345 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003346 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003347}
3348
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003349int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003350{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003351 struct vcpu_vmx *vmx = to_vmx(vcpu);
3352
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003353 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003354 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003355 else {
3356 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003357 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003358 }
Avi Kivity69c73022011-03-07 15:26:44 +02003359}
3360
Avi Kivity653e3102007-05-07 10:55:37 +03003361static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363 u32 ar;
3364
Avi Kivityf0495f92012-06-07 17:06:10 +03003365 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366 ar = 1 << 16;
3367 else {
3368 ar = var->type & 15;
3369 ar |= (var->s & 1) << 4;
3370 ar |= (var->dpl & 3) << 5;
3371 ar |= (var->present & 1) << 7;
3372 ar |= (var->avl & 1) << 12;
3373 ar |= (var->l & 1) << 13;
3374 ar |= (var->db & 1) << 14;
3375 ar |= (var->g & 1) << 15;
3376 }
Avi Kivity653e3102007-05-07 10:55:37 +03003377
3378 return ar;
3379}
3380
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003381void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03003382{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003383 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003384 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003385
Avi Kivity2fb92db2011-04-27 19:42:18 +03003386 vmx_segment_cache_clear(vmx);
3387
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003388 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3389 vmx->rmode.segs[seg] = *var;
3390 if (seg == VCPU_SREG_TR)
3391 vmcs_write16(sf->selector, var->selector);
3392 else if (var->s)
3393 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003394 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003395 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003396
Avi Kivity653e3102007-05-07 10:55:37 +03003397 vmcs_writel(sf->base, var->base);
3398 vmcs_write32(sf->limit, var->limit);
3399 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003400
3401 /*
3402 * Fix the "Accessed" bit in AR field of segment registers for older
3403 * qemu binaries.
3404 * IA32 arch specifies that at the time of processor reset the
3405 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003406 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003407 * state vmexit when "unrestricted guest" mode is turned on.
3408 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3409 * tree. Newer qemu binaries with that qemu fix would not need this
3410 * kvm hack.
3411 */
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003412 if (is_unrestricted_guest(vcpu) && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003413 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003414
Gleb Natapovf924d662012-12-12 19:10:55 +02003415 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003416
3417out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003418 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419}
3420
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3422{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003423 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424
3425 *db = (ar >> 14) & 1;
3426 *l = (ar >> 13) & 1;
3427}
3428
Gleb Natapov89a27f42010-02-16 10:51:48 +02003429static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003431 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3432 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433}
3434
Gleb Natapov89a27f42010-02-16 10:51:48 +02003435static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003437 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3438 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439}
3440
Gleb Natapov89a27f42010-02-16 10:51:48 +02003441static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003443 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3444 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445}
3446
Gleb Natapov89a27f42010-02-16 10:51:48 +02003447static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003449 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3450 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003451}
3452
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003453static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3454{
3455 struct kvm_segment var;
3456 u32 ar;
3457
3458 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003459 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003460 if (seg == VCPU_SREG_CS)
3461 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003462 ar = vmx_segment_access_rights(&var);
3463
3464 if (var.base != (var.selector << 4))
3465 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003466 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003467 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003468 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003469 return false;
3470
3471 return true;
3472}
3473
3474static bool code_segment_valid(struct kvm_vcpu *vcpu)
3475{
3476 struct kvm_segment cs;
3477 unsigned int cs_rpl;
3478
3479 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003480 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003481
Avi Kivity1872a3f2009-01-04 23:26:52 +02003482 if (cs.unusable)
3483 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003484 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003485 return false;
3486 if (!cs.s)
3487 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003488 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003489 if (cs.dpl > cs_rpl)
3490 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003491 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003492 if (cs.dpl != cs_rpl)
3493 return false;
3494 }
3495 if (!cs.present)
3496 return false;
3497
3498 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3499 return true;
3500}
3501
3502static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3503{
3504 struct kvm_segment ss;
3505 unsigned int ss_rpl;
3506
3507 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003508 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003509
Avi Kivity1872a3f2009-01-04 23:26:52 +02003510 if (ss.unusable)
3511 return true;
3512 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003513 return false;
3514 if (!ss.s)
3515 return false;
3516 if (ss.dpl != ss_rpl) /* DPL != RPL */
3517 return false;
3518 if (!ss.present)
3519 return false;
3520
3521 return true;
3522}
3523
3524static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3525{
3526 struct kvm_segment var;
3527 unsigned int rpl;
3528
3529 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03003530 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003531
Avi Kivity1872a3f2009-01-04 23:26:52 +02003532 if (var.unusable)
3533 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003534 if (!var.s)
3535 return false;
3536 if (!var.present)
3537 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003538 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003539 if (var.dpl < rpl) /* DPL < RPL */
3540 return false;
3541 }
3542
3543 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3544 * rights flags
3545 */
3546 return true;
3547}
3548
3549static bool tr_valid(struct kvm_vcpu *vcpu)
3550{
3551 struct kvm_segment tr;
3552
3553 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3554
Avi Kivity1872a3f2009-01-04 23:26:52 +02003555 if (tr.unusable)
3556 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03003557 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003558 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003559 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003560 return false;
3561 if (!tr.present)
3562 return false;
3563
3564 return true;
3565}
3566
3567static bool ldtr_valid(struct kvm_vcpu *vcpu)
3568{
3569 struct kvm_segment ldtr;
3570
3571 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3572
Avi Kivity1872a3f2009-01-04 23:26:52 +02003573 if (ldtr.unusable)
3574 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03003575 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003576 return false;
3577 if (ldtr.type != 2)
3578 return false;
3579 if (!ldtr.present)
3580 return false;
3581
3582 return true;
3583}
3584
3585static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3586{
3587 struct kvm_segment cs, ss;
3588
3589 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3590 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3591
Nadav Amitb32a9912015-03-29 16:33:04 +03003592 return ((cs.selector & SEGMENT_RPL_MASK) ==
3593 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003594}
3595
3596/*
3597 * Check if guest state is valid. Returns true if valid, false if
3598 * not.
3599 * We assume that registers are always usable
3600 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07003601bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003602{
3603 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003604 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003605 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3606 return false;
3607 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3608 return false;
3609 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3610 return false;
3611 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3612 return false;
3613 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3614 return false;
3615 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3616 return false;
3617 } else {
3618 /* protected mode guest state checks */
3619 if (!cs_ss_rpl_check(vcpu))
3620 return false;
3621 if (!code_segment_valid(vcpu))
3622 return false;
3623 if (!stack_segment_valid(vcpu))
3624 return false;
3625 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3626 return false;
3627 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3628 return false;
3629 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3630 return false;
3631 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3632 return false;
3633 if (!tr_valid(vcpu))
3634 return false;
3635 if (!ldtr_valid(vcpu))
3636 return false;
3637 }
3638 /* TODO:
3639 * - Add checks on RIP
3640 * - Add checks on RFLAGS
3641 */
3642
3643 return true;
3644}
3645
Peter Xuff5a9832020-09-30 21:20:33 -04003646static int init_rmode_tss(struct kvm *kvm, void __user *ua)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003647{
Peter Xuff5a9832020-09-30 21:20:33 -04003648 const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
3649 u16 data;
3650 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003651
Peter Xuff5a9832020-09-30 21:20:33 -04003652 for (i = 0; i < 3; i++) {
3653 if (__copy_to_user(ua + PAGE_SIZE * i, zero_page, PAGE_SIZE))
3654 return -EFAULT;
3655 }
3656
Izik Eidus195aefd2007-10-01 22:14:18 +02003657 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Peter Xuff5a9832020-09-30 21:20:33 -04003658 if (__copy_to_user(ua + TSS_IOPB_BASE_OFFSET, &data, sizeof(u16)))
3659 return -EFAULT;
3660
Izik Eidus195aefd2007-10-01 22:14:18 +02003661 data = ~0;
Peter Xuff5a9832020-09-30 21:20:33 -04003662 if (__copy_to_user(ua + RMODE_TSS_SIZE - 1, &data, sizeof(u8)))
3663 return -EFAULT;
3664
3665 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003666}
3667
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003668static int init_rmode_identity_map(struct kvm *kvm)
3669{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003670 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Peter Xu2a5755b2020-01-09 09:57:14 -05003671 int i, r = 0;
Peter Xuff5a9832020-09-30 21:20:33 -04003672 void __user *uaddr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003673 u32 tmp;
3674
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003675 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003676 mutex_lock(&kvm->slots_lock);
3677
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003678 if (likely(kvm_vmx->ept_identity_pagetable_done))
Peter Xu2a5755b2020-01-09 09:57:14 -05003679 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003680
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003681 if (!kvm_vmx->ept_identity_map_addr)
3682 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chena255d472014-09-16 18:41:58 +08003683
Peter Xuff5a9832020-09-30 21:20:33 -04003684 uaddr = __x86_set_memory_region(kvm,
3685 IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
3686 kvm_vmx->ept_identity_map_addr,
3687 PAGE_SIZE);
3688 if (IS_ERR(uaddr)) {
3689 r = PTR_ERR(uaddr);
Peter Xu2a5755b2020-01-09 09:57:14 -05003690 goto out;
Peter Xuff5a9832020-09-30 21:20:33 -04003691 }
Tang Chena255d472014-09-16 18:41:58 +08003692
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003693 /* Set up identity-mapping pagetable for EPT in real mode */
3694 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3695 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3696 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
Peter Xuff5a9832020-09-30 21:20:33 -04003697 if (__copy_to_user(uaddr + i * sizeof(tmp), &tmp, sizeof(tmp))) {
3698 r = -EFAULT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003699 goto out;
Peter Xuff5a9832020-09-30 21:20:33 -04003700 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003701 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003702 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003703
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003704out:
Tang Chena255d472014-09-16 18:41:58 +08003705 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003706 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003707}
3708
Avi Kivity6aa8b732006-12-10 02:21:36 -08003709static void seg_setup(int seg)
3710{
Mathias Krause772e0312012-08-30 01:30:19 +02003711 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003712 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003713
3714 vmcs_write16(sf->selector, 0);
3715 vmcs_writel(sf->base, 0);
3716 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003717 ar = 0x93;
3718 if (seg == VCPU_SREG_CS)
3719 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003720
3721 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003722}
3723
Sheng Yangf78e0e22007-10-29 09:40:42 +08003724static int alloc_apic_access_page(struct kvm *kvm)
3725{
Xiao Guangrong44841412012-09-07 14:14:20 +08003726 struct page *page;
Peter Xuff5a9832020-09-30 21:20:33 -04003727 void __user *hva;
3728 int ret = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003729
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003730 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003731 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003732 goto out;
Peter Xuff5a9832020-09-30 21:20:33 -04003733 hva = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3734 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
3735 if (IS_ERR(hva)) {
3736 ret = PTR_ERR(hva);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003737 goto out;
Peter Xuff5a9832020-09-30 21:20:33 -04003738 }
Izik Eidus72dc67a2008-02-10 18:04:15 +02003739
Tang Chen73a6d942014-09-11 13:38:00 +08003740 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003741 if (is_error_page(page)) {
Peter Xuff5a9832020-09-30 21:20:33 -04003742 ret = -EFAULT;
Xiao Guangrong44841412012-09-07 14:14:20 +08003743 goto out;
3744 }
3745
Tang Chenc24ae0d2014-09-24 15:57:58 +08003746 /*
3747 * Do not pin the page in memory, so that memory hot-unplug
3748 * is able to migrate it.
3749 */
3750 put_page(page);
3751 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003752out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003753 mutex_unlock(&kvm->slots_lock);
Peter Xuff5a9832020-09-30 21:20:33 -04003754 return ret;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003755}
3756
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003757int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003758{
3759 int vpid;
3760
Avi Kivity919818a2009-03-23 18:01:29 +02003761 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003762 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003763 spin_lock(&vmx_vpid_lock);
3764 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003765 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003766 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003767 else
3768 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003769 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003770 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003771}
3772
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003773void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003774{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003775 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003776 return;
3777 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003778 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003779 spin_unlock(&vmx_vpid_lock);
3780}
3781
Alexander Graf3eb90012020-09-25 16:34:20 +02003782static void vmx_clear_msr_bitmap_read(ulong *msr_bitmap, u32 msr)
3783{
3784 int f = sizeof(unsigned long);
3785
3786 if (msr <= 0x1fff)
3787 __clear_bit(msr, msr_bitmap + 0x000 / f);
3788 else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff))
3789 __clear_bit(msr & 0x1fff, msr_bitmap + 0x400 / f);
3790}
3791
3792static void vmx_clear_msr_bitmap_write(ulong *msr_bitmap, u32 msr)
3793{
3794 int f = sizeof(unsigned long);
3795
3796 if (msr <= 0x1fff)
3797 __clear_bit(msr, msr_bitmap + 0x800 / f);
3798 else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff))
3799 __clear_bit(msr & 0x1fff, msr_bitmap + 0xc00 / f);
3800}
3801
3802static void vmx_set_msr_bitmap_read(ulong *msr_bitmap, u32 msr)
3803{
3804 int f = sizeof(unsigned long);
3805
3806 if (msr <= 0x1fff)
3807 __set_bit(msr, msr_bitmap + 0x000 / f);
3808 else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff))
3809 __set_bit(msr & 0x1fff, msr_bitmap + 0x400 / f);
3810}
3811
3812static void vmx_set_msr_bitmap_write(ulong *msr_bitmap, u32 msr)
3813{
3814 int f = sizeof(unsigned long);
3815
3816 if (msr <= 0x1fff)
3817 __set_bit(msr, msr_bitmap + 0x800 / f);
3818 else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff))
3819 __set_bit(msr & 0x1fff, msr_bitmap + 0xc00 / f);
3820}
3821
Sean Christophersone23f6d42021-04-23 15:19:12 -07003822void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003823{
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003824 struct vcpu_vmx *vmx = to_vmx(vcpu);
3825 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
Sheng Yang25c5f222008-03-28 13:18:56 +08003826
3827 if (!cpu_has_vmx_msr_bitmap())
3828 return;
3829
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003830 if (static_branch_unlikely(&enable_evmcs))
3831 evmcs_touch_msr_bitmap();
3832
Sheng Yang25c5f222008-03-28 13:18:56 +08003833 /*
Alexander Graf3eb90012020-09-25 16:34:20 +02003834 * Mark the desired intercept state in shadow bitmap, this is needed
3835 * for resync when the MSR filters change.
3836 */
3837 if (is_valid_passthrough_msr(msr)) {
3838 int idx = possible_passthrough_msr_slot(msr);
Yang Zhang8d146952013-01-25 10:18:50 +08003839
Alexander Graf3eb90012020-09-25 16:34:20 +02003840 if (idx != -ENOENT) {
3841 if (type & MSR_TYPE_R)
3842 clear_bit(idx, vmx->shadow_msr_intercept.read);
3843 if (type & MSR_TYPE_W)
3844 clear_bit(idx, vmx->shadow_msr_intercept.write);
3845 }
Yang Zhang8d146952013-01-25 10:18:50 +08003846 }
Alexander Graf3eb90012020-09-25 16:34:20 +02003847
3848 if ((type & MSR_TYPE_R) &&
3849 !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) {
3850 vmx_set_msr_bitmap_read(msr_bitmap, msr);
3851 type &= ~MSR_TYPE_R;
3852 }
3853
3854 if ((type & MSR_TYPE_W) &&
3855 !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) {
3856 vmx_set_msr_bitmap_write(msr_bitmap, msr);
3857 type &= ~MSR_TYPE_W;
3858 }
3859
3860 if (type & MSR_TYPE_R)
3861 vmx_clear_msr_bitmap_read(msr_bitmap, msr);
3862
3863 if (type & MSR_TYPE_W)
3864 vmx_clear_msr_bitmap_write(msr_bitmap, msr);
Yang Zhang8d146952013-01-25 10:18:50 +08003865}
3866
Sean Christophersone23f6d42021-04-23 15:19:12 -07003867void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003868{
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003869 struct vcpu_vmx *vmx = to_vmx(vcpu);
3870 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003871
3872 if (!cpu_has_vmx_msr_bitmap())
3873 return;
3874
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003875 if (static_branch_unlikely(&enable_evmcs))
3876 evmcs_touch_msr_bitmap();
3877
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003878 /*
Alexander Graf3eb90012020-09-25 16:34:20 +02003879 * Mark the desired intercept state in shadow bitmap, this is needed
3880 * for resync when the MSR filter changes.
3881 */
3882 if (is_valid_passthrough_msr(msr)) {
3883 int idx = possible_passthrough_msr_slot(msr);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003884
Alexander Graf3eb90012020-09-25 16:34:20 +02003885 if (idx != -ENOENT) {
3886 if (type & MSR_TYPE_R)
3887 set_bit(idx, vmx->shadow_msr_intercept.read);
3888 if (type & MSR_TYPE_W)
3889 set_bit(idx, vmx->shadow_msr_intercept.write);
3890 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003891 }
Alexander Graf3eb90012020-09-25 16:34:20 +02003892
3893 if (type & MSR_TYPE_R)
3894 vmx_set_msr_bitmap_read(msr_bitmap, msr);
3895
3896 if (type & MSR_TYPE_W)
3897 vmx_set_msr_bitmap_write(msr_bitmap, msr);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003898}
3899
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003900static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003901{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003902 u8 mode = 0;
3903
3904 if (cpu_has_secondary_exec_ctrls() &&
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003905 (secondary_exec_controls_get(to_vmx(vcpu)) &
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003906 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3907 mode |= MSR_BITMAP_MODE_X2APIC;
3908 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3909 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3910 }
3911
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003912 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003913}
3914
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003915static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003916{
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003917 unsigned long *msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
3918 unsigned long read_intercept;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003919 int msr;
3920
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003921 read_intercept = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003922
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003923 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3924 unsigned int read_idx = msr / BITS_PER_LONG;
3925 unsigned int write_idx = read_idx + (0x800 / sizeof(long));
3926
3927 msr_bitmap[read_idx] = read_intercept;
3928 msr_bitmap[write_idx] = ~0ul;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003929 }
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003930}
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003931
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003932static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu, u8 mode)
3933{
3934 if (!cpu_has_vmx_msr_bitmap())
3935 return;
3936
3937 vmx_reset_x2apic_msrs(vcpu, mode);
3938
3939 /*
3940 * TPR reads and writes can be virtualized even if virtual interrupt
3941 * delivery is not in use.
3942 */
3943 vmx_set_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW,
3944 !(mode & MSR_BITMAP_MODE_X2APIC));
3945
3946 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
3947 vmx_enable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_RW);
3948 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3949 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003950 }
3951}
3952
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003953void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003954{
3955 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003956 u8 mode = vmx_msr_bitmap_mode(vcpu);
3957 u8 changed = mode ^ vmx->msr_bitmap_mode;
3958
3959 if (!changed)
3960 return;
3961
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003962 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
Alexander Graf3eb90012020-09-25 16:34:20 +02003963 vmx_update_msr_bitmap_x2apic(vcpu, mode);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003964
3965 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003966}
3967
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003968void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu)
Chao Pengb08c2892018-10-24 16:05:15 +08003969{
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003970 struct vcpu_vmx *vmx = to_vmx(vcpu);
Chao Pengb08c2892018-10-24 16:05:15 +08003971 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
3972 u32 i;
3973
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003974 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_STATUS, MSR_TYPE_RW, flag);
3975 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_BASE, MSR_TYPE_RW, flag);
3976 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_MASK, MSR_TYPE_RW, flag);
3977 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_CR3_MATCH, MSR_TYPE_RW, flag);
Chao Pengb08c2892018-10-24 16:05:15 +08003978 for (i = 0; i < vmx->pt_desc.addr_range; i++) {
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003979 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
3980 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
Chao Pengb08c2892018-10-24 16:05:15 +08003981 }
3982}
3983
Liran Alone6c67d82018-09-04 10:56:52 +03003984static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3985{
3986 struct vcpu_vmx *vmx = to_vmx(vcpu);
3987 void *vapic_page;
3988 u32 vppr;
3989 int rvi;
3990
3991 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3992 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003993 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
Liran Alone6c67d82018-09-04 10:56:52 +03003994 return false;
3995
Paolo Bonzini7e712682018-10-03 13:44:26 +02003996 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003997
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003998 vapic_page = vmx->nested.virtual_apic_map.hva;
Liran Alone6c67d82018-09-04 10:56:52 +03003999 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
Liran Alone6c67d82018-09-04 10:56:52 +03004000
4001 return ((rvi & 0xf0) > (vppr & 0xf0));
4002}
4003
Alexander Graf3eb90012020-09-25 16:34:20 +02004004static void vmx_msr_filter_changed(struct kvm_vcpu *vcpu)
4005{
4006 struct vcpu_vmx *vmx = to_vmx(vcpu);
4007 u32 i;
4008
4009 /*
4010 * Set intercept permissions for all potentially passed through MSRs
4011 * again. They will automatically get filtered through the MSR filter,
4012 * so we are back in sync after this.
4013 */
4014 for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++) {
4015 u32 msr = vmx_possible_passthrough_msrs[i];
4016 bool read = test_bit(i, vmx->shadow_msr_intercept.read);
4017 bool write = test_bit(i, vmx->shadow_msr_intercept.write);
4018
4019 vmx_set_intercept_for_msr(vcpu, msr, MSR_TYPE_R, read);
4020 vmx_set_intercept_for_msr(vcpu, msr, MSR_TYPE_W, write);
4021 }
4022
4023 pt_update_intercept_for_msr(vcpu);
4024 vmx_update_msr_bitmap_x2apic(vcpu, vmx_msr_bitmap_mode(vcpu));
4025}
4026
Wincy Van06a55242017-04-28 13:13:59 +08004027static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
4028 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004029{
4030#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08004031 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
4032
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004033 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004034 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08004035 * The vector of interrupt to be delivered to vcpu had
4036 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08004037 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08004038 * Following cases will be reached in this block, and
4039 * we always send a notification event in all cases as
4040 * explained below.
4041 *
4042 * Case 1: vcpu keeps in non-root mode. Sending a
4043 * notification event posts the interrupt to vcpu.
4044 *
4045 * Case 2: vcpu exits to root mode and is still
4046 * runnable. PIR will be synced to vIRR before the
4047 * next vcpu entry. Sending a notification event in
4048 * this case has no effect, as vcpu is not in root
4049 * mode.
4050 *
4051 * Case 3: vcpu exits to root mode and is blocked.
4052 * vcpu_block() has already synced PIR to vIRR and
4053 * never blocks vcpu if vIRR is not cleared. Therefore,
4054 * a blocked vcpu here does not wait for any requested
4055 * interrupts in PIR, and sending a notification event
4056 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08004057 */
Feng Wu28b835d2015-09-18 22:29:54 +08004058
Wincy Van06a55242017-04-28 13:13:59 +08004059 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004060 return true;
4061 }
4062#endif
4063 return false;
4064}
4065
Wincy Van705699a2015-02-03 23:58:17 +08004066static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4067 int vector)
4068{
4069 struct vcpu_vmx *vmx = to_vmx(vcpu);
4070
4071 if (is_guest_mode(vcpu) &&
4072 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08004073 /*
4074 * If a posted intr is not recognized by hardware,
4075 * we will accomplish it in the next vmentry.
4076 */
4077 vmx->nested.pi_pending = true;
4078 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02004079 /* the PIR and ON have been set by L1. */
4080 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
4081 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004082 return 0;
4083 }
4084 return -1;
4085}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004087 * Send interrupt to vcpu via posted interrupt way.
4088 * 1. If target vcpu is running(non-root mode), send posted interrupt
4089 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4090 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4091 * interrupt from PIR in next vmentry.
4092 */
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004093static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
Yang Zhanga20ed542013-04-11 19:25:15 +08004094{
4095 struct vcpu_vmx *vmx = to_vmx(vcpu);
4096 int r;
4097
Wincy Van705699a2015-02-03 23:58:17 +08004098 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4099 if (!r)
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004100 return 0;
4101
4102 if (!vcpu->arch.apicv_active)
4103 return -1;
Wincy Van705699a2015-02-03 23:58:17 +08004104
Yang Zhanga20ed542013-04-11 19:25:15 +08004105 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004106 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08004107
Paolo Bonzinib95234c2016-12-19 13:57:33 +01004108 /* If a previous notification has sent the IPI, nothing to do. */
4109 if (pi_test_and_set_on(&vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004110 return 0;
Paolo Bonzinib95234c2016-12-19 13:57:33 +01004111
Wanpeng Li379a3c82020-04-28 14:23:27 +08004112 if (vcpu != kvm_get_running_vcpu() &&
4113 !kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08004114 kvm_vcpu_kick(vcpu);
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004115
4116 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08004117}
4118
Avi Kivity6aa8b732006-12-10 02:21:36 -08004119/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004120 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4121 * will not change in the lifetime of the guest.
4122 * Note that host-state that does change is set elsewhere. E.g., host-state
4123 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4124 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004125void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004126{
4127 u32 low32, high32;
4128 unsigned long tmpl;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004129 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004130
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07004131 cr0 = read_cr0();
4132 WARN_ON(cr0 & X86_CR0_TS);
4133 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004134
4135 /*
4136 * Save the most likely value for this task's CR3 in the VMCS.
4137 * We can't use __get_current_cr3_fast() because we're not atomic.
4138 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07004139 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004140 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07004141 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004142
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004143 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004144 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004145 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07004146 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004147
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004148 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004149#ifdef CONFIG_X86_64
4150 /*
4151 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07004152 * vmx_prepare_switch_to_host(), in case userspace uses
4153 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03004154 */
4155 vmcs_write16(HOST_DS_SELECTOR, 0);
4156 vmcs_write16(HOST_ES_SELECTOR, 0);
4157#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004158 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4159 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004160#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004161 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4162 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4163
Sean Christopherson23420802019-04-19 22:50:57 -07004164 vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004165
Sean Christopherson453eafb2018-12-20 12:25:17 -08004166 vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004167
4168 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4169 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4170 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4171 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4172
4173 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4174 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4175 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4176 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004177
Sean Christophersonc73da3f2018-12-03 13:53:00 -08004178 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004179 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004180}
4181
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004182void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004183{
Sean Christopherson2ed41aa2020-09-29 21:16:58 -07004184 struct kvm_vcpu *vcpu = &vmx->vcpu;
4185
4186 vcpu->arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS &
4187 ~vcpu->arch.cr4_guest_rsvd_bits;
Sean Christophersonfa71e952020-07-02 21:04:22 -07004188 if (!enable_ept)
Sean Christopherson2ed41aa2020-09-29 21:16:58 -07004189 vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004190 if (is_guest_mode(&vmx->vcpu))
Sean Christopherson2ed41aa2020-09-29 21:16:58 -07004191 vcpu->arch.cr4_guest_owned_bits &=
4192 ~get_vmcs12(vcpu)->cr4_guest_host_mask;
4193 vmcs_writel(CR4_GUEST_HOST_MASK, ~vcpu->arch.cr4_guest_owned_bits);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004194}
4195
Sean Christophersonc075c3e2019-05-07 12:17:53 -07004196u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
Yang Zhang01e439b2013-04-11 19:25:12 +08004197{
4198 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4199
Andrey Smetanind62caab2015-11-10 15:36:33 +03004200 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004201 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004202
4203 if (!enable_vnmi)
4204 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
4205
Sean Christopherson804939e2019-05-07 12:18:05 -07004206 if (!enable_preemption_timer)
4207 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4208
Yang Zhang01e439b2013-04-11 19:25:12 +08004209 return pin_based_exec_ctrl;
4210}
4211
Andrey Smetanind62caab2015-11-10 15:36:33 +03004212static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4213{
4214 struct vcpu_vmx *vmx = to_vmx(vcpu);
4215
Sean Christophersonc5f2c762019-05-07 12:17:55 -07004216 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004217 if (cpu_has_secondary_exec_ctrls()) {
4218 if (kvm_vcpu_apicv_active(vcpu))
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004219 secondary_exec_controls_setbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004220 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4221 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4222 else
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004223 secondary_exec_controls_clearbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004224 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4225 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4226 }
4227
4228 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004229 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004230}
4231
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004232u32 vmx_exec_control(struct vcpu_vmx *vmx)
4233{
4234 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4235
4236 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4237 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4238
4239 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4240 exec_control &= ~CPU_BASED_TPR_SHADOW;
4241#ifdef CONFIG_X86_64
4242 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4243 CPU_BASED_CR8_LOAD_EXITING;
4244#endif
4245 }
4246 if (!enable_ept)
4247 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4248 CPU_BASED_CR3_LOAD_EXITING |
4249 CPU_BASED_INVLPG_EXITING;
4250 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4251 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4252 CPU_BASED_MONITOR_EXITING);
4253 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4254 exec_control &= ~CPU_BASED_HLT_EXITING;
4255 return exec_control;
4256}
4257
Sean Christopherson8b50b922020-09-24 17:30:11 -07004258/*
4259 * Adjust a single secondary execution control bit to intercept/allow an
4260 * instruction in the guest. This is usually done based on whether or not a
4261 * feature has been exposed to the guest in order to correctly emulate faults.
4262 */
4263static inline void
4264vmx_adjust_secondary_exec_control(struct vcpu_vmx *vmx, u32 *exec_control,
4265 u32 control, bool enabled, bool exiting)
4266{
4267 /*
4268 * If the control is for an opt-in feature, clear the control if the
4269 * feature is not exposed to the guest, i.e. not enabled. If the
4270 * control is opt-out, i.e. an exiting control, clear the control if
4271 * the feature _is_ exposed to the guest, i.e. exiting/interception is
4272 * disabled for the associated instruction. Note, the caller is
4273 * responsible presetting exec_control to set all supported bits.
4274 */
4275 if (enabled == exiting)
4276 *exec_control &= ~control;
4277
4278 /*
4279 * Update the nested MSR settings so that a nested VMM can/can't set
4280 * controls for features that are/aren't exposed to the guest.
4281 */
4282 if (nested) {
4283 if (enabled)
4284 vmx->nested.msrs.secondary_ctls_high |= control;
4285 else
4286 vmx->nested.msrs.secondary_ctls_high &= ~control;
4287 }
4288}
4289
4290/*
4291 * Wrapper macro for the common case of adjusting a secondary execution control
4292 * based on a single guest CPUID bit, with a dedicated feature bit. This also
4293 * verifies that the control is actually supported by KVM and hardware.
4294 */
4295#define vmx_adjust_sec_exec_control(vmx, exec_control, name, feat_name, ctrl_name, exiting) \
4296({ \
4297 bool __enabled; \
4298 \
4299 if (cpu_has_vmx_##name()) { \
4300 __enabled = guest_cpuid_has(&(vmx)->vcpu, \
4301 X86_FEATURE_##feat_name); \
4302 vmx_adjust_secondary_exec_control(vmx, exec_control, \
4303 SECONDARY_EXEC_##ctrl_name, __enabled, exiting); \
4304 } \
4305})
4306
4307/* More macro magic for ENABLE_/opt-in versus _EXITING/opt-out controls. */
4308#define vmx_adjust_sec_exec_feature(vmx, exec_control, lname, uname) \
4309 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, ENABLE_##uname, false)
4310
4311#define vmx_adjust_sec_exec_exiting(vmx, exec_control, lname, uname) \
4312 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, uname##_EXITING, true)
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004313
Paolo Bonzini80154d72017-08-24 13:55:35 +02004314static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004315{
Paolo Bonzini80154d72017-08-24 13:55:35 +02004316 struct kvm_vcpu *vcpu = &vmx->vcpu;
4317
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004318 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004319
Sean Christopherson2ef76192020-03-02 15:56:22 -08004320 if (vmx_pt_mode_is_system())
Chao Pengf99e3da2018-10-24 16:05:10 +08004321 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004322 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004323 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4324 if (vmx->vpid == 0)
4325 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4326 if (!enable_ept) {
4327 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4328 enable_unrestricted_guest = 0;
4329 }
4330 if (!enable_unrestricted_guest)
4331 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004332 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004333 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02004334 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004335 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4336 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004337 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004338
4339 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4340 * in vmx_set_cr4. */
4341 exec_control &= ~SECONDARY_EXEC_DESC;
4342
Abel Gordonabc4fc52013-04-18 14:35:25 +03004343 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4344 (handle_vmptrld).
4345 We can NOT enable shadow_vmcs here because we don't have yet
4346 a current VMCS12
4347 */
4348 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004349
Makarand Sonarea85863c2021-02-12 16:50:12 -08004350 /*
4351 * PML is enabled/disabled when dirty logging of memsmlots changes, but
4352 * it needs to be set here when dirty logging is already active, e.g.
4353 * if this vCPU was created after dirty logging was enabled.
4354 */
4355 if (!vcpu->kvm->arch.cpu_dirty_logging_count)
Kai Huanga3eaa862015-11-04 13:46:05 +08004356 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004357
Sean Christophersonbecdad82020-09-23 09:50:45 -07004358 if (cpu_has_vmx_xsaves()) {
Paolo Bonzini3db13482017-08-24 14:48:03 +02004359 /* Exposing XSAVES only when XSAVE is exposed */
4360 bool xsaves_enabled =
Sean Christopherson96be4e02019-12-10 14:44:15 -08004361 boot_cpu_has(X86_FEATURE_XSAVE) &&
Paolo Bonzini3db13482017-08-24 14:48:03 +02004362 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4363 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4364
Aaron Lewis72041602019-10-21 16:30:20 -07004365 vcpu->arch.xsaves_enabled = xsaves_enabled;
4366
Sean Christopherson8b50b922020-09-24 17:30:11 -07004367 vmx_adjust_secondary_exec_control(vmx, &exec_control,
4368 SECONDARY_EXEC_XSAVES,
4369 xsaves_enabled, false);
Paolo Bonzini3db13482017-08-24 14:48:03 +02004370 }
4371
Sean Christopherson36fa06f2021-05-04 10:17:26 -07004372 /*
4373 * RDPID is also gated by ENABLE_RDTSCP, turn on the control if either
4374 * feature is exposed to the guest. This creates a virtualization hole
4375 * if both are supported in hardware but only one is exposed to the
4376 * guest, but letting the guest execute RDTSCP or RDPID when either one
4377 * is advertised is preferable to emulating the advertised instruction
4378 * in KVM on #UD, and obviously better than incorrectly injecting #UD.
4379 */
4380 if (cpu_has_vmx_rdtscp()) {
4381 bool rdpid_or_rdtscp_enabled =
4382 guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) ||
4383 guest_cpuid_has(vcpu, X86_FEATURE_RDPID);
4384
4385 vmx_adjust_secondary_exec_control(vmx, &exec_control,
4386 SECONDARY_EXEC_ENABLE_RDTSCP,
4387 rdpid_or_rdtscp_enabled, false);
4388 }
Sean Christopherson8b50b922020-09-24 17:30:11 -07004389 vmx_adjust_sec_exec_feature(vmx, &exec_control, invpcid, INVPCID);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004390
Sean Christopherson8b50b922020-09-24 17:30:11 -07004391 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdrand, RDRAND);
4392 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdseed, RDSEED);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004393
Sean Christopherson8b50b922020-09-24 17:30:11 -07004394 vmx_adjust_sec_exec_control(vmx, &exec_control, waitpkg, WAITPKG,
4395 ENABLE_USR_WAIT_PAUSE, false);
Tao Xue69e72fa2019-07-16 14:55:49 +08004396
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08004397 if (!vcpu->kvm->arch.bus_lock_detection_enabled)
4398 exec_control &= ~SECONDARY_EXEC_BUS_LOCK_DETECTION;
4399
Paolo Bonzini80154d72017-08-24 13:55:35 +02004400 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004401}
4402
Wanpeng Lif53cd632014-12-02 19:14:58 +08004403#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08004404
Sean Christopherson944c3462018-12-03 13:53:09 -08004405/*
Xiaoyao Li1b842922019-10-20 17:11:01 +08004406 * Noting that the initialization of Guest-state Area of VMCS is in
4407 * vmx_vcpu_reset().
Sean Christopherson944c3462018-12-03 13:53:09 -08004408 */
Xiaoyao Li1b842922019-10-20 17:11:01 +08004409static void init_vmcs(struct vcpu_vmx *vmx)
Sean Christopherson944c3462018-12-03 13:53:09 -08004410{
Sean Christopherson944c3462018-12-03 13:53:09 -08004411 if (nested)
Xiaoyao Li1b842922019-10-20 17:11:01 +08004412 nested_vmx_set_vmcs_shadowing_bitmap();
Sean Christopherson944c3462018-12-03 13:53:09 -08004413
Sheng Yang25c5f222008-03-28 13:18:56 +08004414 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004415 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08004416
Avi Kivity6aa8b732006-12-10 02:21:36 -08004417 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4418
Avi Kivity6aa8b732006-12-10 02:21:36 -08004419 /* Control */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004420 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004421
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004422 exec_controls_set(vmx, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004423
Dan Williamsdfa169b2016-06-02 11:17:24 -07004424 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004425 vmx_compute_secondary_exec_control(vmx);
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004426 secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07004427 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004428
Andrey Smetanind62caab2015-11-10 15:36:33 +03004429 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004430 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4431 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4432 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4433 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4434
4435 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004436
Li RongQing0bcf2612015-12-03 13:29:34 +08004437 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004438 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004439 }
4440
Wanpeng Lib31c1142018-03-12 04:53:04 -07004441 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004442 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004443 vmx->ple_window = ple_window;
4444 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004445 }
4446
Xiao Guangrongc3707952011-07-12 03:28:04 +08004447 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4448 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4450
Avi Kivity9581d442010-10-19 16:46:55 +02004451 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4452 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004453 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004454 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4455 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004456
Bandan Das2a499e42017-08-03 15:54:41 -04004457 if (cpu_has_vmx_vmfunc())
4458 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4459
Eddie Dong2cc51562007-05-21 07:28:09 +03004460 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4461 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004462 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03004463 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004464 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465
Radim Krčmář74545702015-04-27 15:11:25 +02004466 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4467 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004468
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004469 vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470
4471 /* 22.2.1, 20.8.1 */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004472 vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004473
Sean Christophersonfa71e952020-07-02 21:04:22 -07004474 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4475 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004476
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004477 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004478
Xiaoyao Li35fbe0d2019-10-20 17:10:58 +08004479 if (vmx->vpid != 0)
4480 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4481
Sean Christophersonbecdad82020-09-23 09:50:45 -07004482 if (cpu_has_vmx_xsaves())
Wanpeng Lif53cd632014-12-02 19:14:58 +08004483 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4484
Peter Feiner4e595162016-07-07 14:49:58 -07004485 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07004486 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4487 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4488 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07004489
Sean Christopherson72add912021-04-12 16:21:42 +12004490 vmx_write_encls_bitmap(&vmx->vcpu, NULL);
Chao Peng2ef444f2018-10-24 16:05:12 +08004491
Sean Christopherson2ef76192020-03-02 15:56:22 -08004492 if (vmx_pt_mode_is_host_guest()) {
Chao Peng2ef444f2018-10-24 16:05:12 +08004493 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
4494 /* Bit[6~0] are forced to 1, writes are ignored. */
4495 vmx->pt_desc.guest.output_mask = 0x7F;
4496 vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
4497 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004498}
4499
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004500static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004501{
4502 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004503 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004504 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004505
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004506 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004507 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004508
Tao Xu6e3ba4a2019-07-16 14:55:50 +08004509 vmx->msr_ia32_umwait_control = 0;
4510
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004511 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Wanpeng Li95c06542019-09-05 14:26:28 +08004512 vmx->hv_deadline_tsc = -1;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004513 kvm_set_cr8(vcpu, 0);
4514
4515 if (!init_event) {
4516 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4517 MSR_IA32_APICBASE_ENABLE;
4518 if (kvm_vcpu_is_reset_bsp(vcpu))
4519 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4520 apic_base_msr.host_initiated = true;
4521 kvm_set_apic_base(vcpu, &apic_base_msr);
4522 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004523
Avi Kivity2fb92db2011-04-27 19:42:18 +03004524 vmx_segment_cache_clear(vmx);
4525
Avi Kivity5706be02008-08-20 15:07:31 +03004526 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004527 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004528 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004529
4530 seg_setup(VCPU_SREG_DS);
4531 seg_setup(VCPU_SREG_ES);
4532 seg_setup(VCPU_SREG_FS);
4533 seg_setup(VCPU_SREG_GS);
4534 seg_setup(VCPU_SREG_SS);
4535
4536 vmcs_write16(GUEST_TR_SELECTOR, 0);
4537 vmcs_writel(GUEST_TR_BASE, 0);
4538 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4539 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4540
4541 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4542 vmcs_writel(GUEST_LDTR_BASE, 0);
4543 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4544 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4545
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004546 if (!init_event) {
4547 vmcs_write32(GUEST_SYSENTER_CS, 0);
4548 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4549 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4550 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4551 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004552
Wanpeng Lic37c2872017-11-20 14:52:21 -08004553 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01004554 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004555
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004556 vmcs_writel(GUEST_GDTR_BASE, 0);
4557 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4558
4559 vmcs_writel(GUEST_IDTR_BASE, 0);
4560 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4561
Anthony Liguori443381a2010-12-06 10:53:38 -06004562 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004563 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004564 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07004565 if (kvm_mpx_supported())
4566 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004567
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004568 setup_msrs(vmx);
4569
Avi Kivity6aa8b732006-12-10 02:21:36 -08004570 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4571
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004572 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004573 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004574 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004575 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004576 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004577 vmcs_write32(TPR_THRESHOLD, 0);
4578 }
4579
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004580 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004581
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004582 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004583 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06004584 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004585 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004586 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004587
Jason Baronb6a7cc32021-01-14 22:27:54 -05004588 vmx_update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004589
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004590 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004591 if (init_event)
4592 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004593}
4594
Jason Baronb6a7cc32021-01-14 22:27:54 -05004595static void vmx_enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004596{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08004597 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004598}
4599
Jason Baronb6a7cc32021-01-14 22:27:54 -05004600static void vmx_enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004601{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004602 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004603 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jason Baronb6a7cc32021-01-14 22:27:54 -05004604 vmx_enable_irq_window(vcpu);
Jan Kiszkac9a79532014-03-07 20:03:15 +01004605 return;
4606 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004607
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08004608 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004609}
4610
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004611static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004612{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004613 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004614 uint32_t intr;
4615 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004616
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004617 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004618
Avi Kivityfa89a812008-09-01 15:57:51 +03004619 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004620 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004621 int inc_eip = 0;
4622 if (vcpu->arch.interrupt.soft)
4623 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004624 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
Eddie Dong85f455f2007-07-06 12:20:49 +03004625 return;
4626 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004627 intr = irq | INTR_INFO_VALID_MASK;
4628 if (vcpu->arch.interrupt.soft) {
4629 intr |= INTR_TYPE_SOFT_INTR;
4630 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4631 vmx->vcpu.arch.event_exit_inst_len);
4632 } else
4633 intr |= INTR_TYPE_EXT_INTR;
4634 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004635
4636 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004637}
4638
Sheng Yangf08864b2008-05-15 18:23:25 +08004639static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4640{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004641 struct vcpu_vmx *vmx = to_vmx(vcpu);
4642
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004643 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004644 /*
4645 * Tracking the NMI-blocked state in software is built upon
4646 * finding the next open IRQ window. This, in turn, depends on
4647 * well-behaving guests: They have to keep IRQs disabled at
4648 * least as long as the NMI handler runs. Otherwise we may
4649 * cause NMI nesting, maybe breaking the guest. But as this is
4650 * highly unlikely, we can live with the residual risk.
4651 */
4652 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
4653 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4654 }
4655
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004656 ++vcpu->stat.nmi_injections;
4657 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004658
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004659 if (vmx->rmode.vm86_active) {
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004660 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004661 return;
4662 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08004663
Sheng Yangf08864b2008-05-15 18:23:25 +08004664 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4665 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004666
4667 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004668}
4669
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004670bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004671{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004672 struct vcpu_vmx *vmx = to_vmx(vcpu);
4673 bool masked;
4674
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004675 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004676 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004677 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02004678 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004679 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4680 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4681 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004682}
4683
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004684void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004685{
4686 struct vcpu_vmx *vmx = to_vmx(vcpu);
4687
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004688 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004689 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4690 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4691 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4692 }
4693 } else {
4694 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4695 if (masked)
4696 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4697 GUEST_INTR_STATE_NMI);
4698 else
4699 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4700 GUEST_INTR_STATE_NMI);
4701 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004702}
4703
Sean Christopherson1b660b62020-04-22 19:25:44 -07004704bool vmx_nmi_blocked(struct kvm_vcpu *vcpu)
4705{
4706 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
4707 return false;
4708
4709 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4710 return true;
4711
4712 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4713 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
4714 GUEST_INTR_STATE_NMI));
4715}
4716
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004717static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Jan Kiszka2505dc92013-04-14 12:12:47 +02004718{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004719 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004720 return -EBUSY;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004721
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004722 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
4723 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004724 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004725
Sean Christopherson1b660b62020-04-22 19:25:44 -07004726 return !vmx_nmi_blocked(vcpu);
4727}
Sean Christopherson429ab572020-04-22 19:25:42 -07004728
Sean Christopherson1b660b62020-04-22 19:25:44 -07004729bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)
4730{
4731 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Sean Christopherson88c604b2020-04-22 19:25:41 -07004732 return false;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004733
Sean Christopherson7ab0abd2020-04-22 19:25:50 -07004734 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) ||
Sean Christopherson1b660b62020-04-22 19:25:44 -07004735 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4736 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Jan Kiszka2505dc92013-04-14 12:12:47 +02004737}
4738
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004739static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Gleb Natapov78646122009-03-23 12:12:11 +02004740{
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004741 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004742 return -EBUSY;
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004743
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004744 /*
4745 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
4746 * e.g. if the IRQ arrived asynchronously after checking nested events.
4747 */
4748 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004749 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004750
Sean Christopherson1b660b62020-04-22 19:25:44 -07004751 return !vmx_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02004752}
4753
Izik Eiduscbc94022007-10-25 00:29:55 +02004754static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4755{
Peter Xuff5a9832020-09-30 21:20:33 -04004756 void __user *ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004757
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004758 if (enable_unrestricted_guest)
4759 return 0;
4760
Peter Xu6a3c6232020-01-09 09:57:16 -05004761 mutex_lock(&kvm->slots_lock);
4762 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4763 PAGE_SIZE * 3);
4764 mutex_unlock(&kvm->slots_lock);
4765
Peter Xuff5a9832020-09-30 21:20:33 -04004766 if (IS_ERR(ret))
4767 return PTR_ERR(ret);
4768
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004769 to_kvm_vmx(kvm)->tss_addr = addr;
Peter Xuff5a9832020-09-30 21:20:33 -04004770
4771 return init_rmode_tss(kvm, ret);
Izik Eiduscbc94022007-10-25 00:29:55 +02004772}
4773
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004774static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4775{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004776 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004777 return 0;
4778}
4779
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004780static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004782 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004783 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004784 /*
4785 * Update instruction length as we may reinject the exception
4786 * from user space while in guest debugging mode.
4787 */
4788 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4789 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004790 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004791 return false;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004792 fallthrough;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004793 case DB_VECTOR:
Miaohe Lina8cfbae2020-02-19 10:45:48 +08004794 return !(vcpu->guest_debug &
4795 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP));
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004796 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004797 case OF_VECTOR:
4798 case BR_VECTOR:
4799 case UD_VECTOR:
4800 case DF_VECTOR:
4801 case SS_VECTOR:
4802 case GP_VECTOR:
4803 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004804 return true;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004805 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004806 return false;
4807}
4808
4809static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4810 int vec, u32 err_code)
4811{
4812 /*
4813 * Instruction with address size override prefix opcode 0x67
4814 * Cause the #SS fault with 0 error code in VM86 mode.
4815 */
4816 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004817 if (kvm_emulate_instruction(vcpu, 0)) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004818 if (vcpu->arch.halt_request) {
4819 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004820 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004821 }
4822 return 1;
4823 }
4824 return 0;
4825 }
4826
4827 /*
4828 * Forward all other exceptions that are valid in real mode.
4829 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4830 * the required debugging infrastructure rework.
4831 */
4832 kvm_queue_exception(vcpu, vec);
4833 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004834}
4835
Avi Kivity851ba692009-08-24 11:10:17 +03004836static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004837{
Sean Christopherson95b5a482019-04-19 22:50:59 -07004838 /* handled by vmx_vcpu_run() */
Andi Kleena0861c02009-06-08 17:37:09 +08004839 return 1;
4840}
4841
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004842/*
4843 * If the host has split lock detection disabled, then #AC is
4844 * unconditionally injected into the guest, which is the pre split lock
4845 * detection behaviour.
4846 *
4847 * If the host has split lock detection enabled then #AC is
4848 * only injected into the guest when:
4849 * - Guest CPL == 3 (user mode)
4850 * - Guest has #AC detection enabled in CR0
4851 * - Guest EFLAGS has AC bit set
4852 */
4853static inline bool guest_inject_ac(struct kvm_vcpu *vcpu)
4854{
4855 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
4856 return true;
4857
4858 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) &&
4859 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
4860}
4861
Sean Christopherson95b5a482019-04-19 22:50:59 -07004862static int handle_exception_nmi(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863{
Avi Kivity1155f762007-11-22 11:30:47 +02004864 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004865 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004866 u32 intr_info, ex_no, error_code;
Yuan Yaoe87e46d2021-05-26 14:38:28 +08004867 unsigned long cr2, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868 u32 vect_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869
Avi Kivity1155f762007-11-22 11:30:47 +02004870 vect_info = vmx->idt_vectoring_info;
Sean Christophersonf27ad732020-04-27 10:18:37 -07004871 intr_info = vmx_get_intr_info(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872
Paolo Bonzini2ea72032019-06-06 14:57:25 +02004873 if (is_machine_check(intr_info) || is_nmi(intr_info))
Sean Christopherson95b5a482019-04-19 22:50:59 -07004874 return 1; /* handled by handle_exception_nmi_irqoff() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004875
Wanpeng Li082d06e2018-04-03 16:28:48 -07004876 if (is_invalid_opcode(intr_info))
4877 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004878
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004880 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004881 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004882
Liran Alon9e869482018-03-12 13:12:51 +02004883 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4884 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christophersona6c6ed12019-08-27 14:40:30 -07004885
4886 /*
4887 * VMware backdoor emulation on #GP interception only handles
4888 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
4889 * error code on #GP.
4890 */
4891 if (error_code) {
4892 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4893 return 1;
4894 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004895 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon9e869482018-03-12 13:12:51 +02004896 }
4897
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004898 /*
4899 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4900 * MMIO, it is better to report an internal error.
4901 * See the comments in vmx_handle_exit.
4902 */
4903 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4904 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4905 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4906 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Jim Mattson1aa561b2020-06-03 16:56:21 -07004907 vcpu->run->internal.ndata = 4;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004908 vcpu->run->internal.data[0] = vect_info;
4909 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004910 vcpu->run->internal.data[2] = error_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07004911 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004912 return 0;
4913 }
4914
Avi Kivity6aa8b732006-12-10 02:21:36 -08004915 if (is_page_fault(intr_info)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07004916 cr2 = vmx_get_exit_qual(vcpu);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004917 if (enable_ept && !vcpu->arch.apf.host_apf_flags) {
4918 /*
4919 * EPT will cause page fault only if we need to
4920 * detect illegal GPAs.
4921 */
Mohammed Gamalb96e6502020-09-03 16:11:22 +02004922 WARN_ON_ONCE(!allow_smaller_maxphyaddr);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004923 kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
4924 return 1;
4925 } else
4926 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004927 }
4928
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004929 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004930
4931 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4932 return handle_rmode_exception(vcpu, ex_no, error_code);
4933
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004934 switch (ex_no) {
4935 case DB_VECTOR:
Sean Christopherson5addc232020-04-15 13:34:53 -07004936 dr6 = vmx_get_exit_qual(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004937 if (!(vcpu->guest_debug &
4938 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004939 if (is_icebp(intr_info))
Sean Christopherson1957aa62019-08-27 14:40:39 -07004940 WARN_ON(!skip_emulated_instruction(vcpu));
Huw Daviesfd2a4452014-04-16 10:02:51 +01004941
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004942 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004943 return 1;
4944 }
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08004945 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004946 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004947 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004948 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004949 /*
4950 * Update instruction length as we may reinject #BP from
4951 * user space while in guest debugging mode. Reading it for
4952 * #DB as well causes no harm, it is not used in that case.
4953 */
4954 vmx->vcpu.arch.event_exit_inst_len =
4955 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004956 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Yuan Yaoe87e46d2021-05-26 14:38:28 +08004957 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004958 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004959 break;
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004960 case AC_VECTOR:
4961 if (guest_inject_ac(vcpu)) {
4962 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4963 return 1;
4964 }
4965
4966 /*
4967 * Handle split lock. Depending on detection mode this will
4968 * either warn and disable split lock detection for this
4969 * task or force SIGBUS on it.
4970 */
4971 if (handle_guest_split_lock(kvm_rip_read(vcpu)))
4972 return 1;
4973 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004974 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004975 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4976 kvm_run->ex.exception = ex_no;
4977 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004978 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980 return 0;
4981}
4982
Andrea Arcangelif399e602019-11-04 17:59:58 -05004983static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004985 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986 return 1;
4987}
4988
Avi Kivity851ba692009-08-24 11:10:17 +03004989static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004990{
Avi Kivity851ba692009-08-24 11:10:17 +03004991 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004992 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004993 return 0;
4994}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004995
Avi Kivity851ba692009-08-24 11:10:17 +03004996static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004997{
He, Qingbfdaab02007-09-12 14:18:28 +08004998 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004999 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005000 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005001
Sean Christopherson5addc232020-04-15 13:34:53 -07005002 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02005003 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005004
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005005 ++vcpu->stat.io_exits;
5006
Sean Christopherson432baf62018-03-08 08:57:26 -08005007 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005008 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005009
5010 port = exit_qualification >> 16;
5011 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08005012 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005013
Sean Christophersondca7f122018-03-08 08:57:27 -08005014 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005015}
5016
Ingo Molnar102d8322007-02-19 14:37:47 +02005017static void
5018vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5019{
5020 /*
5021 * Patch in the VMCALL instruction:
5022 */
5023 hypercall[0] = 0x0f;
5024 hypercall[1] = 0x01;
5025 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005026}
5027
Guo Chao0fa06072012-06-28 15:16:19 +08005028/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005029static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5030{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005031 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005032 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5033 unsigned long orig_val = val;
5034
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005035 /*
5036 * We get here when L2 changed cr0 in a way that did not change
5037 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005038 * but did change L0 shadowed bits. So we first calculate the
5039 * effective cr0 value that L1 would like to write into the
5040 * hardware. It consists of the L2-owned bits from the new
5041 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005042 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005043 val = (val & ~vmcs12->cr0_guest_host_mask) |
5044 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5045
David Matlack38991522016-11-29 18:14:08 -08005046 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005047 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005048
5049 if (kvm_set_cr0(vcpu, val))
5050 return 1;
5051 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005052 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005053 } else {
5054 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08005055 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005056 return 1;
David Matlack38991522016-11-29 18:14:08 -08005057
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005058 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005059 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005060}
5061
5062static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5063{
5064 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005065 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5066 unsigned long orig_val = val;
5067
5068 /* analogously to handle_set_cr0 */
5069 val = (val & ~vmcs12->cr4_guest_host_mask) |
5070 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5071 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005072 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005073 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005074 return 0;
5075 } else
5076 return kvm_set_cr4(vcpu, val);
5077}
5078
Paolo Bonzini0367f202016-07-12 10:44:55 +02005079static int handle_desc(struct kvm_vcpu *vcpu)
5080{
5081 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005082 return kvm_emulate_instruction(vcpu, 0);
Paolo Bonzini0367f202016-07-12 10:44:55 +02005083}
5084
Avi Kivity851ba692009-08-24 11:10:17 +03005085static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005086{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005087 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005088 int cr;
5089 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005090 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005091 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005092
Sean Christopherson5addc232020-04-15 13:34:53 -07005093 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005094 cr = exit_qualification & 15;
5095 reg = (exit_qualification >> 8) & 15;
5096 switch ((exit_qualification >> 4) & 3) {
5097 case 0: /* mov to cr */
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005098 val = kvm_register_read(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005099 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100 switch (cr) {
5101 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005102 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005103 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005104 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005105 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03005106 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005107 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005108 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005109 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005110 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005111 case 8: {
5112 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005113 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005114 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005115 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005116 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08005117 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005118 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005119 return ret;
5120 /*
5121 * TODO: we might be squashing a
5122 * KVM_GUESTDBG_SINGLESTEP-triggered
5123 * KVM_EXIT_DEBUG here.
5124 */
Avi Kivity851ba692009-08-24 11:10:17 +03005125 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005126 return 0;
5127 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005128 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005129 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005130 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005131 WARN_ONCE(1, "Guest should always own CR0.TS");
5132 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005133 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005134 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005135 case 1: /*mov from cr*/
5136 switch (cr) {
5137 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005138 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02005139 val = kvm_read_cr3(vcpu);
5140 kvm_register_write(vcpu, reg, val);
5141 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005142 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005143 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005144 val = kvm_get_cr8(vcpu);
5145 kvm_register_write(vcpu, reg, val);
5146 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005147 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005148 }
5149 break;
5150 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005151 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005152 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005153 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005154
Kyle Huey6affcbe2016-11-29 12:40:40 -08005155 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005156 default:
5157 break;
5158 }
Avi Kivity851ba692009-08-24 11:10:17 +03005159 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005160 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005161 (int)(exit_qualification >> 4) & 3, cr);
5162 return 0;
5163}
5164
Avi Kivity851ba692009-08-24 11:10:17 +03005165static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005166{
He, Qingbfdaab02007-09-12 14:18:28 +08005167 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005168 int dr, dr7, reg;
Paolo Bonzini996ff542020-12-14 07:49:54 -05005169 int err = 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005170
Sean Christopherson5addc232020-04-15 13:34:53 -07005171 exit_qualification = vmx_get_exit_qual(vcpu);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005172 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5173
5174 /* First, if DR does not exist, trigger UD */
5175 if (!kvm_require_dr(vcpu, dr))
5176 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005177
Paolo Bonzini996ff542020-12-14 07:49:54 -05005178 if (kvm_x86_ops.get_cpl(vcpu) > 0)
5179 goto out;
5180
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005181 dr7 = vmcs_readl(GUEST_DR7);
5182 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005183 /*
5184 * As the vm-exit takes precedence over the debug trap, we
5185 * need to emulate the latter, either for the host or the
5186 * guest debugging itself.
5187 */
5188 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08005189 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_ACTIVE_LOW;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005190 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005191 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005192 vcpu->run->debug.arch.exception = DB_VECTOR;
5193 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005194 return 0;
5195 } else {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04005196 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005197 return 1;
5198 }
5199 }
5200
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005201 if (vcpu->guest_debug == 0) {
Sean Christopherson2183f562019-05-07 12:17:56 -07005202 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005203
5204 /*
5205 * No more DR vmexits; force a reload of the debug registers
5206 * and reenter on this instruction. The next vmexit will
5207 * retrieve the full state of the debug registers.
5208 */
5209 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5210 return 1;
5211 }
5212
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005213 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5214 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005215 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005216
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05005217 kvm_get_dr(vcpu, dr, &val);
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005218 kvm_register_write(vcpu, reg, val);
Paolo Bonzini996ff542020-12-14 07:49:54 -05005219 err = 0;
5220 } else {
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005221 err = kvm_set_dr(vcpu, dr, kvm_register_read(vcpu, reg));
Paolo Bonzini996ff542020-12-14 07:49:54 -05005222 }
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005223
Paolo Bonzini996ff542020-12-14 07:49:54 -05005224out:
5225 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005226}
5227
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005228static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5229{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005230 get_debugreg(vcpu->arch.db[0], 0);
5231 get_debugreg(vcpu->arch.db[1], 1);
5232 get_debugreg(vcpu->arch.db[2], 2);
5233 get_debugreg(vcpu->arch.db[3], 3);
5234 get_debugreg(vcpu->arch.dr6, 6);
5235 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5236
5237 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Sean Christopherson2183f562019-05-07 12:17:56 -07005238 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005239}
5240
Gleb Natapov020df072010-04-13 10:05:23 +03005241static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5242{
5243 vmcs_writel(GUEST_DR7, val);
5244}
5245
Avi Kivity851ba692009-08-24 11:10:17 +03005246static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005247{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01005248 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005249 return 1;
5250}
5251
Avi Kivity851ba692009-08-24 11:10:17 +03005252static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005253{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005254 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005255
Avi Kivity3842d132010-07-27 12:30:24 +03005256 kvm_make_request(KVM_REQ_EVENT, vcpu);
5257
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005258 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005259 return 1;
5260}
5261
Avi Kivity851ba692009-08-24 11:10:17 +03005262static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005263{
Sean Christopherson5addc232020-04-15 13:34:53 -07005264 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005265
5266 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005267 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005268}
5269
Avi Kivity851ba692009-08-24 11:10:17 +03005270static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005271{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005272 if (likely(fasteoi)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07005273 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005274 int access_type, offset;
5275
5276 access_type = exit_qualification & APIC_ACCESS_TYPE;
5277 offset = exit_qualification & APIC_ACCESS_OFFSET;
5278 /*
5279 * Sane guest uses MOV to write EOI, with written value
5280 * not cared. So make a short-circuit here by avoiding
5281 * heavy instruction emulation.
5282 */
5283 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5284 (offset == APIC_EOI)) {
5285 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005286 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005287 }
5288 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005289 return kvm_emulate_instruction(vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005290}
5291
Yang Zhangc7c9c562013-01-25 10:18:51 +08005292static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5293{
Sean Christopherson5addc232020-04-15 13:34:53 -07005294 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005295 int vector = exit_qualification & 0xff;
5296
5297 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5298 kvm_apic_set_eoi_accelerated(vcpu, vector);
5299 return 1;
5300}
5301
Yang Zhang83d4c282013-01-25 10:18:49 +08005302static int handle_apic_write(struct kvm_vcpu *vcpu)
5303{
Sean Christopherson5addc232020-04-15 13:34:53 -07005304 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhang83d4c282013-01-25 10:18:49 +08005305 u32 offset = exit_qualification & 0xfff;
5306
5307 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5308 kvm_apic_write_nodecode(vcpu, offset);
5309 return 1;
5310}
5311
Avi Kivity851ba692009-08-24 11:10:17 +03005312static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005313{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005314 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005315 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005316 bool has_error_code = false;
5317 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005318 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005319 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005320
5321 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005322 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005323 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005324
Sean Christopherson5addc232020-04-15 13:34:53 -07005325 exit_qualification = vmx_get_exit_qual(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005326
5327 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005328 if (reason == TASK_SWITCH_GATE && idt_v) {
5329 switch (type) {
5330 case INTR_TYPE_NMI_INTR:
5331 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005332 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005333 break;
5334 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005335 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005336 kvm_clear_interrupt_queue(vcpu);
5337 break;
5338 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005339 if (vmx->idt_vectoring_info &
5340 VECTORING_INFO_DELIVER_CODE_MASK) {
5341 has_error_code = true;
5342 error_code =
5343 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5344 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005345 fallthrough;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005346 case INTR_TYPE_SOFT_EXCEPTION:
5347 kvm_clear_exception_queue(vcpu);
5348 break;
5349 default:
5350 break;
5351 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005352 }
Izik Eidus37817f22008-03-24 23:14:53 +02005353 tss_selector = exit_qualification;
5354
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005355 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5356 type != INTR_TYPE_EXT_INTR &&
5357 type != INTR_TYPE_NMI_INTR))
Sean Christopherson1957aa62019-08-27 14:40:39 -07005358 WARN_ON(!skip_emulated_instruction(vcpu));
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005359
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005360 /*
5361 * TODO: What about debug traps on tss switch?
5362 * Are we supposed to inject them and update dr6?
5363 */
Sean Christopherson10517782019-08-27 14:40:35 -07005364 return kvm_task_switch(vcpu, tss_selector,
5365 type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005366 reason, has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02005367}
5368
Avi Kivity851ba692009-08-24 11:10:17 +03005369static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005370{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005371 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005372 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01005373 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005374
Sean Christopherson5addc232020-04-15 13:34:53 -07005375 exit_qualification = vmx_get_exit_qual(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005376
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005377 /*
5378 * EPT violation happened while executing iret from NMI,
5379 * "blocked by NMI" bit has to be set before next VM entry.
5380 * There are errata that may cause this bit to not be set:
5381 * AAK134, BY25.
5382 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005383 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005384 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005385 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005386 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5387
Sheng Yang14394422008-04-28 12:24:45 +08005388 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005389 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005390
Junaid Shahid27959a42016-12-06 16:46:10 -08005391 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005392 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08005393 ? PFERR_USER_MASK : 0;
5394 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005395 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08005396 ? PFERR_WRITE_MASK : 0;
5397 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005398 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08005399 ? PFERR_FETCH_MASK : 0;
5400 /* ept page table entry is present? */
5401 error_code |= (exit_qualification &
5402 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5403 EPT_VIOLATION_EXECUTABLE))
5404 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005405
Isaku Yamahata108356022021-04-22 17:22:29 -07005406 error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) != 0 ?
Paolo Bonzinieebed242016-11-28 14:39:58 +01005407 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005408
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005409 vcpu->arch.exit_qualification = exit_qualification;
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005410
5411 /*
5412 * Check that the GPA doesn't exceed physical memory limits, as that is
5413 * a guest page fault. We have to emulate the instruction here, because
5414 * if the illegal address is that of a paging structure, then
5415 * EPT_VIOLATION_ACC_WRITE bit is set. Alternatively, if supported we
5416 * would also use advanced VM-exit information for EPT violations to
5417 * reconstruct the page fault error code.
5418 */
Paolo Bonzinic0623f52020-10-21 18:05:58 -04005419 if (unlikely(allow_smaller_maxphyaddr && kvm_vcpu_is_illegal_gpa(vcpu, gpa)))
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005420 return kvm_emulate_instruction(vcpu, 0);
5421
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005422 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005423}
5424
Avi Kivity851ba692009-08-24 11:10:17 +03005425static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005426{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005427 gpa_t gpa;
5428
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12005429 if (!vmx_can_emulate_instruction(vcpu, NULL, 0))
5430 return 1;
5431
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005432 /*
5433 * A nested guest cannot optimize MMIO vmexits, because we have an
5434 * nGPA here instead of the required GPA.
5435 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005436 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005437 if (!is_guest_mode(vcpu) &&
5438 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08005439 trace_kvm_fast_mmio(gpa);
Sean Christopherson1957aa62019-08-27 14:40:39 -07005440 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005441 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005442
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07005443 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005444}
5445
Avi Kivity851ba692009-08-24 11:10:17 +03005446static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005447{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005448 WARN_ON_ONCE(!enable_vnmi);
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005449 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Sheng Yangf08864b2008-05-15 18:23:25 +08005450 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005451 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005452
5453 return 1;
5454}
5455
Mohammed Gamal80ced182009-09-01 12:48:18 +02005456static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005457{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005458 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity49e9d552010-09-19 14:34:08 +02005459 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005460 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005461
Sean Christopherson2183f562019-05-07 12:17:56 -07005462 intr_window_requested = exec_controls_get(vmx) &
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005463 CPU_BASED_INTR_WINDOW_EXITING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005464
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005465 while (vmx->emulation_required && count-- != 0) {
Sean Christophersondb438592020-04-22 19:25:48 -07005466 if (intr_window_requested && !vmx_interrupt_blocked(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005467 return handle_interrupt_window(&vmx->vcpu);
5468
Radim Krčmář72875d82017-04-26 22:32:19 +02005469 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005470 return 1;
5471
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005472 if (!kvm_emulate_instruction(vcpu, 0))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005473 return 0;
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005474
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005475 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
Sean Christopherson8fff2712019-08-27 14:40:37 -07005476 vcpu->arch.exception.pending) {
5477 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5478 vcpu->run->internal.suberror =
5479 KVM_INTERNAL_ERROR_EMULATION;
5480 vcpu->run->internal.ndata = 0;
5481 return 0;
5482 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005483
Gleb Natapov8d76c492013-05-08 18:38:44 +03005484 if (vcpu->arch.halt_request) {
5485 vcpu->arch.halt_request = 0;
Sean Christopherson8fff2712019-08-27 14:40:37 -07005486 return kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005487 }
5488
Sean Christopherson8fff2712019-08-27 14:40:37 -07005489 /*
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005490 * Note, return 1 and not 0, vcpu_run() will invoke
5491 * xfer_to_guest_mode() which will create a proper return
5492 * code.
Sean Christopherson8fff2712019-08-27 14:40:37 -07005493 */
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005494 if (__xfer_to_guest_mode_work_pending())
Sean Christopherson8fff2712019-08-27 14:40:37 -07005495 return 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005496 }
5497
Sean Christopherson8fff2712019-08-27 14:40:37 -07005498 return 1;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005499}
5500
5501static void grow_ple_window(struct kvm_vcpu *vcpu)
5502{
5503 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005504 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005505
Babu Mogerc8e88712018-03-16 16:37:24 -04005506 vmx->ple_window = __grow_ple_window(old, ple_window,
5507 ple_window_grow,
5508 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005509
Peter Xu4f75bcc2019-09-06 10:17:22 +08005510 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005511 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005512 trace_kvm_ple_window_update(vcpu->vcpu_id,
5513 vmx->ple_window, old);
5514 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005515}
5516
5517static void shrink_ple_window(struct kvm_vcpu *vcpu)
5518{
5519 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005520 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005521
Babu Mogerc8e88712018-03-16 16:37:24 -04005522 vmx->ple_window = __shrink_ple_window(old, ple_window,
5523 ple_window_shrink,
5524 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005525
Peter Xu4f75bcc2019-09-06 10:17:22 +08005526 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005527 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005528 trace_kvm_ple_window_update(vcpu->vcpu_id,
5529 vmx->ple_window, old);
5530 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005531}
5532
Avi Kivity6aa8b732006-12-10 02:21:36 -08005533/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005534 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5535 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5536 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005537static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005538{
Wanpeng Lib31c1142018-03-12 04:53:04 -07005539 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005540 grow_ple_window(vcpu);
5541
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08005542 /*
5543 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
5544 * VM-execution control is ignored if CPL > 0. OTOH, KVM
5545 * never set PAUSE_EXITING and just set PLE if supported,
5546 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
5547 */
5548 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005549 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005550}
5551
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005552static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5553{
5554 return 1;
5555}
5556
Junaid Shahideb4b2482018-06-27 14:59:14 -07005557static int handle_invpcid(struct kvm_vcpu *vcpu)
5558{
5559 u32 vmx_instruction_info;
5560 unsigned long type;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005561 gva_t gva;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005562 struct {
5563 u64 pcid;
5564 u64 gla;
5565 } operand;
5566
5567 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5568 kvm_queue_exception(vcpu, UD_VECTOR);
5569 return 1;
5570 }
5571
5572 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005573 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005574
5575 if (type > 3) {
5576 kvm_inject_gp(vcpu, 0);
5577 return 1;
5578 }
5579
5580 /* According to the Intel instruction reference, the memory operand
5581 * is read even if it isn't needed (e.g., for type==all)
5582 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005583 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005584 vmx_instruction_info, false,
5585 sizeof(operand), &gva))
Junaid Shahideb4b2482018-06-27 14:59:14 -07005586 return 1;
5587
Babu Moger97150922020-09-11 14:29:12 -05005588 return kvm_handle_invpcid(vcpu, type, gva);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005589}
5590
Kai Huang843e4332015-01-28 10:54:28 +08005591static int handle_pml_full(struct kvm_vcpu *vcpu)
5592{
5593 unsigned long exit_qualification;
5594
5595 trace_kvm_pml_full(vcpu->vcpu_id);
5596
Sean Christopherson5addc232020-04-15 13:34:53 -07005597 exit_qualification = vmx_get_exit_qual(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005598
5599 /*
5600 * PML buffer FULL happened while executing iret from NMI,
5601 * "blocked by NMI" bit has to be set before next VM entry.
5602 */
5603 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005604 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005605 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5606 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5607 GUEST_INTR_STATE_NMI);
5608
5609 /*
5610 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5611 * here.., and there's no userspace involvement needed for PML.
5612 */
5613 return 1;
5614}
5615
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005616static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07005617{
Sean Christopherson804939e2019-05-07 12:18:05 -07005618 struct vcpu_vmx *vmx = to_vmx(vcpu);
5619
5620 if (!vmx->req_immediate_exit &&
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005621 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) {
Sean Christophersond264ee02018-08-27 15:21:12 -07005622 kvm_lapic_expired_hv_timer(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005623 return EXIT_FASTPATH_REENTER_GUEST;
5624 }
Sean Christopherson804939e2019-05-07 12:18:05 -07005625
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005626 return EXIT_FASTPATH_NONE;
5627}
5628
5629static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5630{
5631 handle_fastpath_preemption_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005632 return 1;
5633}
5634
Sean Christophersone4027cf2018-12-03 13:53:12 -08005635/*
5636 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5637 * are overwritten by nested_vmx_setup() when nested=1.
5638 */
5639static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5640{
5641 kvm_queue_exception(vcpu, UD_VECTOR);
5642 return 1;
5643}
5644
Sean Christopherson9798adb2021-04-12 16:21:38 +12005645#ifndef CONFIG_X86_SGX_KVM
Sean Christopherson0b665d32018-08-14 09:33:34 -07005646static int handle_encls(struct kvm_vcpu *vcpu)
5647{
5648 /*
Sean Christopherson9798adb2021-04-12 16:21:38 +12005649 * SGX virtualization is disabled. There is no software enable bit for
5650 * SGX, so KVM intercepts all ENCLS leafs and injects a #UD to prevent
5651 * the guest from executing ENCLS (when SGX is supported by hardware).
Sean Christopherson0b665d32018-08-14 09:33:34 -07005652 */
5653 kvm_queue_exception(vcpu, UD_VECTOR);
5654 return 1;
5655}
Sean Christopherson9798adb2021-04-12 16:21:38 +12005656#endif /* CONFIG_X86_SGX_KVM */
Sean Christopherson0b665d32018-08-14 09:33:34 -07005657
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005658static int handle_bus_lock_vmexit(struct kvm_vcpu *vcpu)
5659{
5660 vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK;
5661 vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK;
5662 return 0;
5663}
5664
Nadav Har'El0140cae2011-05-25 23:06:28 +03005665/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005666 * The exit handlers return 1 if the exit was handled fully and guest execution
5667 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5668 * to be done to userspace and return 0.
5669 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005670static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Sean Christopherson95b5a482019-04-19 22:50:59 -07005671 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005672 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005673 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005674 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005675 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005676 [EXIT_REASON_CR_ACCESS] = handle_cr,
5677 [EXIT_REASON_DR_ACCESS] = handle_dr,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005678 [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
5679 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
5680 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005681 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005682 [EXIT_REASON_HLT] = kvm_emulate_halt,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005683 [EXIT_REASON_INVD] = kvm_emulate_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005684 [EXIT_REASON_INVLPG] = handle_invlpg,
Sean Christophersonc483c452021-02-04 16:57:48 -08005685 [EXIT_REASON_RDPMC] = kvm_emulate_rdpmc,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005686 [EXIT_REASON_VMCALL] = kvm_emulate_hypercall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005687 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5688 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5689 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5690 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5691 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5692 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5693 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5694 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5695 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005696 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5697 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005698 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005699 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005700 [EXIT_REASON_WBINVD] = kvm_emulate_wbinvd,
Sean Christopherson92f98952021-02-04 16:57:46 -08005701 [EXIT_REASON_XSETBV] = kvm_emulate_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005702 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005703 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005704 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5705 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005706 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5707 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005708 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005709 [EXIT_REASON_MWAIT_INSTRUCTION] = kvm_emulate_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005710 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005711 [EXIT_REASON_MONITOR_INSTRUCTION] = kvm_emulate_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005712 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5713 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005714 [EXIT_REASON_RDRAND] = kvm_handle_invalid_op,
5715 [EXIT_REASON_RDSEED] = kvm_handle_invalid_op,
Kai Huang843e4332015-01-28 10:54:28 +08005716 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005717 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005718 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005719 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005720 [EXIT_REASON_ENCLS] = handle_encls,
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005721 [EXIT_REASON_BUS_LOCK] = handle_bus_lock_vmexit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005722};
5723
5724static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005725 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005726
Sean Christopherson235ba742020-09-23 13:13:46 -07005727static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
5728 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02005729{
Sean Christopherson235ba742020-09-23 13:13:46 -07005730 struct vcpu_vmx *vmx = to_vmx(vcpu);
5731
Sean Christopherson5addc232020-04-15 13:34:53 -07005732 *info1 = vmx_get_exit_qual(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08005733 if (!(vmx->exit_reason.failed_vmentry)) {
Sean Christopherson235ba742020-09-23 13:13:46 -07005734 *info2 = vmx->idt_vectoring_info;
5735 *intr_info = vmx_get_intr_info(vcpu);
5736 if (is_exception_with_error_code(*intr_info))
5737 *error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5738 else
5739 *error_code = 0;
5740 } else {
5741 *info2 = 0;
5742 *intr_info = 0;
5743 *error_code = 0;
5744 }
Avi Kivity586f9602010-11-18 13:09:54 +02005745}
5746
Kai Huanga3eaa862015-11-04 13:46:05 +08005747static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005748{
Kai Huanga3eaa862015-11-04 13:46:05 +08005749 if (vmx->pml_pg) {
5750 __free_page(vmx->pml_pg);
5751 vmx->pml_pg = NULL;
5752 }
Kai Huang843e4332015-01-28 10:54:28 +08005753}
5754
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005755static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005756{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005757 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005758 u64 *pml_buf;
5759 u16 pml_idx;
5760
5761 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5762
5763 /* Do nothing if PML buffer is empty */
5764 if (pml_idx == (PML_ENTITY_NUM - 1))
5765 return;
5766
5767 /* PML index always points to next available PML buffer entity */
5768 if (pml_idx >= PML_ENTITY_NUM)
5769 pml_idx = 0;
5770 else
5771 pml_idx++;
5772
5773 pml_buf = page_address(vmx->pml_pg);
5774 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5775 u64 gpa;
5776
5777 gpa = pml_buf[pml_idx];
5778 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005779 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005780 }
5781
5782 /* reset PML index */
5783 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5784}
5785
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005786static void vmx_dump_sel(char *name, uint32_t sel)
5787{
5788 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005789 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005790 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5791 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5792 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5793}
5794
5795static void vmx_dump_dtsel(char *name, uint32_t limit)
5796{
5797 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5798 name, vmcs_read32(limit),
5799 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5800}
5801
David Edmondson84860392021-03-18 12:08:41 +00005802static void vmx_dump_msrs(char *name, struct vmx_msrs *m)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005803{
David Edmondson84860392021-03-18 12:08:41 +00005804 unsigned int i;
5805 struct vmx_msr_entry *e;
5806
5807 pr_err("MSR %s:\n", name);
5808 for (i = 0, e = m->val; i < m->nr; ++i, ++e)
5809 pr_err(" %2d: msr=0x%08x value=0x%016llx\n", i, e->index, e->value);
5810}
5811
David Edmondson0702a3c2021-03-18 12:08:40 +00005812void dump_vmcs(struct kvm_vcpu *vcpu)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005813{
David Edmondson0702a3c2021-03-18 12:08:40 +00005814 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005815 u32 vmentry_ctl, vmexit_ctl;
5816 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
5817 unsigned long cr4;
David Edmondson0702a3c2021-03-18 12:08:40 +00005818 int efer_slot;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005819
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005820 if (!dump_invalid_vmcs) {
5821 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
5822 return;
5823 }
5824
5825 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5826 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5827 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5828 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5829 cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005830 secondary_exec_control = 0;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005831 if (cpu_has_secondary_exec_ctrls())
5832 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5833
5834 pr_err("*** Guest State ***\n");
5835 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5836 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5837 vmcs_readl(CR0_GUEST_HOST_MASK));
5838 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5839 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5840 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
David Edmondsond9e46d32021-03-18 12:08:37 +00005841 if (cpu_has_vmx_ept()) {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005842 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5843 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5844 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5845 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005846 }
5847 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5848 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5849 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5850 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5851 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5852 vmcs_readl(GUEST_SYSENTER_ESP),
5853 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5854 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5855 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5856 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5857 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5858 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5859 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5860 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5861 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5862 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5863 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
David Edmondson0702a3c2021-03-18 12:08:40 +00005864 efer_slot = vmx_find_loadstore_msr_slot(&vmx->msr_autoload.guest, MSR_EFER);
David Edmondson5518da62021-03-18 12:08:39 +00005865 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_EFER)
David Edmondson699e1b22021-03-18 12:08:38 +00005866 pr_err("EFER= 0x%016llx\n", vmcs_read64(GUEST_IA32_EFER));
David Edmondson0702a3c2021-03-18 12:08:40 +00005867 else if (efer_slot >= 0)
5868 pr_err("EFER= 0x%016llx (autoload)\n",
5869 vmx->msr_autoload.guest.val[efer_slot].value);
5870 else if (vmentry_ctl & VM_ENTRY_IA32E_MODE)
5871 pr_err("EFER= 0x%016llx (effective)\n",
5872 vcpu->arch.efer | (EFER_LMA | EFER_LME));
5873 else
5874 pr_err("EFER= 0x%016llx (effective)\n",
5875 vcpu->arch.efer & ~(EFER_LMA | EFER_LME));
David Edmondson5518da62021-03-18 12:08:39 +00005876 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PAT)
David Edmondson699e1b22021-03-18 12:08:38 +00005877 pr_err("PAT = 0x%016llx\n", vmcs_read64(GUEST_IA32_PAT));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005878 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5879 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005880 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005881 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005882 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005883 pr_err("PerfGlobCtl = 0x%016llx\n",
5884 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005885 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005886 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005887 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5888 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5889 vmcs_read32(GUEST_ACTIVITY_STATE));
5890 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5891 pr_err("InterruptStatus = %04x\n",
5892 vmcs_read16(GUEST_INTR_STATUS));
David Edmondson84860392021-03-18 12:08:41 +00005893 if (vmcs_read32(VM_ENTRY_MSR_LOAD_COUNT) > 0)
5894 vmx_dump_msrs("guest autoload", &vmx->msr_autoload.guest);
5895 if (vmcs_read32(VM_EXIT_MSR_STORE_COUNT) > 0)
5896 vmx_dump_msrs("guest autostore", &vmx->msr_autostore.guest);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005897
5898 pr_err("*** Host State ***\n");
5899 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5900 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5901 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5902 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5903 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5904 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5905 vmcs_read16(HOST_TR_SELECTOR));
5906 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5907 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5908 vmcs_readl(HOST_TR_BASE));
5909 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5910 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5911 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5912 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5913 vmcs_readl(HOST_CR4));
5914 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5915 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5916 vmcs_read32(HOST_IA32_SYSENTER_CS),
5917 vmcs_readl(HOST_IA32_SYSENTER_EIP));
David Edmondson699e1b22021-03-18 12:08:38 +00005918 if (vmexit_ctl & VM_EXIT_LOAD_IA32_EFER)
5919 pr_err("EFER= 0x%016llx\n", vmcs_read64(HOST_IA32_EFER));
5920 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PAT)
5921 pr_err("PAT = 0x%016llx\n", vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005922 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005923 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005924 pr_err("PerfGlobCtl = 0x%016llx\n",
5925 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
David Edmondson84860392021-03-18 12:08:41 +00005926 if (vmcs_read32(VM_EXIT_MSR_LOAD_COUNT) > 0)
5927 vmx_dump_msrs("host autoload", &vmx->msr_autoload.host);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005928
5929 pr_err("*** Control State ***\n");
5930 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5931 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5932 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5933 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5934 vmcs_read32(EXCEPTION_BITMAP),
5935 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5936 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5937 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5938 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5939 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5940 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5941 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5942 vmcs_read32(VM_EXIT_INTR_INFO),
5943 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5944 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5945 pr_err(" reason=%08x qualification=%016lx\n",
5946 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5947 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5948 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5949 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005950 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005951 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005952 pr_err("TSC Multiplier = 0x%016llx\n",
5953 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005954 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
5955 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
5956 u16 status = vmcs_read16(GUEST_INTR_STATUS);
5957 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
5958 }
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005959 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005960 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
5961 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005962 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005963 }
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005964 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5965 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5966 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005967 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005968 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5969 pr_err("PLE Gap=%08x Window=%08x\n",
5970 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5971 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5972 pr_err("Virtual processor ID = 0x%04x\n",
5973 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5974}
5975
Avi Kivity6aa8b732006-12-10 02:21:36 -08005976/*
5977 * The guest has exited. See if we can fix it or if we need userspace
5978 * assistance.
5979 */
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005980static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005981{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005982 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08005983 union vmx_exit_reason exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005984 u32 vectoring_info = vmx->idt_vectoring_info;
Sean Christopherson8e533242020-11-06 17:03:12 +08005985 u16 exit_handler_index;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005986
Kai Huang843e4332015-01-28 10:54:28 +08005987 /*
5988 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5989 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5990 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5991 * mode as if vcpus is in root mode, the PML buffer must has been
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005992 * flushed already. Note, PML is never enabled in hardware while
5993 * running L2.
Kai Huang843e4332015-01-28 10:54:28 +08005994 */
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005995 if (enable_pml && !is_guest_mode(vcpu))
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005996 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005997
Sean Christophersondb438592020-04-22 19:25:48 -07005998 /*
5999 * We should never reach this point with a pending nested VM-Enter, and
6000 * more specifically emulation of L2 due to invalid guest state (see
6001 * below) should never happen as that means we incorrectly allowed a
6002 * nested VM-Enter with an invalid vmcs12.
6003 */
6004 WARN_ON_ONCE(vmx->nested.nested_run_pending);
6005
Mohammed Gamal80ced182009-09-01 12:48:18 +02006006 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006007 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006008 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006009
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006010 if (is_guest_mode(vcpu)) {
6011 /*
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08006012 * PML is never enabled when running L2, bail immediately if a
6013 * PML full exit occurs as something is horribly wrong.
6014 */
6015 if (exit_reason.basic == EXIT_REASON_PML_FULL)
6016 goto unexpected_vmexit;
6017
6018 /*
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006019 * The host physical addresses of some pages of guest memory
6020 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
6021 * Page). The CPU may write to these pages via their host
6022 * physical address while L2 is running, bypassing any
6023 * address-translation-based dirty tracking (e.g. EPT write
6024 * protection).
6025 *
6026 * Mark them dirty on every exit from L2 to prevent them from
6027 * getting out of sync with dirty tracking.
6028 */
6029 nested_mark_vmcs12_pages_dirty(vcpu);
6030
Sean Christophersonf47baae2020-04-15 10:55:16 -07006031 if (nested_vmx_reflect_vmexit(vcpu))
Sean Christopherson789afc52020-04-15 10:55:10 -07006032 return 1;
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006033 }
Nadav Har'El644d7112011-05-25 23:12:35 +03006034
Sean Christopherson8e533242020-11-06 17:03:12 +08006035 if (exit_reason.failed_vmentry) {
David Edmondson0702a3c2021-03-18 12:08:40 +00006036 dump_vmcs(vcpu);
Mohammed Gamal51207022010-05-31 22:40:54 +03006037 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6038 vcpu->run->fail_entry.hardware_entry_failure_reason
Sean Christopherson8e533242020-11-06 17:03:12 +08006039 = exit_reason.full;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006040 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Mohammed Gamal51207022010-05-31 22:40:54 +03006041 return 0;
6042 }
6043
Avi Kivity29bd8a72007-09-10 17:27:03 +03006044 if (unlikely(vmx->fail)) {
David Edmondson0702a3c2021-03-18 12:08:40 +00006045 dump_vmcs(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006046 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6047 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006048 = vmcs_read32(VM_INSTRUCTION_ERROR);
Jim Mattson8a14fe42020-06-03 16:56:22 -07006049 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006050 return 0;
6051 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006052
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006053 /*
6054 * Note:
6055 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6056 * delivery event since it indicates guest is accessing MMIO.
6057 * The vm-exit can be triggered again after return to guest that
6058 * will cause infinite loop.
6059 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006060 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sean Christopherson8e533242020-11-06 17:03:12 +08006061 (exit_reason.basic != EXIT_REASON_EXCEPTION_NMI &&
6062 exit_reason.basic != EXIT_REASON_EPT_VIOLATION &&
6063 exit_reason.basic != EXIT_REASON_PML_FULL &&
6064 exit_reason.basic != EXIT_REASON_APIC_ACCESS &&
6065 exit_reason.basic != EXIT_REASON_TASK_SWITCH)) {
Reiji Watanabe04c4f2e2021-04-13 15:47:40 +00006066 int ndata = 3;
6067
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006068 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6069 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006070 vcpu->run->internal.data[0] = vectoring_info;
Sean Christopherson8e533242020-11-06 17:03:12 +08006071 vcpu->run->internal.data[1] = exit_reason.full;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006072 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
Sean Christopherson8e533242020-11-06 17:03:12 +08006073 if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG) {
Reiji Watanabe04c4f2e2021-04-13 15:47:40 +00006074 vcpu->run->internal.data[ndata++] =
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006075 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6076 }
Reiji Watanabe04c4f2e2021-04-13 15:47:40 +00006077 vcpu->run->internal.data[ndata++] = vcpu->arch.last_vmentry_cpu;
6078 vcpu->run->internal.ndata = ndata;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006079 return 0;
6080 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006081
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006082 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006083 vmx->loaded_vmcs->soft_vnmi_blocked)) {
Sean Christophersondb438592020-04-22 19:25:48 -07006084 if (!vmx_interrupt_blocked(vcpu)) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006085 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6086 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
6087 vcpu->arch.nmi_pending) {
6088 /*
6089 * This CPU don't support us in finding the end of an
6090 * NMI-blocked window if the guest runs with IRQs
6091 * disabled. So we pull the trigger after 1 s of
6092 * futile waiting, but inform the user about this.
6093 */
6094 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6095 "state on VCPU %d after 1 s timeout\n",
6096 __func__, vcpu->vcpu_id);
6097 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6098 }
6099 }
6100
Wanpeng Li404d5d72020-04-28 14:23:25 +08006101 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08006102 return 1;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006103
Sean Christopherson8e533242020-11-06 17:03:12 +08006104 if (exit_reason.basic >= kvm_vmx_max_exit_handlers)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006105 goto unexpected_vmexit;
6106#ifdef CONFIG_RETPOLINE
Sean Christopherson8e533242020-11-06 17:03:12 +08006107 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006108 return kvm_emulate_wrmsr(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006109 else if (exit_reason.basic == EXIT_REASON_PREEMPTION_TIMER)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006110 return handle_preemption_timer(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006111 else if (exit_reason.basic == EXIT_REASON_INTERRUPT_WINDOW)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006112 return handle_interrupt_window(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006113 else if (exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006114 return handle_external_interrupt(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006115 else if (exit_reason.basic == EXIT_REASON_HLT)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006116 return kvm_emulate_halt(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006117 else if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006118 return handle_ept_misconfig(vcpu);
6119#endif
6120
Sean Christopherson8e533242020-11-06 17:03:12 +08006121 exit_handler_index = array_index_nospec((u16)exit_reason.basic,
6122 kvm_vmx_max_exit_handlers);
6123 if (!kvm_vmx_exit_handlers[exit_handler_index])
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006124 goto unexpected_vmexit;
6125
Sean Christopherson8e533242020-11-06 17:03:12 +08006126 return kvm_vmx_exit_handlers[exit_handler_index](vcpu);
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006127
6128unexpected_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08006129 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
6130 exit_reason.full);
David Edmondson0702a3c2021-03-18 12:08:40 +00006131 dump_vmcs(vcpu);
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006132 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6133 vcpu->run->internal.suberror =
6134 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
Jim Mattson1aa561b2020-06-03 16:56:21 -07006135 vcpu->run->internal.ndata = 2;
Sean Christopherson8e533242020-11-06 17:03:12 +08006136 vcpu->run->internal.data[0] = exit_reason.full;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006137 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006138 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006139}
6140
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08006141static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
6142{
6143 int ret = __vmx_handle_exit(vcpu, exit_fastpath);
6144
6145 /*
6146 * Even when current exit reason is handled by KVM internally, we
6147 * still need to exit to user space when bus lock detected to inform
6148 * that there is a bus lock in guest.
6149 */
6150 if (to_vmx(vcpu)->exit_reason.bus_lock_detected) {
6151 if (ret > 0)
6152 vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK;
6153
6154 vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK;
6155 return 0;
6156 }
6157 return ret;
6158}
6159
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006160/*
6161 * Software based L1D cache flush which is used when microcode providing
6162 * the cache control MSR is not loaded.
6163 *
6164 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
6165 * flush it is required to read in 64 KiB because the replacement algorithm
6166 * is not exactly LRU. This could be sized at runtime via topology
6167 * information but as all relevant affected CPUs have 32KiB L1D cache size
6168 * there is no point in doing so.
6169 */
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006170static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006171{
6172 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006173
6174 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02006175 * This code is only executed when the the flush mode is 'cond' or
6176 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006177 */
Nicolai Stange427362a2018-07-21 22:25:00 +02006178 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02006179 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006180
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006181 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02006182 * Clear the per-vcpu flush bit, it gets set again
6183 * either from vcpu_run() or from one of the unsafe
6184 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006185 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02006186 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02006187 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02006188
6189 /*
6190 * Clear the per-cpu flush bit, it gets set again from
6191 * the interrupt handlers.
6192 */
6193 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
6194 kvm_clear_cpu_l1tf_flush_l1d();
6195
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006196 if (!flush_l1d)
6197 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006198 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006199
6200 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006201
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006202 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006203 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006204 return;
6205 }
6206
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006207 asm volatile(
6208 /* First ensure the pages are in the TLB */
6209 "xorl %%eax, %%eax\n"
6210 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02006211 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006212 "addl $4096, %%eax\n\t"
6213 "cmpl %%eax, %[size]\n\t"
6214 "jne .Lpopulate_tlb\n\t"
6215 "xorl %%eax, %%eax\n\t"
6216 "cpuid\n\t"
6217 /* Now fill the cache */
6218 "xorl %%eax, %%eax\n"
6219 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006220 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006221 "addl $64, %%eax\n\t"
6222 "cmpl %%eax, %[size]\n\t"
6223 "jne .Lfill_cache\n\t"
6224 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006225 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006226 [size] "r" (size)
6227 : "eax", "ebx", "ecx", "edx");
6228}
6229
Jason Baronb6a7cc32021-01-14 22:27:54 -05006230static void vmx_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006231{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006232 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Liran Alon132f4f72019-11-11 14:30:54 +02006233 int tpr_threshold;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006234
6235 if (is_guest_mode(vcpu) &&
6236 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6237 return;
6238
Liran Alon132f4f72019-11-11 14:30:54 +02006239 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
Liran Alon02d496cf2019-11-11 14:30:55 +02006240 if (is_guest_mode(vcpu))
6241 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6242 else
6243 vmcs_write32(TPR_THRESHOLD, tpr_threshold);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006244}
6245
Sean Christopherson97b7ead2018-12-03 13:53:16 -08006246void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08006247{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006248 struct vcpu_vmx *vmx = to_vmx(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006249 u32 sec_exec_control;
6250
Jim Mattson8d860bb2018-05-09 16:56:05 -04006251 if (!lapic_in_kernel(vcpu))
6252 return;
6253
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07006254 if (!flexpriority_enabled &&
6255 !cpu_has_vmx_virtualize_x2apic_mode())
6256 return;
6257
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006258 /* Postpone execution until vmcs01 is the current VMCS. */
6259 if (is_guest_mode(vcpu)) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006260 vmx->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006261 return;
6262 }
6263
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006264 sec_exec_control = secondary_exec_controls_get(vmx);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006265 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6266 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08006267
Jim Mattson8d860bb2018-05-09 16:56:05 -04006268 switch (kvm_get_apic_mode(vcpu)) {
6269 case LAPIC_MODE_INVALID:
6270 WARN_ONCE(true, "Invalid local APIC state");
Gustavo A. R. Silva551912d2021-05-28 15:07:56 -05006271 break;
Jim Mattson8d860bb2018-05-09 16:56:05 -04006272 case LAPIC_MODE_DISABLED:
6273 break;
6274 case LAPIC_MODE_XAPIC:
6275 if (flexpriority_enabled) {
6276 sec_exec_control |=
6277 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006278 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6279
6280 /*
6281 * Flush the TLB, reloading the APIC access page will
6282 * only do so if its physical address has changed, but
6283 * the guest may have inserted a non-APIC mapping into
6284 * the TLB while the APIC access page was disabled.
6285 */
6286 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006287 }
6288 break;
6289 case LAPIC_MODE_X2APIC:
6290 if (cpu_has_vmx_virtualize_x2apic_mode())
6291 sec_exec_control |=
6292 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6293 break;
Yang Zhang8d146952013-01-25 10:18:50 +08006294 }
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006295 secondary_exec_controls_set(vmx, sec_exec_control);
Yang Zhang8d146952013-01-25 10:18:50 +08006296
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006297 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006298}
6299
Sean Christophersona4148b72020-03-20 14:28:24 -07006300static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
Tang Chen38b99172014-09-24 15:57:54 +08006301{
Sean Christophersona4148b72020-03-20 14:28:24 -07006302 struct page *page;
6303
Sean Christopherson1196cb92020-03-20 14:28:23 -07006304 /* Defer reload until vmcs01 is the current VMCS. */
6305 if (is_guest_mode(vcpu)) {
6306 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true;
6307 return;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07006308 }
Sean Christopherson1196cb92020-03-20 14:28:23 -07006309
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006310 if (!(secondary_exec_controls_get(to_vmx(vcpu)) &
6311 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
6312 return;
6313
Sean Christophersona4148b72020-03-20 14:28:24 -07006314 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
6315 if (is_error_page(page))
6316 return;
6317
6318 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
Sean Christopherson1196cb92020-03-20 14:28:23 -07006319 vmx_flush_tlb_current(vcpu);
Sean Christophersona4148b72020-03-20 14:28:24 -07006320
6321 /*
6322 * Do not pin apic access page in memory, the MMU notifier
6323 * will call us again if it is migrated or swapped out.
6324 */
6325 put_page(page);
Tang Chen38b99172014-09-24 15:57:54 +08006326}
6327
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006328static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006329{
6330 u16 status;
6331 u8 old;
6332
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006333 if (max_isr == -1)
6334 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006335
6336 status = vmcs_read16(GUEST_INTR_STATUS);
6337 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006338 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006339 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006340 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006341 vmcs_write16(GUEST_INTR_STATUS, status);
6342 }
6343}
6344
6345static void vmx_set_rvi(int vector)
6346{
6347 u16 status;
6348 u8 old;
6349
Wei Wang4114c272014-11-05 10:53:43 +08006350 if (vector == -1)
6351 vector = 0;
6352
Yang Zhangc7c9c562013-01-25 10:18:51 +08006353 status = vmcs_read16(GUEST_INTR_STATUS);
6354 old = (u8)status & 0xff;
6355 if ((u8)vector != old) {
6356 status &= ~0xff;
6357 status |= (u8)vector;
6358 vmcs_write16(GUEST_INTR_STATUS, status);
6359 }
6360}
6361
6362static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6363{
Liran Alon851c1a182017-12-24 18:12:56 +02006364 /*
6365 * When running L2, updating RVI is only relevant when
6366 * vmcs12 virtual-interrupt-delivery enabled.
6367 * However, it can be enabled only when L1 also
6368 * intercepts external-interrupts and in that case
6369 * we should not update vmcs02 RVI but instead intercept
6370 * interrupt. Therefore, do nothing when running L2.
6371 */
6372 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08006373 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006374}
6375
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006376static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006377{
6378 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006379 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02006380 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006381
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006382 WARN_ON(!vcpu->arch.apicv_active);
6383 if (pi_test_on(&vmx->pi_desc)) {
6384 pi_clear_on(&vmx->pi_desc);
6385 /*
Liran Alond9ff2742019-11-11 14:25:25 +02006386 * IOMMU can write to PID.ON, so the barrier matters even on UP.
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006387 * But on x86 this is just a compiler barrier anyway.
6388 */
6389 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02006390 max_irr_updated =
6391 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6392
6393 /*
6394 * If we are running L2 and L1 has a new pending interrupt
6395 * which can be injected, we should re-evaluate
6396 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02006397 * If L1 intercepts external-interrupts, we should
6398 * exit from L2 to L1. Otherwise, interrupt should be
6399 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02006400 */
Liran Alon851c1a182017-12-24 18:12:56 +02006401 if (is_guest_mode(vcpu) && max_irr_updated) {
6402 if (nested_exit_on_intr(vcpu))
6403 kvm_vcpu_exiting_guest_mode(vcpu);
6404 else
6405 kvm_make_request(KVM_REQ_EVENT, vcpu);
6406 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006407 } else {
6408 max_irr = kvm_lapic_find_highest_irr(vcpu);
6409 }
6410 vmx_hwapic_irr_update(vcpu, max_irr);
6411 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006412}
6413
Andrey Smetanin63086302015-11-10 15:36:32 +03006414static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006415{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006416 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08006417 return;
6418
Yang Zhangc7c9c562013-01-25 10:18:51 +08006419 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6420 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6421 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6422 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6423}
6424
Paolo Bonzini967235d2016-12-19 14:03:45 +01006425static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
6426{
6427 struct vcpu_vmx *vmx = to_vmx(vcpu);
6428
6429 pi_clear_on(&vmx->pi_desc);
6430 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
6431}
6432
Sean Christopherson535f7ef2020-09-15 12:15:04 -07006433void vmx_do_interrupt_nmi_irqoff(unsigned long entry);
6434
Lai Jiangshana217a652021-05-04 21:50:14 +02006435static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu,
6436 unsigned long entry)
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006437{
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006438 kvm_before_interrupt(vcpu);
Lai Jiangshana217a652021-05-04 21:50:14 +02006439 vmx_do_interrupt_nmi_irqoff(entry);
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006440 kvm_after_interrupt(vcpu);
6441}
6442
Sean Christopherson95b5a482019-04-19 22:50:59 -07006443static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006444{
Lai Jiangshana217a652021-05-04 21:50:14 +02006445 const unsigned long nmi_entry = (unsigned long)asm_exc_nmi_noist;
Sean Christopherson87915852020-04-15 13:34:54 -07006446 u32 intr_info = vmx_get_intr_info(&vmx->vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006447
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006448 /* if exit due to PF check for async PF */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006449 if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02006450 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags();
Andi Kleena0861c02009-06-08 17:37:09 +08006451 /* Handle machine checks before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006452 else if (is_machine_check(intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08006453 kvm_machine_check();
Gleb Natapov20f65982009-05-11 13:35:55 +03006454 /* We need to handle NMIs before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006455 else if (is_nmi(intr_info))
Lai Jiangshana217a652021-05-04 21:50:14 +02006456 handle_interrupt_nmi_irqoff(&vmx->vcpu, nmi_entry);
Avi Kivity51aa01d2010-07-20 14:31:20 +03006457}
Gleb Natapov20f65982009-05-11 13:35:55 +03006458
Sean Christopherson95b5a482019-04-19 22:50:59 -07006459static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006460{
Sean Christopherson87915852020-04-15 13:34:54 -07006461 u32 intr_info = vmx_get_intr_info(vcpu);
Lai Jiangshana217a652021-05-04 21:50:14 +02006462 unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK;
6463 gate_desc *desc = (gate_desc *)host_idt_base + vector;
Yang Zhanga547c6d2013-04-11 19:25:10 +08006464
Sean Christopherson49def502019-04-19 22:50:56 -07006465 if (WARN_ONCE(!is_external_intr(intr_info),
6466 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
6467 return;
6468
Lai Jiangshana217a652021-05-04 21:50:14 +02006469 handle_interrupt_nmi_irqoff(vcpu, gate_offset(desc));
Yang Zhanga547c6d2013-04-11 19:25:10 +08006470}
Sean Christopherson95b5a482019-04-19 22:50:59 -07006471
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006472static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006473{
6474 struct vcpu_vmx *vmx = to_vmx(vcpu);
6475
Sean Christopherson8e533242020-11-06 17:03:12 +08006476 if (vmx->exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006477 handle_external_interrupt_irqoff(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006478 else if (vmx->exit_reason.basic == EXIT_REASON_EXCEPTION_NMI)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006479 handle_exception_nmi_irqoff(vmx);
6480}
Yang Zhanga547c6d2013-04-11 19:25:10 +08006481
Tom Lendacky57194552020-12-10 11:10:00 -06006482/*
6483 * The kvm parameter can be NULL (module initialization, or invocation before
6484 * VM creation). Be sure to check the kvm parameter before using it.
6485 */
6486static bool vmx_has_emulated_msr(struct kvm *kvm, u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006487{
Tom Lendackybc226f02018-05-10 22:06:39 +02006488 switch (index) {
6489 case MSR_IA32_SMBASE:
6490 /*
6491 * We cannot do SMM unless we can run the guest in big
6492 * real mode.
6493 */
6494 return enable_unrestricted_guest || emulate_invalid_guest_state;
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02006495 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
6496 return nested;
Tom Lendackybc226f02018-05-10 22:06:39 +02006497 case MSR_AMD64_VIRT_SPEC_CTRL:
6498 /* This is AMD only. */
6499 return false;
6500 default:
6501 return true;
6502 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006503}
6504
Avi Kivity51aa01d2010-07-20 14:31:20 +03006505static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6506{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006507 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006508 bool unblock_nmi;
6509 u8 vector;
6510 bool idtv_info_valid;
6511
6512 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006513
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006514 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006515 if (vmx->loaded_vmcs->nmi_known_unmasked)
6516 return;
Sean Christopherson87915852020-04-15 13:34:54 -07006517
6518 exit_intr_info = vmx_get_intr_info(&vmx->vcpu);
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006519 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6520 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6521 /*
6522 * SDM 3: 27.7.1.2 (September 2008)
6523 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6524 * a guest IRET fault.
6525 * SDM 3: 23.2.2 (September 2008)
6526 * Bit 12 is undefined in any of the following cases:
6527 * If the VM exit sets the valid bit in the IDT-vectoring
6528 * information field.
6529 * If the VM exit is due to a double fault.
6530 */
6531 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6532 vector != DF_VECTOR && !idtv_info_valid)
6533 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6534 GUEST_INTR_STATE_NMI);
6535 else
6536 vmx->loaded_vmcs->nmi_known_unmasked =
6537 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6538 & GUEST_INTR_STATE_NMI);
6539 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
6540 vmx->loaded_vmcs->vnmi_blocked_time +=
6541 ktime_to_ns(ktime_sub(ktime_get(),
6542 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006543}
6544
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006545static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006546 u32 idt_vectoring_info,
6547 int instr_len_field,
6548 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006549{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006550 u8 vector;
6551 int type;
6552 bool idtv_info_valid;
6553
6554 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006555
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006556 vcpu->arch.nmi_injected = false;
6557 kvm_clear_exception_queue(vcpu);
6558 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006559
6560 if (!idtv_info_valid)
6561 return;
6562
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006563 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006564
Avi Kivity668f6122008-07-02 09:28:55 +03006565 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6566 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006567
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006568 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006569 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006570 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006571 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006572 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006573 * Clear bit "block by NMI" before VM entry if a NMI
6574 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006575 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006576 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006577 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006578 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006579 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006580 fallthrough;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006581 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006582 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006583 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03006584 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006585 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03006586 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006587 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006588 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006589 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006590 fallthrough;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006591 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006592 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006593 break;
6594 default:
6595 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006596 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006597}
6598
Avi Kivity83422e12010-07-20 14:43:23 +03006599static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6600{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006601 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006602 VM_EXIT_INSTRUCTION_LEN,
6603 IDT_VECTORING_ERROR_CODE);
6604}
6605
Avi Kivityb463a6f2010-07-20 15:06:17 +03006606static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6607{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006608 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006609 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6610 VM_ENTRY_INSTRUCTION_LEN,
6611 VM_ENTRY_EXCEPTION_ERROR_CODE);
6612
6613 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6614}
6615
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006616static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6617{
6618 int i, nr_msrs;
6619 struct perf_guest_switch_msr *msrs;
6620
Sean Christophersonc8e2fe12021-03-09 09:10:19 -08006621 /* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006622 msrs = perf_guest_get_msrs(&nr_msrs);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006623 if (!msrs)
6624 return;
6625
6626 for (i = 0; i < nr_msrs; i++)
6627 if (msrs[i].host == msrs[i].guest)
6628 clear_atomic_switch_msr(vmx, msrs[i].msr);
6629 else
6630 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006631 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006632}
6633
Sean Christophersonf459a702018-08-27 15:21:11 -07006634static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006635{
6636 struct vcpu_vmx *vmx = to_vmx(vcpu);
6637 u64 tscl;
6638 u32 delta_tsc;
6639
Sean Christophersond264ee02018-08-27 15:21:12 -07006640 if (vmx->req_immediate_exit) {
Sean Christopherson804939e2019-05-07 12:18:05 -07006641 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
6642 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6643 } else if (vmx->hv_deadline_tsc != -1) {
Sean Christophersonf459a702018-08-27 15:21:11 -07006644 tscl = rdtsc();
6645 if (vmx->hv_deadline_tsc > tscl)
6646 /* set_hv_timer ensures the delta fits in 32-bits */
6647 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6648 cpu_preemption_timer_multi);
6649 else
6650 delta_tsc = 0;
6651
Sean Christopherson804939e2019-05-07 12:18:05 -07006652 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
6653 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6654 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
6655 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
6656 vmx->loaded_vmcs->hv_timer_soft_disabled = true;
Sean Christophersonf459a702018-08-27 15:21:11 -07006657 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006658}
6659
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006660void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006661{
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006662 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
6663 vmx->loaded_vmcs->host_state.rsp = host_rsp;
6664 vmcs_writel(HOST_RSP, host_rsp);
6665 }
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006666}
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006667
Wanpeng Li404d5d72020-04-28 14:23:25 +08006668static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006669{
Sean Christopherson8e533242020-11-06 17:03:12 +08006670 switch (to_vmx(vcpu)->exit_reason.basic) {
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006671 case EXIT_REASON_MSR_WRITE:
6672 return handle_fastpath_set_msr_irqoff(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04006673 case EXIT_REASON_PREEMPTION_TIMER:
6674 return handle_fastpath_preemption_timer(vcpu);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006675 default:
6676 return EXIT_FASTPATH_NONE;
6677 }
6678}
6679
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006680static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
6681 struct vcpu_vmx *vmx)
6682{
Sean Christophersonbc908e02021-05-04 17:27:35 -07006683 kvm_guest_enter_irqoff();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006684
6685 /* L1D Flush includes CPU buffer clear to mitigate MDS */
6686 if (static_branch_unlikely(&vmx_l1d_should_flush))
6687 vmx_l1d_flush(vcpu);
6688 else if (static_branch_unlikely(&mds_user_clear))
6689 mds_clear_cpu_buffers();
6690
Thomas Gleixner2245d392020-07-08 21:52:00 +02006691 if (vcpu->arch.cr2 != native_read_cr2())
6692 native_write_cr2(vcpu->arch.cr2);
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006693
6694 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
6695 vmx->loaded_vmcs->launched);
6696
Thomas Gleixner2245d392020-07-08 21:52:00 +02006697 vcpu->arch.cr2 = native_read_cr2();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006698
Sean Christophersonbc908e02021-05-04 17:27:35 -07006699 kvm_guest_exit_irqoff();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006700}
6701
Wanpeng Li404d5d72020-04-28 14:23:25 +08006702static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006703{
6704 struct vcpu_vmx *vmx = to_vmx(vcpu);
6705 unsigned long cr3, cr4;
6706
6707 /* Record the guest's net vcpu time for enforced NMI injections. */
6708 if (unlikely(!enable_vnmi &&
6709 vmx->loaded_vmcs->soft_vnmi_blocked))
6710 vmx->loaded_vmcs->entry_time = ktime_get();
6711
6712 /* Don't enter VMX if guest state is invalid, let the exit handler
6713 start emulation until we arrive back to a valid state */
6714 if (vmx->emulation_required)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006715 return EXIT_FASTPATH_NONE;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006716
Lorenzo Bresciad95df952020-12-23 14:45:07 +00006717 trace_kvm_entry(vcpu);
6718
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006719 if (vmx->ple_window_dirty) {
6720 vmx->ple_window_dirty = false;
6721 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6722 }
6723
wanpeng lic9dfd3f2020-02-17 18:37:43 +08006724 /*
6725 * We did this in prepare_switch_to_guest, because it needs to
6726 * be within srcu_read_lock.
6727 */
6728 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006729
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006730 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006731 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006732 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006733 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6734
6735 cr3 = __get_current_cr3_fast();
6736 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6737 vmcs_writel(HOST_CR3, cr3);
6738 vmx->loaded_vmcs->host_state.cr3 = cr3;
6739 }
6740
6741 cr4 = cr4_read_shadow();
6742 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
6743 vmcs_writel(HOST_CR4, cr4);
6744 vmx->loaded_vmcs->host_state.cr4 = cr4;
6745 }
6746
6747 /* When single-stepping over STI and MOV SS, we must clear the
6748 * corresponding interruptibility bits in the guest state. Otherwise
6749 * vmentry fails as it then expects bit 14 (BS) in pending debug
6750 * exceptions being set, but that's not correct for the guest debugging
6751 * case. */
6752 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6753 vmx_set_interrupt_shadow(vcpu, 0);
6754
Aaron Lewis139a12c2019-10-21 16:30:25 -07006755 kvm_load_guest_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006756
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006757 pt_guest_enter(vmx);
6758
Vitaly Kuznetsov49097762020-06-19 11:40:46 +02006759 atomic_switch_perf_msrs(vmx);
Like Xu1b5ac3222021-02-01 13:10:34 +08006760 if (intel_pmu_lbr_is_enabled(vcpu))
6761 vmx_passthrough_lbr_msrs(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006762
Sean Christopherson804939e2019-05-07 12:18:05 -07006763 if (enable_preemption_timer)
6764 vmx_update_hv_timer(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006765
Wanpeng Li010fd372020-09-10 17:50:41 +08006766 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08006767
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006768 /*
6769 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6770 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6771 * is no need to worry about the conditional branch over the wrmsr
6772 * being speculatively taken.
6773 */
6774 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
6775
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006776 /* The actual VMENTER/EXIT is in the .noinstr.text section. */
6777 vmx_vcpu_enter_exit(vcpu, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006778
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006779 /*
6780 * We do not use IBRS in the kernel. If this vCPU has used the
6781 * SPEC_CTRL MSR it may have left it on; save the value and
6782 * turn it off. This is much more efficient than blindly adding
6783 * it to the atomic save/restore list. Especially as the former
6784 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6785 *
6786 * For non-nested case:
6787 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6788 * save it.
6789 *
6790 * For nested case:
6791 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6792 * save it.
6793 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006794 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006795 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006796
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006797 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006798
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006799 /* All fields are clean at this point */
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01006800 if (static_branch_unlikely(&enable_evmcs)) {
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006801 current_evmcs->hv_clean_fields |=
6802 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6803
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +01006804 current_evmcs->hv_vp_id = kvm_hv_get_vpindex(vcpu);
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01006805 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08006806
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006807 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006808 if (vmx->host_debugctlmsr)
6809 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006810
Avi Kivityaa67f602012-08-01 16:48:03 +03006811#ifndef CONFIG_X86_64
6812 /*
6813 * The sysexit path does not restore ds/es, so we must set them to
6814 * a reasonable value ourselves.
6815 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006816 * We can't defer this to vmx_prepare_switch_to_host() since that
6817 * function may be executed in interrupt context, which saves and
6818 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006819 */
6820 loadsegment(ds, __USER_DS);
6821 loadsegment(es, __USER_DS);
6822#endif
6823
Sean Christophersone5d03de2020-04-15 13:34:51 -07006824 vmx_register_cache_reset(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006825
Chao Peng2ef444f2018-10-24 16:05:12 +08006826 pt_guest_exit(vmx);
6827
Aaron Lewis139a12c2019-10-21 16:30:25 -07006828 kvm_load_host_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006829
Gleb Natapove0b890d2013-09-25 12:51:33 +03006830 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006831 vmx->idt_vectoring_info = 0;
6832
Sean Christopherson873e1da2020-04-10 10:47:02 -07006833 if (unlikely(vmx->fail)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08006834 vmx->exit_reason.full = 0xdead;
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006835 return EXIT_FASTPATH_NONE;
Sean Christopherson873e1da2020-04-10 10:47:02 -07006836 }
6837
Sean Christopherson8e533242020-11-06 17:03:12 +08006838 vmx->exit_reason.full = vmcs_read32(VM_EXIT_REASON);
6839 if (unlikely((u16)vmx->exit_reason.basic == EXIT_REASON_MCE_DURING_VMENTRY))
Sean Christophersonbeb8d932019-04-19 22:50:55 -07006840 kvm_machine_check();
6841
Maxim Levitskyf5c59b52021-02-17 16:57:12 +02006842 if (likely(!vmx->exit_reason.failed_vmentry))
6843 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
6844
Sean Christopherson8e533242020-11-06 17:03:12 +08006845 trace_kvm_exit(vmx->exit_reason.full, vcpu, KVM_ISA_VMX);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006846
Sean Christopherson8e533242020-11-06 17:03:12 +08006847 if (unlikely(vmx->exit_reason.failed_vmentry))
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006848 return EXIT_FASTPATH_NONE;
6849
Jim Mattsonb060ca32017-09-14 16:31:42 -07006850 vmx->loaded_vmcs->launched = 1;
Gleb Natapove0b890d2013-09-25 12:51:33 +03006851
Avi Kivity51aa01d2010-07-20 14:31:20 +03006852 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006853 vmx_complete_interrupts(vmx);
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006854
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006855 if (is_guest_mode(vcpu))
6856 return EXIT_FASTPATH_NONE;
6857
Paolo Bonzinid89d04a2021-02-02 10:44:23 -05006858 return vmx_exit_handlers_fastpath(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006859}
6860
Avi Kivity6aa8b732006-12-10 02:21:36 -08006861static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6862{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006863 struct vcpu_vmx *vmx = to_vmx(vcpu);
6864
Kai Huang843e4332015-01-28 10:54:28 +08006865 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006866 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006867 free_vpid(vmx->vpid);
Sean Christopherson55d23752018-12-03 13:53:18 -08006868 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006869 free_loaded_vmcs(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006870}
6871
Sean Christopherson987b2592019-12-18 13:54:55 -08006872static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006873{
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -07006874 struct vmx_uret_msr *tsx_ctrl;
Ben Gardon41836832019-02-11 11:02:52 -08006875 struct vcpu_vmx *vmx;
Sean Christopherson34109c02019-12-18 13:54:50 -08006876 int i, cpu, err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006877
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006878 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0);
6879 vmx = to_vmx(vcpu);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006880
Peter Feiner4e595162016-07-07 14:49:58 -07006881 err = -ENOMEM;
6882
Sean Christopherson034d8e22019-12-18 13:54:49 -08006883 vmx->vpid = allocate_vpid();
6884
Peter Feiner4e595162016-07-07 14:49:58 -07006885 /*
6886 * If PML is turned on, failure on enabling PML just results in failure
6887 * of creating the vcpu, therefore we can simplify PML logic (by
6888 * avoiding dealing with cases, such as enabling PML partially on vcpus
Miaohe Lin67b0ae42019-12-11 14:26:22 +08006889 * for the guest), etc.
Peter Feiner4e595162016-07-07 14:49:58 -07006890 */
6891 if (enable_pml) {
Ben Gardon41836832019-02-11 11:02:52 -08006892 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Peter Feiner4e595162016-07-07 14:49:58 -07006893 if (!vmx->pml_pg)
Sean Christopherson987b2592019-12-18 13:54:55 -08006894 goto free_vpid;
Peter Feiner4e595162016-07-07 14:49:58 -07006895 }
6896
Sean Christophersone5fda4b2021-05-04 10:17:32 -07006897 for (i = 0; i < kvm_nr_uret_msrs; ++i) {
Sean Christophersonb6194b92021-05-04 10:17:27 -07006898 vmx->guest_uret_msrs[i].data = 0;
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -07006899 vmx->guest_uret_msrs[i].mask = -1ull;
6900 }
Sean Christopherson5e17c622021-05-04 10:17:30 -07006901 if (boot_cpu_has(X86_FEATURE_RTM)) {
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -07006902 /*
6903 * TSX_CTRL_CPUID_CLEAR is handled in the CPUID interception.
6904 * Keep the host value unchanged to avoid changing CPUID bits
6905 * under the host kernel's feet.
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -07006906 */
Sean Christopherson5e17c622021-05-04 10:17:30 -07006907 tsx_ctrl = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL);
6908 if (tsx_ctrl)
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -07006909 vmx->guest_uret_msrs[i].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
Xiaoyao Li4be53412019-10-20 17:11:00 +08006910 }
6911
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006912 err = alloc_loaded_vmcs(&vmx->vmcs01);
6913 if (err < 0)
Jim Mattson7d737102019-12-03 16:24:42 -08006914 goto free_pml;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006915
Alexander Graf3eb90012020-09-25 16:34:20 +02006916 /* The MSR bitmap starts with all ones */
6917 bitmap_fill(vmx->shadow_msr_intercept.read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
6918 bitmap_fill(vmx->shadow_msr_intercept.write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
6919
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006920 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_TSC, MSR_TYPE_R);
Sean Christophersondbdd0962021-04-21 19:38:31 -07006921#ifdef CONFIG_X86_64
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006922 vmx_disable_intercept_for_msr(vcpu, MSR_FS_BASE, MSR_TYPE_RW);
6923 vmx_disable_intercept_for_msr(vcpu, MSR_GS_BASE, MSR_TYPE_RW);
6924 vmx_disable_intercept_for_msr(vcpu, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
Sean Christophersondbdd0962021-04-21 19:38:31 -07006925#endif
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006926 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6927 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6928 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
Sean Christopherson987b2592019-12-18 13:54:55 -08006929 if (kvm_cstate_in_guest(vcpu->kvm)) {
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006930 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C1_RES, MSR_TYPE_R);
6931 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
6932 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
6933 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
Wanpeng Lib5170062019-05-21 14:06:53 +08006934 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006935 vmx->msr_bitmap_mode = 0;
6936
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006937 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03006938 cpu = get_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006939 vmx_vcpu_load(vcpu, cpu);
6940 vcpu->cpu = cpu;
Xiaoyao Li1b842922019-10-20 17:11:01 +08006941 init_vmcs(vmx);
Sean Christopherson34109c02019-12-18 13:54:50 -08006942 vmx_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006943 put_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006944 if (cpu_need_virtualize_apic_accesses(vcpu)) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006945 err = alloc_apic_access_page(vcpu->kvm);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006946 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006947 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02006948 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006949
Sean Christophersone90008d2018-03-05 12:04:37 -08006950 if (enable_ept && !enable_unrestricted_guest) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006951 err = init_rmode_identity_map(vcpu->kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08006952 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02006953 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006954 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006955
Roman Kagan63aff652018-07-19 21:59:07 +03006956 if (nested)
Chenyi Qiangb9757a42020-08-28 16:56:22 +08006957 memcpy(&vmx->nested.msrs, &vmcs_config.nested, sizeof(vmx->nested.msrs));
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006958 else
6959 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08006960
Sean Christopherson8f102442021-04-12 16:21:40 +12006961 vcpu_setup_sgx_lepubkeyhash(vcpu);
6962
Wincy Van705699a2015-02-03 23:58:17 +08006963 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006964 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006965
Paolo Bonzinibab0c312020-02-11 18:40:58 +01006966 vcpu->arch.microcode_version = 0x100000000ULL;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08006967 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006968
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02006969 /*
6970 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
6971 * or POSTED_INTR_WAKEUP_VECTOR.
6972 */
6973 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
6974 vmx->pi_desc.sn = 1;
6975
Sean Christophersonee366562021-03-05 10:31:21 -08006976#if IS_ENABLED(CONFIG_HYPERV)
Sean Christopherson978c8342021-03-05 10:31:23 -08006977 vmx->hv_root_ept = INVALID_PAGE;
Sean Christophersonee366562021-03-05 10:31:21 -08006978#endif
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006979 return 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006980
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006981free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006982 free_loaded_vmcs(vmx->loaded_vmcs);
Peter Feiner4e595162016-07-07 14:49:58 -07006983free_pml:
6984 vmx_destroy_pml_buffer(vmx);
Sean Christopherson987b2592019-12-18 13:54:55 -08006985free_vpid:
Wanpeng Li991e7a02015-09-16 17:30:05 +08006986 free_vpid(vmx->vpid);
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006987 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006988}
6989
Thomas Gleixner65fd4cb2019-02-19 11:10:49 +01006990#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"
6991#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 -04006992
Wanpeng Lib31c1142018-03-12 04:53:04 -07006993static int vmx_vm_init(struct kvm *kvm)
6994{
Sean Christophersonee366562021-03-05 10:31:21 -08006995#if IS_ENABLED(CONFIG_HYPERV)
Sean Christopherson978c8342021-03-05 10:31:23 -08006996 spin_lock_init(&to_kvm_vmx(kvm)->hv_root_ept_lock);
Sean Christophersonee366562021-03-05 10:31:21 -08006997#endif
Tianyu Lan877ad952018-07-19 08:40:23 +00006998
Wanpeng Lib31c1142018-03-12 04:53:04 -07006999 if (!ple_gap)
7000 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007001
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007002 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
7003 switch (l1tf_mitigation) {
7004 case L1TF_MITIGATION_OFF:
7005 case L1TF_MITIGATION_FLUSH_NOWARN:
7006 /* 'I explicitly don't care' is set */
7007 break;
7008 case L1TF_MITIGATION_FLUSH:
7009 case L1TF_MITIGATION_FLUSH_NOSMT:
7010 case L1TF_MITIGATION_FULL:
7011 /*
7012 * Warn upon starting the first VM in a potentially
7013 * insecure environment.
7014 */
Josh Poimboeufb2849092019-01-30 07:13:58 -06007015 if (sched_smt_active())
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007016 pr_warn_once(L1TF_MSG_SMT);
7017 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
7018 pr_warn_once(L1TF_MSG_L1D);
7019 break;
7020 case L1TF_MITIGATION_FULL_FORCE:
7021 /* Flush is enforced */
7022 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007023 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007024 }
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007025 kvm_apicv_init(kvm, enable_apicv);
Wanpeng Lib31c1142018-03-12 04:53:04 -07007026 return 0;
7027}
7028
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007029static int __init vmx_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03007030{
7031 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08007032 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007033
Sean Christophersonff10e222019-12-20 20:45:10 -08007034 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
7035 !this_cpu_has(X86_FEATURE_VMX)) {
7036 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id());
7037 return -EIO;
7038 }
7039
Sean Christopherson7caaa712018-12-03 13:53:01 -08007040 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007041 return -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007042 if (nested)
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01007043 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept);
Yang, Sheng002c7f72007-07-31 14:23:01 +03007044 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7045 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7046 smp_processor_id());
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007047 return -EIO;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007048 }
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007049 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007050}
7051
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007052static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007053{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007054 u8 cache;
7055 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007056
Chia-I Wu222f06e2020-02-13 13:30:34 -08007057 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in
7058 * memory aliases with conflicting memory types and sometimes MCEs.
7059 * We have to be careful as to what are honored and when.
7060 *
7061 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to
7062 * UC. The effective memory type is UC or WC depending on guest PAT.
7063 * This was historically the source of MCEs and we want to be
7064 * conservative.
7065 *
7066 * When there is no need to deal with noncoherent DMA (e.g., no VT-d
7067 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The
7068 * EPT memory type is set to WB. The effective memory type is forced
7069 * WB.
7070 *
7071 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The
7072 * EPT memory type is used to emulate guest CD/MTRR.
Sheng Yang522c68c2009-04-27 20:35:43 +08007073 */
Chia-I Wu222f06e2020-02-13 13:30:34 -08007074
Paolo Bonzini606decd2015-10-01 13:12:47 +02007075 if (is_mmio) {
7076 cache = MTRR_TYPE_UNCACHABLE;
7077 goto exit;
7078 }
7079
7080 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007081 ipat = VMX_EPT_IPAT_BIT;
7082 cache = MTRR_TYPE_WRBACK;
7083 goto exit;
7084 }
7085
7086 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
7087 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02007088 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08007089 cache = MTRR_TYPE_WRBACK;
7090 else
7091 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007092 goto exit;
7093 }
7094
Xiao Guangrongff536042015-06-15 16:55:22 +08007095 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007096
7097exit:
7098 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08007099}
7100
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007101static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007102{
7103 /*
7104 * These bits in the secondary execution controls field
7105 * are dynamic, the others are mostly based on the hypervisor
7106 * architecture and the guest's CPUID. Do not touch the
7107 * dynamic bits.
7108 */
7109 u32 mask =
7110 SECONDARY_EXEC_SHADOW_VMCS |
7111 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02007112 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7113 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007114
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007115 u32 new_ctl = vmx->secondary_exec_control;
7116 u32 cur_ctl = secondary_exec_controls_get(vmx);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007117
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007118 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007119}
7120
David Matlack8322ebb2016-11-29 18:14:09 -08007121/*
7122 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
7123 * (indicating "allowed-1") if they are supported in the guest's CPUID.
7124 */
7125static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
7126{
7127 struct vcpu_vmx *vmx = to_vmx(vcpu);
7128 struct kvm_cpuid_entry2 *entry;
7129
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007130 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
7131 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08007132
7133#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
7134 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007135 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08007136} while (0)
7137
7138 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007139 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME));
7140 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME));
7141 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC));
7142 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE));
7143 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE));
7144 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE));
7145 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE));
7146 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE));
7147 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR));
7148 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM));
7149 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX));
7150 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX));
7151 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID));
7152 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE));
David Matlack8322ebb2016-11-29 18:14:09 -08007153
7154 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007155 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE));
7156 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP));
7157 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP));
7158 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU));
7159 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP));
7160 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57));
David Matlack8322ebb2016-11-29 18:14:09 -08007161
7162#undef cr4_fixed1_update
7163}
7164
Liran Alon5f76f6f2018-09-14 03:25:52 +03007165static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
7166{
7167 struct vcpu_vmx *vmx = to_vmx(vcpu);
7168
7169 if (kvm_mpx_supported()) {
7170 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
7171
7172 if (mpx_enabled) {
7173 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7174 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7175 } else {
7176 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7177 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7178 }
7179 }
7180}
7181
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007182static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7183{
7184 struct vcpu_vmx *vmx = to_vmx(vcpu);
7185 struct kvm_cpuid_entry2 *best = NULL;
7186 int i;
7187
7188 for (i = 0; i < PT_CPUID_LEAVES; i++) {
7189 best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7190 if (!best)
7191 return;
7192 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7193 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7194 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7195 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7196 }
7197
7198 /* Get the number of configurable Address Ranges for filtering */
7199 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7200 PT_CAP_num_address_ranges);
7201
7202 /* Initialize and clear the no dependency bits */
7203 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7204 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7205
7206 /*
7207 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7208 * will inject an #GP
7209 */
7210 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7211 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7212
7213 /*
7214 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7215 * PSBFreq can be set
7216 */
7217 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7218 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7219 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7220
7221 /*
7222 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7223 * MTCFreq can be set
7224 */
7225 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7226 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7227 RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7228
7229 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7230 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7231 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7232 RTIT_CTL_PTW_EN);
7233
7234 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7235 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7236 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7237
7238 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7239 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7240 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7241
Ingo Molnard9f6e122021-03-18 15:28:01 +01007242 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabricEn can be set */
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007243 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7244 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7245
7246 /* unmask address range configure area */
7247 for (i = 0; i < vmx->pt_desc.addr_range; i++)
Gustavo A. R. Silvad14eff12018-12-26 14:40:59 -06007248 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007249}
7250
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007251static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08007252{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007253 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007254
Aaron Lewis72041602019-10-21 16:30:20 -07007255 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7256 vcpu->arch.xsaves_enabled = false;
7257
Paolo Bonzini80154d72017-08-24 13:55:35 +02007258 if (cpu_has_secondary_exec_ctrls()) {
7259 vmx_compute_secondary_exec_control(vmx);
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007260 vmcs_set_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007261 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007262
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007263 if (nested_vmx_allowed(vcpu))
7264 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007265 FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7266 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007267 else
7268 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007269 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7270 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX);
David Matlack8322ebb2016-11-29 18:14:09 -08007271
Liran Alon5f76f6f2018-09-14 03:25:52 +03007272 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08007273 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03007274 nested_vmx_entry_exit_ctls_update(vcpu);
7275 }
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007276
7277 if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7278 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7279 update_intel_pt_cfg(vcpu);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007280
7281 if (boot_cpu_has(X86_FEATURE_RTM)) {
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07007282 struct vmx_uret_msr *msr;
Sean Christophersond85a8032020-09-23 11:04:06 -07007283 msr = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007284 if (msr) {
7285 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
Sean Christopherson7bf662b2020-09-23 11:04:07 -07007286 vmx_set_guest_uret_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007287 }
7288 }
Sean Christophersona6337a32020-09-29 21:16:57 -07007289
Sean Christopherson2ed41aa2020-09-29 21:16:58 -07007290 set_cr4_guest_host_mask(vmx);
7291
Sean Christopherson72add912021-04-12 16:21:42 +12007292 vmx_write_encls_bitmap(vcpu, NULL);
7293 if (guest_cpuid_has(vcpu, X86_FEATURE_SGX))
7294 vmx->msr_ia32_feature_control_valid_bits |= FEAT_CTL_SGX_ENABLED;
7295 else
7296 vmx->msr_ia32_feature_control_valid_bits &= ~FEAT_CTL_SGX_ENABLED;
7297
7298 if (guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC))
7299 vmx->msr_ia32_feature_control_valid_bits |=
7300 FEAT_CTL_SGX_LC_ENABLED;
7301 else
7302 vmx->msr_ia32_feature_control_valid_bits &=
7303 ~FEAT_CTL_SGX_LC_ENABLED;
7304
Sean Christophersona6337a32020-09-29 21:16:57 -07007305 /* Refresh #PF interception to account for MAXPHYADDR changes. */
Jason Baronb6a7cc32021-01-14 22:27:54 -05007306 vmx_update_exception_bitmap(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08007307}
7308
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007309static __init void vmx_set_cpu_caps(void)
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007310{
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007311 kvm_set_cpu_caps();
7312
7313 /* CPUID 0x1 */
7314 if (nested)
7315 kvm_cpu_cap_set(X86_FEATURE_VMX);
7316
7317 /* CPUID 0x7 */
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007318 if (kvm_mpx_supported())
7319 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX);
Sean Christophersone4203332021-02-11 16:34:10 -08007320 if (!cpu_has_vmx_invpcid())
7321 kvm_cpu_cap_clear(X86_FEATURE_INVPCID);
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007322 if (vmx_pt_mode_is_host_guest())
7323 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT);
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007324
Sean Christopherson72add912021-04-12 16:21:42 +12007325 if (!enable_sgx) {
7326 kvm_cpu_cap_clear(X86_FEATURE_SGX);
7327 kvm_cpu_cap_clear(X86_FEATURE_SGX_LC);
7328 kvm_cpu_cap_clear(X86_FEATURE_SGX1);
7329 kvm_cpu_cap_clear(X86_FEATURE_SGX2);
7330 }
7331
Sean Christopherson90d2f602020-03-02 15:56:47 -08007332 if (vmx_umip_emulated())
7333 kvm_cpu_cap_set(X86_FEATURE_UMIP);
7334
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007335 /* CPUID 0xD.1 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01007336 supported_xss = 0;
Sean Christophersonbecdad82020-09-23 09:50:45 -07007337 if (!cpu_has_vmx_xsaves())
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007338 kvm_cpu_cap_clear(X86_FEATURE_XSAVES);
7339
Sean Christopherson8aec21c2021-05-04 10:17:20 -07007340 /* CPUID 0x80000001 and 0x7 (RDPID) */
7341 if (!cpu_has_vmx_rdtscp()) {
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007342 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
Sean Christopherson8aec21c2021-05-04 10:17:20 -07007343 kvm_cpu_cap_clear(X86_FEATURE_RDPID);
7344 }
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007345
Sean Christophersonbecdad82020-09-23 09:50:45 -07007346 if (cpu_has_vmx_waitpkg())
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007347 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007348}
7349
Sean Christophersond264ee02018-08-27 15:21:12 -07007350static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7351{
7352 to_vmx(vcpu)->req_immediate_exit = true;
7353}
7354
Oliver Upton35a57132020-02-04 15:26:31 -08007355static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
7356 struct x86_instruction_info *info)
7357{
7358 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7359 unsigned short port;
7360 bool intercept;
7361 int size;
7362
7363 if (info->intercept == x86_intercept_in ||
7364 info->intercept == x86_intercept_ins) {
7365 port = info->src_val;
7366 size = info->dst_bytes;
7367 } else {
7368 port = info->dst_val;
7369 size = info->src_bytes;
7370 }
7371
7372 /*
7373 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
7374 * VM-exits depend on the 'unconditional IO exiting' VM-execution
7375 * control.
7376 *
7377 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
7378 */
7379 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7380 intercept = nested_cpu_has(vmcs12,
7381 CPU_BASED_UNCOND_IO_EXITING);
7382 else
7383 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
7384
Oliver Upton86f7e902020-02-29 11:30:14 -08007385 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
Oliver Upton35a57132020-02-04 15:26:31 -08007386 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
7387}
7388
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007389static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7390 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007391 enum x86_intercept_stage stage,
7392 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007393{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007394 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007395
Oliver Upton35a57132020-02-04 15:26:31 -08007396 switch (info->intercept) {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007397 /*
7398 * RDPID causes #UD if disabled through secondary execution controls.
7399 * Because it is marked as EmulateOnUD, we need to intercept it here.
Sean Christopherson2183de42021-05-04 10:17:23 -07007400 * Note, RDPID is hidden behind ENABLE_RDTSCP.
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007401 */
Sean Christopherson2183de42021-05-04 10:17:23 -07007402 case x86_intercept_rdpid:
Sean Christopherson7f3603b2020-09-23 09:50:47 -07007403 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_RDTSCP)) {
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007404 exception->vector = UD_VECTOR;
7405 exception->error_code_valid = false;
Oliver Upton35a57132020-02-04 15:26:31 -08007406 return X86EMUL_PROPAGATE_FAULT;
7407 }
7408 break;
7409
7410 case x86_intercept_in:
7411 case x86_intercept_ins:
7412 case x86_intercept_out:
7413 case x86_intercept_outs:
7414 return vmx_check_intercept_io(vcpu, info);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007415
Oliver Upton86f7e902020-02-29 11:30:14 -08007416 case x86_intercept_lgdt:
7417 case x86_intercept_lidt:
7418 case x86_intercept_lldt:
7419 case x86_intercept_ltr:
7420 case x86_intercept_sgdt:
7421 case x86_intercept_sidt:
7422 case x86_intercept_sldt:
7423 case x86_intercept_str:
7424 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
7425 return X86EMUL_CONTINUE;
7426
7427 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
7428 break;
7429
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007430 /* TODO: check more intercepts... */
Oliver Upton35a57132020-02-04 15:26:31 -08007431 default:
7432 break;
7433 }
7434
Paolo Bonzini07721fe2020-02-04 15:26:29 -08007435 return X86EMUL_UNHANDLEABLE;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007436}
7437
Yunhong Jiang64672c92016-06-13 14:19:59 -07007438#ifdef CONFIG_X86_64
7439/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
7440static inline int u64_shl_div_u64(u64 a, unsigned int shift,
7441 u64 divisor, u64 *result)
7442{
7443 u64 low = a << shift, high = a >> (64 - shift);
7444
7445 /* To avoid the overflow on divq */
7446 if (high >= divisor)
7447 return 1;
7448
7449 /* Low hold the result, high hold rem which is discarded */
7450 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
7451 "rm" (divisor), "0" (low), "1" (high));
7452 *result = low;
7453
7454 return 0;
7455}
7456
Sean Christophersonf9927982019-04-16 13:32:46 -07007457static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
7458 bool *expired)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007459{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007460 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007461 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
Sean Christopherson39497d72019-04-17 10:15:32 -07007462 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007463
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007464 vmx = to_vmx(vcpu);
7465 tscl = rdtsc();
7466 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
7467 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Sean Christopherson39497d72019-04-17 10:15:32 -07007468 lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
7469 ktimer->timer_advance_ns);
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007470
7471 if (delta_tsc > lapic_timer_advance_cycles)
7472 delta_tsc -= lapic_timer_advance_cycles;
7473 else
7474 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007475
7476 /* Convert to host delta tsc if tsc scaling is enabled */
Ilias Stamatis805d7052021-05-26 19:44:09 +01007477 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
Sean Christopherson0967fa12019-04-16 13:32:48 -07007478 delta_tsc && u64_shl_div_u64(delta_tsc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007479 kvm_tsc_scaling_ratio_frac_bits,
Ilias Stamatis805d7052021-05-26 19:44:09 +01007480 vcpu->arch.l1_tsc_scaling_ratio, &delta_tsc))
Yunhong Jiang64672c92016-06-13 14:19:59 -07007481 return -ERANGE;
7482
7483 /*
7484 * If the delta tsc can't fit in the 32 bit after the multi shift,
7485 * we can't use the preemption timer.
7486 * It's possible that it fits on later vmentries, but checking
7487 * on every vmentry is costly so we just use an hrtimer.
7488 */
7489 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
7490 return -ERANGE;
7491
7492 vmx->hv_deadline_tsc = tscl + delta_tsc;
Sean Christophersonf9927982019-04-16 13:32:46 -07007493 *expired = !delta_tsc;
7494 return 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007495}
7496
7497static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
7498{
Sean Christophersonf459a702018-08-27 15:21:11 -07007499 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007500}
7501#endif
7502
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007503static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007504{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007505 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007506 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007507}
7508
Makarand Sonarea85863c2021-02-12 16:50:12 -08007509void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu)
7510{
7511 struct vcpu_vmx *vmx = to_vmx(vcpu);
7512
7513 if (is_guest_mode(vcpu)) {
7514 vmx->nested.update_vmcs01_cpu_dirty_logging = true;
7515 return;
7516 }
7517
7518 /*
7519 * Note, cpu_dirty_logging_count can be changed concurrent with this
7520 * code, but in that case another update request will be made and so
7521 * the guest will never run with a stale PML value.
7522 */
7523 if (vcpu->kvm->arch.cpu_dirty_logging_count)
7524 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_ENABLE_PML);
7525 else
7526 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_ENABLE_PML);
7527}
7528
Yunhong Jiangbc225122016-06-13 14:19:58 -07007529static int vmx_pre_block(struct kvm_vcpu *vcpu)
7530{
7531 if (pi_pre_block(vcpu))
7532 return 1;
7533
Yunhong Jiang64672c92016-06-13 14:19:59 -07007534 if (kvm_lapic_hv_timer_in_use(vcpu))
7535 kvm_lapic_switch_to_sw_timer(vcpu);
7536
Yunhong Jiangbc225122016-06-13 14:19:58 -07007537 return 0;
7538}
7539
Yunhong Jiangbc225122016-06-13 14:19:58 -07007540static void vmx_post_block(struct kvm_vcpu *vcpu)
7541{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007542 if (kvm_x86_ops.set_hv_timer)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007543 kvm_lapic_switch_to_hv_timer(vcpu);
7544
Yunhong Jiangbc225122016-06-13 14:19:58 -07007545 pi_post_block(vcpu);
7546}
7547
Ashok Rajc45dcc72016-06-22 14:59:56 +08007548static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7549{
7550 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7551 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007552 FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007553 else
7554 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007555 ~FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007556}
7557
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007558static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Ladi Prosek72d7b372017-10-11 16:54:41 +02007559{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007560 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7561 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007562 return -EBUSY;
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -04007563 return !is_smm(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02007564}
7565
Ladi Prosek0234bf82017-10-11 16:54:40 +02007566static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7567{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007568 struct vcpu_vmx *vmx = to_vmx(vcpu);
7569
7570 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7571 if (vmx->nested.smm.guest_mode)
7572 nested_vmx_vmexit(vcpu, -1, 0, 0);
7573
7574 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7575 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007576 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007577 return 0;
7578}
7579
Sean Christophersoned193212019-04-02 08:03:09 -07007580static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007581{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007582 struct vcpu_vmx *vmx = to_vmx(vcpu);
7583 int ret;
7584
7585 if (vmx->nested.smm.vmxon) {
7586 vmx->nested.vmxon = true;
7587 vmx->nested.smm.vmxon = false;
7588 }
7589
7590 if (vmx->nested.smm.guest_mode) {
Sean Christophersona633e412018-09-26 09:23:47 -07007591 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007592 if (ret)
7593 return ret;
7594
7595 vmx->nested.smm.guest_mode = false;
7596 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007597 return 0;
7598}
7599
Jason Baronb6a7cc32021-01-14 22:27:54 -05007600static void vmx_enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007601{
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007602 /* RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007603}
7604
Liran Alon4b9852f2019-08-26 13:24:49 +03007605static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7606{
Paolo Bonzini1c96dcc2020-11-05 11:20:49 -05007607 return to_vmx(vcpu)->nested.vmxon && !is_guest_mode(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03007608}
7609
Jim Mattson93dff2f2020-05-08 13:36:43 -07007610static void vmx_migrate_timers(struct kvm_vcpu *vcpu)
7611{
7612 if (is_guest_mode(vcpu)) {
7613 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer;
7614
7615 if (hrtimer_try_to_cancel(timer) == 1)
7616 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
7617 }
7618}
7619
Sean Christopherson6e4fd062020-03-21 13:26:01 -07007620static void hardware_unsetup(void)
Sean Christophersona3203382018-12-03 13:53:11 -08007621{
7622 if (nested)
7623 nested_vmx_hardware_unsetup();
7624
7625 free_kvm_area();
7626}
7627
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007628static bool vmx_check_apicv_inhibit_reasons(ulong bit)
7629{
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -06007630 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
7631 BIT(APICV_INHIBIT_REASON_HYPERV);
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007632
7633 return supported & BIT(bit);
7634}
7635
Sean Christophersone286ac02020-03-21 13:26:02 -07007636static struct kvm_x86_ops vmx_x86_ops __initdata = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007637 .hardware_unsetup = hardware_unsetup,
Sean Christopherson484014f2020-03-21 13:25:57 -07007638
Avi Kivity6aa8b732006-12-10 02:21:36 -08007639 .hardware_enable = hardware_enable,
7640 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007641 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007642 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007643
Sean Christopherson484014f2020-03-21 13:25:57 -07007644 .vm_size = sizeof(struct kvm_vmx),
Wanpeng Lib31c1142018-03-12 04:53:04 -07007645 .vm_init = vmx_vm_init,
7646
Avi Kivity6aa8b732006-12-10 02:21:36 -08007647 .vcpu_create = vmx_create_vcpu,
7648 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007649 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007650
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007651 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007652 .vcpu_load = vmx_vcpu_load,
7653 .vcpu_put = vmx_vcpu_put,
7654
Jason Baronb6a7cc32021-01-14 22:27:54 -05007655 .update_exception_bitmap = vmx_update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007656 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007657 .get_msr = vmx_get_msr,
7658 .set_msr = vmx_set_msr,
7659 .get_segment_base = vmx_get_segment_base,
7660 .get_segment = vmx_get_segment,
7661 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007662 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007663 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
7664 .set_cr0 = vmx_set_cr0,
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07007665 .is_valid_cr4 = vmx_is_valid_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007666 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007667 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007668 .get_idt = vmx_get_idt,
7669 .set_idt = vmx_set_idt,
7670 .get_gdt = vmx_get_gdt,
7671 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007672 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007673 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007674 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007675 .get_rflags = vmx_get_rflags,
7676 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007677
Sean Christopherson77809382020-03-20 14:28:18 -07007678 .tlb_flush_all = vmx_flush_tlb_all,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07007679 .tlb_flush_current = vmx_flush_tlb_current,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007680 .tlb_flush_gva = vmx_flush_tlb_gva,
Sean Christophersone64419d2020-03-20 14:28:10 -07007681 .tlb_flush_guest = vmx_flush_tlb_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007682
Avi Kivity6aa8b732006-12-10 02:21:36 -08007683 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007684 .handle_exit = vmx_handle_exit,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08007685 .skip_emulated_instruction = vmx_skip_emulated_instruction,
7686 .update_emulated_instruction = vmx_update_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007687 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7688 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007689 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007690 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007691 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007692 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007693 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007694 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007695 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007696 .get_nmi_mask = vmx_get_nmi_mask,
7697 .set_nmi_mask = vmx_set_nmi_mask,
Jason Baronb6a7cc32021-01-14 22:27:54 -05007698 .enable_nmi_window = vmx_enable_nmi_window,
7699 .enable_irq_window = vmx_enable_irq_window,
7700 .update_cr8_intercept = vmx_update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007701 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007702 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007703 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007704 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007705 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007706 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007707 .hwapic_irr_update = vmx_hwapic_irr_update,
7708 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007709 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007710 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7711 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007712 .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007713
Izik Eiduscbc94022007-10-25 00:29:55 +02007714 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007715 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007716 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007717
Avi Kivity586f9602010-11-18 13:09:54 +02007718 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007719
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007720 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007721
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007722 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007723
Ilias Stamatis307a94c2021-05-26 19:44:13 +01007724 .get_l2_tsc_offset = vmx_get_l2_tsc_offset,
7725 .get_l2_tsc_multiplier = vmx_get_l2_tsc_multiplier,
Leonid Shatz326e7422018-11-06 12:14:25 +02007726 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007727
Sean Christopherson484014f2020-03-21 13:25:57 -07007728 .load_mmu_pgd = vmx_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007729
7730 .check_intercept = vmx_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007731 .handle_exit_irqoff = vmx_handle_exit_irqoff,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007732
Sean Christophersond264ee02018-08-27 15:21:12 -07007733 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007734
7735 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007736
Sean Christopherson6dd03802021-02-12 16:50:09 -08007737 .cpu_dirty_log_size = PML_ENTITY_NUM,
Makarand Sonarea85863c2021-02-12 16:50:12 -08007738 .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,
Wei Huang25462f72015-06-19 15:45:05 +02007739
Feng Wubf9f6ac2015-09-18 22:29:55 +08007740 .pre_block = vmx_pre_block,
7741 .post_block = vmx_post_block,
7742
Wei Huang25462f72015-06-19 15:45:05 +02007743 .pmu_ops = &intel_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04007744 .nested_ops = &vmx_nested_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007745
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007746 .update_pi_irte = pi_update_irte,
Marcelo Tosattia2486022021-05-26 14:20:14 -03007747 .start_assignment = vmx_pi_start_assignment,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007748
7749#ifdef CONFIG_X86_64
7750 .set_hv_timer = vmx_set_hv_timer,
7751 .cancel_hv_timer = vmx_cancel_hv_timer,
7752#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007753
7754 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007755
Ladi Prosek72d7b372017-10-11 16:54:41 +02007756 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007757 .pre_enter_smm = vmx_pre_enter_smm,
7758 .pre_leave_smm = vmx_pre_leave_smm,
Jason Baronb6a7cc32021-01-14 22:27:54 -05007759 .enable_smi_window = vmx_enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007760
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007761 .can_emulate_instruction = vmx_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03007762 .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
Jim Mattson93dff2f2020-05-08 13:36:43 -07007763 .migrate_timers = vmx_migrate_timers,
Alexander Graf3eb90012020-09-25 16:34:20 +02007764
7765 .msr_filter_changed = vmx_msr_filter_changed,
Paolo Bonzinif9a4d622020-12-14 10:26:51 -05007766 .complete_emulated_msr = kvm_complete_insn_gp,
Tom Lendacky647daca2021-01-04 14:20:01 -06007767
7768 .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007769};
7770
Sean Christophersonb6194b92021-05-04 10:17:27 -07007771static __init void vmx_setup_user_return_msrs(void)
7772{
Sean Christopherson8ea8b8d2021-05-04 10:17:29 -07007773
7774 /*
7775 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
7776 * will emulate SYSCALL in legacy mode if the vendor string in guest
7777 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
7778 * support this emulation, MSR_STAR is included in the list for i386,
7779 * but is never loaded into hardware. MSR_CSTAR is also never loaded
7780 * into hardware and is here purely for emulation purposes.
7781 */
7782 const u32 vmx_uret_msrs_list[] = {
7783 #ifdef CONFIG_X86_64
7784 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
7785 #endif
7786 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
7787 MSR_IA32_TSX_CTRL,
7788 };
Sean Christophersonb6194b92021-05-04 10:17:27 -07007789 int i;
7790
7791 BUILD_BUG_ON(ARRAY_SIZE(vmx_uret_msrs_list) != MAX_NR_USER_RETURN_MSRS);
7792
Sean Christophersone5fda4b2021-05-04 10:17:32 -07007793 for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i)
7794 kvm_add_user_return_msr(vmx_uret_msrs_list[i]);
Sean Christophersonb6194b92021-05-04 10:17:27 -07007795}
7796
Avi Kivity6aa8b732006-12-10 02:21:36 -08007797static __init int hardware_setup(void)
7798{
7799 unsigned long host_bndcfgs;
7800 struct desc_ptr dt;
Sean Christophersonb6194b92021-05-04 10:17:27 -07007801 int r, ept_lpage_level;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007802
Avi Kivity6aa8b732006-12-10 02:21:36 -08007803 store_idt(&dt);
7804 host_idt_base = dt.address;
7805
Sean Christophersonb6194b92021-05-04 10:17:27 -07007806 vmx_setup_user_return_msrs();
Avi Kivity6aa8b732006-12-10 02:21:36 -08007807
7808 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
7809 return -EIO;
7810
7811 if (boot_cpu_has(X86_FEATURE_NX))
7812 kvm_enable_efer_bits(EFER_NX);
7813
7814 if (boot_cpu_has(X86_FEATURE_MPX)) {
7815 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7816 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7817 }
7818
Sean Christopherson7f5581f2020-03-02 15:56:24 -08007819 if (!cpu_has_vmx_mpx())
Sean Christophersoncfc48182020-03-02 15:56:23 -08007820 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
7821 XFEATURE_MASK_BNDCSR);
7822
Avi Kivity6aa8b732006-12-10 02:21:36 -08007823 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7824 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7825 enable_vpid = 0;
7826
7827 if (!cpu_has_vmx_ept() ||
7828 !cpu_has_vmx_ept_4levels() ||
7829 !cpu_has_vmx_ept_mt_wb() ||
7830 !cpu_has_vmx_invept_global())
7831 enable_ept = 0;
7832
7833 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7834 enable_ept_ad_bits = 0;
7835
7836 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Avi Kivity873a7c42006-12-13 00:34:14 -08007837 enable_unrestricted_guest = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007838
7839 if (!cpu_has_vmx_flexpriority())
7840 flexpriority_enabled = 0;
7841
7842 if (!cpu_has_virtual_nmis())
7843 enable_vnmi = 0;
7844
7845 /*
7846 * set_apic_access_page_addr() is used to reload apic access
7847 * page upon invalidation. No need to do anything if not
7848 * using the APIC_ACCESS_ADDR VMCS field.
7849 */
7850 if (!flexpriority_enabled)
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007851 vmx_x86_ops.set_apic_access_page_addr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007852
7853 if (!cpu_has_vmx_tpr_shadow())
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007854 vmx_x86_ops.update_cr8_intercept = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007855
Avi Kivity6aa8b732006-12-10 02:21:36 -08007856#if IS_ENABLED(CONFIG_HYPERV)
7857 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7858 && enable_ept) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007859 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
7860 vmx_x86_ops.tlb_remote_flush_with_range =
Avi Kivity6aa8b732006-12-10 02:21:36 -08007861 hv_remote_flush_tlb_with_range;
7862 }
7863#endif
7864
7865 if (!cpu_has_vmx_ple()) {
7866 ple_gap = 0;
7867 ple_window = 0;
7868 ple_window_grow = 0;
7869 ple_window_max = 0;
7870 ple_window_shrink = 0;
7871 }
7872
7873 if (!cpu_has_vmx_apicv()) {
7874 enable_apicv = 0;
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007875 vmx_x86_ops.sync_pir_to_irr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007876 }
7877
7878 if (cpu_has_vmx_tsc_scaling()) {
7879 kvm_has_tsc_control = true;
7880 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7881 kvm_tsc_scaling_ratio_frac_bits = 48;
7882 }
7883
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08007884 kvm_has_bus_lock_exit = cpu_has_vmx_bus_lock_detection();
7885
Avi Kivity6aa8b732006-12-10 02:21:36 -08007886 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7887
7888 if (enable_ept)
Sean Christophersone7b7bde2021-02-25 12:47:42 -08007889 kvm_mmu_set_ept_masks(enable_ept_ad_bits,
7890 cpu_has_vmx_ept_execute_only());
Sean Christopherson703c3352020-03-02 15:57:03 -08007891
7892 if (!enable_ept)
7893 ept_lpage_level = 0;
7894 else if (cpu_has_vmx_ept_1g_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007895 ept_lpage_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08007896 else if (cpu_has_vmx_ept_2m_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007897 ept_lpage_level = PG_LEVEL_2M;
Sean Christopherson703c3352020-03-02 15:57:03 -08007898 else
Sean Christopherson3bae0452020-04-27 17:54:22 -07007899 ept_lpage_level = PG_LEVEL_4K;
Sean Christopherson83013052020-07-15 20:41:22 -07007900 kvm_configure_mmu(enable_ept, vmx_get_max_tdp_level(), ept_lpage_level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007901
7902 /*
7903 * Only enable PML when hardware supports PML feature, and both EPT
7904 * and EPT A/D bit features are enabled -- PML depends on them to work.
7905 */
7906 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7907 enable_pml = 0;
7908
Sean Christophersona018eba2021-02-12 16:50:10 -08007909 if (!enable_pml)
Sean Christopherson6dd03802021-02-12 16:50:09 -08007910 vmx_x86_ops.cpu_dirty_log_size = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007911
7912 if (!cpu_has_vmx_preemption_timer())
7913 enable_preemption_timer = false;
7914
7915 if (enable_preemption_timer) {
7916 u64 use_timer_freq = 5000ULL * 1000 * 1000;
7917 u64 vmx_msr;
7918
7919 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7920 cpu_preemption_timer_multi =
7921 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7922
7923 if (tsc_khz)
7924 use_timer_freq = (u64)tsc_khz * 1000;
7925 use_timer_freq >>= cpu_preemption_timer_multi;
7926
7927 /*
7928 * KVM "disables" the preemption timer by setting it to its max
7929 * value. Don't use the timer if it might cause spurious exits
7930 * at a rate faster than 0.1 Hz (of uninterrupted guest time).
7931 */
7932 if (use_timer_freq > 0xffffffffu / 10)
7933 enable_preemption_timer = false;
7934 }
7935
7936 if (!enable_preemption_timer) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007937 vmx_x86_ops.set_hv_timer = NULL;
7938 vmx_x86_ops.cancel_hv_timer = NULL;
7939 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007940 }
7941
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007942 kvm_set_posted_intr_wakeup_handler(pi_wakeup_handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007943
7944 kvm_mce_cap_supported |= MCG_LMCE_P;
7945
7946 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
7947 return -EINVAL;
7948 if (!enable_ept || !cpu_has_vmx_intel_pt())
7949 pt_mode = PT_MODE_SYSTEM;
7950
Sean Christopherson8f102442021-04-12 16:21:40 +12007951 setup_default_sgx_lepubkeyhash();
7952
Avi Kivity6aa8b732006-12-10 02:21:36 -08007953 if (nested) {
7954 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
7955 vmx_capability.ept);
7956
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07007957 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007958 if (r)
7959 return r;
7960 }
7961
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007962 vmx_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08007963
Avi Kivity6aa8b732006-12-10 02:21:36 -08007964 r = alloc_kvm_area();
7965 if (r)
7966 nested_vmx_hardware_unsetup();
7967 return r;
7968}
7969
Sean Christophersond008dfd2020-03-21 13:25:56 -07007970static struct kvm_x86_init_ops vmx_init_ops __initdata = {
7971 .cpu_has_kvm_support = cpu_has_kvm_support,
7972 .disabled_by_bios = vmx_disabled_by_bios,
7973 .check_processor_compatibility = vmx_check_processor_compat,
7974 .hardware_setup = hardware_setup,
7975
7976 .runtime_ops = &vmx_x86_ops,
7977};
7978
Avi Kivity6aa8b732006-12-10 02:21:36 -08007979static void vmx_cleanup_l1d_flush(void)
7980{
7981 if (vmx_l1d_flush_pages) {
7982 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
7983 vmx_l1d_flush_pages = NULL;
7984 }
7985 /* Restore state so sysfs ignores VMX */
7986 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
7987}
7988
7989static void vmx_exit(void)
7990{
7991#ifdef CONFIG_KEXEC_CORE
7992 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
7993 synchronize_rcu();
7994#endif
7995
7996 kvm_exit();
7997
7998#if IS_ENABLED(CONFIG_HYPERV)
7999 if (static_branch_unlikely(&enable_evmcs)) {
8000 int cpu;
8001 struct hv_vp_assist_page *vp_ap;
8002 /*
8003 * Reset everything to support using non-enlightened VMCS
8004 * access later (e.g. when we reload the module with
8005 * enlightened_vmcs=0)
8006 */
8007 for_each_online_cpu(cpu) {
8008 vp_ap = hv_get_vp_assist_page(cpu);
8009
8010 if (!vp_ap)
8011 continue;
8012
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008013 vp_ap->nested_control.features.directhypercall = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008014 vp_ap->current_nested_vmcs = 0;
8015 vp_ap->enlighten_vmentry = 0;
8016 }
8017
8018 static_branch_disable(&enable_evmcs);
8019 }
8020#endif
8021 vmx_cleanup_l1d_flush();
8022}
8023module_exit(vmx_exit);
8024
8025static int __init vmx_init(void)
8026{
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008027 int r, cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008028
8029#if IS_ENABLED(CONFIG_HYPERV)
8030 /*
8031 * Enlightened VMCS usage should be recommended and the host needs
8032 * to support eVMCS v1 or above. We can also disable eVMCS support
8033 * with module parameter.
8034 */
8035 if (enlightened_vmcs &&
8036 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
8037 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
8038 KVM_EVMCS_VERSION) {
8039 int cpu;
8040
8041 /* Check that we have assist pages on all online CPUs */
8042 for_each_online_cpu(cpu) {
8043 if (!hv_get_vp_assist_page(cpu)) {
8044 enlightened_vmcs = false;
8045 break;
8046 }
8047 }
8048
8049 if (enlightened_vmcs) {
8050 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
8051 static_branch_enable(&enable_evmcs);
8052 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008053
8054 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
8055 vmx_x86_ops.enable_direct_tlbflush
8056 = hv_enable_direct_tlbflush;
8057
Avi Kivity6aa8b732006-12-10 02:21:36 -08008058 } else {
8059 enlightened_vmcs = false;
8060 }
8061#endif
8062
Sean Christophersond008dfd2020-03-21 13:25:56 -07008063 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008064 __alignof__(struct vcpu_vmx), THIS_MODULE);
8065 if (r)
8066 return r;
8067
8068 /*
8069 * Must be called after kvm_init() so enable_ept is properly set
8070 * up. Hand the parameter mitigation value in which was stored in
8071 * the pre module init parser. If no parameter was given, it will
8072 * contain 'auto' which will be turned into the default 'cond'
8073 * mitigation mode.
8074 */
Waiman Long19a36d32019-08-26 15:30:23 -04008075 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
8076 if (r) {
8077 vmx_exit();
8078 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008079 }
8080
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008081 for_each_possible_cpu(cpu) {
8082 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07008083
Paolo Bonzinia3ff25f2020-10-24 04:08:37 -04008084 pi_init_cpu(cpu);
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008085 }
8086
Avi Kivity6aa8b732006-12-10 02:21:36 -08008087#ifdef CONFIG_KEXEC_CORE
8088 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8089 crash_vmclear_local_loaded_vmcss);
8090#endif
8091 vmx_check_vmcs12_offsets();
8092
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008093 /*
Mohammed Gamalb96e6502020-09-03 16:11:22 +02008094 * Shadow paging doesn't have a (further) performance penalty
8095 * from GUEST_MAXPHYADDR < HOST_MAXPHYADDR so enable it
8096 * by default
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008097 */
Mohammed Gamalb96e6502020-09-03 16:11:22 +02008098 if (!enable_ept)
8099 allow_smaller_maxphyaddr = true;
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008100
Avi Kivity6aa8b732006-12-10 02:21:36 -08008101 return 0;
8102}
8103module_init(vmx_init);