blob: d000cddbd7349dc81aa0bf4731d971dda0e95b92 [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
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300458/*
Jim Mattson898a8112018-12-05 15:28:59 -0800459 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
460 * will emulate SYSCALL in legacy mode if the vendor string in guest
461 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
462 * support this emulation, IA32_STAR must always be included in
Sean Christopherson14a61b62020-09-23 11:04:08 -0700463 * vmx_uret_msrs_list[], even in i386 builds.
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300464 */
kernel test robot6a2e0922020-09-28 23:37:14 +0800465static const u32 vmx_uret_msrs_list[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800466#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300467 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400469 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Paolo Bonzinic11f83e2019-11-18 12:23:00 -0500470 MSR_IA32_TSX_CTRL,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100473#if IS_ENABLED(CONFIG_HYPERV)
474static bool __read_mostly enlightened_vmcs = true;
475module_param(enlightened_vmcs, bool, 0444);
476
Yi Wang8997f652019-01-21 15:27:05 +0800477static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800478 void *data)
479{
480 struct kvm_tlb_range *range = data;
481
482 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
483 range->pages);
484}
485
Sean Christopherson978c8342021-03-05 10:31:23 -0800486static inline int hv_remote_flush_root_ept(hpa_t root_ept,
487 struct kvm_tlb_range *range)
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800488{
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800489 if (range)
Sean Christopherson978c8342021-03-05 10:31:23 -0800490 return hyperv_flush_guest_mapping_range(root_ept,
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800491 kvm_fill_hv_flush_list_func, (void *)range);
492 else
Sean Christopherson978c8342021-03-05 10:31:23 -0800493 return hyperv_flush_guest_mapping(root_ept);
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800494}
495
496static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
497 struct kvm_tlb_range *range)
Tianyu Lan877ad952018-07-19 08:40:23 +0000498{
Sean Christophersonb68aa15c2021-03-05 10:31:15 -0800499 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Lan Tianyua5c214d2018-10-13 22:54:05 +0800500 struct kvm_vcpu *vcpu;
Sean Christopherson978c8342021-03-05 10:31:23 -0800501 int ret = 0, i, nr_unique_valid_roots;
502 hpa_t root;
Tianyu Lan877ad952018-07-19 08:40:23 +0000503
Sean Christopherson978c8342021-03-05 10:31:23 -0800504 spin_lock(&kvm_vmx->hv_root_ept_lock);
Tianyu Lan877ad952018-07-19 08:40:23 +0000505
Sean Christopherson978c8342021-03-05 10:31:23 -0800506 if (!VALID_PAGE(kvm_vmx->hv_root_ept)) {
507 nr_unique_valid_roots = 0;
Tianyu Lan877ad952018-07-19 08:40:23 +0000508
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800509 /*
Sean Christopherson978c8342021-03-05 10:31:23 -0800510 * Flush all valid roots, and see if all vCPUs have converged
511 * on a common root, in which case future flushes can skip the
512 * loop and flush the common root.
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800513 */
Lan Tianyu53963a72018-12-06 15:34:36 +0800514 kvm_for_each_vcpu(i, vcpu, kvm) {
Sean Christopherson978c8342021-03-05 10:31:23 -0800515 root = to_vmx(vcpu)->hv_root_ept;
516 if (!VALID_PAGE(root) || root == kvm_vmx->hv_root_ept)
Sean Christopherson288bee22021-03-05 10:31:16 -0800517 continue;
518
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800519 /*
Sean Christopherson978c8342021-03-05 10:31:23 -0800520 * Set the tracked root to the first valid root. Keep
521 * this root for the entirety of the loop even if more
522 * roots are encountered as a low effort optimization
523 * to avoid flushing the same (first) root again.
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800524 */
Sean Christopherson978c8342021-03-05 10:31:23 -0800525 if (++nr_unique_valid_roots == 1)
526 kvm_vmx->hv_root_ept = root;
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +0200527
Sean Christopherson14072e52021-03-05 10:31:22 -0800528 if (!ret)
Sean Christopherson978c8342021-03-05 10:31:23 -0800529 ret = hv_remote_flush_root_ept(root, range);
Sean Christopherson14072e52021-03-05 10:31:22 -0800530
531 /*
Sean Christopherson978c8342021-03-05 10:31:23 -0800532 * Stop processing roots if a failure occurred and
533 * multiple valid roots have already been detected.
Sean Christopherson14072e52021-03-05 10:31:22 -0800534 */
Sean Christopherson978c8342021-03-05 10:31:23 -0800535 if (ret && nr_unique_valid_roots > 1)
Sean Christopherson14072e52021-03-05 10:31:22 -0800536 break;
Lan Tianyu53963a72018-12-06 15:34:36 +0800537 }
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800538
539 /*
Sean Christopherson978c8342021-03-05 10:31:23 -0800540 * The optimized flush of a single root can't be used if there
541 * are multiple valid roots (obviously).
Sean Christophersoncdbd4b42021-03-05 10:31:18 -0800542 */
Sean Christopherson978c8342021-03-05 10:31:23 -0800543 if (nr_unique_valid_roots > 1)
544 kvm_vmx->hv_root_ept = INVALID_PAGE;
Lan Tianyua5c214d2018-10-13 22:54:05 +0800545 } else {
Sean Christopherson978c8342021-03-05 10:31:23 -0800546 ret = hv_remote_flush_root_ept(kvm_vmx->hv_root_ept, range);
Tianyu Lan877ad952018-07-19 08:40:23 +0000547 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000548
Sean Christopherson978c8342021-03-05 10:31:23 -0800549 spin_unlock(&kvm_vmx->hv_root_ept_lock);
Tianyu Lan877ad952018-07-19 08:40:23 +0000550 return ret;
551}
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800552static int hv_remote_flush_tlb(struct kvm *kvm)
553{
554 return hv_remote_flush_tlb_with_range(kvm, NULL);
555}
556
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800557static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)
558{
559 struct hv_enlightened_vmcs *evmcs;
560 struct hv_partition_assist_pg **p_hv_pa_pg =
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100561 &to_kvm_hv(vcpu->kvm)->hv_pa_pg;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800562 /*
563 * Synthetic VM-Exit is not enabled in current code and so All
564 * evmcs in singe VM shares same assist page.
565 */
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200566 if (!*p_hv_pa_pg)
Sean Christophersoneba04b22021-03-30 19:30:25 -0700567 *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL_ACCOUNT);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200568
569 if (!*p_hv_pa_pg)
570 return -ENOMEM;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800571
572 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs;
573
574 evmcs->partition_assist_page =
575 __pa(*p_hv_pa_pg);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200576 evmcs->hv_vm_id = (unsigned long)vcpu->kvm;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800577 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;
578
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800579 return 0;
580}
581
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100582#endif /* IS_ENABLED(CONFIG_HYPERV) */
583
Sean Christopherson978c8342021-03-05 10:31:23 -0800584static void hv_track_root_ept(struct kvm_vcpu *vcpu, hpa_t root_ept)
Sean Christophersonc82f1b62021-03-05 10:31:20 -0800585{
586#if IS_ENABLED(CONFIG_HYPERV)
587 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
588
589 if (kvm_x86_ops.tlb_remote_flush == hv_remote_flush_tlb) {
Sean Christopherson978c8342021-03-05 10:31:23 -0800590 spin_lock(&kvm_vmx->hv_root_ept_lock);
591 to_vmx(vcpu)->hv_root_ept = root_ept;
592 if (root_ept != kvm_vmx->hv_root_ept)
593 kvm_vmx->hv_root_ept = INVALID_PAGE;
594 spin_unlock(&kvm_vmx->hv_root_ept_lock);
Sean Christophersonc82f1b62021-03-05 10:31:20 -0800595 }
596#endif
597}
598
Yunhong Jiang64672c92016-06-13 14:19:59 -0700599/*
600 * Comment's format: document - errata name - stepping - processor name.
601 * Refer from
602 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
603 */
604static u32 vmx_preemption_cpu_tfms[] = {
605/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
6060x000206E6,
607/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
608/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
609/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
6100x00020652,
611/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
6120x00020655,
613/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
614/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
615/*
616 * 320767.pdf - AAP86 - B1 -
617 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
618 */
6190x000106E5,
620/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
6210x000106A0,
622/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
6230x000106A1,
624/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
6250x000106A4,
626 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
627 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
628 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
6290x000106A5,
Wei Huang3d82c562018-12-03 14:13:32 -0600630 /* Xeon E3-1220 V2 */
6310x000306A8,
Yunhong Jiang64672c92016-06-13 14:19:59 -0700632};
633
634static inline bool cpu_has_broken_vmx_preemption_timer(void)
635{
636 u32 eax = cpuid_eax(0x00000001), i;
637
638 /* Clear the reserved bits */
639 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000640 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700641 if (eax == vmx_preemption_cpu_tfms[i])
642 return true;
643
644 return false;
645}
646
Paolo Bonzini35754c92015-07-29 12:05:37 +0200647static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800648{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200649 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800650}
651
Sheng Yang04547152009-04-01 15:52:31 +0800652static inline bool report_flexpriority(void)
653{
654 return flexpriority_enabled;
655}
656
Alexander Graf3eb90012020-09-25 16:34:20 +0200657static int possible_passthrough_msr_slot(u32 msr)
658{
659 u32 i;
660
661 for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++)
662 if (vmx_possible_passthrough_msrs[i] == msr)
663 return i;
664
665 return -ENOENT;
666}
667
668static bool is_valid_passthrough_msr(u32 msr)
669{
670 bool r;
671
672 switch (msr) {
673 case 0x800 ... 0x8ff:
674 /* x2APIC MSRs. These are handled in vmx_update_msr_bitmap_x2apic() */
675 return true;
676 case MSR_IA32_RTIT_STATUS:
677 case MSR_IA32_RTIT_OUTPUT_BASE:
678 case MSR_IA32_RTIT_OUTPUT_MASK:
679 case MSR_IA32_RTIT_CR3_MATCH:
680 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
681 /* PT MSRs. These are handled in pt_update_intercept_for_msr() */
Like Xu1b5ac3222021-02-01 13:10:34 +0800682 case MSR_LBR_SELECT:
683 case MSR_LBR_TOS:
684 case MSR_LBR_INFO_0 ... MSR_LBR_INFO_0 + 31:
685 case MSR_LBR_NHM_FROM ... MSR_LBR_NHM_FROM + 31:
686 case MSR_LBR_NHM_TO ... MSR_LBR_NHM_TO + 31:
687 case MSR_LBR_CORE_FROM ... MSR_LBR_CORE_FROM + 8:
688 case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8:
689 /* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */
Alexander Graf3eb90012020-09-25 16:34:20 +0200690 return true;
691 }
692
693 r = possible_passthrough_msr_slot(msr) != -ENOENT;
694
695 WARN(!r, "Invalid MSR %x, please adapt vmx_possible_passthrough_msrs[]", msr);
696
697 return r;
698}
699
Sean Christopherson1e7a4832020-09-23 11:04:02 -0700700static inline int __vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800701{
702 int i;
703
Sean Christophersonfbc18002020-09-23 11:03:59 -0700704 for (i = 0; i < vmx->nr_uret_msrs; ++i)
Sean Christopherson802145c2020-09-23 11:04:09 -0700705 if (vmx_uret_msrs_list[vmx->guest_uret_msrs[i].slot] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300706 return i;
707 return -1;
708}
709
Sean Christophersond85a8032020-09-23 11:04:06 -0700710struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300711{
712 int i;
713
Sean Christopherson1e7a4832020-09-23 11:04:02 -0700714 i = __vmx_find_uret_msr(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300715 if (i >= 0)
Sean Christophersoneb3db1b2020-09-23 11:03:58 -0700716 return &vmx->guest_uret_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000717 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800718}
719
Sean Christopherson7bf662b2020-09-23 11:04:07 -0700720static int vmx_set_guest_uret_msr(struct vcpu_vmx *vmx,
721 struct vmx_uret_msr *msr, u64 data)
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500722{
723 int ret = 0;
724
725 u64 old_msr_data = msr->data;
726 msr->data = data;
Sean Christophersone9bb1ae2020-09-23 11:04:00 -0700727 if (msr - vmx->guest_uret_msrs < vmx->nr_active_uret_msrs) {
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500728 preempt_disable();
Sean Christopherson802145c2020-09-23 11:04:09 -0700729 ret = kvm_set_user_return_msr(msr->slot, msr->data, msr->mask);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500730 preempt_enable();
731 if (ret)
732 msr->data = old_msr_data;
733 }
734 return ret;
735}
736
Dave Young2965faa2015-09-09 15:38:55 -0700737#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800738static void crash_vmclear_local_loaded_vmcss(void)
739{
740 int cpu = raw_smp_processor_id();
741 struct loaded_vmcs *v;
742
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800743 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
744 loaded_vmcss_on_cpu_link)
745 vmcs_clear(v->vmcs);
746}
Dave Young2965faa2015-09-09 15:38:55 -0700747#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800748
Nadav Har'Eld462b812011-05-24 15:26:10 +0300749static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300751 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800752 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753
Nadav Har'Eld462b812011-05-24 15:26:10 +0300754 if (loaded_vmcs->cpu != cpu)
755 return; /* vcpu migration can race with cpu offline */
756 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 per_cpu(current_vmcs, cpu) = NULL;
Sean Christopherson31603d42020-03-21 12:37:49 -0700758
759 vmcs_clear(loaded_vmcs->vmcs);
760 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
761 vmcs_clear(loaded_vmcs->shadow_vmcs);
762
Nadav Har'Eld462b812011-05-24 15:26:10 +0300763 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800764
765 /*
Sean Christopherson31603d42020-03-21 12:37:49 -0700766 * Ensure all writes to loaded_vmcs, including deleting it from its
767 * current percpu list, complete before setting loaded_vmcs->vcpu to
768 * -1, otherwise a different cpu can see vcpu == -1 first and add
769 * loaded_vmcs to its percpu list before it's deleted from this cpu's
770 * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800771 */
772 smp_wmb();
773
Sean Christopherson31603d42020-03-21 12:37:49 -0700774 loaded_vmcs->cpu = -1;
775 loaded_vmcs->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776}
777
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800778void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800779{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800780 int cpu = loaded_vmcs->cpu;
781
782 if (cpu != -1)
783 smp_call_function_single(cpu,
784 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800785}
786
Avi Kivity2fb92db2011-04-27 19:42:18 +0300787static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
788 unsigned field)
789{
790 bool ret;
791 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
792
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700793 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) {
794 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS);
Avi Kivity2fb92db2011-04-27 19:42:18 +0300795 vmx->segment_cache.bitmask = 0;
796 }
797 ret = vmx->segment_cache.bitmask & mask;
798 vmx->segment_cache.bitmask |= mask;
799 return ret;
800}
801
802static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
803{
804 u16 *p = &vmx->segment_cache.seg[seg].selector;
805
806 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
807 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
808 return *p;
809}
810
811static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
812{
813 ulong *p = &vmx->segment_cache.seg[seg].base;
814
815 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
816 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
817 return *p;
818}
819
820static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
821{
822 u32 *p = &vmx->segment_cache.seg[seg].limit;
823
824 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
825 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
826 return *p;
827}
828
829static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
830{
831 u32 *p = &vmx->segment_cache.seg[seg].ar;
832
833 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
834 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
835 return *p;
836}
837
Jason Baronb6a7cc32021-01-14 22:27:54 -0500838void vmx_update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300839{
840 u32 eb;
841
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100842 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800843 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200844 /*
845 * Guest access to VMware backdoor ports could legitimately
846 * trigger #GP because of TSS I/O permission bitmap.
847 * We intercept those #GP and allow access to them anyway
848 * as VMware does.
849 */
850 if (enable_vmware_backdoor)
851 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100852 if ((vcpu->guest_debug &
853 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
854 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
855 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300856 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300857 eb = ~0;
Paolo Bonzinia0c13432020-07-10 17:48:08 +0200858 if (!vmx_need_pf_intercept(vcpu))
Miaohe Lin49f933d2020-02-27 11:20:54 +0800859 eb &= ~(1u << PF_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300860
861 /* When we are running a nested L2 guest and L1 specified for it a
862 * certain exception bitmap, we must trap the same exceptions and pass
863 * them to L1. When running L2, we will only handle the exceptions
864 * specified above if L1 did not want them.
865 */
866 if (is_guest_mode(vcpu))
867 eb |= get_vmcs12(vcpu)->exception_bitmap;
Paolo Bonzinib502e6e2020-09-29 08:31:32 -0400868 else {
869 /*
870 * If EPT is enabled, #PF is only trapped if MAXPHYADDR is mismatched
871 * between guest and host. In that case we only care about present
872 * faults. For vmcs02, however, PFEC_MASK and PFEC_MATCH are set in
873 * prepare_vmcs02_rare.
874 */
875 bool selective_pf_trap = enable_ept && (eb & (1u << PF_VECTOR));
876 int mask = selective_pf_trap ? PFERR_PRESENT_MASK : 0;
877 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, mask);
878 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, mask);
879 }
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300880
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300881 vmcs_write32(EXCEPTION_BITMAP, eb);
882}
883
Ashok Raj15d45072018-02-01 22:59:43 +0100884/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100885 * Check if MSR is intercepted for currently loaded MSR bitmap.
886 */
887static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
888{
889 unsigned long *msr_bitmap;
890 int f = sizeof(unsigned long);
891
892 if (!cpu_has_vmx_msr_bitmap())
893 return true;
894
895 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
896
897 if (msr <= 0x1fff) {
898 return !!test_bit(msr, msr_bitmap + 0x800 / f);
899 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
900 msr &= 0x1fff;
901 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
902 }
903
904 return true;
905}
906
Gleb Natapov2961e8762013-11-25 15:37:13 +0200907static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
908 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200909{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200910 vm_entry_controls_clearbit(vmx, entry);
911 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200912}
913
Sean Christophersona128a932020-09-23 11:03:57 -0700914int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr)
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400915{
916 unsigned int i;
917
918 for (i = 0; i < m->nr; ++i) {
919 if (m->val[i].index == msr)
920 return i;
921 }
922 return -ENOENT;
923}
924
Avi Kivity61d2ef22010-04-28 16:40:38 +0300925static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
926{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400927 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300928 struct msr_autoload *m = &vmx->msr_autoload;
929
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200930 switch (msr) {
931 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800932 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200933 clear_atomic_switch_msr_special(vmx,
934 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200935 VM_EXIT_LOAD_IA32_EFER);
936 return;
937 }
938 break;
939 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800940 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200941 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200942 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
943 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
944 return;
945 }
946 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200947 }
Sean Christophersona128a932020-09-23 11:03:57 -0700948 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400949 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400950 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400951 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400952 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400953 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200954
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400955skip_guest:
Sean Christophersona128a932020-09-23 11:03:57 -0700956 i = vmx_find_loadstore_msr_slot(&m->host, msr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400957 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300958 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400959
960 --m->host.nr;
961 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400962 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300963}
964
Gleb Natapov2961e8762013-11-25 15:37:13 +0200965static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
966 unsigned long entry, unsigned long exit,
967 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
968 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200969{
970 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700971 if (host_val_vmcs != HOST_IA32_EFER)
972 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200973 vm_entry_controls_setbit(vmx, entry);
974 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200975}
976
Avi Kivity61d2ef22010-04-28 16:40:38 +0300977static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400978 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300979{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400980 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300981 struct msr_autoload *m = &vmx->msr_autoload;
982
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200983 switch (msr) {
984 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800985 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200986 add_atomic_switch_msr_special(vmx,
987 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200988 VM_EXIT_LOAD_IA32_EFER,
989 GUEST_IA32_EFER,
990 HOST_IA32_EFER,
991 guest_val, host_val);
992 return;
993 }
994 break;
995 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800996 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200997 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200998 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
999 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1000 GUEST_IA32_PERF_GLOBAL_CTRL,
1001 HOST_IA32_PERF_GLOBAL_CTRL,
1002 guest_val, host_val);
1003 return;
1004 }
1005 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001006 case MSR_IA32_PEBS_ENABLE:
1007 /* PEBS needs a quiescent period after being disabled (to write
1008 * a record). Disabling PEBS through VMX MSR swapping doesn't
1009 * provide that period, so a CPU could write host's record into
1010 * guest's memory.
1011 */
1012 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001013 }
1014
Sean Christophersona128a932020-09-23 11:03:57 -07001015 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001016 if (!entry_only)
Sean Christophersona128a932020-09-23 11:03:57 -07001017 j = vmx_find_loadstore_msr_slot(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03001018
Sean Christophersonce833b22020-09-23 11:03:56 -07001019 if ((i < 0 && m->guest.nr == MAX_NR_LOADSTORE_MSRS) ||
1020 (j < 0 && m->host.nr == MAX_NR_LOADSTORE_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001021 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001022 "Can't add msr %x\n", msr);
1023 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001024 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001025 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04001026 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001027 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001028 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001029 m->guest.val[i].index = msr;
1030 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001031
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001032 if (entry_only)
1033 return;
1034
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001035 if (j < 0) {
1036 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001037 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03001038 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001039 m->host.val[j].index = msr;
1040 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001041}
1042
Sean Christopherson86e3e492020-09-23 11:04:04 -07001043static bool update_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +03001044{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001045 u64 guest_efer = vmx->vcpu.arch.efer;
1046 u64 ignore_bits = 0;
Sean Christopherson86e3e492020-09-23 11:04:04 -07001047 int i;
Eddie Dong2cc51562007-05-21 07:28:09 +03001048
Paolo Bonzini9167ab72019-10-27 16:23:23 +01001049 /* Shadow paging assumes NX to be available. */
1050 if (!enable_ept)
1051 guest_efer |= EFER_NX;
Roel Kluin3a34a882009-08-04 02:08:45 -07001052
Avi Kivity51c6cf62007-08-29 03:48:05 +03001053 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001054 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03001055 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001056 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001057#ifdef CONFIG_X86_64
1058 ignore_bits |= EFER_LMA | EFER_LME;
1059 /* SCE is meaningful only in long mode on Intel */
1060 if (guest_efer & EFER_LMA)
1061 ignore_bits &= ~(u64)EFER_SCE;
1062#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03001063
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001064 /*
1065 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1066 * On CPUs that support "load IA32_EFER", always switch EFER
1067 * atomically, since it's faster than switching it manually.
1068 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -08001069 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001070 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03001071 if (!(guest_efer & EFER_LMA))
1072 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001073 if (guest_efer != host_efer)
1074 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001075 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07001076 else
1077 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03001078 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001079 }
Sean Christopherson86e3e492020-09-23 11:04:04 -07001080
1081 i = __vmx_find_uret_msr(vmx, MSR_EFER);
1082 if (i < 0)
1083 return false;
1084
1085 clear_atomic_switch_msr(vmx, MSR_EFER);
1086
1087 guest_efer &= ~ignore_bits;
1088 guest_efer |= host_efer & ignore_bits;
1089
1090 vmx->guest_uret_msrs[i].data = guest_efer;
1091 vmx->guest_uret_msrs[i].mask = ~ignore_bits;
1092
1093 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001094}
1095
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001096#ifdef CONFIG_X86_32
1097/*
1098 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1099 * VMCS rather than the segment table. KVM uses this helper to figure
1100 * out the current bases to poke them into the VMCS before entry.
1101 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001102static unsigned long segment_base(u16 selector)
1103{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001104 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001105 unsigned long v;
1106
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001107 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001108 return 0;
1109
Thomas Garnier45fc8752017-03-14 10:05:08 -07001110 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001111
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001112 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001113 u16 ldt_selector = kvm_read_ldt();
1114
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001115 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001116 return 0;
1117
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001118 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001119 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001120 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001121 return v;
1122}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001123#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001124
Sean Christophersone348ac72019-12-10 15:24:33 -08001125static inline bool pt_can_write_msr(struct vcpu_vmx *vmx)
1126{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001127 return vmx_pt_mode_is_host_guest() &&
Sean Christophersone348ac72019-12-10 15:24:33 -08001128 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
1129}
1130
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001131static inline bool pt_output_base_valid(struct kvm_vcpu *vcpu, u64 base)
1132{
1133 /* The base must be 128-byte aligned and a legal physical address. */
Sean Christopherson636e8b72021-02-03 16:01:10 -08001134 return kvm_vcpu_is_legal_aligned_gpa(vcpu, base, 128);
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001135}
1136
Chao Peng2ef444f2018-10-24 16:05:12 +08001137static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range)
1138{
1139 u32 i;
1140
1141 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1142 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1143 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1144 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1145 for (i = 0; i < addr_range; i++) {
1146 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1147 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1148 }
1149}
1150
1151static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range)
1152{
1153 u32 i;
1154
1155 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1156 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1157 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1158 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1159 for (i = 0; i < addr_range; i++) {
1160 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1161 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1162 }
1163}
1164
1165static void pt_guest_enter(struct vcpu_vmx *vmx)
1166{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001167 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001168 return;
1169
Chao Peng2ef444f2018-10-24 16:05:12 +08001170 /*
Chao Pengb08c2892018-10-24 16:05:15 +08001171 * GUEST_IA32_RTIT_CTL is already set in the VMCS.
1172 * Save host state before VM entry.
Chao Peng2ef444f2018-10-24 16:05:12 +08001173 */
Chao Pengb08c2892018-10-24 16:05:15 +08001174 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
Chao Peng2ef444f2018-10-24 16:05:12 +08001175 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1176 wrmsrl(MSR_IA32_RTIT_CTL, 0);
1177 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1178 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1179 }
1180}
1181
1182static void pt_guest_exit(struct vcpu_vmx *vmx)
1183{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001184 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001185 return;
1186
1187 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1188 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1189 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1190 }
1191
1192 /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */
1193 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
1194}
1195
Sean Christopherson13b964a2019-05-07 09:06:31 -07001196void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1197 unsigned long fs_base, unsigned long gs_base)
1198{
1199 if (unlikely(fs_sel != host->fs_sel)) {
1200 if (!(fs_sel & 7))
1201 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1202 else
1203 vmcs_write16(HOST_FS_SELECTOR, 0);
1204 host->fs_sel = fs_sel;
1205 }
1206 if (unlikely(gs_sel != host->gs_sel)) {
1207 if (!(gs_sel & 7))
1208 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1209 else
1210 vmcs_write16(HOST_GS_SELECTOR, 0);
1211 host->gs_sel = gs_sel;
1212 }
1213 if (unlikely(fs_base != host->fs_base)) {
1214 vmcs_writel(HOST_FS_BASE, fs_base);
1215 host->fs_base = fs_base;
1216 }
1217 if (unlikely(gs_base != host->gs_base)) {
1218 vmcs_writel(HOST_GS_BASE, gs_base);
1219 host->gs_base = gs_base;
1220 }
1221}
1222
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001223void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001224{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001225 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001226 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001227#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01001228 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001229#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001230 unsigned long fs_base, gs_base;
1231 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03001232 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001233
Sean Christophersond264ee02018-08-27 15:21:12 -07001234 vmx->req_immediate_exit = false;
1235
Liran Alonf48b4712018-11-20 18:03:25 +02001236 /*
1237 * Note that guest MSRs to be saved/restored can also be changed
1238 * when guest state is loaded. This happens when guest transitions
1239 * to/from long-mode by setting MSR_EFER.LMA.
1240 */
Sean Christopherson658ece82020-09-23 11:04:01 -07001241 if (!vmx->guest_uret_msrs_loaded) {
1242 vmx->guest_uret_msrs_loaded = true;
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001243 for (i = 0; i < vmx->nr_active_uret_msrs; ++i)
Sean Christopherson802145c2020-09-23 11:04:09 -07001244 kvm_set_user_return_msr(vmx->guest_uret_msrs[i].slot,
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001245 vmx->guest_uret_msrs[i].data,
1246 vmx->guest_uret_msrs[i].mask);
Liran Alonf48b4712018-11-20 18:03:25 +02001247
1248 }
wanpeng lic9dfd3f2020-02-17 18:37:43 +08001249
1250 if (vmx->nested.need_vmcs12_to_shadow_sync)
1251 nested_sync_vmcs12_to_shadow(vcpu);
1252
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001253 if (vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001254 return;
1255
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001256 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001257
Avi Kivity33ed6322007-05-02 16:54:03 +03001258 /*
1259 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1260 * allow segment selectors with cpl > 0 or ti == 1.
1261 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07001262 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01001263
1264#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001265 savesegment(ds, host_state->ds_sel);
1266 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07001267
1268 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001269 if (likely(is_64bit_mm(current->mm))) {
Thomas Gleixner67580342020-05-28 16:13:52 -04001270 current_save_fsgs();
Sean Christophersone368b872018-07-23 12:32:41 -07001271 fs_sel = current->thread.fsindex;
1272 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001273 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07001274 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001275 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07001276 savesegment(fs, fs_sel);
1277 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001278 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07001279 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03001280 }
1281
Paolo Bonzini4679b612018-09-24 17:23:01 +02001282 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03001283#else
Sean Christophersone368b872018-07-23 12:32:41 -07001284 savesegment(fs, fs_sel);
1285 savesegment(gs, gs_sel);
1286 fs_base = segment_base(fs_sel);
1287 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001288#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001289
Sean Christopherson13b964a2019-05-07 09:06:31 -07001290 vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001291 vmx->guest_state_loaded = true;
Avi Kivity33ed6322007-05-02 16:54:03 +03001292}
1293
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001294static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001295{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001296 struct vmcs_host_state *host_state;
1297
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001298 if (!vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001299 return;
1300
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001301 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001302
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001303 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001304
Avi Kivityc8770e72010-11-11 12:37:26 +02001305#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001306 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001307#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001308 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1309 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001310#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001311 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001312#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001313 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001314#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001315 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001316 if (host_state->fs_sel & 7)
1317 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001318#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001319 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1320 loadsegment(ds, host_state->ds_sel);
1321 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001322 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001323#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001324 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001325#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001326 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001327#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001328 load_fixmap_gdt(raw_smp_processor_id());
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001329 vmx->guest_state_loaded = false;
Sean Christopherson658ece82020-09-23 11:04:01 -07001330 vmx->guest_uret_msrs_loaded = false;
Avi Kivity33ed6322007-05-02 16:54:03 +03001331}
1332
Sean Christopherson678e3152018-07-23 12:32:43 -07001333#ifdef CONFIG_X86_64
1334static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001335{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001336 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001337 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001338 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1339 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001340 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001341}
1342
Sean Christopherson678e3152018-07-23 12:32:43 -07001343static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1344{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001345 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001346 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001347 wrmsrl(MSR_KERNEL_GS_BASE, data);
1348 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001349 vmx->msr_guest_kernel_gs_base = data;
1350}
1351#endif
1352
Sean Christopherson5c911be2020-05-01 09:31:17 -07001353void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
1354 struct loaded_vmcs *buddy)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001356 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001357 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Sean Christopherson5c911be2020-05-01 09:31:17 -07001358 struct vmcs *prev;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001360 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001361 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001362 local_irq_disable();
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001363
1364 /*
Sean Christopherson31603d42020-03-21 12:37:49 -07001365 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
1366 * this cpu's percpu list, otherwise it may not yet be deleted
1367 * from its previous cpu's percpu list. Pairs with the
1368 * smb_wmb() in __loaded_vmcs_clear().
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001369 */
1370 smp_rmb();
1371
Nadav Har'Eld462b812011-05-24 15:26:10 +03001372 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1373 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001374 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001375 }
1376
Sean Christopherson5c911be2020-05-01 09:31:17 -07001377 prev = per_cpu(current_vmcs, cpu);
1378 if (prev != vmx->loaded_vmcs->vmcs) {
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001379 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1380 vmcs_load(vmx->loaded_vmcs->vmcs);
Sean Christopherson5c911be2020-05-01 09:31:17 -07001381
1382 /*
1383 * No indirect branch prediction barrier needed when switching
1384 * the active VMCS within a guest, e.g. on nested VM-Enter.
1385 * The L1 VMM can protect itself with retpolines, IBPB or IBRS.
1386 */
1387 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev))
1388 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001389 }
1390
1391 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001392 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001393 unsigned long sysenter_esp;
1394
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001395 /*
1396 * Flush all EPTP/VPID contexts, the new pCPU may have stale
1397 * TLB entries from its previous association with the vCPU.
1398 */
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001399 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001400
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 /*
1402 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001403 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001405 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001406 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001407 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408
1409 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1410 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001411
Nadav Har'Eld462b812011-05-24 15:26:10 +03001412 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413 }
Feng Wu28b835d2015-09-18 22:29:54 +08001414
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001415 /* Setup TSC multiplier */
1416 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001417 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1418 decache_tsc_multiplier(vmx);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001419}
1420
1421/*
1422 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1423 * vcpu mutex is already taken.
1424 */
Sean Christopherson1af1bb02020-05-06 16:58:50 -07001425static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001426{
1427 struct vcpu_vmx *vmx = to_vmx(vcpu);
1428
Sean Christopherson5c911be2020-05-01 09:31:17 -07001429 vmx_vcpu_load_vmcs(vcpu, cpu, NULL);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001430
Feng Wu28b835d2015-09-18 22:29:54 +08001431 vmx_vcpu_pi_load(vcpu, cpu);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001432
Wanpeng Li74c55932017-11-29 01:31:20 -08001433 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001434}
1435
Sean Christopherson13b964a2019-05-07 09:06:31 -07001436static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437{
Feng Wu28b835d2015-09-18 22:29:54 +08001438 vmx_vcpu_pi_put(vcpu);
1439
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001440 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441}
1442
Wanpeng Lif244dee2017-07-20 01:11:54 -07001443static bool emulation_required(struct kvm_vcpu *vcpu)
1444{
Sean Christopherson2ba44932020-09-23 11:44:48 -07001445 return emulate_invalid_guest_state && !vmx_guest_state_valid(vcpu);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001446}
1447
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001448unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001450 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001451 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001452
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001453 if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) {
1454 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Avi Kivity6de12732011-03-07 12:51:22 +02001455 rflags = vmcs_readl(GUEST_RFLAGS);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001456 if (vmx->rmode.vm86_active) {
Avi Kivity6de12732011-03-07 12:51:22 +02001457 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
Sean Christophersone7bddc52019-09-27 14:45:18 -07001458 save_rflags = vmx->rmode.save_rflags;
Avi Kivity6de12732011-03-07 12:51:22 +02001459 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1460 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001461 vmx->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001462 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001463 return vmx->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464}
1465
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001466void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001468 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001469 unsigned long old_rflags;
Wanpeng Lif244dee2017-07-20 01:11:54 -07001470
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00001471 if (is_unrestricted_guest(vcpu)) {
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001472 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001473 vmx->rflags = rflags;
1474 vmcs_writel(GUEST_RFLAGS, rflags);
1475 return;
1476 }
1477
1478 old_rflags = vmx_get_rflags(vcpu);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001479 vmx->rflags = rflags;
1480 if (vmx->rmode.vm86_active) {
1481 vmx->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001482 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001483 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001485
Sean Christophersone7bddc52019-09-27 14:45:18 -07001486 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM)
1487 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488}
1489
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001490u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001491{
1492 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1493 int ret = 0;
1494
1495 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001496 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001497 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001498 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001499
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001500 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001501}
1502
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001503void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001504{
1505 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1506 u32 interruptibility = interruptibility_old;
1507
1508 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1509
Jan Kiszka48005f62010-02-19 19:38:07 +01001510 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001511 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001512 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001513 interruptibility |= GUEST_INTR_STATE_STI;
1514
1515 if ((interruptibility != interruptibility_old))
1516 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1517}
1518
Chao Pengbf8c55d2018-10-24 16:05:14 +08001519static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data)
1520{
1521 struct vcpu_vmx *vmx = to_vmx(vcpu);
1522 unsigned long value;
1523
1524 /*
1525 * Any MSR write that attempts to change bits marked reserved will
1526 * case a #GP fault.
1527 */
1528 if (data & vmx->pt_desc.ctl_bitmask)
1529 return 1;
1530
1531 /*
1532 * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will
1533 * result in a #GP unless the same write also clears TraceEn.
1534 */
1535 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) &&
1536 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN))
1537 return 1;
1538
1539 /*
1540 * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit
1541 * and FabricEn would cause #GP, if
1542 * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0
1543 */
1544 if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) &&
1545 !(data & RTIT_CTL_FABRIC_EN) &&
1546 !intel_pt_validate_cap(vmx->pt_desc.caps,
1547 PT_CAP_single_range_output))
1548 return 1;
1549
1550 /*
1551 * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that
Ingo Molnard9f6e122021-03-18 15:28:01 +01001552 * utilize encodings marked reserved will cause a #GP fault.
Chao Pengbf8c55d2018-10-24 16:05:14 +08001553 */
1554 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods);
1555 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) &&
1556 !test_bit((data & RTIT_CTL_MTC_RANGE) >>
1557 RTIT_CTL_MTC_RANGE_OFFSET, &value))
1558 return 1;
1559 value = intel_pt_validate_cap(vmx->pt_desc.caps,
1560 PT_CAP_cycle_thresholds);
1561 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1562 !test_bit((data & RTIT_CTL_CYC_THRESH) >>
1563 RTIT_CTL_CYC_THRESH_OFFSET, &value))
1564 return 1;
1565 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods);
1566 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1567 !test_bit((data & RTIT_CTL_PSB_FREQ) >>
1568 RTIT_CTL_PSB_FREQ_OFFSET, &value))
1569 return 1;
1570
1571 /*
1572 * If ADDRx_CFG is reserved or the encodings is >2 will
1573 * cause a #GP fault.
1574 */
1575 value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET;
1576 if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2))
1577 return 1;
1578 value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET;
1579 if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2))
1580 return 1;
1581 value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET;
1582 if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2))
1583 return 1;
1584 value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET;
1585 if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2))
1586 return 1;
1587
1588 return 0;
1589}
1590
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07001591static bool vmx_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
1592{
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001593 /*
1594 * Emulation of instructions in SGX enclaves is impossible as RIP does
1595 * not point tthe failing instruction, and even if it did, the code
1596 * stream is inaccessible. Inject #UD instead of exiting to userspace
1597 * so that guest userspace can't DoS the guest simply by triggering
1598 * emulation (enclaves are CPL3 only).
1599 */
1600 if (to_vmx(vcpu)->exit_reason.enclave_mode) {
1601 kvm_queue_exception(vcpu, UD_VECTOR);
1602 return false;
1603 }
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07001604 return true;
1605}
1606
Sean Christopherson1957aa62019-08-27 14:40:39 -07001607static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608{
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001609 union vmx_exit_reason exit_reason = to_vmx(vcpu)->exit_reason;
Paolo Bonzinifede8072020-04-27 11:55:59 -04001610 unsigned long rip, orig_rip;
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001611 u32 instr_len;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612
Sean Christopherson1957aa62019-08-27 14:40:39 -07001613 /*
1614 * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on
1615 * undefined behavior: Intel's SDM doesn't mandate the VMCS field be
1616 * set when EPT misconfig occurs. In practice, real hardware updates
1617 * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors
1618 * (namely Hyper-V) don't set it due to it being undefined behavior,
1619 * i.e. we end up advancing IP with some random value.
1620 */
1621 if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001622 exit_reason.basic != EXIT_REASON_EPT_MISCONFIG) {
1623 instr_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
1624
1625 /*
1626 * Emulating an enclave's instructions isn't supported as KVM
1627 * cannot access the enclave's memory or its true RIP, e.g. the
1628 * vmcs.GUEST_RIP points at the exit point of the enclave, not
1629 * the RIP that actually triggered the VM-Exit. But, because
1630 * most instructions that cause VM-Exit will #UD in an enclave,
1631 * most instruction-based VM-Exits simply do not occur.
1632 *
1633 * There are a few exceptions, notably the debug instructions
1634 * INT1ICEBRK and INT3, as they are allowed in debug enclaves
1635 * and generate #DB/#BP as expected, which KVM might intercept.
1636 * But again, the CPU does the dirty work and saves an instr
1637 * length of zero so VMMs don't shoot themselves in the foot.
1638 * WARN if KVM tries to skip a non-zero length instruction on
1639 * a VM-Exit from an enclave.
1640 */
1641 if (!instr_len)
1642 goto rip_updated;
1643
1644 WARN(exit_reason.enclave_mode,
1645 "KVM: skipping instruction after SGX enclave VM-Exit");
1646
Paolo Bonzinifede8072020-04-27 11:55:59 -04001647 orig_rip = kvm_rip_read(vcpu);
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001648 rip = orig_rip + instr_len;
Paolo Bonzinifede8072020-04-27 11:55:59 -04001649#ifdef CONFIG_X86_64
1650 /*
1651 * We need to mask out the high 32 bits of RIP if not in 64-bit
1652 * mode, but just finding out that we are in 64-bit mode is
1653 * quite expensive. Only do it if there was a carry.
1654 */
1655 if (unlikely(((rip ^ orig_rip) >> 31) == 3) && !is_64_bit_mode(vcpu))
1656 rip = (u32)rip;
1657#endif
Sean Christopherson1957aa62019-08-27 14:40:39 -07001658 kvm_rip_write(vcpu, rip);
1659 } else {
1660 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
1661 return 0;
1662 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12001664rip_updated:
Glauber Costa2809f5d2009-05-12 16:21:05 -04001665 /* skipping an emulated instruction also counts */
1666 vmx_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001667
Sean Christopherson60fc3d02019-08-27 14:40:38 -07001668 return 1;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001669}
1670
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02001671/*
Oliver Upton5ef8acb2020-02-07 02:36:07 -08001672 * Recognizes a pending MTF VM-exit and records the nested state for later
1673 * delivery.
1674 */
1675static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)
1676{
1677 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1678 struct vcpu_vmx *vmx = to_vmx(vcpu);
1679
1680 if (!is_guest_mode(vcpu))
1681 return;
1682
1683 /*
1684 * Per the SDM, MTF takes priority over debug-trap exceptions besides
1685 * T-bit traps. As instruction emulation is completed (i.e. at the
1686 * instruction boundary), any #DB exception pending delivery must be a
1687 * debug-trap. Record the pending MTF state to be delivered in
1688 * vmx_check_nested_events().
1689 */
1690 if (nested_cpu_has_mtf(vmcs12) &&
1691 (!vcpu->arch.exception.pending ||
1692 vcpu->arch.exception.nr == DB_VECTOR))
1693 vmx->nested.mtf_pending = true;
1694 else
1695 vmx->nested.mtf_pending = false;
1696}
1697
1698static int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu)
1699{
1700 vmx_update_emulated_instruction(vcpu);
1701 return skip_emulated_instruction(vcpu);
1702}
1703
Wanpeng Licaa057a2018-03-12 04:53:03 -07001704static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1705{
1706 /*
1707 * Ensure that we clear the HLT state in the VMCS. We don't need to
1708 * explicitly skip the instruction because if the HLT state is set,
1709 * then the instruction is already executing and RIP has already been
1710 * advanced.
1711 */
1712 if (kvm_hlt_in_guest(vcpu->kvm) &&
1713 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1714 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1715}
1716
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001717static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001718{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001719 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001720 unsigned nr = vcpu->arch.exception.nr;
1721 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001722 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001723 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001724
Jim Mattsonda998b42018-10-16 14:29:22 -07001725 kvm_deliver_exception_payload(vcpu);
1726
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001727 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001728 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001729 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1730 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001731
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001732 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001733 int inc_eip = 0;
1734 if (kvm_exception_is_soft(nr))
1735 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07001736 kvm_inject_realmode_interrupt(vcpu, nr, inc_eip);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001737 return;
1738 }
1739
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001740 WARN_ON_ONCE(vmx->emulation_required);
1741
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001742 if (kvm_exception_is_soft(nr)) {
1743 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1744 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001745 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1746 } else
1747 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1748
1749 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001750
1751 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001752}
1753
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001754static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001755{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001756 struct vmx_uret_msr tmp;
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001757 int from, to;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001758
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001759 from = __vmx_find_uret_msr(vmx, msr);
1760 if (from < 0)
1761 return;
1762 to = vmx->nr_active_uret_msrs++;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001763
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001764 tmp = vmx->guest_uret_msrs[to];
1765 vmx->guest_uret_msrs[to] = vmx->guest_uret_msrs[from];
1766 vmx->guest_uret_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001767}
1768
1769/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001770 * Set up the vmcs to automatically save and restore system
1771 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1772 * mode, as fiddling with msrs is very expensive.
1773 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001774static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001775{
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001776 vmx->guest_uret_msrs_loaded = false;
1777 vmx->nr_active_uret_msrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001778#ifdef CONFIG_X86_64
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001779 /*
1780 * The SYSCALL MSRs are only needed on long mode guests, and only
1781 * when EFER.SCE is set.
1782 */
1783 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001784 vmx_setup_uret_msr(vmx, MSR_STAR);
1785 vmx_setup_uret_msr(vmx, MSR_LSTAR);
1786 vmx_setup_uret_msr(vmx, MSR_SYSCALL_MASK);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001787 }
Eddie Donga75beee2007-05-17 18:55:15 +03001788#endif
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001789 if (update_transition_efer(vmx))
1790 vmx_setup_uret_msr(vmx, MSR_EFER);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001791
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001792 if (guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
1793 vmx_setup_uret_msr(vmx, MSR_TSC_AUX);
1794
1795 vmx_setup_uret_msr(vmx, MSR_IA32_TSX_CTRL);
Avi Kivity58972972009-02-24 22:26:47 +02001796
Yang Zhang8d146952013-01-25 10:18:50 +08001797 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001798 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001799}
1800
Leonid Shatz326e7422018-11-06 12:14:25 +02001801static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001803 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1804 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001805
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001806 /*
1807 * We're here if L1 chose not to trap WRMSR to TSC. According
1808 * to the spec, this should set L1's TSC; The offset that L1
1809 * set for L2 remains unchanged, and still needs to be added
1810 * to the newly set TSC to get L2's TSC.
1811 */
1812 if (is_guest_mode(vcpu) &&
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08001813 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001814 g_tsc_offset = vmcs12->tsc_offset;
1815
1816 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1817 vcpu->arch.tsc_offset - g_tsc_offset,
1818 offset);
1819 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1820 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001821}
1822
Nadav Har'El801d3422011-05-25 23:02:23 +03001823/*
1824 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1825 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1826 * all guests if the "nested" module option is off, and can also be disabled
1827 * for a single guest by disabling its VMX cpuid bit.
1828 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001829bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001830{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001831 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001832}
1833
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001834static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1835 uint64_t val)
1836{
1837 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1838
1839 return !(val & ~valid_bits);
1840}
1841
Tom Lendacky801e4592018-02-21 13:39:51 -06001842static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1843{
Paolo Bonzini13893092018-02-26 13:40:09 +01001844 switch (msr->index) {
1845 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1846 if (!nested)
1847 return 1;
1848 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
Like Xu27461da32020-05-29 15:43:45 +08001849 case MSR_IA32_PERF_CAPABILITIES:
1850 msr->data = vmx_get_perf_capabilities();
1851 return 0;
Paolo Bonzini13893092018-02-26 13:40:09 +01001852 default:
Peter Xu12bc2132020-06-22 18:04:42 -04001853 return KVM_MSR_RET_INVALID;
Paolo Bonzini13893092018-02-26 13:40:09 +01001854 }
Tom Lendacky801e4592018-02-21 13:39:51 -06001855}
1856
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001857/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 * Reads an msr value (of 'msr_index') into 'pdata'.
1859 * Returns 0 on success, non-0 otherwise.
1860 * Assumes vcpu_load() was already called.
1861 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001862static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001864 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001865 struct vmx_uret_msr *msr;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001866 u32 index;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001868 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001869#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001871 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872 break;
1873 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001874 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001876 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001877 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001878 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001879#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001881 return kvm_get_msr_common(vcpu, msr_info);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001882 case MSR_IA32_TSX_CTRL:
1883 if (!msr_info->host_initiated &&
1884 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
1885 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001886 goto find_uret_msr;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001887 case MSR_IA32_UMWAIT_CONTROL:
1888 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
1889 return 1;
1890
1891 msr_info->data = vmx->msr_ia32_umwait_control;
1892 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001893 case MSR_IA32_SPEC_CTRL:
1894 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05001895 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001896 return 1;
1897
1898 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1899 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001901 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902 break;
1903 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001904 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 break;
1906 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001907 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001909 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001910 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001911 (!msr_info->host_initiated &&
1912 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001913 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001914 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001915 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001916 case MSR_IA32_MCG_EXT_CTL:
1917 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001918 !(vmx->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001919 FEAT_CTL_LMCE_ENABLED))
Jan Kiszkacae50132014-01-04 18:47:22 +01001920 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001921 msr_info->data = vcpu->arch.mcg_ext_ctl;
1922 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001923 case MSR_IA32_FEAT_CTL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001924 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001925 break;
Sean Christopherson8f102442021-04-12 16:21:40 +12001926 case MSR_IA32_SGXLEPUBKEYHASH0 ... MSR_IA32_SGXLEPUBKEYHASH3:
1927 if (!msr_info->host_initiated &&
1928 !guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC))
1929 return 1;
1930 msr_info->data = to_vmx(vcpu)->msr_ia32_sgxlepubkeyhash
1931 [msr_info->index - MSR_IA32_SGXLEPUBKEYHASH0];
1932 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01001933 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1934 if (!nested_vmx_allowed(vcpu))
1935 return 1;
Vitaly Kuznetsov31de3d22020-02-05 13:30:33 +01001936 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1937 &msr_info->data))
1938 return 1;
1939 /*
1940 * Enlightened VMCS v1 doesn't have certain fields, but buggy
1941 * Hyper-V versions are still trying to use corresponding
1942 * features when they are exposed. Filter out the essential
1943 * minimum.
1944 */
1945 if (!msr_info->host_initiated &&
1946 vmx->nested.enlightened_vmcs_enabled)
1947 nested_evmcs_filter_control_msr(msr_info->index,
1948 &msr_info->data);
1949 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001950 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001951 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001952 return 1;
1953 msr_info->data = vmx->pt_desc.guest.ctl;
1954 break;
1955 case MSR_IA32_RTIT_STATUS:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001956 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001957 return 1;
1958 msr_info->data = vmx->pt_desc.guest.status;
1959 break;
1960 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001961 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001962 !intel_pt_validate_cap(vmx->pt_desc.caps,
1963 PT_CAP_cr3_filtering))
1964 return 1;
1965 msr_info->data = vmx->pt_desc.guest.cr3_match;
1966 break;
1967 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001968 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001969 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1970 PT_CAP_topa_output) &&
1971 !intel_pt_validate_cap(vmx->pt_desc.caps,
1972 PT_CAP_single_range_output)))
1973 return 1;
1974 msr_info->data = vmx->pt_desc.guest.output_base;
1975 break;
1976 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001977 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001978 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1979 PT_CAP_topa_output) &&
1980 !intel_pt_validate_cap(vmx->pt_desc.caps,
1981 PT_CAP_single_range_output)))
1982 return 1;
1983 msr_info->data = vmx->pt_desc.guest.output_mask;
1984 break;
1985 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
1986 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christopherson2ef76192020-03-02 15:56:22 -08001987 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001988 (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
1989 PT_CAP_num_address_ranges)))
1990 return 1;
1991 if (index % 2)
1992 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
1993 else
1994 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
1995 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001996 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001997 if (!msr_info->host_initiated &&
1998 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001999 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002000 goto find_uret_msr;
Like Xud8550662021-01-08 09:36:55 +08002001 case MSR_IA32_DEBUGCTLMSR:
2002 msr_info->data = vmcs_read64(GUEST_IA32_DEBUGCTL);
2003 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002005 find_uret_msr:
Sean Christophersond85a8032020-09-23 11:04:06 -07002006 msr = vmx_find_uret_msr(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002007 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002008 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08002009 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002011 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 }
2013
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014 return 0;
2015}
2016
Sean Christopherson24085002020-04-28 16:10:24 -07002017static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu,
2018 u64 data)
2019{
2020#ifdef CONFIG_X86_64
2021 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
2022 return (u32)data;
2023#endif
2024 return (unsigned long)data;
2025}
2026
Like Xuc6462362021-02-01 13:10:31 +08002027static u64 vcpu_supported_debugctl(struct kvm_vcpu *vcpu)
2028{
2029 u64 debugctl = vmx_supported_debugctl();
2030
2031 if (!intel_pmu_lbr_is_enabled(vcpu))
Like Xue6209a32021-02-01 13:10:36 +08002032 debugctl &= ~DEBUGCTLMSR_LBR_MASK;
Like Xuc6462362021-02-01 13:10:31 +08002033
2034 return debugctl;
2035}
2036
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037/*
Miaohe Lin311497e2019-12-11 14:26:25 +08002038 * Writes msr value into the appropriate "register".
Avi Kivity6aa8b732006-12-10 02:21:36 -08002039 * Returns 0 on success, non-0 otherwise.
2040 * Assumes vcpu_load() was already called.
2041 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002042static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002044 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002045 struct vmx_uret_msr *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002046 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002047 u32 msr_index = msr_info->index;
2048 u64 data = msr_info->data;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002049 u32 index;
Eddie Dong2cc51562007-05-21 07:28:09 +03002050
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002052 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002053 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002054 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002055#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002057 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058 vmcs_writel(GUEST_FS_BASE, data);
2059 break;
2060 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002061 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062 vmcs_writel(GUEST_GS_BASE, data);
2063 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002064 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07002065 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002066 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067#endif
2068 case MSR_IA32_SYSENTER_CS:
Sean Christophersonde70d272019-05-07 09:06:36 -07002069 if (is_guest_mode(vcpu))
2070 get_vmcs12(vcpu)->guest_sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071 vmcs_write32(GUEST_SYSENTER_CS, data);
2072 break;
2073 case MSR_IA32_SYSENTER_EIP:
Sean Christopherson24085002020-04-28 16:10:24 -07002074 if (is_guest_mode(vcpu)) {
2075 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002076 get_vmcs12(vcpu)->guest_sysenter_eip = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002077 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002078 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 break;
2080 case MSR_IA32_SYSENTER_ESP:
Sean Christopherson24085002020-04-28 16:10:24 -07002081 if (is_guest_mode(vcpu)) {
2082 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002083 get_vmcs12(vcpu)->guest_sysenter_esp = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002084 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002085 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086 break;
Like Xud8550662021-01-08 09:36:55 +08002087 case MSR_IA32_DEBUGCTLMSR: {
Like Xuc6462362021-02-01 13:10:31 +08002088 u64 invalid = data & ~vcpu_supported_debugctl(vcpu);
Like Xud8550662021-01-08 09:36:55 +08002089 if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) {
2090 if (report_ignored_msrs)
2091 vcpu_unimpl(vcpu, "%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n",
2092 __func__, data);
2093 data &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR);
2094 invalid &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR);
2095 }
2096
2097 if (invalid)
2098 return 1;
2099
Sean Christopherson699a1ac2019-05-07 09:06:37 -07002100 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
2101 VM_EXIT_SAVE_DEBUG_CONTROLS)
2102 get_vmcs12(vcpu)->guest_ia32_debugctl = data;
2103
Like Xud8550662021-01-08 09:36:55 +08002104 vmcs_write64(GUEST_IA32_DEBUGCTL, data);
Like Xu8e129112021-02-01 13:10:33 +08002105 if (intel_pmu_lbr_is_enabled(vcpu) && !to_vmx(vcpu)->lbr_desc.event &&
2106 (data & DEBUGCTLMSR_LBR))
2107 intel_pmu_create_guest_lbr_event(vcpu);
Like Xud8550662021-01-08 09:36:55 +08002108 return 0;
2109 }
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002110 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08002111 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02002112 (!msr_info->host_initiated &&
2113 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002114 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002115 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07002116 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002118 vmcs_write64(GUEST_BNDCFGS, data);
2119 break;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08002120 case MSR_IA32_UMWAIT_CONTROL:
2121 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
2122 return 1;
2123
2124 /* The reserved bit 1 and non-32 bit [63:32] should be zero */
2125 if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32)))
2126 return 1;
2127
2128 vmx->msr_ia32_umwait_control = data;
2129 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002130 case MSR_IA32_SPEC_CTRL:
2131 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002132 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002133 return 1;
2134
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002135 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002136 return 1;
2137
2138 vmx->spec_ctrl = data;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002139 if (!data)
2140 break;
2141
2142 /*
2143 * For non-nested:
2144 * When it's written (to non-zero) for the first time, pass
2145 * it through.
2146 *
2147 * For nested:
2148 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002149 * nested_vmx_prepare_msr_bitmap. We should not touch the
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002150 * vmcs02.msr_bitmap here since it gets completely overwritten
2151 * in the merging. We update the vmcs01 here for L1 as well
2152 * since it will end up touching the MSR anyway now.
2153 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002154 vmx_disable_intercept_for_msr(vcpu,
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002155 MSR_IA32_SPEC_CTRL,
2156 MSR_TYPE_RW);
2157 break;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002158 case MSR_IA32_TSX_CTRL:
2159 if (!msr_info->host_initiated &&
2160 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
2161 return 1;
2162 if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR))
2163 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002164 goto find_uret_msr;
Ashok Raj15d45072018-02-01 22:59:43 +01002165 case MSR_IA32_PRED_CMD:
2166 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002167 !guest_has_pred_cmd_msr(vcpu))
Ashok Raj15d45072018-02-01 22:59:43 +01002168 return 1;
2169
2170 if (data & ~PRED_CMD_IBPB)
2171 return 1;
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002172 if (!boot_cpu_has(X86_FEATURE_IBPB))
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002173 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002174 if (!data)
2175 break;
2176
2177 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2178
2179 /*
2180 * For non-nested:
2181 * When it's written (to non-zero) for the first time, pass
2182 * it through.
2183 *
2184 * For nested:
2185 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002186 * nested_vmx_prepare_msr_bitmap. We should not touch the
Ashok Raj15d45072018-02-01 22:59:43 +01002187 * vmcs02.msr_bitmap here since it gets completely overwritten
2188 * in the merging.
2189 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002190 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W);
Ashok Raj15d45072018-02-01 22:59:43 +01002191 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002192 case MSR_IA32_CR_PAT:
Sean Christophersond28f4292019-05-07 09:06:27 -07002193 if (!kvm_pat_valid(data))
2194 return 1;
2195
Sean Christopherson142e4be2019-05-07 09:06:35 -07002196 if (is_guest_mode(vcpu) &&
2197 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
2198 get_vmcs12(vcpu)->guest_ia32_pat = data;
2199
Sheng Yang468d4722008-10-09 16:01:55 +08002200 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2201 vmcs_write64(GUEST_IA32_PAT, data);
2202 vcpu->arch.pat = data;
2203 break;
2204 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002205 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002206 break;
Will Auldba904632012-11-29 12:42:50 -08002207 case MSR_IA32_TSC_ADJUST:
2208 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002209 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002210 case MSR_IA32_MCG_EXT_CTL:
2211 if ((!msr_info->host_initiated &&
2212 !(to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002213 FEAT_CTL_LMCE_ENABLED)) ||
Ashok Rajc45dcc72016-06-22 14:59:56 +08002214 (data & ~MCG_EXT_CTL_LMCE_EN))
2215 return 1;
2216 vcpu->arch.mcg_ext_ctl = data;
2217 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002218 case MSR_IA32_FEAT_CTL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002219 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08002220 (to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002221 FEAT_CTL_LOCKED && !msr_info->host_initiated))
Jan Kiszkacae50132014-01-04 18:47:22 +01002222 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08002223 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002224 if (msr_info->host_initiated && data == 0)
2225 vmx_leave_nested(vcpu);
Sean Christopherson72add912021-04-12 16:21:42 +12002226
2227 /* SGX may be enabled/disabled by guest's firmware */
2228 vmx_write_encls_bitmap(vcpu, NULL);
Jan Kiszkacae50132014-01-04 18:47:22 +01002229 break;
Sean Christopherson8f102442021-04-12 16:21:40 +12002230 case MSR_IA32_SGXLEPUBKEYHASH0 ... MSR_IA32_SGXLEPUBKEYHASH3:
2231 /*
2232 * On real hardware, the LE hash MSRs are writable before
2233 * the firmware sets bit 0 in MSR 0x7a ("activating" SGX),
2234 * at which point SGX related bits in IA32_FEATURE_CONTROL
2235 * become writable.
2236 *
2237 * KVM does not emulate SGX activation for simplicity, so
2238 * allow writes to the LE hash MSRs if IA32_FEATURE_CONTROL
2239 * is unlocked. This is technically not architectural
2240 * behavior, but it's close enough.
2241 */
2242 if (!msr_info->host_initiated &&
2243 (!guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC) ||
2244 ((vmx->msr_ia32_feature_control & FEAT_CTL_LOCKED) &&
2245 !(vmx->msr_ia32_feature_control & FEAT_CTL_SGX_LC_ENABLED))))
2246 return 1;
2247 vmx->msr_ia32_sgxlepubkeyhash
2248 [msr_index - MSR_IA32_SGXLEPUBKEYHASH0] = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002249 break;
2250 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08002251 if (!msr_info->host_initiated)
2252 return 1; /* they are read-only */
2253 if (!nested_vmx_allowed(vcpu))
2254 return 1;
2255 return vmx_set_vmx_msr(vcpu, msr_index, data);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002256 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08002257 if (!vmx_pt_mode_is_host_guest() ||
Luwei Kangee85dec2018-10-24 16:05:16 +08002258 vmx_rtit_ctl_check(vcpu, data) ||
2259 vmx->nested.vmxon)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002260 return 1;
2261 vmcs_write64(GUEST_IA32_RTIT_CTL, data);
2262 vmx->pt_desc.guest.ctl = data;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002263 pt_update_intercept_for_msr(vcpu);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002264 break;
2265 case MSR_IA32_RTIT_STATUS:
Sean Christophersone348ac72019-12-10 15:24:33 -08002266 if (!pt_can_write_msr(vmx))
2267 return 1;
2268 if (data & MSR_IA32_RTIT_STATUS_MASK)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002269 return 1;
2270 vmx->pt_desc.guest.status = data;
2271 break;
2272 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christophersone348ac72019-12-10 15:24:33 -08002273 if (!pt_can_write_msr(vmx))
2274 return 1;
2275 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2276 PT_CAP_cr3_filtering))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002277 return 1;
2278 vmx->pt_desc.guest.cr3_match = data;
2279 break;
2280 case MSR_IA32_RTIT_OUTPUT_BASE:
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_topa_output) &&
2285 !intel_pt_validate_cap(vmx->pt_desc.caps,
2286 PT_CAP_single_range_output))
2287 return 1;
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07002288 if (!pt_output_base_valid(vcpu, data))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002289 return 1;
2290 vmx->pt_desc.guest.output_base = data;
2291 break;
2292 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christophersone348ac72019-12-10 15:24:33 -08002293 if (!pt_can_write_msr(vmx))
2294 return 1;
2295 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2296 PT_CAP_topa_output) &&
2297 !intel_pt_validate_cap(vmx->pt_desc.caps,
2298 PT_CAP_single_range_output))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002299 return 1;
2300 vmx->pt_desc.guest.output_mask = data;
2301 break;
2302 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christophersone348ac72019-12-10 15:24:33 -08002303 if (!pt_can_write_msr(vmx))
2304 return 1;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002305 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christophersone348ac72019-12-10 15:24:33 -08002306 if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
2307 PT_CAP_num_address_ranges))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002308 return 1;
Sean Christophersonfe6ed362019-12-10 15:24:32 -08002309 if (is_noncanonical_address(data, vcpu))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002310 return 1;
2311 if (index % 2)
2312 vmx->pt_desc.guest.addr_b[index / 2] = data;
2313 else
2314 vmx->pt_desc.guest.addr_a[index / 2] = data;
2315 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002316 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002317 if (!msr_info->host_initiated &&
2318 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002319 return 1;
2320 /* Check reserved bit, higher 32 bits should be zero */
2321 if ((data >> 32) != 0)
2322 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002323 goto find_uret_msr;
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 Christopherson8b50b922020-09-24 17:30:11 -07004372 vmx_adjust_sec_exec_feature(vmx, &exec_control, rdtscp, RDTSCP);
Sean Christopherson8b50b922020-09-24 17:30:11 -07004373 vmx_adjust_sec_exec_feature(vmx, &exec_control, invpcid, INVPCID);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004374
Sean Christopherson8b50b922020-09-24 17:30:11 -07004375 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdrand, RDRAND);
4376 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdseed, RDSEED);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004377
Sean Christopherson8b50b922020-09-24 17:30:11 -07004378 vmx_adjust_sec_exec_control(vmx, &exec_control, waitpkg, WAITPKG,
4379 ENABLE_USR_WAIT_PAUSE, false);
Tao Xue69e72fa2019-07-16 14:55:49 +08004380
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08004381 if (!vcpu->kvm->arch.bus_lock_detection_enabled)
4382 exec_control &= ~SECONDARY_EXEC_BUS_LOCK_DETECTION;
4383
Paolo Bonzini80154d72017-08-24 13:55:35 +02004384 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004385}
4386
Wanpeng Lif53cd632014-12-02 19:14:58 +08004387#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08004388
Sean Christopherson944c3462018-12-03 13:53:09 -08004389/*
Xiaoyao Li1b842922019-10-20 17:11:01 +08004390 * Noting that the initialization of Guest-state Area of VMCS is in
4391 * vmx_vcpu_reset().
Sean Christopherson944c3462018-12-03 13:53:09 -08004392 */
Xiaoyao Li1b842922019-10-20 17:11:01 +08004393static void init_vmcs(struct vcpu_vmx *vmx)
Sean Christopherson944c3462018-12-03 13:53:09 -08004394{
Sean Christopherson944c3462018-12-03 13:53:09 -08004395 if (nested)
Xiaoyao Li1b842922019-10-20 17:11:01 +08004396 nested_vmx_set_vmcs_shadowing_bitmap();
Sean Christopherson944c3462018-12-03 13:53:09 -08004397
Sheng Yang25c5f222008-03-28 13:18:56 +08004398 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004399 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08004400
Avi Kivity6aa8b732006-12-10 02:21:36 -08004401 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4402
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403 /* Control */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004404 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004405
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004406 exec_controls_set(vmx, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004407
Dan Williamsdfa169b2016-06-02 11:17:24 -07004408 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004409 vmx_compute_secondary_exec_control(vmx);
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004410 secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07004411 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004412
Andrey Smetanind62caab2015-11-10 15:36:33 +03004413 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004414 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4415 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4416 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4417 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4418
4419 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004420
Li RongQing0bcf2612015-12-03 13:29:34 +08004421 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004422 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004423 }
4424
Wanpeng Lib31c1142018-03-12 04:53:04 -07004425 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004426 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004427 vmx->ple_window = ple_window;
4428 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004429 }
4430
Xiao Guangrongc3707952011-07-12 03:28:04 +08004431 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4432 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4434
Avi Kivity9581d442010-10-19 16:46:55 +02004435 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4436 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004437 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004438 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4439 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004440
Bandan Das2a499e42017-08-03 15:54:41 -04004441 if (cpu_has_vmx_vmfunc())
4442 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4443
Eddie Dong2cc51562007-05-21 07:28:09 +03004444 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4445 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004446 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03004447 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004448 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449
Radim Krčmář74545702015-04-27 15:11:25 +02004450 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4451 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004452
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004453 vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08004454
4455 /* 22.2.1, 20.8.1 */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004456 vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004457
Sean Christophersonfa71e952020-07-02 21:04:22 -07004458 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4459 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004460
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004461 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004462
Xiaoyao Li35fbe0d2019-10-20 17:10:58 +08004463 if (vmx->vpid != 0)
4464 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4465
Sean Christophersonbecdad82020-09-23 09:50:45 -07004466 if (cpu_has_vmx_xsaves())
Wanpeng Lif53cd632014-12-02 19:14:58 +08004467 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4468
Peter Feiner4e595162016-07-07 14:49:58 -07004469 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07004470 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4471 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4472 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07004473
Sean Christopherson72add912021-04-12 16:21:42 +12004474 vmx_write_encls_bitmap(&vmx->vcpu, NULL);
Chao Peng2ef444f2018-10-24 16:05:12 +08004475
Sean Christopherson2ef76192020-03-02 15:56:22 -08004476 if (vmx_pt_mode_is_host_guest()) {
Chao Peng2ef444f2018-10-24 16:05:12 +08004477 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
4478 /* Bit[6~0] are forced to 1, writes are ignored. */
4479 vmx->pt_desc.guest.output_mask = 0x7F;
4480 vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
4481 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004482}
4483
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004484static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004485{
4486 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004487 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004488 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004489
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004490 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004491 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004492
Tao Xu6e3ba4a2019-07-16 14:55:50 +08004493 vmx->msr_ia32_umwait_control = 0;
4494
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004495 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Wanpeng Li95c06542019-09-05 14:26:28 +08004496 vmx->hv_deadline_tsc = -1;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004497 kvm_set_cr8(vcpu, 0);
4498
4499 if (!init_event) {
4500 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4501 MSR_IA32_APICBASE_ENABLE;
4502 if (kvm_vcpu_is_reset_bsp(vcpu))
4503 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4504 apic_base_msr.host_initiated = true;
4505 kvm_set_apic_base(vcpu, &apic_base_msr);
4506 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004507
Avi Kivity2fb92db2011-04-27 19:42:18 +03004508 vmx_segment_cache_clear(vmx);
4509
Avi Kivity5706be02008-08-20 15:07:31 +03004510 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004511 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004512 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004513
4514 seg_setup(VCPU_SREG_DS);
4515 seg_setup(VCPU_SREG_ES);
4516 seg_setup(VCPU_SREG_FS);
4517 seg_setup(VCPU_SREG_GS);
4518 seg_setup(VCPU_SREG_SS);
4519
4520 vmcs_write16(GUEST_TR_SELECTOR, 0);
4521 vmcs_writel(GUEST_TR_BASE, 0);
4522 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4523 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4524
4525 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4526 vmcs_writel(GUEST_LDTR_BASE, 0);
4527 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4528 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4529
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004530 if (!init_event) {
4531 vmcs_write32(GUEST_SYSENTER_CS, 0);
4532 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4533 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4534 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4535 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004536
Wanpeng Lic37c2872017-11-20 14:52:21 -08004537 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01004538 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004539
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004540 vmcs_writel(GUEST_GDTR_BASE, 0);
4541 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4542
4543 vmcs_writel(GUEST_IDTR_BASE, 0);
4544 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4545
Anthony Liguori443381a2010-12-06 10:53:38 -06004546 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004547 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004548 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07004549 if (kvm_mpx_supported())
4550 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004551
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004552 setup_msrs(vmx);
4553
Avi Kivity6aa8b732006-12-10 02:21:36 -08004554 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4555
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004556 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004557 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004558 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004559 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004560 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004561 vmcs_write32(TPR_THRESHOLD, 0);
4562 }
4563
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004564 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004566 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004567 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06004568 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004569 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004570 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004571
Jason Baronb6a7cc32021-01-14 22:27:54 -05004572 vmx_update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004573
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004574 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004575 if (init_event)
4576 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004577}
4578
Jason Baronb6a7cc32021-01-14 22:27:54 -05004579static void vmx_enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004580{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08004581 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004582}
4583
Jason Baronb6a7cc32021-01-14 22:27:54 -05004584static void vmx_enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004585{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004586 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004587 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jason Baronb6a7cc32021-01-14 22:27:54 -05004588 vmx_enable_irq_window(vcpu);
Jan Kiszkac9a79532014-03-07 20:03:15 +01004589 return;
4590 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004591
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08004592 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004593}
4594
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004595static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004596{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004597 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004598 uint32_t intr;
4599 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004600
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004601 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004602
Avi Kivityfa89a812008-09-01 15:57:51 +03004603 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004604 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004605 int inc_eip = 0;
4606 if (vcpu->arch.interrupt.soft)
4607 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004608 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
Eddie Dong85f455f2007-07-06 12:20:49 +03004609 return;
4610 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004611 intr = irq | INTR_INFO_VALID_MASK;
4612 if (vcpu->arch.interrupt.soft) {
4613 intr |= INTR_TYPE_SOFT_INTR;
4614 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4615 vmx->vcpu.arch.event_exit_inst_len);
4616 } else
4617 intr |= INTR_TYPE_EXT_INTR;
4618 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004619
4620 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004621}
4622
Sheng Yangf08864b2008-05-15 18:23:25 +08004623static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4624{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004625 struct vcpu_vmx *vmx = to_vmx(vcpu);
4626
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004627 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004628 /*
4629 * Tracking the NMI-blocked state in software is built upon
4630 * finding the next open IRQ window. This, in turn, depends on
4631 * well-behaving guests: They have to keep IRQs disabled at
4632 * least as long as the NMI handler runs. Otherwise we may
4633 * cause NMI nesting, maybe breaking the guest. But as this is
4634 * highly unlikely, we can live with the residual risk.
4635 */
4636 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
4637 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4638 }
4639
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004640 ++vcpu->stat.nmi_injections;
4641 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004642
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004643 if (vmx->rmode.vm86_active) {
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004644 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004645 return;
4646 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08004647
Sheng Yangf08864b2008-05-15 18:23:25 +08004648 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4649 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004650
4651 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004652}
4653
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004654bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004655{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004656 struct vcpu_vmx *vmx = to_vmx(vcpu);
4657 bool masked;
4658
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004659 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004660 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004661 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02004662 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004663 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4664 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4665 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004666}
4667
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004668void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004669{
4670 struct vcpu_vmx *vmx = to_vmx(vcpu);
4671
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004672 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004673 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4674 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4675 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4676 }
4677 } else {
4678 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4679 if (masked)
4680 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4681 GUEST_INTR_STATE_NMI);
4682 else
4683 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4684 GUEST_INTR_STATE_NMI);
4685 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004686}
4687
Sean Christopherson1b660b62020-04-22 19:25:44 -07004688bool vmx_nmi_blocked(struct kvm_vcpu *vcpu)
4689{
4690 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
4691 return false;
4692
4693 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4694 return true;
4695
4696 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4697 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
4698 GUEST_INTR_STATE_NMI));
4699}
4700
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004701static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Jan Kiszka2505dc92013-04-14 12:12:47 +02004702{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004703 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004704 return -EBUSY;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004705
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004706 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
4707 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004708 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004709
Sean Christopherson1b660b62020-04-22 19:25:44 -07004710 return !vmx_nmi_blocked(vcpu);
4711}
Sean Christopherson429ab572020-04-22 19:25:42 -07004712
Sean Christopherson1b660b62020-04-22 19:25:44 -07004713bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)
4714{
4715 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Sean Christopherson88c604b2020-04-22 19:25:41 -07004716 return false;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004717
Sean Christopherson7ab0abd2020-04-22 19:25:50 -07004718 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) ||
Sean Christopherson1b660b62020-04-22 19:25:44 -07004719 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4720 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Jan Kiszka2505dc92013-04-14 12:12:47 +02004721}
4722
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004723static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Gleb Natapov78646122009-03-23 12:12:11 +02004724{
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004725 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004726 return -EBUSY;
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004727
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004728 /*
4729 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
4730 * e.g. if the IRQ arrived asynchronously after checking nested events.
4731 */
4732 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004733 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004734
Sean Christopherson1b660b62020-04-22 19:25:44 -07004735 return !vmx_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02004736}
4737
Izik Eiduscbc94022007-10-25 00:29:55 +02004738static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4739{
Peter Xuff5a9832020-09-30 21:20:33 -04004740 void __user *ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004741
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004742 if (enable_unrestricted_guest)
4743 return 0;
4744
Peter Xu6a3c6232020-01-09 09:57:16 -05004745 mutex_lock(&kvm->slots_lock);
4746 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4747 PAGE_SIZE * 3);
4748 mutex_unlock(&kvm->slots_lock);
4749
Peter Xuff5a9832020-09-30 21:20:33 -04004750 if (IS_ERR(ret))
4751 return PTR_ERR(ret);
4752
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004753 to_kvm_vmx(kvm)->tss_addr = addr;
Peter Xuff5a9832020-09-30 21:20:33 -04004754
4755 return init_rmode_tss(kvm, ret);
Izik Eiduscbc94022007-10-25 00:29:55 +02004756}
4757
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004758static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4759{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004760 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004761 return 0;
4762}
4763
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004764static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004765{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004766 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004767 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004768 /*
4769 * Update instruction length as we may reinject the exception
4770 * from user space while in guest debugging mode.
4771 */
4772 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4773 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004774 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004775 return false;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004776 fallthrough;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004777 case DB_VECTOR:
Miaohe Lina8cfbae2020-02-19 10:45:48 +08004778 return !(vcpu->guest_debug &
4779 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP));
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004780 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004781 case OF_VECTOR:
4782 case BR_VECTOR:
4783 case UD_VECTOR:
4784 case DF_VECTOR:
4785 case SS_VECTOR:
4786 case GP_VECTOR:
4787 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004788 return true;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004789 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004790 return false;
4791}
4792
4793static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4794 int vec, u32 err_code)
4795{
4796 /*
4797 * Instruction with address size override prefix opcode 0x67
4798 * Cause the #SS fault with 0 error code in VM86 mode.
4799 */
4800 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004801 if (kvm_emulate_instruction(vcpu, 0)) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004802 if (vcpu->arch.halt_request) {
4803 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004804 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004805 }
4806 return 1;
4807 }
4808 return 0;
4809 }
4810
4811 /*
4812 * Forward all other exceptions that are valid in real mode.
4813 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4814 * the required debugging infrastructure rework.
4815 */
4816 kvm_queue_exception(vcpu, vec);
4817 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004818}
4819
Avi Kivity851ba692009-08-24 11:10:17 +03004820static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004821{
Sean Christopherson95b5a482019-04-19 22:50:59 -07004822 /* handled by vmx_vcpu_run() */
Andi Kleena0861c02009-06-08 17:37:09 +08004823 return 1;
4824}
4825
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004826/*
4827 * If the host has split lock detection disabled, then #AC is
4828 * unconditionally injected into the guest, which is the pre split lock
4829 * detection behaviour.
4830 *
4831 * If the host has split lock detection enabled then #AC is
4832 * only injected into the guest when:
4833 * - Guest CPL == 3 (user mode)
4834 * - Guest has #AC detection enabled in CR0
4835 * - Guest EFLAGS has AC bit set
4836 */
4837static inline bool guest_inject_ac(struct kvm_vcpu *vcpu)
4838{
4839 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
4840 return true;
4841
4842 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) &&
4843 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
4844}
4845
Sean Christopherson95b5a482019-04-19 22:50:59 -07004846static int handle_exception_nmi(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004847{
Avi Kivity1155f762007-11-22 11:30:47 +02004848 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004849 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004850 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004851 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004852 u32 vect_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004853
Avi Kivity1155f762007-11-22 11:30:47 +02004854 vect_info = vmx->idt_vectoring_info;
Sean Christophersonf27ad732020-04-27 10:18:37 -07004855 intr_info = vmx_get_intr_info(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856
Paolo Bonzini2ea72032019-06-06 14:57:25 +02004857 if (is_machine_check(intr_info) || is_nmi(intr_info))
Sean Christopherson95b5a482019-04-19 22:50:59 -07004858 return 1; /* handled by handle_exception_nmi_irqoff() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004859
Wanpeng Li082d06e2018-04-03 16:28:48 -07004860 if (is_invalid_opcode(intr_info))
4861 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004862
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004864 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004866
Liran Alon9e869482018-03-12 13:12:51 +02004867 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4868 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christophersona6c6ed12019-08-27 14:40:30 -07004869
4870 /*
4871 * VMware backdoor emulation on #GP interception only handles
4872 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
4873 * error code on #GP.
4874 */
4875 if (error_code) {
4876 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4877 return 1;
4878 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004879 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon9e869482018-03-12 13:12:51 +02004880 }
4881
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004882 /*
4883 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4884 * MMIO, it is better to report an internal error.
4885 * See the comments in vmx_handle_exit.
4886 */
4887 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4888 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4889 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4890 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Jim Mattson1aa561b2020-06-03 16:56:21 -07004891 vcpu->run->internal.ndata = 4;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004892 vcpu->run->internal.data[0] = vect_info;
4893 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004894 vcpu->run->internal.data[2] = error_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07004895 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004896 return 0;
4897 }
4898
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899 if (is_page_fault(intr_info)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07004900 cr2 = vmx_get_exit_qual(vcpu);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004901 if (enable_ept && !vcpu->arch.apf.host_apf_flags) {
4902 /*
4903 * EPT will cause page fault only if we need to
4904 * detect illegal GPAs.
4905 */
Mohammed Gamalb96e6502020-09-03 16:11:22 +02004906 WARN_ON_ONCE(!allow_smaller_maxphyaddr);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004907 kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
4908 return 1;
4909 } else
4910 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004911 }
4912
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004913 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004914
4915 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4916 return handle_rmode_exception(vcpu, ex_no, error_code);
4917
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004918 switch (ex_no) {
4919 case DB_VECTOR:
Sean Christopherson5addc232020-04-15 13:34:53 -07004920 dr6 = vmx_get_exit_qual(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004921 if (!(vcpu->guest_debug &
4922 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004923 if (is_icebp(intr_info))
Sean Christopherson1957aa62019-08-27 14:40:39 -07004924 WARN_ON(!skip_emulated_instruction(vcpu));
Huw Daviesfd2a4452014-04-16 10:02:51 +01004925
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004926 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004927 return 1;
4928 }
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08004929 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004930 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004931 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004932 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004933 /*
4934 * Update instruction length as we may reinject #BP from
4935 * user space while in guest debugging mode. Reading it for
4936 * #DB as well causes no harm, it is not used in that case.
4937 */
4938 vmx->vcpu.arch.event_exit_inst_len =
4939 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004940 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004941 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004942 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4943 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004944 break;
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004945 case AC_VECTOR:
4946 if (guest_inject_ac(vcpu)) {
4947 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4948 return 1;
4949 }
4950
4951 /*
4952 * Handle split lock. Depending on detection mode this will
4953 * either warn and disable split lock detection for this
4954 * task or force SIGBUS on it.
4955 */
4956 if (handle_guest_split_lock(kvm_rip_read(vcpu)))
4957 return 1;
4958 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004959 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004960 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4961 kvm_run->ex.exception = ex_no;
4962 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004963 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004964 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004965 return 0;
4966}
4967
Andrea Arcangelif399e602019-11-04 17:59:58 -05004968static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004970 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971 return 1;
4972}
4973
Avi Kivity851ba692009-08-24 11:10:17 +03004974static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004975{
Avi Kivity851ba692009-08-24 11:10:17 +03004976 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004977 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004978 return 0;
4979}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980
Avi Kivity851ba692009-08-24 11:10:17 +03004981static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982{
He, Qingbfdaab02007-09-12 14:18:28 +08004983 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004984 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004985 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986
Sean Christopherson5addc232020-04-15 13:34:53 -07004987 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02004988 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004989
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004990 ++vcpu->stat.io_exits;
4991
Sean Christopherson432baf62018-03-08 08:57:26 -08004992 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004993 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004994
4995 port = exit_qualification >> 16;
4996 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004997 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004998
Sean Christophersondca7f122018-03-08 08:57:27 -08004999 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000}
5001
Ingo Molnar102d8322007-02-19 14:37:47 +02005002static void
5003vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5004{
5005 /*
5006 * Patch in the VMCALL instruction:
5007 */
5008 hypercall[0] = 0x0f;
5009 hypercall[1] = 0x01;
5010 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005011}
5012
Guo Chao0fa06072012-06-28 15:16:19 +08005013/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005014static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5015{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005016 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005017 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5018 unsigned long orig_val = val;
5019
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005020 /*
5021 * We get here when L2 changed cr0 in a way that did not change
5022 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005023 * but did change L0 shadowed bits. So we first calculate the
5024 * effective cr0 value that L1 would like to write into the
5025 * hardware. It consists of the L2-owned bits from the new
5026 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005027 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005028 val = (val & ~vmcs12->cr0_guest_host_mask) |
5029 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5030
David Matlack38991522016-11-29 18:14:08 -08005031 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005032 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005033
5034 if (kvm_set_cr0(vcpu, val))
5035 return 1;
5036 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005037 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005038 } else {
5039 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08005040 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005041 return 1;
David Matlack38991522016-11-29 18:14:08 -08005042
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005043 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005044 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005045}
5046
5047static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5048{
5049 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005050 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5051 unsigned long orig_val = val;
5052
5053 /* analogously to handle_set_cr0 */
5054 val = (val & ~vmcs12->cr4_guest_host_mask) |
5055 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5056 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005057 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005058 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005059 return 0;
5060 } else
5061 return kvm_set_cr4(vcpu, val);
5062}
5063
Paolo Bonzini0367f202016-07-12 10:44:55 +02005064static int handle_desc(struct kvm_vcpu *vcpu)
5065{
5066 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005067 return kvm_emulate_instruction(vcpu, 0);
Paolo Bonzini0367f202016-07-12 10:44:55 +02005068}
5069
Avi Kivity851ba692009-08-24 11:10:17 +03005070static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005071{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005072 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005073 int cr;
5074 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005075 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005076 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005077
Sean Christopherson5addc232020-04-15 13:34:53 -07005078 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079 cr = exit_qualification & 15;
5080 reg = (exit_qualification >> 8) & 15;
5081 switch ((exit_qualification >> 4) & 3) {
5082 case 0: /* mov to cr */
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005083 val = kvm_register_read(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005084 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005085 switch (cr) {
5086 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005087 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005088 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005089 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005090 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03005091 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005092 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005093 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005094 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005095 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005096 case 8: {
5097 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005098 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005099 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005100 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005101 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08005102 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005103 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005104 return ret;
5105 /*
5106 * TODO: we might be squashing a
5107 * KVM_GUESTDBG_SINGLESTEP-triggered
5108 * KVM_EXIT_DEBUG here.
5109 */
Avi Kivity851ba692009-08-24 11:10:17 +03005110 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005111 return 0;
5112 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005113 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005114 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005115 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005116 WARN_ONCE(1, "Guest should always own CR0.TS");
5117 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005118 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005119 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005120 case 1: /*mov from cr*/
5121 switch (cr) {
5122 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005123 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02005124 val = kvm_read_cr3(vcpu);
5125 kvm_register_write(vcpu, reg, val);
5126 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005127 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005128 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005129 val = kvm_get_cr8(vcpu);
5130 kvm_register_write(vcpu, reg, val);
5131 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005132 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005133 }
5134 break;
5135 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005136 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005137 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005138 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005139
Kyle Huey6affcbe2016-11-29 12:40:40 -08005140 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005141 default:
5142 break;
5143 }
Avi Kivity851ba692009-08-24 11:10:17 +03005144 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005145 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005146 (int)(exit_qualification >> 4) & 3, cr);
5147 return 0;
5148}
5149
Avi Kivity851ba692009-08-24 11:10:17 +03005150static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005151{
He, Qingbfdaab02007-09-12 14:18:28 +08005152 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005153 int dr, dr7, reg;
Paolo Bonzini996ff542020-12-14 07:49:54 -05005154 int err = 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005155
Sean Christopherson5addc232020-04-15 13:34:53 -07005156 exit_qualification = vmx_get_exit_qual(vcpu);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005157 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5158
5159 /* First, if DR does not exist, trigger UD */
5160 if (!kvm_require_dr(vcpu, dr))
5161 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005162
Paolo Bonzini996ff542020-12-14 07:49:54 -05005163 if (kvm_x86_ops.get_cpl(vcpu) > 0)
5164 goto out;
5165
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005166 dr7 = vmcs_readl(GUEST_DR7);
5167 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005168 /*
5169 * As the vm-exit takes precedence over the debug trap, we
5170 * need to emulate the latter, either for the host or the
5171 * guest debugging itself.
5172 */
5173 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08005174 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_ACTIVE_LOW;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005175 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005176 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005177 vcpu->run->debug.arch.exception = DB_VECTOR;
5178 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005179 return 0;
5180 } else {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04005181 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005182 return 1;
5183 }
5184 }
5185
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005186 if (vcpu->guest_debug == 0) {
Sean Christopherson2183f562019-05-07 12:17:56 -07005187 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005188
5189 /*
5190 * No more DR vmexits; force a reload of the debug registers
5191 * and reenter on this instruction. The next vmexit will
5192 * retrieve the full state of the debug registers.
5193 */
5194 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5195 return 1;
5196 }
5197
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005198 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5199 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005200 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005201
Paolo Bonzini29d6ca42021-02-03 03:42:41 -05005202 kvm_get_dr(vcpu, dr, &val);
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005203 kvm_register_write(vcpu, reg, val);
Paolo Bonzini996ff542020-12-14 07:49:54 -05005204 err = 0;
5205 } else {
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005206 err = kvm_set_dr(vcpu, dr, kvm_register_read(vcpu, reg));
Paolo Bonzini996ff542020-12-14 07:49:54 -05005207 }
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005208
Paolo Bonzini996ff542020-12-14 07:49:54 -05005209out:
5210 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005211}
5212
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005213static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5214{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005215 get_debugreg(vcpu->arch.db[0], 0);
5216 get_debugreg(vcpu->arch.db[1], 1);
5217 get_debugreg(vcpu->arch.db[2], 2);
5218 get_debugreg(vcpu->arch.db[3], 3);
5219 get_debugreg(vcpu->arch.dr6, 6);
5220 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5221
5222 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Sean Christopherson2183f562019-05-07 12:17:56 -07005223 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005224}
5225
Gleb Natapov020df072010-04-13 10:05:23 +03005226static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5227{
5228 vmcs_writel(GUEST_DR7, val);
5229}
5230
Avi Kivity851ba692009-08-24 11:10:17 +03005231static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005232{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01005233 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005234 return 1;
5235}
5236
Avi Kivity851ba692009-08-24 11:10:17 +03005237static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005238{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005239 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005240
Avi Kivity3842d132010-07-27 12:30:24 +03005241 kvm_make_request(KVM_REQ_EVENT, vcpu);
5242
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005243 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005244 return 1;
5245}
5246
Avi Kivity851ba692009-08-24 11:10:17 +03005247static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005248{
Sean Christopherson5addc232020-04-15 13:34:53 -07005249 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005250
5251 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005252 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005253}
5254
Avi Kivity851ba692009-08-24 11:10:17 +03005255static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005256{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005257 if (likely(fasteoi)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07005258 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005259 int access_type, offset;
5260
5261 access_type = exit_qualification & APIC_ACCESS_TYPE;
5262 offset = exit_qualification & APIC_ACCESS_OFFSET;
5263 /*
5264 * Sane guest uses MOV to write EOI, with written value
5265 * not cared. So make a short-circuit here by avoiding
5266 * heavy instruction emulation.
5267 */
5268 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5269 (offset == APIC_EOI)) {
5270 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005271 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005272 }
5273 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005274 return kvm_emulate_instruction(vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005275}
5276
Yang Zhangc7c9c562013-01-25 10:18:51 +08005277static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5278{
Sean Christopherson5addc232020-04-15 13:34:53 -07005279 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005280 int vector = exit_qualification & 0xff;
5281
5282 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5283 kvm_apic_set_eoi_accelerated(vcpu, vector);
5284 return 1;
5285}
5286
Yang Zhang83d4c282013-01-25 10:18:49 +08005287static int handle_apic_write(struct kvm_vcpu *vcpu)
5288{
Sean Christopherson5addc232020-04-15 13:34:53 -07005289 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhang83d4c282013-01-25 10:18:49 +08005290 u32 offset = exit_qualification & 0xfff;
5291
5292 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5293 kvm_apic_write_nodecode(vcpu, offset);
5294 return 1;
5295}
5296
Avi Kivity851ba692009-08-24 11:10:17 +03005297static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005298{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005299 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005300 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005301 bool has_error_code = false;
5302 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005303 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005304 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005305
5306 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005307 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005308 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005309
Sean Christopherson5addc232020-04-15 13:34:53 -07005310 exit_qualification = vmx_get_exit_qual(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005311
5312 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005313 if (reason == TASK_SWITCH_GATE && idt_v) {
5314 switch (type) {
5315 case INTR_TYPE_NMI_INTR:
5316 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005317 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005318 break;
5319 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005320 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005321 kvm_clear_interrupt_queue(vcpu);
5322 break;
5323 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005324 if (vmx->idt_vectoring_info &
5325 VECTORING_INFO_DELIVER_CODE_MASK) {
5326 has_error_code = true;
5327 error_code =
5328 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5329 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005330 fallthrough;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005331 case INTR_TYPE_SOFT_EXCEPTION:
5332 kvm_clear_exception_queue(vcpu);
5333 break;
5334 default:
5335 break;
5336 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005337 }
Izik Eidus37817f22008-03-24 23:14:53 +02005338 tss_selector = exit_qualification;
5339
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005340 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5341 type != INTR_TYPE_EXT_INTR &&
5342 type != INTR_TYPE_NMI_INTR))
Sean Christopherson1957aa62019-08-27 14:40:39 -07005343 WARN_ON(!skip_emulated_instruction(vcpu));
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005344
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005345 /*
5346 * TODO: What about debug traps on tss switch?
5347 * Are we supposed to inject them and update dr6?
5348 */
Sean Christopherson10517782019-08-27 14:40:35 -07005349 return kvm_task_switch(vcpu, tss_selector,
5350 type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005351 reason, has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02005352}
5353
Avi Kivity851ba692009-08-24 11:10:17 +03005354static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005355{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005356 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005357 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01005358 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005359
Sean Christopherson5addc232020-04-15 13:34:53 -07005360 exit_qualification = vmx_get_exit_qual(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005361
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005362 /*
5363 * EPT violation happened while executing iret from NMI,
5364 * "blocked by NMI" bit has to be set before next VM entry.
5365 * There are errata that may cause this bit to not be set:
5366 * AAK134, BY25.
5367 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005368 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005369 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005370 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005371 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5372
Sheng Yang14394422008-04-28 12:24:45 +08005373 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005374 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005375
Junaid Shahid27959a42016-12-06 16:46:10 -08005376 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005377 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08005378 ? PFERR_USER_MASK : 0;
5379 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005380 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08005381 ? PFERR_WRITE_MASK : 0;
5382 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005383 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08005384 ? PFERR_FETCH_MASK : 0;
5385 /* ept page table entry is present? */
5386 error_code |= (exit_qualification &
5387 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5388 EPT_VIOLATION_EXECUTABLE))
5389 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005390
Isaku Yamahata108356022021-04-22 17:22:29 -07005391 error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) != 0 ?
Paolo Bonzinieebed242016-11-28 14:39:58 +01005392 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005393
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005394 vcpu->arch.exit_qualification = exit_qualification;
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005395
5396 /*
5397 * Check that the GPA doesn't exceed physical memory limits, as that is
5398 * a guest page fault. We have to emulate the instruction here, because
5399 * if the illegal address is that of a paging structure, then
5400 * EPT_VIOLATION_ACC_WRITE bit is set. Alternatively, if supported we
5401 * would also use advanced VM-exit information for EPT violations to
5402 * reconstruct the page fault error code.
5403 */
Paolo Bonzinic0623f52020-10-21 18:05:58 -04005404 if (unlikely(allow_smaller_maxphyaddr && kvm_vcpu_is_illegal_gpa(vcpu, gpa)))
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005405 return kvm_emulate_instruction(vcpu, 0);
5406
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005407 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005408}
5409
Avi Kivity851ba692009-08-24 11:10:17 +03005410static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005411{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005412 gpa_t gpa;
5413
Sean Christopherson3c0c2ad2021-04-12 16:21:37 +12005414 if (!vmx_can_emulate_instruction(vcpu, NULL, 0))
5415 return 1;
5416
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005417 /*
5418 * A nested guest cannot optimize MMIO vmexits, because we have an
5419 * nGPA here instead of the required GPA.
5420 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005421 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005422 if (!is_guest_mode(vcpu) &&
5423 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08005424 trace_kvm_fast_mmio(gpa);
Sean Christopherson1957aa62019-08-27 14:40:39 -07005425 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005426 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005427
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07005428 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005429}
5430
Avi Kivity851ba692009-08-24 11:10:17 +03005431static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005432{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005433 WARN_ON_ONCE(!enable_vnmi);
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005434 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Sheng Yangf08864b2008-05-15 18:23:25 +08005435 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005436 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005437
5438 return 1;
5439}
5440
Mohammed Gamal80ced182009-09-01 12:48:18 +02005441static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005442{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005443 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity49e9d552010-09-19 14:34:08 +02005444 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005445 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005446
Sean Christopherson2183f562019-05-07 12:17:56 -07005447 intr_window_requested = exec_controls_get(vmx) &
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005448 CPU_BASED_INTR_WINDOW_EXITING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005449
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005450 while (vmx->emulation_required && count-- != 0) {
Sean Christophersondb438592020-04-22 19:25:48 -07005451 if (intr_window_requested && !vmx_interrupt_blocked(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005452 return handle_interrupt_window(&vmx->vcpu);
5453
Radim Krčmář72875d82017-04-26 22:32:19 +02005454 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005455 return 1;
5456
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005457 if (!kvm_emulate_instruction(vcpu, 0))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005458 return 0;
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005459
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005460 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
Sean Christopherson8fff2712019-08-27 14:40:37 -07005461 vcpu->arch.exception.pending) {
5462 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5463 vcpu->run->internal.suberror =
5464 KVM_INTERNAL_ERROR_EMULATION;
5465 vcpu->run->internal.ndata = 0;
5466 return 0;
5467 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005468
Gleb Natapov8d76c492013-05-08 18:38:44 +03005469 if (vcpu->arch.halt_request) {
5470 vcpu->arch.halt_request = 0;
Sean Christopherson8fff2712019-08-27 14:40:37 -07005471 return kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005472 }
5473
Sean Christopherson8fff2712019-08-27 14:40:37 -07005474 /*
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005475 * Note, return 1 and not 0, vcpu_run() will invoke
5476 * xfer_to_guest_mode() which will create a proper return
5477 * code.
Sean Christopherson8fff2712019-08-27 14:40:37 -07005478 */
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005479 if (__xfer_to_guest_mode_work_pending())
Sean Christopherson8fff2712019-08-27 14:40:37 -07005480 return 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005481 }
5482
Sean Christopherson8fff2712019-08-27 14:40:37 -07005483 return 1;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005484}
5485
5486static void grow_ple_window(struct kvm_vcpu *vcpu)
5487{
5488 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005489 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005490
Babu Mogerc8e88712018-03-16 16:37:24 -04005491 vmx->ple_window = __grow_ple_window(old, ple_window,
5492 ple_window_grow,
5493 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005494
Peter Xu4f75bcc2019-09-06 10:17:22 +08005495 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005496 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005497 trace_kvm_ple_window_update(vcpu->vcpu_id,
5498 vmx->ple_window, old);
5499 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005500}
5501
5502static void shrink_ple_window(struct kvm_vcpu *vcpu)
5503{
5504 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005505 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005506
Babu Mogerc8e88712018-03-16 16:37:24 -04005507 vmx->ple_window = __shrink_ple_window(old, ple_window,
5508 ple_window_shrink,
5509 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005510
Peter Xu4f75bcc2019-09-06 10:17:22 +08005511 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005512 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005513 trace_kvm_ple_window_update(vcpu->vcpu_id,
5514 vmx->ple_window, old);
5515 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005516}
5517
Avi Kivity6aa8b732006-12-10 02:21:36 -08005518/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005519 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5520 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5521 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005522static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005523{
Wanpeng Lib31c1142018-03-12 04:53:04 -07005524 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005525 grow_ple_window(vcpu);
5526
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08005527 /*
5528 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
5529 * VM-execution control is ignored if CPL > 0. OTOH, KVM
5530 * never set PAUSE_EXITING and just set PLE if supported,
5531 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
5532 */
5533 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005534 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005535}
5536
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005537static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5538{
5539 return 1;
5540}
5541
Junaid Shahideb4b2482018-06-27 14:59:14 -07005542static int handle_invpcid(struct kvm_vcpu *vcpu)
5543{
5544 u32 vmx_instruction_info;
5545 unsigned long type;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005546 gva_t gva;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005547 struct {
5548 u64 pcid;
5549 u64 gla;
5550 } operand;
5551
5552 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5553 kvm_queue_exception(vcpu, UD_VECTOR);
5554 return 1;
5555 }
5556
5557 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07005558 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005559
5560 if (type > 3) {
5561 kvm_inject_gp(vcpu, 0);
5562 return 1;
5563 }
5564
5565 /* According to the Intel instruction reference, the memory operand
5566 * is read even if it isn't needed (e.g., for type==all)
5567 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005568 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005569 vmx_instruction_info, false,
5570 sizeof(operand), &gva))
Junaid Shahideb4b2482018-06-27 14:59:14 -07005571 return 1;
5572
Babu Moger97150922020-09-11 14:29:12 -05005573 return kvm_handle_invpcid(vcpu, type, gva);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005574}
5575
Kai Huang843e4332015-01-28 10:54:28 +08005576static int handle_pml_full(struct kvm_vcpu *vcpu)
5577{
5578 unsigned long exit_qualification;
5579
5580 trace_kvm_pml_full(vcpu->vcpu_id);
5581
Sean Christopherson5addc232020-04-15 13:34:53 -07005582 exit_qualification = vmx_get_exit_qual(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005583
5584 /*
5585 * PML buffer FULL happened while executing iret from NMI,
5586 * "blocked by NMI" bit has to be set before next VM entry.
5587 */
5588 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005589 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005590 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5591 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5592 GUEST_INTR_STATE_NMI);
5593
5594 /*
5595 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5596 * here.., and there's no userspace involvement needed for PML.
5597 */
5598 return 1;
5599}
5600
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005601static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07005602{
Sean Christopherson804939e2019-05-07 12:18:05 -07005603 struct vcpu_vmx *vmx = to_vmx(vcpu);
5604
5605 if (!vmx->req_immediate_exit &&
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005606 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) {
Sean Christophersond264ee02018-08-27 15:21:12 -07005607 kvm_lapic_expired_hv_timer(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005608 return EXIT_FASTPATH_REENTER_GUEST;
5609 }
Sean Christopherson804939e2019-05-07 12:18:05 -07005610
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005611 return EXIT_FASTPATH_NONE;
5612}
5613
5614static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5615{
5616 handle_fastpath_preemption_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005617 return 1;
5618}
5619
Sean Christophersone4027cf2018-12-03 13:53:12 -08005620/*
5621 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5622 * are overwritten by nested_vmx_setup() when nested=1.
5623 */
5624static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5625{
5626 kvm_queue_exception(vcpu, UD_VECTOR);
5627 return 1;
5628}
5629
Sean Christopherson9798adb2021-04-12 16:21:38 +12005630#ifndef CONFIG_X86_SGX_KVM
Sean Christopherson0b665d32018-08-14 09:33:34 -07005631static int handle_encls(struct kvm_vcpu *vcpu)
5632{
5633 /*
Sean Christopherson9798adb2021-04-12 16:21:38 +12005634 * SGX virtualization is disabled. There is no software enable bit for
5635 * SGX, so KVM intercepts all ENCLS leafs and injects a #UD to prevent
5636 * the guest from executing ENCLS (when SGX is supported by hardware).
Sean Christopherson0b665d32018-08-14 09:33:34 -07005637 */
5638 kvm_queue_exception(vcpu, UD_VECTOR);
5639 return 1;
5640}
Sean Christopherson9798adb2021-04-12 16:21:38 +12005641#endif /* CONFIG_X86_SGX_KVM */
Sean Christopherson0b665d32018-08-14 09:33:34 -07005642
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005643static int handle_bus_lock_vmexit(struct kvm_vcpu *vcpu)
5644{
5645 vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK;
5646 vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK;
5647 return 0;
5648}
5649
Nadav Har'El0140cae2011-05-25 23:06:28 +03005650/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005651 * The exit handlers return 1 if the exit was handled fully and guest execution
5652 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5653 * to be done to userspace and return 0.
5654 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005655static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Sean Christopherson95b5a482019-04-19 22:50:59 -07005656 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005657 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005658 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005659 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005660 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005661 [EXIT_REASON_CR_ACCESS] = handle_cr,
5662 [EXIT_REASON_DR_ACCESS] = handle_dr,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005663 [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
5664 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
5665 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005666 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005667 [EXIT_REASON_HLT] = kvm_emulate_halt,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005668 [EXIT_REASON_INVD] = kvm_emulate_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005669 [EXIT_REASON_INVLPG] = handle_invlpg,
Sean Christophersonc483c452021-02-04 16:57:48 -08005670 [EXIT_REASON_RDPMC] = kvm_emulate_rdpmc,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005671 [EXIT_REASON_VMCALL] = kvm_emulate_hypercall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005672 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5673 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5674 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5675 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5676 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5677 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5678 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5679 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5680 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005681 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5682 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005683 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005684 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005685 [EXIT_REASON_WBINVD] = kvm_emulate_wbinvd,
Sean Christopherson92f98952021-02-04 16:57:46 -08005686 [EXIT_REASON_XSETBV] = kvm_emulate_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005687 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005688 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005689 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5690 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005691 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5692 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005693 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005694 [EXIT_REASON_MWAIT_INSTRUCTION] = kvm_emulate_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005695 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005696 [EXIT_REASON_MONITOR_INSTRUCTION] = kvm_emulate_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005697 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5698 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08005699 [EXIT_REASON_RDRAND] = kvm_handle_invalid_op,
5700 [EXIT_REASON_RDSEED] = kvm_handle_invalid_op,
Kai Huang843e4332015-01-28 10:54:28 +08005701 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005702 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005703 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005704 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005705 [EXIT_REASON_ENCLS] = handle_encls,
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005706 [EXIT_REASON_BUS_LOCK] = handle_bus_lock_vmexit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005707};
5708
5709static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005710 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005711
Sean Christopherson235ba742020-09-23 13:13:46 -07005712static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
5713 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02005714{
Sean Christopherson235ba742020-09-23 13:13:46 -07005715 struct vcpu_vmx *vmx = to_vmx(vcpu);
5716
Sean Christopherson5addc232020-04-15 13:34:53 -07005717 *info1 = vmx_get_exit_qual(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08005718 if (!(vmx->exit_reason.failed_vmentry)) {
Sean Christopherson235ba742020-09-23 13:13:46 -07005719 *info2 = vmx->idt_vectoring_info;
5720 *intr_info = vmx_get_intr_info(vcpu);
5721 if (is_exception_with_error_code(*intr_info))
5722 *error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5723 else
5724 *error_code = 0;
5725 } else {
5726 *info2 = 0;
5727 *intr_info = 0;
5728 *error_code = 0;
5729 }
Avi Kivity586f9602010-11-18 13:09:54 +02005730}
5731
Kai Huanga3eaa862015-11-04 13:46:05 +08005732static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005733{
Kai Huanga3eaa862015-11-04 13:46:05 +08005734 if (vmx->pml_pg) {
5735 __free_page(vmx->pml_pg);
5736 vmx->pml_pg = NULL;
5737 }
Kai Huang843e4332015-01-28 10:54:28 +08005738}
5739
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005740static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005741{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005742 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005743 u64 *pml_buf;
5744 u16 pml_idx;
5745
5746 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5747
5748 /* Do nothing if PML buffer is empty */
5749 if (pml_idx == (PML_ENTITY_NUM - 1))
5750 return;
5751
5752 /* PML index always points to next available PML buffer entity */
5753 if (pml_idx >= PML_ENTITY_NUM)
5754 pml_idx = 0;
5755 else
5756 pml_idx++;
5757
5758 pml_buf = page_address(vmx->pml_pg);
5759 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5760 u64 gpa;
5761
5762 gpa = pml_buf[pml_idx];
5763 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005764 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005765 }
5766
5767 /* reset PML index */
5768 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5769}
5770
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005771static void vmx_dump_sel(char *name, uint32_t sel)
5772{
5773 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005774 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005775 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5776 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5777 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5778}
5779
5780static void vmx_dump_dtsel(char *name, uint32_t limit)
5781{
5782 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5783 name, vmcs_read32(limit),
5784 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5785}
5786
David Edmondson84860392021-03-18 12:08:41 +00005787static void vmx_dump_msrs(char *name, struct vmx_msrs *m)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005788{
David Edmondson84860392021-03-18 12:08:41 +00005789 unsigned int i;
5790 struct vmx_msr_entry *e;
5791
5792 pr_err("MSR %s:\n", name);
5793 for (i = 0, e = m->val; i < m->nr; ++i, ++e)
5794 pr_err(" %2d: msr=0x%08x value=0x%016llx\n", i, e->index, e->value);
5795}
5796
David Edmondson0702a3c2021-03-18 12:08:40 +00005797void dump_vmcs(struct kvm_vcpu *vcpu)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005798{
David Edmondson0702a3c2021-03-18 12:08:40 +00005799 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005800 u32 vmentry_ctl, vmexit_ctl;
5801 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
5802 unsigned long cr4;
David Edmondson0702a3c2021-03-18 12:08:40 +00005803 int efer_slot;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005804
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005805 if (!dump_invalid_vmcs) {
5806 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
5807 return;
5808 }
5809
5810 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5811 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5812 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5813 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5814 cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005815 secondary_exec_control = 0;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005816 if (cpu_has_secondary_exec_ctrls())
5817 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5818
5819 pr_err("*** Guest State ***\n");
5820 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5821 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5822 vmcs_readl(CR0_GUEST_HOST_MASK));
5823 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5824 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5825 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
David Edmondsond9e46d32021-03-18 12:08:37 +00005826 if (cpu_has_vmx_ept()) {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005827 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5828 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5829 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5830 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005831 }
5832 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5833 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5834 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5835 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5836 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5837 vmcs_readl(GUEST_SYSENTER_ESP),
5838 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5839 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5840 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5841 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5842 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5843 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5844 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5845 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5846 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5847 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5848 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
David Edmondson0702a3c2021-03-18 12:08:40 +00005849 efer_slot = vmx_find_loadstore_msr_slot(&vmx->msr_autoload.guest, MSR_EFER);
David Edmondson5518da62021-03-18 12:08:39 +00005850 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_EFER)
David Edmondson699e1b22021-03-18 12:08:38 +00005851 pr_err("EFER= 0x%016llx\n", vmcs_read64(GUEST_IA32_EFER));
David Edmondson0702a3c2021-03-18 12:08:40 +00005852 else if (efer_slot >= 0)
5853 pr_err("EFER= 0x%016llx (autoload)\n",
5854 vmx->msr_autoload.guest.val[efer_slot].value);
5855 else if (vmentry_ctl & VM_ENTRY_IA32E_MODE)
5856 pr_err("EFER= 0x%016llx (effective)\n",
5857 vcpu->arch.efer | (EFER_LMA | EFER_LME));
5858 else
5859 pr_err("EFER= 0x%016llx (effective)\n",
5860 vcpu->arch.efer & ~(EFER_LMA | EFER_LME));
David Edmondson5518da62021-03-18 12:08:39 +00005861 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PAT)
David Edmondson699e1b22021-03-18 12:08:38 +00005862 pr_err("PAT = 0x%016llx\n", vmcs_read64(GUEST_IA32_PAT));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005863 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5864 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005865 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005866 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005867 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005868 pr_err("PerfGlobCtl = 0x%016llx\n",
5869 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005870 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005871 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005872 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5873 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5874 vmcs_read32(GUEST_ACTIVITY_STATE));
5875 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5876 pr_err("InterruptStatus = %04x\n",
5877 vmcs_read16(GUEST_INTR_STATUS));
David Edmondson84860392021-03-18 12:08:41 +00005878 if (vmcs_read32(VM_ENTRY_MSR_LOAD_COUNT) > 0)
5879 vmx_dump_msrs("guest autoload", &vmx->msr_autoload.guest);
5880 if (vmcs_read32(VM_EXIT_MSR_STORE_COUNT) > 0)
5881 vmx_dump_msrs("guest autostore", &vmx->msr_autostore.guest);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005882
5883 pr_err("*** Host State ***\n");
5884 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5885 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5886 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5887 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5888 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5889 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5890 vmcs_read16(HOST_TR_SELECTOR));
5891 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5892 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5893 vmcs_readl(HOST_TR_BASE));
5894 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5895 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5896 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5897 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5898 vmcs_readl(HOST_CR4));
5899 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5900 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5901 vmcs_read32(HOST_IA32_SYSENTER_CS),
5902 vmcs_readl(HOST_IA32_SYSENTER_EIP));
David Edmondson699e1b22021-03-18 12:08:38 +00005903 if (vmexit_ctl & VM_EXIT_LOAD_IA32_EFER)
5904 pr_err("EFER= 0x%016llx\n", vmcs_read64(HOST_IA32_EFER));
5905 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PAT)
5906 pr_err("PAT = 0x%016llx\n", vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005907 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005908 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005909 pr_err("PerfGlobCtl = 0x%016llx\n",
5910 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
David Edmondson84860392021-03-18 12:08:41 +00005911 if (vmcs_read32(VM_EXIT_MSR_LOAD_COUNT) > 0)
5912 vmx_dump_msrs("host autoload", &vmx->msr_autoload.host);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005913
5914 pr_err("*** Control State ***\n");
5915 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5916 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5917 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5918 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5919 vmcs_read32(EXCEPTION_BITMAP),
5920 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5921 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5922 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5923 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5924 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5925 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5926 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5927 vmcs_read32(VM_EXIT_INTR_INFO),
5928 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5929 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5930 pr_err(" reason=%08x qualification=%016lx\n",
5931 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5932 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5933 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5934 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005935 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005936 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005937 pr_err("TSC Multiplier = 0x%016llx\n",
5938 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005939 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
5940 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
5941 u16 status = vmcs_read16(GUEST_INTR_STATUS);
5942 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
5943 }
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005944 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005945 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
5946 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005947 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005948 }
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005949 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5950 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5951 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005952 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005953 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5954 pr_err("PLE Gap=%08x Window=%08x\n",
5955 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5956 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5957 pr_err("Virtual processor ID = 0x%04x\n",
5958 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5959}
5960
Avi Kivity6aa8b732006-12-10 02:21:36 -08005961/*
5962 * The guest has exited. See if we can fix it or if we need userspace
5963 * assistance.
5964 */
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005965static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005966{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005967 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08005968 union vmx_exit_reason exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005969 u32 vectoring_info = vmx->idt_vectoring_info;
Sean Christopherson8e533242020-11-06 17:03:12 +08005970 u16 exit_handler_index;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005971
Kai Huang843e4332015-01-28 10:54:28 +08005972 /*
5973 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5974 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5975 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5976 * mode as if vcpus is in root mode, the PML buffer must has been
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005977 * flushed already. Note, PML is never enabled in hardware while
5978 * running L2.
Kai Huang843e4332015-01-28 10:54:28 +08005979 */
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005980 if (enable_pml && !is_guest_mode(vcpu))
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005981 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005982
Sean Christophersondb438592020-04-22 19:25:48 -07005983 /*
5984 * We should never reach this point with a pending nested VM-Enter, and
5985 * more specifically emulation of L2 due to invalid guest state (see
5986 * below) should never happen as that means we incorrectly allowed a
5987 * nested VM-Enter with an invalid vmcs12.
5988 */
5989 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5990
Mohammed Gamal80ced182009-09-01 12:48:18 +02005991 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02005992 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02005993 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005994
Paolo Bonzini96b100c2020-03-17 18:32:50 +01005995 if (is_guest_mode(vcpu)) {
5996 /*
Sean Christophersonc3bb9a22021-02-12 16:50:07 -08005997 * PML is never enabled when running L2, bail immediately if a
5998 * PML full exit occurs as something is horribly wrong.
5999 */
6000 if (exit_reason.basic == EXIT_REASON_PML_FULL)
6001 goto unexpected_vmexit;
6002
6003 /*
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006004 * The host physical addresses of some pages of guest memory
6005 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
6006 * Page). The CPU may write to these pages via their host
6007 * physical address while L2 is running, bypassing any
6008 * address-translation-based dirty tracking (e.g. EPT write
6009 * protection).
6010 *
6011 * Mark them dirty on every exit from L2 to prevent them from
6012 * getting out of sync with dirty tracking.
6013 */
6014 nested_mark_vmcs12_pages_dirty(vcpu);
6015
Sean Christophersonf47baae2020-04-15 10:55:16 -07006016 if (nested_vmx_reflect_vmexit(vcpu))
Sean Christopherson789afc52020-04-15 10:55:10 -07006017 return 1;
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006018 }
Nadav Har'El644d7112011-05-25 23:12:35 +03006019
Sean Christopherson8e533242020-11-06 17:03:12 +08006020 if (exit_reason.failed_vmentry) {
David Edmondson0702a3c2021-03-18 12:08:40 +00006021 dump_vmcs(vcpu);
Mohammed Gamal51207022010-05-31 22:40:54 +03006022 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6023 vcpu->run->fail_entry.hardware_entry_failure_reason
Sean Christopherson8e533242020-11-06 17:03:12 +08006024 = exit_reason.full;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006025 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Mohammed Gamal51207022010-05-31 22:40:54 +03006026 return 0;
6027 }
6028
Avi Kivity29bd8a72007-09-10 17:27:03 +03006029 if (unlikely(vmx->fail)) {
David Edmondson0702a3c2021-03-18 12:08:40 +00006030 dump_vmcs(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006031 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6032 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006033 = vmcs_read32(VM_INSTRUCTION_ERROR);
Jim Mattson8a14fe42020-06-03 16:56:22 -07006034 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006035 return 0;
6036 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006037
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006038 /*
6039 * Note:
6040 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6041 * delivery event since it indicates guest is accessing MMIO.
6042 * The vm-exit can be triggered again after return to guest that
6043 * will cause infinite loop.
6044 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006045 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sean Christopherson8e533242020-11-06 17:03:12 +08006046 (exit_reason.basic != EXIT_REASON_EXCEPTION_NMI &&
6047 exit_reason.basic != EXIT_REASON_EPT_VIOLATION &&
6048 exit_reason.basic != EXIT_REASON_PML_FULL &&
6049 exit_reason.basic != EXIT_REASON_APIC_ACCESS &&
6050 exit_reason.basic != EXIT_REASON_TASK_SWITCH)) {
Reiji Watanabe04c4f2e2021-04-13 15:47:40 +00006051 int ndata = 3;
6052
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006053 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6054 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006055 vcpu->run->internal.data[0] = vectoring_info;
Sean Christopherson8e533242020-11-06 17:03:12 +08006056 vcpu->run->internal.data[1] = exit_reason.full;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006057 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
Sean Christopherson8e533242020-11-06 17:03:12 +08006058 if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG) {
Reiji Watanabe04c4f2e2021-04-13 15:47:40 +00006059 vcpu->run->internal.data[ndata++] =
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006060 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6061 }
Reiji Watanabe04c4f2e2021-04-13 15:47:40 +00006062 vcpu->run->internal.data[ndata++] = vcpu->arch.last_vmentry_cpu;
6063 vcpu->run->internal.ndata = ndata;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006064 return 0;
6065 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006066
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006067 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006068 vmx->loaded_vmcs->soft_vnmi_blocked)) {
Sean Christophersondb438592020-04-22 19:25:48 -07006069 if (!vmx_interrupt_blocked(vcpu)) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006070 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6071 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
6072 vcpu->arch.nmi_pending) {
6073 /*
6074 * This CPU don't support us in finding the end of an
6075 * NMI-blocked window if the guest runs with IRQs
6076 * disabled. So we pull the trigger after 1 s of
6077 * futile waiting, but inform the user about this.
6078 */
6079 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6080 "state on VCPU %d after 1 s timeout\n",
6081 __func__, vcpu->vcpu_id);
6082 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6083 }
6084 }
6085
Wanpeng Li404d5d72020-04-28 14:23:25 +08006086 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08006087 return 1;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006088
Sean Christopherson8e533242020-11-06 17:03:12 +08006089 if (exit_reason.basic >= kvm_vmx_max_exit_handlers)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006090 goto unexpected_vmexit;
6091#ifdef CONFIG_RETPOLINE
Sean Christopherson8e533242020-11-06 17:03:12 +08006092 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006093 return kvm_emulate_wrmsr(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006094 else if (exit_reason.basic == EXIT_REASON_PREEMPTION_TIMER)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006095 return handle_preemption_timer(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006096 else if (exit_reason.basic == EXIT_REASON_INTERRUPT_WINDOW)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006097 return handle_interrupt_window(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006098 else if (exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006099 return handle_external_interrupt(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006100 else if (exit_reason.basic == EXIT_REASON_HLT)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006101 return kvm_emulate_halt(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006102 else if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006103 return handle_ept_misconfig(vcpu);
6104#endif
6105
Sean Christopherson8e533242020-11-06 17:03:12 +08006106 exit_handler_index = array_index_nospec((u16)exit_reason.basic,
6107 kvm_vmx_max_exit_handlers);
6108 if (!kvm_vmx_exit_handlers[exit_handler_index])
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006109 goto unexpected_vmexit;
6110
Sean Christopherson8e533242020-11-06 17:03:12 +08006111 return kvm_vmx_exit_handlers[exit_handler_index](vcpu);
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006112
6113unexpected_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08006114 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
6115 exit_reason.full);
David Edmondson0702a3c2021-03-18 12:08:40 +00006116 dump_vmcs(vcpu);
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006117 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6118 vcpu->run->internal.suberror =
6119 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
Jim Mattson1aa561b2020-06-03 16:56:21 -07006120 vcpu->run->internal.ndata = 2;
Sean Christopherson8e533242020-11-06 17:03:12 +08006121 vcpu->run->internal.data[0] = exit_reason.full;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006122 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006123 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006124}
6125
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08006126static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
6127{
6128 int ret = __vmx_handle_exit(vcpu, exit_fastpath);
6129
6130 /*
6131 * Even when current exit reason is handled by KVM internally, we
6132 * still need to exit to user space when bus lock detected to inform
6133 * that there is a bus lock in guest.
6134 */
6135 if (to_vmx(vcpu)->exit_reason.bus_lock_detected) {
6136 if (ret > 0)
6137 vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK;
6138
6139 vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK;
6140 return 0;
6141 }
6142 return ret;
6143}
6144
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006145/*
6146 * Software based L1D cache flush which is used when microcode providing
6147 * the cache control MSR is not loaded.
6148 *
6149 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
6150 * flush it is required to read in 64 KiB because the replacement algorithm
6151 * is not exactly LRU. This could be sized at runtime via topology
6152 * information but as all relevant affected CPUs have 32KiB L1D cache size
6153 * there is no point in doing so.
6154 */
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006155static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006156{
6157 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006158
6159 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02006160 * This code is only executed when the the flush mode is 'cond' or
6161 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006162 */
Nicolai Stange427362a2018-07-21 22:25:00 +02006163 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02006164 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006165
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006166 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02006167 * Clear the per-vcpu flush bit, it gets set again
6168 * either from vcpu_run() or from one of the unsafe
6169 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006170 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02006171 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02006172 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02006173
6174 /*
6175 * Clear the per-cpu flush bit, it gets set again from
6176 * the interrupt handlers.
6177 */
6178 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
6179 kvm_clear_cpu_l1tf_flush_l1d();
6180
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006181 if (!flush_l1d)
6182 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006183 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006184
6185 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006186
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006187 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006188 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006189 return;
6190 }
6191
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006192 asm volatile(
6193 /* First ensure the pages are in the TLB */
6194 "xorl %%eax, %%eax\n"
6195 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02006196 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006197 "addl $4096, %%eax\n\t"
6198 "cmpl %%eax, %[size]\n\t"
6199 "jne .Lpopulate_tlb\n\t"
6200 "xorl %%eax, %%eax\n\t"
6201 "cpuid\n\t"
6202 /* Now fill the cache */
6203 "xorl %%eax, %%eax\n"
6204 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006205 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006206 "addl $64, %%eax\n\t"
6207 "cmpl %%eax, %[size]\n\t"
6208 "jne .Lfill_cache\n\t"
6209 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006210 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006211 [size] "r" (size)
6212 : "eax", "ebx", "ecx", "edx");
6213}
6214
Jason Baronb6a7cc32021-01-14 22:27:54 -05006215static void vmx_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006216{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006217 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Liran Alon132f4f72019-11-11 14:30:54 +02006218 int tpr_threshold;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006219
6220 if (is_guest_mode(vcpu) &&
6221 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6222 return;
6223
Liran Alon132f4f72019-11-11 14:30:54 +02006224 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
Liran Alon02d496cf2019-11-11 14:30:55 +02006225 if (is_guest_mode(vcpu))
6226 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6227 else
6228 vmcs_write32(TPR_THRESHOLD, tpr_threshold);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006229}
6230
Sean Christopherson97b7ead2018-12-03 13:53:16 -08006231void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08006232{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006233 struct vcpu_vmx *vmx = to_vmx(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006234 u32 sec_exec_control;
6235
Jim Mattson8d860bb2018-05-09 16:56:05 -04006236 if (!lapic_in_kernel(vcpu))
6237 return;
6238
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07006239 if (!flexpriority_enabled &&
6240 !cpu_has_vmx_virtualize_x2apic_mode())
6241 return;
6242
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006243 /* Postpone execution until vmcs01 is the current VMCS. */
6244 if (is_guest_mode(vcpu)) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006245 vmx->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006246 return;
6247 }
6248
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006249 sec_exec_control = secondary_exec_controls_get(vmx);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006250 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6251 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08006252
Jim Mattson8d860bb2018-05-09 16:56:05 -04006253 switch (kvm_get_apic_mode(vcpu)) {
6254 case LAPIC_MODE_INVALID:
6255 WARN_ONCE(true, "Invalid local APIC state");
6256 case LAPIC_MODE_DISABLED:
6257 break;
6258 case LAPIC_MODE_XAPIC:
6259 if (flexpriority_enabled) {
6260 sec_exec_control |=
6261 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006262 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6263
6264 /*
6265 * Flush the TLB, reloading the APIC access page will
6266 * only do so if its physical address has changed, but
6267 * the guest may have inserted a non-APIC mapping into
6268 * the TLB while the APIC access page was disabled.
6269 */
6270 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006271 }
6272 break;
6273 case LAPIC_MODE_X2APIC:
6274 if (cpu_has_vmx_virtualize_x2apic_mode())
6275 sec_exec_control |=
6276 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6277 break;
Yang Zhang8d146952013-01-25 10:18:50 +08006278 }
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006279 secondary_exec_controls_set(vmx, sec_exec_control);
Yang Zhang8d146952013-01-25 10:18:50 +08006280
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006281 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006282}
6283
Sean Christophersona4148b72020-03-20 14:28:24 -07006284static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
Tang Chen38b99172014-09-24 15:57:54 +08006285{
Sean Christophersona4148b72020-03-20 14:28:24 -07006286 struct page *page;
6287
Sean Christopherson1196cb92020-03-20 14:28:23 -07006288 /* Defer reload until vmcs01 is the current VMCS. */
6289 if (is_guest_mode(vcpu)) {
6290 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true;
6291 return;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07006292 }
Sean Christopherson1196cb92020-03-20 14:28:23 -07006293
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006294 if (!(secondary_exec_controls_get(to_vmx(vcpu)) &
6295 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
6296 return;
6297
Sean Christophersona4148b72020-03-20 14:28:24 -07006298 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
6299 if (is_error_page(page))
6300 return;
6301
6302 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
Sean Christopherson1196cb92020-03-20 14:28:23 -07006303 vmx_flush_tlb_current(vcpu);
Sean Christophersona4148b72020-03-20 14:28:24 -07006304
6305 /*
6306 * Do not pin apic access page in memory, the MMU notifier
6307 * will call us again if it is migrated or swapped out.
6308 */
6309 put_page(page);
Tang Chen38b99172014-09-24 15:57:54 +08006310}
6311
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006312static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006313{
6314 u16 status;
6315 u8 old;
6316
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006317 if (max_isr == -1)
6318 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006319
6320 status = vmcs_read16(GUEST_INTR_STATUS);
6321 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006322 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006323 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006324 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006325 vmcs_write16(GUEST_INTR_STATUS, status);
6326 }
6327}
6328
6329static void vmx_set_rvi(int vector)
6330{
6331 u16 status;
6332 u8 old;
6333
Wei Wang4114c272014-11-05 10:53:43 +08006334 if (vector == -1)
6335 vector = 0;
6336
Yang Zhangc7c9c562013-01-25 10:18:51 +08006337 status = vmcs_read16(GUEST_INTR_STATUS);
6338 old = (u8)status & 0xff;
6339 if ((u8)vector != old) {
6340 status &= ~0xff;
6341 status |= (u8)vector;
6342 vmcs_write16(GUEST_INTR_STATUS, status);
6343 }
6344}
6345
6346static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6347{
Liran Alon851c1a182017-12-24 18:12:56 +02006348 /*
6349 * When running L2, updating RVI is only relevant when
6350 * vmcs12 virtual-interrupt-delivery enabled.
6351 * However, it can be enabled only when L1 also
6352 * intercepts external-interrupts and in that case
6353 * we should not update vmcs02 RVI but instead intercept
6354 * interrupt. Therefore, do nothing when running L2.
6355 */
6356 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08006357 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006358}
6359
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006360static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006361{
6362 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006363 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02006364 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006365
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006366 WARN_ON(!vcpu->arch.apicv_active);
6367 if (pi_test_on(&vmx->pi_desc)) {
6368 pi_clear_on(&vmx->pi_desc);
6369 /*
Liran Alond9ff2742019-11-11 14:25:25 +02006370 * IOMMU can write to PID.ON, so the barrier matters even on UP.
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006371 * But on x86 this is just a compiler barrier anyway.
6372 */
6373 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02006374 max_irr_updated =
6375 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6376
6377 /*
6378 * If we are running L2 and L1 has a new pending interrupt
6379 * which can be injected, we should re-evaluate
6380 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02006381 * If L1 intercepts external-interrupts, we should
6382 * exit from L2 to L1. Otherwise, interrupt should be
6383 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02006384 */
Liran Alon851c1a182017-12-24 18:12:56 +02006385 if (is_guest_mode(vcpu) && max_irr_updated) {
6386 if (nested_exit_on_intr(vcpu))
6387 kvm_vcpu_exiting_guest_mode(vcpu);
6388 else
6389 kvm_make_request(KVM_REQ_EVENT, vcpu);
6390 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006391 } else {
6392 max_irr = kvm_lapic_find_highest_irr(vcpu);
6393 }
6394 vmx_hwapic_irr_update(vcpu, max_irr);
6395 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006396}
6397
Andrey Smetanin63086302015-11-10 15:36:32 +03006398static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006399{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006400 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08006401 return;
6402
Yang Zhangc7c9c562013-01-25 10:18:51 +08006403 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6404 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6405 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6406 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6407}
6408
Paolo Bonzini967235d2016-12-19 14:03:45 +01006409static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
6410{
6411 struct vcpu_vmx *vmx = to_vmx(vcpu);
6412
6413 pi_clear_on(&vmx->pi_desc);
6414 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
6415}
6416
Sean Christopherson535f7ef2020-09-15 12:15:04 -07006417void vmx_do_interrupt_nmi_irqoff(unsigned long entry);
6418
Lai Jiangshana217a652021-05-04 21:50:14 +02006419static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu,
6420 unsigned long entry)
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006421{
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006422 kvm_before_interrupt(vcpu);
Lai Jiangshana217a652021-05-04 21:50:14 +02006423 vmx_do_interrupt_nmi_irqoff(entry);
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006424 kvm_after_interrupt(vcpu);
6425}
6426
Sean Christopherson95b5a482019-04-19 22:50:59 -07006427static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006428{
Lai Jiangshana217a652021-05-04 21:50:14 +02006429 const unsigned long nmi_entry = (unsigned long)asm_exc_nmi_noist;
Sean Christopherson87915852020-04-15 13:34:54 -07006430 u32 intr_info = vmx_get_intr_info(&vmx->vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006431
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006432 /* if exit due to PF check for async PF */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006433 if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02006434 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags();
Andi Kleena0861c02009-06-08 17:37:09 +08006435 /* Handle machine checks before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006436 else if (is_machine_check(intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08006437 kvm_machine_check();
Gleb Natapov20f65982009-05-11 13:35:55 +03006438 /* We need to handle NMIs before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006439 else if (is_nmi(intr_info))
Lai Jiangshana217a652021-05-04 21:50:14 +02006440 handle_interrupt_nmi_irqoff(&vmx->vcpu, nmi_entry);
Avi Kivity51aa01d2010-07-20 14:31:20 +03006441}
Gleb Natapov20f65982009-05-11 13:35:55 +03006442
Sean Christopherson95b5a482019-04-19 22:50:59 -07006443static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006444{
Sean Christopherson87915852020-04-15 13:34:54 -07006445 u32 intr_info = vmx_get_intr_info(vcpu);
Lai Jiangshana217a652021-05-04 21:50:14 +02006446 unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK;
6447 gate_desc *desc = (gate_desc *)host_idt_base + vector;
Yang Zhanga547c6d2013-04-11 19:25:10 +08006448
Sean Christopherson49def502019-04-19 22:50:56 -07006449 if (WARN_ONCE(!is_external_intr(intr_info),
6450 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
6451 return;
6452
Lai Jiangshana217a652021-05-04 21:50:14 +02006453 handle_interrupt_nmi_irqoff(vcpu, gate_offset(desc));
Yang Zhanga547c6d2013-04-11 19:25:10 +08006454}
Sean Christopherson95b5a482019-04-19 22:50:59 -07006455
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006456static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006457{
6458 struct vcpu_vmx *vmx = to_vmx(vcpu);
6459
Sean Christopherson8e533242020-11-06 17:03:12 +08006460 if (vmx->exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006461 handle_external_interrupt_irqoff(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006462 else if (vmx->exit_reason.basic == EXIT_REASON_EXCEPTION_NMI)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006463 handle_exception_nmi_irqoff(vmx);
6464}
Yang Zhanga547c6d2013-04-11 19:25:10 +08006465
Tom Lendacky57194552020-12-10 11:10:00 -06006466/*
6467 * The kvm parameter can be NULL (module initialization, or invocation before
6468 * VM creation). Be sure to check the kvm parameter before using it.
6469 */
6470static bool vmx_has_emulated_msr(struct kvm *kvm, u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006471{
Tom Lendackybc226f02018-05-10 22:06:39 +02006472 switch (index) {
6473 case MSR_IA32_SMBASE:
6474 /*
6475 * We cannot do SMM unless we can run the guest in big
6476 * real mode.
6477 */
6478 return enable_unrestricted_guest || emulate_invalid_guest_state;
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02006479 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
6480 return nested;
Tom Lendackybc226f02018-05-10 22:06:39 +02006481 case MSR_AMD64_VIRT_SPEC_CTRL:
6482 /* This is AMD only. */
6483 return false;
6484 default:
6485 return true;
6486 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006487}
6488
Avi Kivity51aa01d2010-07-20 14:31:20 +03006489static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6490{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006491 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006492 bool unblock_nmi;
6493 u8 vector;
6494 bool idtv_info_valid;
6495
6496 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006497
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006498 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006499 if (vmx->loaded_vmcs->nmi_known_unmasked)
6500 return;
Sean Christopherson87915852020-04-15 13:34:54 -07006501
6502 exit_intr_info = vmx_get_intr_info(&vmx->vcpu);
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006503 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6504 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6505 /*
6506 * SDM 3: 27.7.1.2 (September 2008)
6507 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6508 * a guest IRET fault.
6509 * SDM 3: 23.2.2 (September 2008)
6510 * Bit 12 is undefined in any of the following cases:
6511 * If the VM exit sets the valid bit in the IDT-vectoring
6512 * information field.
6513 * If the VM exit is due to a double fault.
6514 */
6515 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6516 vector != DF_VECTOR && !idtv_info_valid)
6517 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6518 GUEST_INTR_STATE_NMI);
6519 else
6520 vmx->loaded_vmcs->nmi_known_unmasked =
6521 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6522 & GUEST_INTR_STATE_NMI);
6523 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
6524 vmx->loaded_vmcs->vnmi_blocked_time +=
6525 ktime_to_ns(ktime_sub(ktime_get(),
6526 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006527}
6528
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006529static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006530 u32 idt_vectoring_info,
6531 int instr_len_field,
6532 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006533{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006534 u8 vector;
6535 int type;
6536 bool idtv_info_valid;
6537
6538 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006539
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006540 vcpu->arch.nmi_injected = false;
6541 kvm_clear_exception_queue(vcpu);
6542 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006543
6544 if (!idtv_info_valid)
6545 return;
6546
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006547 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006548
Avi Kivity668f6122008-07-02 09:28:55 +03006549 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6550 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006551
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006552 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006553 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006554 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006555 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006556 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006557 * Clear bit "block by NMI" before VM entry if a NMI
6558 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006559 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006560 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006561 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006562 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006563 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006564 fallthrough;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006565 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006566 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006567 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03006568 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006569 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03006570 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006571 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006572 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006573 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006574 fallthrough;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006575 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006576 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006577 break;
6578 default:
6579 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006580 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006581}
6582
Avi Kivity83422e12010-07-20 14:43:23 +03006583static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6584{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006585 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006586 VM_EXIT_INSTRUCTION_LEN,
6587 IDT_VECTORING_ERROR_CODE);
6588}
6589
Avi Kivityb463a6f2010-07-20 15:06:17 +03006590static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6591{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006592 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006593 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6594 VM_ENTRY_INSTRUCTION_LEN,
6595 VM_ENTRY_EXCEPTION_ERROR_CODE);
6596
6597 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6598}
6599
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006600static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6601{
6602 int i, nr_msrs;
6603 struct perf_guest_switch_msr *msrs;
6604
Sean Christophersonc8e2fe12021-03-09 09:10:19 -08006605 /* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006606 msrs = perf_guest_get_msrs(&nr_msrs);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006607 if (!msrs)
6608 return;
6609
6610 for (i = 0; i < nr_msrs; i++)
6611 if (msrs[i].host == msrs[i].guest)
6612 clear_atomic_switch_msr(vmx, msrs[i].msr);
6613 else
6614 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006615 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006616}
6617
Sean Christophersonf459a702018-08-27 15:21:11 -07006618static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006619{
6620 struct vcpu_vmx *vmx = to_vmx(vcpu);
6621 u64 tscl;
6622 u32 delta_tsc;
6623
Sean Christophersond264ee02018-08-27 15:21:12 -07006624 if (vmx->req_immediate_exit) {
Sean Christopherson804939e2019-05-07 12:18:05 -07006625 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
6626 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6627 } else if (vmx->hv_deadline_tsc != -1) {
Sean Christophersonf459a702018-08-27 15:21:11 -07006628 tscl = rdtsc();
6629 if (vmx->hv_deadline_tsc > tscl)
6630 /* set_hv_timer ensures the delta fits in 32-bits */
6631 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6632 cpu_preemption_timer_multi);
6633 else
6634 delta_tsc = 0;
6635
Sean Christopherson804939e2019-05-07 12:18:05 -07006636 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
6637 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6638 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
6639 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
6640 vmx->loaded_vmcs->hv_timer_soft_disabled = true;
Sean Christophersonf459a702018-08-27 15:21:11 -07006641 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006642}
6643
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006644void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006645{
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006646 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
6647 vmx->loaded_vmcs->host_state.rsp = host_rsp;
6648 vmcs_writel(HOST_RSP, host_rsp);
6649 }
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006650}
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006651
Wanpeng Li404d5d72020-04-28 14:23:25 +08006652static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006653{
Sean Christopherson8e533242020-11-06 17:03:12 +08006654 switch (to_vmx(vcpu)->exit_reason.basic) {
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006655 case EXIT_REASON_MSR_WRITE:
6656 return handle_fastpath_set_msr_irqoff(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04006657 case EXIT_REASON_PREEMPTION_TIMER:
6658 return handle_fastpath_preemption_timer(vcpu);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006659 default:
6660 return EXIT_FASTPATH_NONE;
6661 }
6662}
6663
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006664static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
6665 struct vcpu_vmx *vmx)
6666{
Sean Christophersonbc908e02021-05-04 17:27:35 -07006667 kvm_guest_enter_irqoff();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006668
6669 /* L1D Flush includes CPU buffer clear to mitigate MDS */
6670 if (static_branch_unlikely(&vmx_l1d_should_flush))
6671 vmx_l1d_flush(vcpu);
6672 else if (static_branch_unlikely(&mds_user_clear))
6673 mds_clear_cpu_buffers();
6674
Thomas Gleixner2245d392020-07-08 21:52:00 +02006675 if (vcpu->arch.cr2 != native_read_cr2())
6676 native_write_cr2(vcpu->arch.cr2);
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006677
6678 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
6679 vmx->loaded_vmcs->launched);
6680
Thomas Gleixner2245d392020-07-08 21:52:00 +02006681 vcpu->arch.cr2 = native_read_cr2();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006682
Sean Christophersonbc908e02021-05-04 17:27:35 -07006683 kvm_guest_exit_irqoff();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006684}
6685
Wanpeng Li404d5d72020-04-28 14:23:25 +08006686static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006687{
6688 struct vcpu_vmx *vmx = to_vmx(vcpu);
6689 unsigned long cr3, cr4;
6690
6691 /* Record the guest's net vcpu time for enforced NMI injections. */
6692 if (unlikely(!enable_vnmi &&
6693 vmx->loaded_vmcs->soft_vnmi_blocked))
6694 vmx->loaded_vmcs->entry_time = ktime_get();
6695
6696 /* Don't enter VMX if guest state is invalid, let the exit handler
6697 start emulation until we arrive back to a valid state */
6698 if (vmx->emulation_required)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006699 return EXIT_FASTPATH_NONE;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006700
Lorenzo Bresciad95df952020-12-23 14:45:07 +00006701 trace_kvm_entry(vcpu);
6702
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006703 if (vmx->ple_window_dirty) {
6704 vmx->ple_window_dirty = false;
6705 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6706 }
6707
wanpeng lic9dfd3f2020-02-17 18:37:43 +08006708 /*
6709 * We did this in prepare_switch_to_guest, because it needs to
6710 * be within srcu_read_lock.
6711 */
6712 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006713
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006714 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006715 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006716 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006717 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6718
6719 cr3 = __get_current_cr3_fast();
6720 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6721 vmcs_writel(HOST_CR3, cr3);
6722 vmx->loaded_vmcs->host_state.cr3 = cr3;
6723 }
6724
6725 cr4 = cr4_read_shadow();
6726 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
6727 vmcs_writel(HOST_CR4, cr4);
6728 vmx->loaded_vmcs->host_state.cr4 = cr4;
6729 }
6730
6731 /* When single-stepping over STI and MOV SS, we must clear the
6732 * corresponding interruptibility bits in the guest state. Otherwise
6733 * vmentry fails as it then expects bit 14 (BS) in pending debug
6734 * exceptions being set, but that's not correct for the guest debugging
6735 * case. */
6736 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6737 vmx_set_interrupt_shadow(vcpu, 0);
6738
Aaron Lewis139a12c2019-10-21 16:30:25 -07006739 kvm_load_guest_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006740
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006741 pt_guest_enter(vmx);
6742
Vitaly Kuznetsov49097762020-06-19 11:40:46 +02006743 atomic_switch_perf_msrs(vmx);
Like Xu1b5ac3222021-02-01 13:10:34 +08006744 if (intel_pmu_lbr_is_enabled(vcpu))
6745 vmx_passthrough_lbr_msrs(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006746
Sean Christopherson804939e2019-05-07 12:18:05 -07006747 if (enable_preemption_timer)
6748 vmx_update_hv_timer(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006749
Wanpeng Li010fd372020-09-10 17:50:41 +08006750 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08006751
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006752 /*
6753 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6754 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6755 * is no need to worry about the conditional branch over the wrmsr
6756 * being speculatively taken.
6757 */
6758 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
6759
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006760 /* The actual VMENTER/EXIT is in the .noinstr.text section. */
6761 vmx_vcpu_enter_exit(vcpu, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006762
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006763 /*
6764 * We do not use IBRS in the kernel. If this vCPU has used the
6765 * SPEC_CTRL MSR it may have left it on; save the value and
6766 * turn it off. This is much more efficient than blindly adding
6767 * it to the atomic save/restore list. Especially as the former
6768 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6769 *
6770 * For non-nested case:
6771 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6772 * save it.
6773 *
6774 * For nested case:
6775 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6776 * save it.
6777 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006778 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006779 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006780
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006781 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006782
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006783 /* All fields are clean at this point */
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01006784 if (static_branch_unlikely(&enable_evmcs)) {
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006785 current_evmcs->hv_clean_fields |=
6786 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6787
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +01006788 current_evmcs->hv_vp_id = kvm_hv_get_vpindex(vcpu);
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01006789 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08006790
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006791 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006792 if (vmx->host_debugctlmsr)
6793 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006794
Avi Kivityaa67f602012-08-01 16:48:03 +03006795#ifndef CONFIG_X86_64
6796 /*
6797 * The sysexit path does not restore ds/es, so we must set them to
6798 * a reasonable value ourselves.
6799 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006800 * We can't defer this to vmx_prepare_switch_to_host() since that
6801 * function may be executed in interrupt context, which saves and
6802 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006803 */
6804 loadsegment(ds, __USER_DS);
6805 loadsegment(es, __USER_DS);
6806#endif
6807
Sean Christophersone5d03de2020-04-15 13:34:51 -07006808 vmx_register_cache_reset(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006809
Chao Peng2ef444f2018-10-24 16:05:12 +08006810 pt_guest_exit(vmx);
6811
Aaron Lewis139a12c2019-10-21 16:30:25 -07006812 kvm_load_host_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006813
Gleb Natapove0b890d2013-09-25 12:51:33 +03006814 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006815 vmx->idt_vectoring_info = 0;
6816
Sean Christopherson873e1da2020-04-10 10:47:02 -07006817 if (unlikely(vmx->fail)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08006818 vmx->exit_reason.full = 0xdead;
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006819 return EXIT_FASTPATH_NONE;
Sean Christopherson873e1da2020-04-10 10:47:02 -07006820 }
6821
Sean Christopherson8e533242020-11-06 17:03:12 +08006822 vmx->exit_reason.full = vmcs_read32(VM_EXIT_REASON);
6823 if (unlikely((u16)vmx->exit_reason.basic == EXIT_REASON_MCE_DURING_VMENTRY))
Sean Christophersonbeb8d932019-04-19 22:50:55 -07006824 kvm_machine_check();
6825
Maxim Levitskyf5c59b52021-02-17 16:57:12 +02006826 if (likely(!vmx->exit_reason.failed_vmentry))
6827 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
6828
Sean Christopherson8e533242020-11-06 17:03:12 +08006829 trace_kvm_exit(vmx->exit_reason.full, vcpu, KVM_ISA_VMX);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006830
Sean Christopherson8e533242020-11-06 17:03:12 +08006831 if (unlikely(vmx->exit_reason.failed_vmentry))
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006832 return EXIT_FASTPATH_NONE;
6833
Jim Mattsonb060ca32017-09-14 16:31:42 -07006834 vmx->loaded_vmcs->launched = 1;
Gleb Natapove0b890d2013-09-25 12:51:33 +03006835
Avi Kivity51aa01d2010-07-20 14:31:20 +03006836 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006837 vmx_complete_interrupts(vmx);
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006838
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006839 if (is_guest_mode(vcpu))
6840 return EXIT_FASTPATH_NONE;
6841
Paolo Bonzinid89d04a2021-02-02 10:44:23 -05006842 return vmx_exit_handlers_fastpath(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006843}
6844
Avi Kivity6aa8b732006-12-10 02:21:36 -08006845static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6846{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006847 struct vcpu_vmx *vmx = to_vmx(vcpu);
6848
Kai Huang843e4332015-01-28 10:54:28 +08006849 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006850 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006851 free_vpid(vmx->vpid);
Sean Christopherson55d23752018-12-03 13:53:18 -08006852 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006853 free_loaded_vmcs(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006854}
6855
Sean Christopherson987b2592019-12-18 13:54:55 -08006856static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006857{
Ben Gardon41836832019-02-11 11:02:52 -08006858 struct vcpu_vmx *vmx;
Sean Christopherson34109c02019-12-18 13:54:50 -08006859 int i, cpu, err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006860
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006861 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0);
6862 vmx = to_vmx(vcpu);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006863
Peter Feiner4e595162016-07-07 14:49:58 -07006864 err = -ENOMEM;
6865
Sean Christopherson034d8e22019-12-18 13:54:49 -08006866 vmx->vpid = allocate_vpid();
6867
Peter Feiner4e595162016-07-07 14:49:58 -07006868 /*
6869 * If PML is turned on, failure on enabling PML just results in failure
6870 * of creating the vcpu, therefore we can simplify PML logic (by
6871 * avoiding dealing with cases, such as enabling PML partially on vcpus
Miaohe Lin67b0ae42019-12-11 14:26:22 +08006872 * for the guest), etc.
Peter Feiner4e595162016-07-07 14:49:58 -07006873 */
6874 if (enable_pml) {
Ben Gardon41836832019-02-11 11:02:52 -08006875 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Peter Feiner4e595162016-07-07 14:49:58 -07006876 if (!vmx->pml_pg)
Sean Christopherson987b2592019-12-18 13:54:55 -08006877 goto free_vpid;
Peter Feiner4e595162016-07-07 14:49:58 -07006878 }
6879
Sean Christopherson14a61b62020-09-23 11:04:08 -07006880 BUILD_BUG_ON(ARRAY_SIZE(vmx_uret_msrs_list) != MAX_NR_USER_RETURN_MSRS);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006881
Sean Christopherson14a61b62020-09-23 11:04:08 -07006882 for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i) {
6883 u32 index = vmx_uret_msrs_list[i];
Xiaoyao Li4be53412019-10-20 17:11:00 +08006884 u32 data_low, data_high;
Sean Christophersonfbc18002020-09-23 11:03:59 -07006885 int j = vmx->nr_uret_msrs;
Xiaoyao Li4be53412019-10-20 17:11:00 +08006886
6887 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6888 continue;
6889 if (wrmsr_safe(index, data_low, data_high) < 0)
6890 continue;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006891
Sean Christopherson802145c2020-09-23 11:04:09 -07006892 vmx->guest_uret_msrs[j].slot = i;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006893 vmx->guest_uret_msrs[j].data = 0;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006894 switch (index) {
6895 case MSR_IA32_TSX_CTRL:
6896 /*
Paolo Bonzini71316362021-01-28 11:45:00 -05006897 * TSX_CTRL_CPUID_CLEAR is handled in the CPUID
6898 * interception. Keep the host value unchanged to avoid
6899 * changing CPUID bits under the host kernel's feet.
6900 *
6901 * hle=0, rtm=0, tsx_ctrl=1 can be found with some
6902 * combinations of new kernel and old userspace. If
6903 * those guests run on a tsx=off host, do allow guests
6904 * to use TSX_CTRL, but do not change the value on the
6905 * host so that TSX remains always disabled.
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006906 */
Paolo Bonzini71316362021-01-28 11:45:00 -05006907 if (boot_cpu_has(X86_FEATURE_RTM))
6908 vmx->guest_uret_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
6909 else
6910 vmx->guest_uret_msrs[j].mask = 0;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006911 break;
6912 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006913 vmx->guest_uret_msrs[j].mask = -1ull;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006914 break;
6915 }
Sean Christophersonfbc18002020-09-23 11:03:59 -07006916 ++vmx->nr_uret_msrs;
Xiaoyao Li4be53412019-10-20 17:11:00 +08006917 }
6918
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006919 err = alloc_loaded_vmcs(&vmx->vmcs01);
6920 if (err < 0)
Jim Mattson7d737102019-12-03 16:24:42 -08006921 goto free_pml;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006922
Alexander Graf3eb90012020-09-25 16:34:20 +02006923 /* The MSR bitmap starts with all ones */
6924 bitmap_fill(vmx->shadow_msr_intercept.read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
6925 bitmap_fill(vmx->shadow_msr_intercept.write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
6926
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006927 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_TSC, MSR_TYPE_R);
Sean Christophersondbdd0962021-04-21 19:38:31 -07006928#ifdef CONFIG_X86_64
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006929 vmx_disable_intercept_for_msr(vcpu, MSR_FS_BASE, MSR_TYPE_RW);
6930 vmx_disable_intercept_for_msr(vcpu, MSR_GS_BASE, MSR_TYPE_RW);
6931 vmx_disable_intercept_for_msr(vcpu, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
Sean Christophersondbdd0962021-04-21 19:38:31 -07006932#endif
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006933 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6934 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6935 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
Sean Christopherson987b2592019-12-18 13:54:55 -08006936 if (kvm_cstate_in_guest(vcpu->kvm)) {
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006937 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C1_RES, MSR_TYPE_R);
6938 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
6939 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
6940 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
Wanpeng Lib5170062019-05-21 14:06:53 +08006941 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006942 vmx->msr_bitmap_mode = 0;
6943
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006944 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03006945 cpu = get_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006946 vmx_vcpu_load(vcpu, cpu);
6947 vcpu->cpu = cpu;
Xiaoyao Li1b842922019-10-20 17:11:01 +08006948 init_vmcs(vmx);
Sean Christopherson34109c02019-12-18 13:54:50 -08006949 vmx_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006950 put_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006951 if (cpu_need_virtualize_apic_accesses(vcpu)) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006952 err = alloc_apic_access_page(vcpu->kvm);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006953 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006954 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02006955 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006956
Sean Christophersone90008d2018-03-05 12:04:37 -08006957 if (enable_ept && !enable_unrestricted_guest) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006958 err = init_rmode_identity_map(vcpu->kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08006959 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02006960 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006961 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006962
Roman Kagan63aff652018-07-19 21:59:07 +03006963 if (nested)
Chenyi Qiangb9757a42020-08-28 16:56:22 +08006964 memcpy(&vmx->nested.msrs, &vmcs_config.nested, sizeof(vmx->nested.msrs));
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006965 else
6966 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08006967
Sean Christopherson8f102442021-04-12 16:21:40 +12006968 vcpu_setup_sgx_lepubkeyhash(vcpu);
6969
Wincy Van705699a2015-02-03 23:58:17 +08006970 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006971 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006972
Paolo Bonzinibab0c312020-02-11 18:40:58 +01006973 vcpu->arch.microcode_version = 0x100000000ULL;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08006974 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006975
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02006976 /*
6977 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
6978 * or POSTED_INTR_WAKEUP_VECTOR.
6979 */
6980 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
6981 vmx->pi_desc.sn = 1;
6982
Sean Christophersonee366562021-03-05 10:31:21 -08006983#if IS_ENABLED(CONFIG_HYPERV)
Sean Christopherson978c8342021-03-05 10:31:23 -08006984 vmx->hv_root_ept = INVALID_PAGE;
Sean Christophersonee366562021-03-05 10:31:21 -08006985#endif
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006986 return 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006987
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006988free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006989 free_loaded_vmcs(vmx->loaded_vmcs);
Peter Feiner4e595162016-07-07 14:49:58 -07006990free_pml:
6991 vmx_destroy_pml_buffer(vmx);
Sean Christopherson987b2592019-12-18 13:54:55 -08006992free_vpid:
Wanpeng Li991e7a02015-09-16 17:30:05 +08006993 free_vpid(vmx->vpid);
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006994 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006995}
6996
Thomas Gleixner65fd4cb2019-02-19 11:10:49 +01006997#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"
6998#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 -04006999
Wanpeng Lib31c1142018-03-12 04:53:04 -07007000static int vmx_vm_init(struct kvm *kvm)
7001{
Sean Christophersonee366562021-03-05 10:31:21 -08007002#if IS_ENABLED(CONFIG_HYPERV)
Sean Christopherson978c8342021-03-05 10:31:23 -08007003 spin_lock_init(&to_kvm_vmx(kvm)->hv_root_ept_lock);
Sean Christophersonee366562021-03-05 10:31:21 -08007004#endif
Tianyu Lan877ad952018-07-19 08:40:23 +00007005
Wanpeng Lib31c1142018-03-12 04:53:04 -07007006 if (!ple_gap)
7007 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007008
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007009 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
7010 switch (l1tf_mitigation) {
7011 case L1TF_MITIGATION_OFF:
7012 case L1TF_MITIGATION_FLUSH_NOWARN:
7013 /* 'I explicitly don't care' is set */
7014 break;
7015 case L1TF_MITIGATION_FLUSH:
7016 case L1TF_MITIGATION_FLUSH_NOSMT:
7017 case L1TF_MITIGATION_FULL:
7018 /*
7019 * Warn upon starting the first VM in a potentially
7020 * insecure environment.
7021 */
Josh Poimboeufb2849092019-01-30 07:13:58 -06007022 if (sched_smt_active())
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007023 pr_warn_once(L1TF_MSG_SMT);
7024 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
7025 pr_warn_once(L1TF_MSG_L1D);
7026 break;
7027 case L1TF_MITIGATION_FULL_FORCE:
7028 /* Flush is enforced */
7029 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007030 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007031 }
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007032 kvm_apicv_init(kvm, enable_apicv);
Wanpeng Lib31c1142018-03-12 04:53:04 -07007033 return 0;
7034}
7035
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007036static int __init vmx_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03007037{
7038 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08007039 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007040
Sean Christophersonff10e222019-12-20 20:45:10 -08007041 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
7042 !this_cpu_has(X86_FEATURE_VMX)) {
7043 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id());
7044 return -EIO;
7045 }
7046
Sean Christopherson7caaa712018-12-03 13:53:01 -08007047 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007048 return -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007049 if (nested)
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01007050 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept);
Yang, Sheng002c7f72007-07-31 14:23:01 +03007051 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7052 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7053 smp_processor_id());
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007054 return -EIO;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007055 }
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007056 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007057}
7058
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007059static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007060{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007061 u8 cache;
7062 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007063
Chia-I Wu222f06e2020-02-13 13:30:34 -08007064 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in
7065 * memory aliases with conflicting memory types and sometimes MCEs.
7066 * We have to be careful as to what are honored and when.
7067 *
7068 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to
7069 * UC. The effective memory type is UC or WC depending on guest PAT.
7070 * This was historically the source of MCEs and we want to be
7071 * conservative.
7072 *
7073 * When there is no need to deal with noncoherent DMA (e.g., no VT-d
7074 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The
7075 * EPT memory type is set to WB. The effective memory type is forced
7076 * WB.
7077 *
7078 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The
7079 * EPT memory type is used to emulate guest CD/MTRR.
Sheng Yang522c68c2009-04-27 20:35:43 +08007080 */
Chia-I Wu222f06e2020-02-13 13:30:34 -08007081
Paolo Bonzini606decd2015-10-01 13:12:47 +02007082 if (is_mmio) {
7083 cache = MTRR_TYPE_UNCACHABLE;
7084 goto exit;
7085 }
7086
7087 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007088 ipat = VMX_EPT_IPAT_BIT;
7089 cache = MTRR_TYPE_WRBACK;
7090 goto exit;
7091 }
7092
7093 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
7094 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02007095 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08007096 cache = MTRR_TYPE_WRBACK;
7097 else
7098 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007099 goto exit;
7100 }
7101
Xiao Guangrongff536042015-06-15 16:55:22 +08007102 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007103
7104exit:
7105 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08007106}
7107
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007108static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007109{
7110 /*
7111 * These bits in the secondary execution controls field
7112 * are dynamic, the others are mostly based on the hypervisor
7113 * architecture and the guest's CPUID. Do not touch the
7114 * dynamic bits.
7115 */
7116 u32 mask =
7117 SECONDARY_EXEC_SHADOW_VMCS |
7118 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02007119 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7120 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007121
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007122 u32 new_ctl = vmx->secondary_exec_control;
7123 u32 cur_ctl = secondary_exec_controls_get(vmx);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007124
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007125 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007126}
7127
David Matlack8322ebb2016-11-29 18:14:09 -08007128/*
7129 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
7130 * (indicating "allowed-1") if they are supported in the guest's CPUID.
7131 */
7132static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
7133{
7134 struct vcpu_vmx *vmx = to_vmx(vcpu);
7135 struct kvm_cpuid_entry2 *entry;
7136
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007137 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
7138 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08007139
7140#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
7141 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007142 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08007143} while (0)
7144
7145 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007146 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME));
7147 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME));
7148 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC));
7149 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE));
7150 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE));
7151 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE));
7152 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE));
7153 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE));
7154 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR));
7155 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM));
7156 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX));
7157 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX));
7158 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID));
7159 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE));
David Matlack8322ebb2016-11-29 18:14:09 -08007160
7161 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007162 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE));
7163 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP));
7164 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP));
7165 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU));
7166 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP));
7167 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57));
David Matlack8322ebb2016-11-29 18:14:09 -08007168
7169#undef cr4_fixed1_update
7170}
7171
Liran Alon5f76f6f2018-09-14 03:25:52 +03007172static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
7173{
7174 struct vcpu_vmx *vmx = to_vmx(vcpu);
7175
7176 if (kvm_mpx_supported()) {
7177 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
7178
7179 if (mpx_enabled) {
7180 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7181 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7182 } else {
7183 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7184 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7185 }
7186 }
7187}
7188
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007189static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7190{
7191 struct vcpu_vmx *vmx = to_vmx(vcpu);
7192 struct kvm_cpuid_entry2 *best = NULL;
7193 int i;
7194
7195 for (i = 0; i < PT_CPUID_LEAVES; i++) {
7196 best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7197 if (!best)
7198 return;
7199 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7200 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7201 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7202 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7203 }
7204
7205 /* Get the number of configurable Address Ranges for filtering */
7206 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7207 PT_CAP_num_address_ranges);
7208
7209 /* Initialize and clear the no dependency bits */
7210 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7211 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7212
7213 /*
7214 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7215 * will inject an #GP
7216 */
7217 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7218 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7219
7220 /*
7221 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7222 * PSBFreq can be set
7223 */
7224 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7225 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7226 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7227
7228 /*
7229 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7230 * MTCFreq can be set
7231 */
7232 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7233 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7234 RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7235
7236 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7237 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7238 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7239 RTIT_CTL_PTW_EN);
7240
7241 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7242 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7243 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7244
7245 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7246 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7247 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7248
Ingo Molnard9f6e122021-03-18 15:28:01 +01007249 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabricEn can be set */
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007250 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7251 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7252
7253 /* unmask address range configure area */
7254 for (i = 0; i < vmx->pt_desc.addr_range; i++)
Gustavo A. R. Silvad14eff12018-12-26 14:40:59 -06007255 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007256}
7257
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007258static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08007259{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007260 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007261
Aaron Lewis72041602019-10-21 16:30:20 -07007262 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7263 vcpu->arch.xsaves_enabled = false;
7264
Paolo Bonzini80154d72017-08-24 13:55:35 +02007265 if (cpu_has_secondary_exec_ctrls()) {
7266 vmx_compute_secondary_exec_control(vmx);
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007267 vmcs_set_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007268 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007269
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007270 if (nested_vmx_allowed(vcpu))
7271 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007272 FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7273 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007274 else
7275 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007276 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7277 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX);
David Matlack8322ebb2016-11-29 18:14:09 -08007278
Liran Alon5f76f6f2018-09-14 03:25:52 +03007279 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08007280 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03007281 nested_vmx_entry_exit_ctls_update(vcpu);
7282 }
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007283
7284 if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7285 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7286 update_intel_pt_cfg(vcpu);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007287
7288 if (boot_cpu_has(X86_FEATURE_RTM)) {
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07007289 struct vmx_uret_msr *msr;
Sean Christophersond85a8032020-09-23 11:04:06 -07007290 msr = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007291 if (msr) {
7292 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
Sean Christopherson7bf662b2020-09-23 11:04:07 -07007293 vmx_set_guest_uret_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007294 }
7295 }
Sean Christophersona6337a32020-09-29 21:16:57 -07007296
Sean Christopherson2ed41aa2020-09-29 21:16:58 -07007297 set_cr4_guest_host_mask(vmx);
7298
Sean Christopherson72add912021-04-12 16:21:42 +12007299 vmx_write_encls_bitmap(vcpu, NULL);
7300 if (guest_cpuid_has(vcpu, X86_FEATURE_SGX))
7301 vmx->msr_ia32_feature_control_valid_bits |= FEAT_CTL_SGX_ENABLED;
7302 else
7303 vmx->msr_ia32_feature_control_valid_bits &= ~FEAT_CTL_SGX_ENABLED;
7304
7305 if (guest_cpuid_has(vcpu, X86_FEATURE_SGX_LC))
7306 vmx->msr_ia32_feature_control_valid_bits |=
7307 FEAT_CTL_SGX_LC_ENABLED;
7308 else
7309 vmx->msr_ia32_feature_control_valid_bits &=
7310 ~FEAT_CTL_SGX_LC_ENABLED;
7311
Sean Christophersona6337a32020-09-29 21:16:57 -07007312 /* Refresh #PF interception to account for MAXPHYADDR changes. */
Jason Baronb6a7cc32021-01-14 22:27:54 -05007313 vmx_update_exception_bitmap(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08007314}
7315
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007316static __init void vmx_set_cpu_caps(void)
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007317{
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007318 kvm_set_cpu_caps();
7319
7320 /* CPUID 0x1 */
7321 if (nested)
7322 kvm_cpu_cap_set(X86_FEATURE_VMX);
7323
7324 /* CPUID 0x7 */
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007325 if (kvm_mpx_supported())
7326 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX);
Sean Christophersone4203332021-02-11 16:34:10 -08007327 if (!cpu_has_vmx_invpcid())
7328 kvm_cpu_cap_clear(X86_FEATURE_INVPCID);
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007329 if (vmx_pt_mode_is_host_guest())
7330 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT);
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007331
Sean Christopherson72add912021-04-12 16:21:42 +12007332 if (!enable_sgx) {
7333 kvm_cpu_cap_clear(X86_FEATURE_SGX);
7334 kvm_cpu_cap_clear(X86_FEATURE_SGX_LC);
7335 kvm_cpu_cap_clear(X86_FEATURE_SGX1);
7336 kvm_cpu_cap_clear(X86_FEATURE_SGX2);
7337 }
7338
Sean Christopherson90d2f602020-03-02 15:56:47 -08007339 if (vmx_umip_emulated())
7340 kvm_cpu_cap_set(X86_FEATURE_UMIP);
7341
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007342 /* CPUID 0xD.1 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01007343 supported_xss = 0;
Sean Christophersonbecdad82020-09-23 09:50:45 -07007344 if (!cpu_has_vmx_xsaves())
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007345 kvm_cpu_cap_clear(X86_FEATURE_XSAVES);
7346
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007347 /* CPUID 0x80000001 */
7348 if (!cpu_has_vmx_rdtscp())
7349 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007350
Sean Christophersonbecdad82020-09-23 09:50:45 -07007351 if (cpu_has_vmx_waitpkg())
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007352 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007353}
7354
Sean Christophersond264ee02018-08-27 15:21:12 -07007355static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7356{
7357 to_vmx(vcpu)->req_immediate_exit = true;
7358}
7359
Oliver Upton35a57132020-02-04 15:26:31 -08007360static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
7361 struct x86_instruction_info *info)
7362{
7363 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7364 unsigned short port;
7365 bool intercept;
7366 int size;
7367
7368 if (info->intercept == x86_intercept_in ||
7369 info->intercept == x86_intercept_ins) {
7370 port = info->src_val;
7371 size = info->dst_bytes;
7372 } else {
7373 port = info->dst_val;
7374 size = info->src_bytes;
7375 }
7376
7377 /*
7378 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
7379 * VM-exits depend on the 'unconditional IO exiting' VM-execution
7380 * control.
7381 *
7382 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
7383 */
7384 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7385 intercept = nested_cpu_has(vmcs12,
7386 CPU_BASED_UNCOND_IO_EXITING);
7387 else
7388 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
7389
Oliver Upton86f7e902020-02-29 11:30:14 -08007390 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
Oliver Upton35a57132020-02-04 15:26:31 -08007391 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
7392}
7393
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007394static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7395 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007396 enum x86_intercept_stage stage,
7397 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007398{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007399 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007400
Oliver Upton35a57132020-02-04 15:26:31 -08007401 switch (info->intercept) {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007402 /*
7403 * RDPID causes #UD if disabled through secondary execution controls.
7404 * Because it is marked as EmulateOnUD, we need to intercept it here.
7405 */
Oliver Upton35a57132020-02-04 15:26:31 -08007406 case x86_intercept_rdtscp:
Sean Christopherson7f3603b2020-09-23 09:50:47 -07007407 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_RDTSCP)) {
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007408 exception->vector = UD_VECTOR;
7409 exception->error_code_valid = false;
Oliver Upton35a57132020-02-04 15:26:31 -08007410 return X86EMUL_PROPAGATE_FAULT;
7411 }
7412 break;
7413
7414 case x86_intercept_in:
7415 case x86_intercept_ins:
7416 case x86_intercept_out:
7417 case x86_intercept_outs:
7418 return vmx_check_intercept_io(vcpu, info);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007419
Oliver Upton86f7e902020-02-29 11:30:14 -08007420 case x86_intercept_lgdt:
7421 case x86_intercept_lidt:
7422 case x86_intercept_lldt:
7423 case x86_intercept_ltr:
7424 case x86_intercept_sgdt:
7425 case x86_intercept_sidt:
7426 case x86_intercept_sldt:
7427 case x86_intercept_str:
7428 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
7429 return X86EMUL_CONTINUE;
7430
7431 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
7432 break;
7433
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007434 /* TODO: check more intercepts... */
Oliver Upton35a57132020-02-04 15:26:31 -08007435 default:
7436 break;
7437 }
7438
Paolo Bonzini07721fe2020-02-04 15:26:29 -08007439 return X86EMUL_UNHANDLEABLE;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007440}
7441
Yunhong Jiang64672c92016-06-13 14:19:59 -07007442#ifdef CONFIG_X86_64
7443/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
7444static inline int u64_shl_div_u64(u64 a, unsigned int shift,
7445 u64 divisor, u64 *result)
7446{
7447 u64 low = a << shift, high = a >> (64 - shift);
7448
7449 /* To avoid the overflow on divq */
7450 if (high >= divisor)
7451 return 1;
7452
7453 /* Low hold the result, high hold rem which is discarded */
7454 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
7455 "rm" (divisor), "0" (low), "1" (high));
7456 *result = low;
7457
7458 return 0;
7459}
7460
Sean Christophersonf9927982019-04-16 13:32:46 -07007461static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
7462 bool *expired)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007463{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007464 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007465 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
Sean Christopherson39497d72019-04-17 10:15:32 -07007466 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007467
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007468 vmx = to_vmx(vcpu);
7469 tscl = rdtsc();
7470 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
7471 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Sean Christopherson39497d72019-04-17 10:15:32 -07007472 lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
7473 ktimer->timer_advance_ns);
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007474
7475 if (delta_tsc > lapic_timer_advance_cycles)
7476 delta_tsc -= lapic_timer_advance_cycles;
7477 else
7478 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007479
7480 /* Convert to host delta tsc if tsc scaling is enabled */
7481 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
Sean Christopherson0967fa12019-04-16 13:32:48 -07007482 delta_tsc && u64_shl_div_u64(delta_tsc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007483 kvm_tsc_scaling_ratio_frac_bits,
Sean Christopherson0967fa12019-04-16 13:32:48 -07007484 vcpu->arch.tsc_scaling_ratio, &delta_tsc))
Yunhong Jiang64672c92016-06-13 14:19:59 -07007485 return -ERANGE;
7486
7487 /*
7488 * If the delta tsc can't fit in the 32 bit after the multi shift,
7489 * we can't use the preemption timer.
7490 * It's possible that it fits on later vmentries, but checking
7491 * on every vmentry is costly so we just use an hrtimer.
7492 */
7493 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
7494 return -ERANGE;
7495
7496 vmx->hv_deadline_tsc = tscl + delta_tsc;
Sean Christophersonf9927982019-04-16 13:32:46 -07007497 *expired = !delta_tsc;
7498 return 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007499}
7500
7501static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
7502{
Sean Christophersonf459a702018-08-27 15:21:11 -07007503 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007504}
7505#endif
7506
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007507static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007508{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007509 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007510 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007511}
7512
Makarand Sonarea85863c2021-02-12 16:50:12 -08007513void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu)
7514{
7515 struct vcpu_vmx *vmx = to_vmx(vcpu);
7516
7517 if (is_guest_mode(vcpu)) {
7518 vmx->nested.update_vmcs01_cpu_dirty_logging = true;
7519 return;
7520 }
7521
7522 /*
7523 * Note, cpu_dirty_logging_count can be changed concurrent with this
7524 * code, but in that case another update request will be made and so
7525 * the guest will never run with a stale PML value.
7526 */
7527 if (vcpu->kvm->arch.cpu_dirty_logging_count)
7528 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_ENABLE_PML);
7529 else
7530 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_ENABLE_PML);
7531}
7532
Yunhong Jiangbc225122016-06-13 14:19:58 -07007533static int vmx_pre_block(struct kvm_vcpu *vcpu)
7534{
7535 if (pi_pre_block(vcpu))
7536 return 1;
7537
Yunhong Jiang64672c92016-06-13 14:19:59 -07007538 if (kvm_lapic_hv_timer_in_use(vcpu))
7539 kvm_lapic_switch_to_sw_timer(vcpu);
7540
Yunhong Jiangbc225122016-06-13 14:19:58 -07007541 return 0;
7542}
7543
Yunhong Jiangbc225122016-06-13 14:19:58 -07007544static void vmx_post_block(struct kvm_vcpu *vcpu)
7545{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007546 if (kvm_x86_ops.set_hv_timer)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007547 kvm_lapic_switch_to_hv_timer(vcpu);
7548
Yunhong Jiangbc225122016-06-13 14:19:58 -07007549 pi_post_block(vcpu);
7550}
7551
Ashok Rajc45dcc72016-06-22 14:59:56 +08007552static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7553{
7554 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7555 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007556 FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007557 else
7558 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007559 ~FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007560}
7561
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007562static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Ladi Prosek72d7b372017-10-11 16:54:41 +02007563{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007564 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7565 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007566 return -EBUSY;
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -04007567 return !is_smm(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02007568}
7569
Ladi Prosek0234bf82017-10-11 16:54:40 +02007570static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7571{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007572 struct vcpu_vmx *vmx = to_vmx(vcpu);
7573
7574 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7575 if (vmx->nested.smm.guest_mode)
7576 nested_vmx_vmexit(vcpu, -1, 0, 0);
7577
7578 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7579 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007580 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007581 return 0;
7582}
7583
Sean Christophersoned193212019-04-02 08:03:09 -07007584static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007585{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007586 struct vcpu_vmx *vmx = to_vmx(vcpu);
7587 int ret;
7588
7589 if (vmx->nested.smm.vmxon) {
7590 vmx->nested.vmxon = true;
7591 vmx->nested.smm.vmxon = false;
7592 }
7593
7594 if (vmx->nested.smm.guest_mode) {
Sean Christophersona633e412018-09-26 09:23:47 -07007595 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007596 if (ret)
7597 return ret;
7598
7599 vmx->nested.smm.guest_mode = false;
7600 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007601 return 0;
7602}
7603
Jason Baronb6a7cc32021-01-14 22:27:54 -05007604static void vmx_enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007605{
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007606 /* RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007607}
7608
Liran Alon4b9852f2019-08-26 13:24:49 +03007609static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7610{
Paolo Bonzini1c96dcc2020-11-05 11:20:49 -05007611 return to_vmx(vcpu)->nested.vmxon && !is_guest_mode(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03007612}
7613
Jim Mattson93dff2f2020-05-08 13:36:43 -07007614static void vmx_migrate_timers(struct kvm_vcpu *vcpu)
7615{
7616 if (is_guest_mode(vcpu)) {
7617 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer;
7618
7619 if (hrtimer_try_to_cancel(timer) == 1)
7620 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
7621 }
7622}
7623
Sean Christopherson6e4fd062020-03-21 13:26:01 -07007624static void hardware_unsetup(void)
Sean Christophersona3203382018-12-03 13:53:11 -08007625{
7626 if (nested)
7627 nested_vmx_hardware_unsetup();
7628
7629 free_kvm_area();
7630}
7631
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007632static bool vmx_check_apicv_inhibit_reasons(ulong bit)
7633{
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -06007634 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
7635 BIT(APICV_INHIBIT_REASON_HYPERV);
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007636
7637 return supported & BIT(bit);
7638}
7639
Sean Christophersone286ac02020-03-21 13:26:02 -07007640static struct kvm_x86_ops vmx_x86_ops __initdata = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007641 .hardware_unsetup = hardware_unsetup,
Sean Christopherson484014f2020-03-21 13:25:57 -07007642
Avi Kivity6aa8b732006-12-10 02:21:36 -08007643 .hardware_enable = hardware_enable,
7644 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007645 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007646 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007647
Sean Christopherson484014f2020-03-21 13:25:57 -07007648 .vm_size = sizeof(struct kvm_vmx),
Wanpeng Lib31c1142018-03-12 04:53:04 -07007649 .vm_init = vmx_vm_init,
7650
Avi Kivity6aa8b732006-12-10 02:21:36 -08007651 .vcpu_create = vmx_create_vcpu,
7652 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007653 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007654
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007655 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007656 .vcpu_load = vmx_vcpu_load,
7657 .vcpu_put = vmx_vcpu_put,
7658
Jason Baronb6a7cc32021-01-14 22:27:54 -05007659 .update_exception_bitmap = vmx_update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007660 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007661 .get_msr = vmx_get_msr,
7662 .set_msr = vmx_set_msr,
7663 .get_segment_base = vmx_get_segment_base,
7664 .get_segment = vmx_get_segment,
7665 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007666 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007667 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
7668 .set_cr0 = vmx_set_cr0,
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07007669 .is_valid_cr4 = vmx_is_valid_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007670 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007671 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007672 .get_idt = vmx_get_idt,
7673 .set_idt = vmx_set_idt,
7674 .get_gdt = vmx_get_gdt,
7675 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007676 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007677 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007678 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007679 .get_rflags = vmx_get_rflags,
7680 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007681
Sean Christopherson77809382020-03-20 14:28:18 -07007682 .tlb_flush_all = vmx_flush_tlb_all,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07007683 .tlb_flush_current = vmx_flush_tlb_current,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007684 .tlb_flush_gva = vmx_flush_tlb_gva,
Sean Christophersone64419d2020-03-20 14:28:10 -07007685 .tlb_flush_guest = vmx_flush_tlb_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007686
Avi Kivity6aa8b732006-12-10 02:21:36 -08007687 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007688 .handle_exit = vmx_handle_exit,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08007689 .skip_emulated_instruction = vmx_skip_emulated_instruction,
7690 .update_emulated_instruction = vmx_update_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007691 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7692 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007693 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007694 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007695 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007696 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007697 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007698 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007699 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007700 .get_nmi_mask = vmx_get_nmi_mask,
7701 .set_nmi_mask = vmx_set_nmi_mask,
Jason Baronb6a7cc32021-01-14 22:27:54 -05007702 .enable_nmi_window = vmx_enable_nmi_window,
7703 .enable_irq_window = vmx_enable_irq_window,
7704 .update_cr8_intercept = vmx_update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007705 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007706 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007707 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007708 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007709 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007710 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007711 .hwapic_irr_update = vmx_hwapic_irr_update,
7712 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007713 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007714 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7715 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007716 .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007717
Izik Eiduscbc94022007-10-25 00:29:55 +02007718 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007719 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007720 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007721
Avi Kivity586f9602010-11-18 13:09:54 +02007722 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007723
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007724 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007725
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007726 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007727
Leonid Shatz326e7422018-11-06 12:14:25 +02007728 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007729
Sean Christopherson484014f2020-03-21 13:25:57 -07007730 .load_mmu_pgd = vmx_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007731
7732 .check_intercept = vmx_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007733 .handle_exit_irqoff = vmx_handle_exit_irqoff,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007734
Sean Christophersond264ee02018-08-27 15:21:12 -07007735 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007736
7737 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007738
Sean Christopherson6dd03802021-02-12 16:50:09 -08007739 .cpu_dirty_log_size = PML_ENTITY_NUM,
Makarand Sonarea85863c2021-02-12 16:50:12 -08007740 .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,
Wei Huang25462f72015-06-19 15:45:05 +02007741
Feng Wubf9f6ac2015-09-18 22:29:55 +08007742 .pre_block = vmx_pre_block,
7743 .post_block = vmx_post_block,
7744
Wei Huang25462f72015-06-19 15:45:05 +02007745 .pmu_ops = &intel_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04007746 .nested_ops = &vmx_nested_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007747
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007748 .update_pi_irte = pi_update_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007749
7750#ifdef CONFIG_X86_64
7751 .set_hv_timer = vmx_set_hv_timer,
7752 .cancel_hv_timer = vmx_cancel_hv_timer,
7753#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007754
7755 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007756
Ladi Prosek72d7b372017-10-11 16:54:41 +02007757 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007758 .pre_enter_smm = vmx_pre_enter_smm,
7759 .pre_leave_smm = vmx_pre_leave_smm,
Jason Baronb6a7cc32021-01-14 22:27:54 -05007760 .enable_smi_window = vmx_enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007761
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007762 .can_emulate_instruction = vmx_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03007763 .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
Jim Mattson93dff2f2020-05-08 13:36:43 -07007764 .migrate_timers = vmx_migrate_timers,
Alexander Graf3eb90012020-09-25 16:34:20 +02007765
7766 .msr_filter_changed = vmx_msr_filter_changed,
Paolo Bonzinif9a4d622020-12-14 10:26:51 -05007767 .complete_emulated_msr = kvm_complete_insn_gp,
Tom Lendacky647daca2021-01-04 14:20:01 -06007768
7769 .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007770};
7771
Avi Kivity6aa8b732006-12-10 02:21:36 -08007772static __init int hardware_setup(void)
7773{
7774 unsigned long host_bndcfgs;
7775 struct desc_ptr dt;
Sean Christopherson703c3352020-03-02 15:57:03 -08007776 int r, i, ept_lpage_level;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007777
Avi Kivity6aa8b732006-12-10 02:21:36 -08007778 store_idt(&dt);
7779 host_idt_base = dt.address;
7780
Sean Christopherson14a61b62020-09-23 11:04:08 -07007781 for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i)
7782 kvm_define_user_return_msr(i, vmx_uret_msrs_list[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007783
7784 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
7785 return -EIO;
7786
7787 if (boot_cpu_has(X86_FEATURE_NX))
7788 kvm_enable_efer_bits(EFER_NX);
7789
7790 if (boot_cpu_has(X86_FEATURE_MPX)) {
7791 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7792 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7793 }
7794
Sean Christopherson7f5581f2020-03-02 15:56:24 -08007795 if (!cpu_has_vmx_mpx())
Sean Christophersoncfc48182020-03-02 15:56:23 -08007796 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
7797 XFEATURE_MASK_BNDCSR);
7798
Avi Kivity6aa8b732006-12-10 02:21:36 -08007799 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7800 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7801 enable_vpid = 0;
7802
7803 if (!cpu_has_vmx_ept() ||
7804 !cpu_has_vmx_ept_4levels() ||
7805 !cpu_has_vmx_ept_mt_wb() ||
7806 !cpu_has_vmx_invept_global())
7807 enable_ept = 0;
7808
7809 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7810 enable_ept_ad_bits = 0;
7811
7812 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Avi Kivity873a7c42006-12-13 00:34:14 -08007813 enable_unrestricted_guest = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007814
7815 if (!cpu_has_vmx_flexpriority())
7816 flexpriority_enabled = 0;
7817
7818 if (!cpu_has_virtual_nmis())
7819 enable_vnmi = 0;
7820
7821 /*
7822 * set_apic_access_page_addr() is used to reload apic access
7823 * page upon invalidation. No need to do anything if not
7824 * using the APIC_ACCESS_ADDR VMCS field.
7825 */
7826 if (!flexpriority_enabled)
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007827 vmx_x86_ops.set_apic_access_page_addr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007828
7829 if (!cpu_has_vmx_tpr_shadow())
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007830 vmx_x86_ops.update_cr8_intercept = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007831
Avi Kivity6aa8b732006-12-10 02:21:36 -08007832#if IS_ENABLED(CONFIG_HYPERV)
7833 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7834 && enable_ept) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007835 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
7836 vmx_x86_ops.tlb_remote_flush_with_range =
Avi Kivity6aa8b732006-12-10 02:21:36 -08007837 hv_remote_flush_tlb_with_range;
7838 }
7839#endif
7840
7841 if (!cpu_has_vmx_ple()) {
7842 ple_gap = 0;
7843 ple_window = 0;
7844 ple_window_grow = 0;
7845 ple_window_max = 0;
7846 ple_window_shrink = 0;
7847 }
7848
7849 if (!cpu_has_vmx_apicv()) {
7850 enable_apicv = 0;
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007851 vmx_x86_ops.sync_pir_to_irr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007852 }
7853
7854 if (cpu_has_vmx_tsc_scaling()) {
7855 kvm_has_tsc_control = true;
7856 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7857 kvm_tsc_scaling_ratio_frac_bits = 48;
7858 }
7859
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08007860 kvm_has_bus_lock_exit = cpu_has_vmx_bus_lock_detection();
7861
Avi Kivity6aa8b732006-12-10 02:21:36 -08007862 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7863
7864 if (enable_ept)
Sean Christophersone7b7bde2021-02-25 12:47:42 -08007865 kvm_mmu_set_ept_masks(enable_ept_ad_bits,
7866 cpu_has_vmx_ept_execute_only());
Sean Christopherson703c3352020-03-02 15:57:03 -08007867
7868 if (!enable_ept)
7869 ept_lpage_level = 0;
7870 else if (cpu_has_vmx_ept_1g_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007871 ept_lpage_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08007872 else if (cpu_has_vmx_ept_2m_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007873 ept_lpage_level = PG_LEVEL_2M;
Sean Christopherson703c3352020-03-02 15:57:03 -08007874 else
Sean Christopherson3bae0452020-04-27 17:54:22 -07007875 ept_lpage_level = PG_LEVEL_4K;
Sean Christopherson83013052020-07-15 20:41:22 -07007876 kvm_configure_mmu(enable_ept, vmx_get_max_tdp_level(), ept_lpage_level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007877
7878 /*
7879 * Only enable PML when hardware supports PML feature, and both EPT
7880 * and EPT A/D bit features are enabled -- PML depends on them to work.
7881 */
7882 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7883 enable_pml = 0;
7884
Sean Christophersona018eba2021-02-12 16:50:10 -08007885 if (!enable_pml)
Sean Christopherson6dd03802021-02-12 16:50:09 -08007886 vmx_x86_ops.cpu_dirty_log_size = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007887
7888 if (!cpu_has_vmx_preemption_timer())
7889 enable_preemption_timer = false;
7890
7891 if (enable_preemption_timer) {
7892 u64 use_timer_freq = 5000ULL * 1000 * 1000;
7893 u64 vmx_msr;
7894
7895 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7896 cpu_preemption_timer_multi =
7897 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7898
7899 if (tsc_khz)
7900 use_timer_freq = (u64)tsc_khz * 1000;
7901 use_timer_freq >>= cpu_preemption_timer_multi;
7902
7903 /*
7904 * KVM "disables" the preemption timer by setting it to its max
7905 * value. Don't use the timer if it might cause spurious exits
7906 * at a rate faster than 0.1 Hz (of uninterrupted guest time).
7907 */
7908 if (use_timer_freq > 0xffffffffu / 10)
7909 enable_preemption_timer = false;
7910 }
7911
7912 if (!enable_preemption_timer) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007913 vmx_x86_ops.set_hv_timer = NULL;
7914 vmx_x86_ops.cancel_hv_timer = NULL;
7915 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007916 }
7917
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007918 kvm_set_posted_intr_wakeup_handler(pi_wakeup_handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007919
7920 kvm_mce_cap_supported |= MCG_LMCE_P;
7921
7922 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
7923 return -EINVAL;
7924 if (!enable_ept || !cpu_has_vmx_intel_pt())
7925 pt_mode = PT_MODE_SYSTEM;
7926
Sean Christopherson8f102442021-04-12 16:21:40 +12007927 setup_default_sgx_lepubkeyhash();
7928
Avi Kivity6aa8b732006-12-10 02:21:36 -08007929 if (nested) {
7930 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
7931 vmx_capability.ept);
7932
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07007933 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007934 if (r)
7935 return r;
7936 }
7937
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007938 vmx_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08007939
Avi Kivity6aa8b732006-12-10 02:21:36 -08007940 r = alloc_kvm_area();
7941 if (r)
7942 nested_vmx_hardware_unsetup();
7943 return r;
7944}
7945
Sean Christophersond008dfd2020-03-21 13:25:56 -07007946static struct kvm_x86_init_ops vmx_init_ops __initdata = {
7947 .cpu_has_kvm_support = cpu_has_kvm_support,
7948 .disabled_by_bios = vmx_disabled_by_bios,
7949 .check_processor_compatibility = vmx_check_processor_compat,
7950 .hardware_setup = hardware_setup,
7951
7952 .runtime_ops = &vmx_x86_ops,
7953};
7954
Avi Kivity6aa8b732006-12-10 02:21:36 -08007955static void vmx_cleanup_l1d_flush(void)
7956{
7957 if (vmx_l1d_flush_pages) {
7958 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
7959 vmx_l1d_flush_pages = NULL;
7960 }
7961 /* Restore state so sysfs ignores VMX */
7962 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
7963}
7964
7965static void vmx_exit(void)
7966{
7967#ifdef CONFIG_KEXEC_CORE
7968 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
7969 synchronize_rcu();
7970#endif
7971
7972 kvm_exit();
7973
7974#if IS_ENABLED(CONFIG_HYPERV)
7975 if (static_branch_unlikely(&enable_evmcs)) {
7976 int cpu;
7977 struct hv_vp_assist_page *vp_ap;
7978 /*
7979 * Reset everything to support using non-enlightened VMCS
7980 * access later (e.g. when we reload the module with
7981 * enlightened_vmcs=0)
7982 */
7983 for_each_online_cpu(cpu) {
7984 vp_ap = hv_get_vp_assist_page(cpu);
7985
7986 if (!vp_ap)
7987 continue;
7988
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08007989 vp_ap->nested_control.features.directhypercall = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007990 vp_ap->current_nested_vmcs = 0;
7991 vp_ap->enlighten_vmentry = 0;
7992 }
7993
7994 static_branch_disable(&enable_evmcs);
7995 }
7996#endif
7997 vmx_cleanup_l1d_flush();
7998}
7999module_exit(vmx_exit);
8000
8001static int __init vmx_init(void)
8002{
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008003 int r, cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008004
8005#if IS_ENABLED(CONFIG_HYPERV)
8006 /*
8007 * Enlightened VMCS usage should be recommended and the host needs
8008 * to support eVMCS v1 or above. We can also disable eVMCS support
8009 * with module parameter.
8010 */
8011 if (enlightened_vmcs &&
8012 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
8013 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
8014 KVM_EVMCS_VERSION) {
8015 int cpu;
8016
8017 /* Check that we have assist pages on all online CPUs */
8018 for_each_online_cpu(cpu) {
8019 if (!hv_get_vp_assist_page(cpu)) {
8020 enlightened_vmcs = false;
8021 break;
8022 }
8023 }
8024
8025 if (enlightened_vmcs) {
8026 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
8027 static_branch_enable(&enable_evmcs);
8028 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008029
8030 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
8031 vmx_x86_ops.enable_direct_tlbflush
8032 = hv_enable_direct_tlbflush;
8033
Avi Kivity6aa8b732006-12-10 02:21:36 -08008034 } else {
8035 enlightened_vmcs = false;
8036 }
8037#endif
8038
Sean Christophersond008dfd2020-03-21 13:25:56 -07008039 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008040 __alignof__(struct vcpu_vmx), THIS_MODULE);
8041 if (r)
8042 return r;
8043
8044 /*
8045 * Must be called after kvm_init() so enable_ept is properly set
8046 * up. Hand the parameter mitigation value in which was stored in
8047 * the pre module init parser. If no parameter was given, it will
8048 * contain 'auto' which will be turned into the default 'cond'
8049 * mitigation mode.
8050 */
Waiman Long19a36d32019-08-26 15:30:23 -04008051 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
8052 if (r) {
8053 vmx_exit();
8054 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008055 }
8056
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008057 for_each_possible_cpu(cpu) {
8058 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07008059
Paolo Bonzinia3ff25f2020-10-24 04:08:37 -04008060 pi_init_cpu(cpu);
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008061 }
8062
Avi Kivity6aa8b732006-12-10 02:21:36 -08008063#ifdef CONFIG_KEXEC_CORE
8064 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8065 crash_vmclear_local_loaded_vmcss);
8066#endif
8067 vmx_check_vmcs12_offsets();
8068
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008069 /*
Mohammed Gamalb96e6502020-09-03 16:11:22 +02008070 * Shadow paging doesn't have a (further) performance penalty
8071 * from GUEST_MAXPHYADDR < HOST_MAXPHYADDR so enable it
8072 * by default
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008073 */
Mohammed Gamalb96e6502020-09-03 16:11:22 +02008074 if (!enable_ept)
8075 allow_smaller_maxphyaddr = true;
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008076
Avi Kivity6aa8b732006-12-10 02:21:36 -08008077 return 0;
8078}
8079module_init(vmx_init);