blob: c5cbef2402b87d2ef242077a7ff882e252978ca7 [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>
39#include <asm/io.h>
Feng Wuefc64402015-09-18 22:29:51 +080040#include <asm/irq_remapping.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080041#include <asm/kexec.h>
42#include <asm/perf_event.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070043#include <asm/mmu_context.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010044#include <asm/mshyperv.h>
Benjamin Thielb10c3072020-01-23 18:29:45 +010045#include <asm/mwait.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080046#include <asm/spec-ctrl.h>
47#include <asm/virtext.h>
48#include <asm/vmx.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Sean Christopherson3077c192018-12-03 13:53:02 -080050#include "capabilities.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080051#include "cpuid.h"
Sean Christopherson4cebd742018-12-03 13:52:58 -080052#include "evmcs.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080053#include "irq.h"
54#include "kvm_cache_regs.h"
55#include "lapic.h"
56#include "mmu.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080057#include "nested.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080059#include "trace.h"
Sean Christophersoncb1d4742018-12-03 13:53:04 -080060#include "vmcs.h"
Sean Christopherson609363c2018-12-03 13:53:05 -080061#include "vmcs12.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080062#include "vmx.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080063#include "x86.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Valdis Klētnieks575b2552020-02-27 21:49:52 -050068#ifdef MODULE
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010070 X86_MATCH_FEATURE(X86_FEATURE_VMX, NULL),
Josh Triplette9bda3b2012-03-20 23:33:51 -070071 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050074#endif
Josh Triplette9bda3b2012-03-20 23:33:51 -070075
Sean Christopherson2c4fd912018-12-03 13:53:03 -080076bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020077module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080078
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010079static bool __read_mostly enable_vnmi = 1;
80module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
81
Sean Christopherson2c4fd912018-12-03 13:53:03 -080082bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020083module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020084
Sean Christopherson2c4fd912018-12-03 13:53:03 -080085bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020086module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080087
Sean Christopherson2c4fd912018-12-03 13:53:03 -080088bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070089module_param_named(unrestricted_guest,
90 enable_unrestricted_guest, bool, S_IRUGO);
91
Sean Christopherson2c4fd912018-12-03 13:53:03 -080092bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080093module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
94
Avi Kivitya27685c2012-06-12 20:30:18 +030095static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020096module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030097
Rusty Russell476bc002012-01-13 09:32:18 +103098static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030099module_param(fasteoi, bool, S_IRUGO);
100
Vitaly Kuznetsova4443262020-02-20 18:22:04 +0100101bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800102module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800103
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200109static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Sean Christopherson2c4fd912018-12-03 13:53:03 -0800112bool __read_mostly enable_pml = 1;
Kai Huang843e4332015-01-28 10:54:28 +0800113module_param_named(pml, enable_pml, bool, S_IRUGO);
114
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200115static bool __read_mostly dump_invalid_vmcs = 0;
116module_param(dump_invalid_vmcs, bool, 0644);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_BITMAP_MODE_X2APIC 1
119#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100120
Haozhong Zhang64903d62015-10-20 15:39:09 +0800121#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
122
Yunhong Jiang64672c92016-06-13 14:19:59 -0700123/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
124static int __read_mostly cpu_preemption_timer_multi;
125static bool __read_mostly enable_preemption_timer = 1;
126#ifdef CONFIG_X86_64
127module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
128#endif
129
Mohammed Gamalb96e6502020-09-03 16:11:22 +0200130extern bool __read_mostly allow_smaller_maxphyaddr;
131module_param(allow_smaller_maxphyaddr, bool, S_IRUGO);
132
Sean Christopherson3de63472018-07-13 08:42:30 -0700133#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800134#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
135#define KVM_VM_CR0_ALWAYS_ON \
136 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
137 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200138
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800139#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200140#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
141#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
142
Avi Kivity78ac8b42010-04-08 18:19:35 +0300143#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
144
Chao Pengbf8c55d2018-10-24 16:05:14 +0800145#define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \
146 RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \
147 RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \
148 RTIT_STATUS_BYTECNT))
149
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800150/*
Alexander Graf3eb90012020-09-25 16:34:20 +0200151 * List of MSRs that can be directly passed to the guest.
152 * In addition to these x2apic and PT MSRs are handled specially.
153 */
154static u32 vmx_possible_passthrough_msrs[MAX_POSSIBLE_PASSTHROUGH_MSRS] = {
155 MSR_IA32_SPEC_CTRL,
156 MSR_IA32_PRED_CMD,
157 MSR_IA32_TSC,
158 MSR_FS_BASE,
159 MSR_GS_BASE,
160 MSR_KERNEL_GS_BASE,
161 MSR_IA32_SYSENTER_CS,
162 MSR_IA32_SYSENTER_ESP,
163 MSR_IA32_SYSENTER_EIP,
164 MSR_CORE_C1_RES,
165 MSR_CORE_C3_RESIDENCY,
166 MSR_CORE_C6_RESIDENCY,
167 MSR_CORE_C7_RESIDENCY,
168};
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800169
170/*
171 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
172 * ple_gap: upper bound on the amount of time between two successive
173 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500174 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800175 * ple_window: upper bound on the amount of time a guest is allowed to execute
176 * in a PAUSE loop. Tests indicate that most spinlocks are held for
177 * less than 2^12 cycles
178 * Time is measured based on a counter that runs at the same rate as the TSC,
179 * refer SDM volume 3b section 21.6.13 & 22.1.3.
180 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400181static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500182module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200183
Babu Moger7fbc85a2018-03-16 16:37:22 -0400184static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
185module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800186
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200187/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400188static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400189module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200190
191/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400192static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400193module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200194
195/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400196static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
197module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200198
Chao Pengf99e3da2018-10-24 16:05:10 +0800199/* Default is SYSTEM mode, 1 for host-guest mode */
200int __read_mostly pt_mode = PT_MODE_SYSTEM;
201module_param(pt_mode, int, S_IRUGO);
202
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200203static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200204static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200205static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200206
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200207/* Storage for pre module init parameter parsing */
208static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200209
210static const struct {
211 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200212 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200213} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200214 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
215 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
216 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
217 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
218 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
219 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200220};
221
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200222#define L1D_CACHE_ORDER 4
223static void *vmx_l1d_flush_pages;
224
225static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
226{
227 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200228 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200229
Waiman Long19a36d32019-08-26 15:30:23 -0400230 if (!boot_cpu_has_bug(X86_BUG_L1TF)) {
231 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
232 return 0;
233 }
234
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200235 if (!enable_ept) {
236 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
237 return 0;
238 }
239
Yi Wangd806afa2018-08-16 13:42:39 +0800240 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
241 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200242
Yi Wangd806afa2018-08-16 13:42:39 +0800243 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
244 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
245 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
246 return 0;
247 }
248 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200249
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200250 /* If set to auto use the default l1tf mitigation method */
251 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
252 switch (l1tf_mitigation) {
253 case L1TF_MITIGATION_OFF:
254 l1tf = VMENTER_L1D_FLUSH_NEVER;
255 break;
256 case L1TF_MITIGATION_FLUSH_NOWARN:
257 case L1TF_MITIGATION_FLUSH:
258 case L1TF_MITIGATION_FLUSH_NOSMT:
259 l1tf = VMENTER_L1D_FLUSH_COND;
260 break;
261 case L1TF_MITIGATION_FULL:
262 case L1TF_MITIGATION_FULL_FORCE:
263 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
264 break;
265 }
266 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
267 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
268 }
269
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200270 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
271 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Ben Gardon41836832019-02-11 11:02:52 -0800272 /*
273 * This allocation for vmx_l1d_flush_pages is not tied to a VM
274 * lifetime and so should not be charged to a memcg.
275 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200276 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
277 if (!page)
278 return -ENOMEM;
279 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200280
281 /*
282 * Initialize each page with a different pattern in
283 * order to protect against KSM in the nested
284 * virtualization case.
285 */
286 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
287 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
288 PAGE_SIZE);
289 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200290 }
291
292 l1tf_vmx_mitigation = l1tf;
293
Thomas Gleixner895ae472018-07-13 16:23:22 +0200294 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
295 static_branch_enable(&vmx_l1d_should_flush);
296 else
297 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200298
Nicolai Stange427362a2018-07-21 22:25:00 +0200299 if (l1tf == VMENTER_L1D_FLUSH_COND)
300 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200301 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200302 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200303 return 0;
304}
305
306static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200307{
308 unsigned int i;
309
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200310 if (s) {
311 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200312 if (vmentry_l1d_param[i].for_parse &&
313 sysfs_streq(s, vmentry_l1d_param[i].option))
314 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200315 }
316 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200317 return -EINVAL;
318}
319
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200320static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
321{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200322 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200323
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200324 l1tf = vmentry_l1d_flush_parse(s);
325 if (l1tf < 0)
326 return l1tf;
327
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200328 if (!boot_cpu_has(X86_BUG_L1TF))
329 return 0;
330
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200331 /*
332 * Has vmx_init() run already? If not then this is the pre init
333 * parameter parsing. In that case just store the value and let
334 * vmx_init() do the proper setup after enable_ept has been
335 * established.
336 */
337 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
338 vmentry_l1d_flush_param = l1tf;
339 return 0;
340 }
341
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200342 mutex_lock(&vmx_l1d_flush_mutex);
343 ret = vmx_setup_l1d_flush(l1tf);
344 mutex_unlock(&vmx_l1d_flush_mutex);
345 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200346}
347
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200348static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
349{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200350 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
351 return sprintf(s, "???\n");
352
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200353 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200354}
355
356static const struct kernel_param_ops vmentry_l1d_flush_ops = {
357 .set = vmentry_l1d_flush_set,
358 .get = vmentry_l1d_flush_get,
359};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200360module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200361
Gleb Natapovd99e4152012-12-20 16:57:45 +0200362static u32 vmx_segment_access_rights(struct kvm_segment *var);
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200363static __always_inline void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu,
Ashok Raj15d45072018-02-01 22:59:43 +0100364 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300365
Sean Christopherson453eafb2018-12-20 12:25:17 -0800366void vmx_vmexit(void);
367
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700368#define vmx_insn_failed(fmt...) \
369do { \
370 WARN_ONCE(1, fmt); \
371 pr_warn_ratelimited(fmt); \
372} while (0)
373
Sean Christopherson6e202092019-07-19 13:41:08 -0700374asmlinkage void vmread_error(unsigned long field, bool fault)
375{
376 if (fault)
377 kvm_spurious_fault();
378 else
379 vmx_insn_failed("kvm: vmread failed: field=%lx\n", field);
380}
381
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700382noinline void vmwrite_error(unsigned long field, unsigned long value)
383{
384 vmx_insn_failed("kvm: vmwrite failed: field=%lx val=%lx err=%d\n",
385 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
386}
387
388noinline void vmclear_error(struct vmcs *vmcs, u64 phys_addr)
389{
390 vmx_insn_failed("kvm: vmclear failed: %p/%llx\n", vmcs, phys_addr);
391}
392
393noinline void vmptrld_error(struct vmcs *vmcs, u64 phys_addr)
394{
395 vmx_insn_failed("kvm: vmptrld failed: %p/%llx\n", vmcs, phys_addr);
396}
397
398noinline void invvpid_error(unsigned long ext, u16 vpid, gva_t gva)
399{
400 vmx_insn_failed("kvm: invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n",
401 ext, vpid, gva);
402}
403
404noinline void invept_error(unsigned long ext, u64 eptp, gpa_t gpa)
405{
406 vmx_insn_failed("kvm: invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n",
407 ext, eptp, gpa);
408}
409
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800411DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300412/*
413 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
414 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
415 */
416static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417
Sheng Yang2384d2b2008-01-17 15:14:33 +0800418static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
419static DEFINE_SPINLOCK(vmx_vpid_lock);
420
Sean Christopherson3077c192018-12-03 13:53:02 -0800421struct vmcs_config vmcs_config;
422struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800423
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424#define VMX_SEGMENT_FIELD(seg) \
425 [VCPU_SREG_##seg] = { \
426 .selector = GUEST_##seg##_SELECTOR, \
427 .base = GUEST_##seg##_BASE, \
428 .limit = GUEST_##seg##_LIMIT, \
429 .ar_bytes = GUEST_##seg##_AR_BYTES, \
430 }
431
Mathias Krause772e0312012-08-30 01:30:19 +0200432static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433 unsigned selector;
434 unsigned base;
435 unsigned limit;
436 unsigned ar_bytes;
437} kvm_vmx_segment_fields[] = {
438 VMX_SEGMENT_FIELD(CS),
439 VMX_SEGMENT_FIELD(DS),
440 VMX_SEGMENT_FIELD(ES),
441 VMX_SEGMENT_FIELD(FS),
442 VMX_SEGMENT_FIELD(GS),
443 VMX_SEGMENT_FIELD(SS),
444 VMX_SEGMENT_FIELD(TR),
445 VMX_SEGMENT_FIELD(LDTR),
446};
447
Sean Christophersonec0241f2020-04-15 13:34:52 -0700448static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
449{
450 vmx->segment_cache.bitmask = 0;
451}
452
Sean Christopherson23420802019-04-19 22:50:57 -0700453static unsigned long host_idt_base;
Avi Kivity26bb0982009-09-07 11:14:12 +0300454
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300455/*
Jim Mattson898a8112018-12-05 15:28:59 -0800456 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
457 * will emulate SYSCALL in legacy mode if the vendor string in guest
458 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
459 * support this emulation, IA32_STAR must always be included in
Sean Christopherson14a61b62020-09-23 11:04:08 -0700460 * vmx_uret_msrs_list[], even in i386 builds.
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300461 */
kernel test robot6a2e0922020-09-28 23:37:14 +0800462static const u32 vmx_uret_msrs_list[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800463#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300464 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400466 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Paolo Bonzinic11f83e2019-11-18 12:23:00 -0500467 MSR_IA32_TSX_CTRL,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100470#if IS_ENABLED(CONFIG_HYPERV)
471static bool __read_mostly enlightened_vmcs = true;
472module_param(enlightened_vmcs, bool, 0444);
473
Tianyu Lan877ad952018-07-19 08:40:23 +0000474/* check_ept_pointer() should be under protection of ept_pointer_lock. */
475static void check_ept_pointer_match(struct kvm *kvm)
476{
477 struct kvm_vcpu *vcpu;
478 u64 tmp_eptp = INVALID_PAGE;
479 int i;
480
481 kvm_for_each_vcpu(i, vcpu, kvm) {
482 if (!VALID_PAGE(tmp_eptp)) {
483 tmp_eptp = to_vmx(vcpu)->ept_pointer;
484 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
485 to_kvm_vmx(kvm)->ept_pointers_match
486 = EPT_POINTERS_MISMATCH;
487 return;
488 }
489 }
490
491 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
492}
493
Yi Wang8997f652019-01-21 15:27:05 +0800494static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800495 void *data)
496{
497 struct kvm_tlb_range *range = data;
498
499 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
500 range->pages);
501}
502
503static inline int __hv_remote_flush_tlb_with_range(struct kvm *kvm,
504 struct kvm_vcpu *vcpu, struct kvm_tlb_range *range)
505{
506 u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
507
508 /*
509 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs address
510 * of the base of EPT PML4 table, strip off EPT configuration
511 * information.
512 */
513 if (range)
514 return hyperv_flush_guest_mapping_range(ept_pointer & PAGE_MASK,
515 kvm_fill_hv_flush_list_func, (void *)range);
516 else
517 return hyperv_flush_guest_mapping(ept_pointer & PAGE_MASK);
518}
519
520static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
521 struct kvm_tlb_range *range)
Tianyu Lan877ad952018-07-19 08:40:23 +0000522{
Lan Tianyua5c214d2018-10-13 22:54:05 +0800523 struct kvm_vcpu *vcpu;
Lan Tianyub7c1c222019-01-04 15:20:44 +0800524 int ret = 0, i;
Tianyu Lan877ad952018-07-19 08:40:23 +0000525
526 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
527
528 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
529 check_ept_pointer_match(kvm);
530
531 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyu53963a72018-12-06 15:34:36 +0800532 kvm_for_each_vcpu(i, vcpu, kvm) {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800533 /* If ept_pointer is invalid pointer, bypass flush request. */
534 if (VALID_PAGE(to_vmx(vcpu)->ept_pointer))
535 ret |= __hv_remote_flush_tlb_with_range(
536 kvm, vcpu, range);
Lan Tianyu53963a72018-12-06 15:34:36 +0800537 }
Lan Tianyua5c214d2018-10-13 22:54:05 +0800538 } else {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800539 ret = __hv_remote_flush_tlb_with_range(kvm,
540 kvm_get_vcpu(kvm, 0), range);
Tianyu Lan877ad952018-07-19 08:40:23 +0000541 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000542
Tianyu Lan877ad952018-07-19 08:40:23 +0000543 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
544 return ret;
545}
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800546static int hv_remote_flush_tlb(struct kvm *kvm)
547{
548 return hv_remote_flush_tlb_with_range(kvm, NULL);
549}
550
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800551static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)
552{
553 struct hv_enlightened_vmcs *evmcs;
554 struct hv_partition_assist_pg **p_hv_pa_pg =
555 &vcpu->kvm->arch.hyperv.hv_pa_pg;
556 /*
557 * Synthetic VM-Exit is not enabled in current code and so All
558 * evmcs in singe VM shares same assist page.
559 */
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200560 if (!*p_hv_pa_pg)
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800561 *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200562
563 if (!*p_hv_pa_pg)
564 return -ENOMEM;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800565
566 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs;
567
568 evmcs->partition_assist_page =
569 __pa(*p_hv_pa_pg);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200570 evmcs->hv_vm_id = (unsigned long)vcpu->kvm;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800571 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;
572
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800573 return 0;
574}
575
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100576#endif /* IS_ENABLED(CONFIG_HYPERV) */
577
Yunhong Jiang64672c92016-06-13 14:19:59 -0700578/*
579 * Comment's format: document - errata name - stepping - processor name.
580 * Refer from
581 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
582 */
583static u32 vmx_preemption_cpu_tfms[] = {
584/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
5850x000206E6,
586/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
587/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
588/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
5890x00020652,
590/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
5910x00020655,
592/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
593/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
594/*
595 * 320767.pdf - AAP86 - B1 -
596 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
597 */
5980x000106E5,
599/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
6000x000106A0,
601/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
6020x000106A1,
603/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
6040x000106A4,
605 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
606 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
607 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
6080x000106A5,
Wei Huang3d82c562018-12-03 14:13:32 -0600609 /* Xeon E3-1220 V2 */
6100x000306A8,
Yunhong Jiang64672c92016-06-13 14:19:59 -0700611};
612
613static inline bool cpu_has_broken_vmx_preemption_timer(void)
614{
615 u32 eax = cpuid_eax(0x00000001), i;
616
617 /* Clear the reserved bits */
618 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000619 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700620 if (eax == vmx_preemption_cpu_tfms[i])
621 return true;
622
623 return false;
624}
625
Paolo Bonzini35754c92015-07-29 12:05:37 +0200626static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800627{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200628 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800629}
630
Sheng Yang04547152009-04-01 15:52:31 +0800631static inline bool report_flexpriority(void)
632{
633 return flexpriority_enabled;
634}
635
Alexander Graf3eb90012020-09-25 16:34:20 +0200636static int possible_passthrough_msr_slot(u32 msr)
637{
638 u32 i;
639
640 for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++)
641 if (vmx_possible_passthrough_msrs[i] == msr)
642 return i;
643
644 return -ENOENT;
645}
646
647static bool is_valid_passthrough_msr(u32 msr)
648{
649 bool r;
650
651 switch (msr) {
652 case 0x800 ... 0x8ff:
653 /* x2APIC MSRs. These are handled in vmx_update_msr_bitmap_x2apic() */
654 return true;
655 case MSR_IA32_RTIT_STATUS:
656 case MSR_IA32_RTIT_OUTPUT_BASE:
657 case MSR_IA32_RTIT_OUTPUT_MASK:
658 case MSR_IA32_RTIT_CR3_MATCH:
659 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
660 /* PT MSRs. These are handled in pt_update_intercept_for_msr() */
661 return true;
662 }
663
664 r = possible_passthrough_msr_slot(msr) != -ENOENT;
665
666 WARN(!r, "Invalid MSR %x, please adapt vmx_possible_passthrough_msrs[]", msr);
667
668 return r;
669}
670
Sean Christopherson1e7a4832020-09-23 11:04:02 -0700671static inline int __vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800672{
673 int i;
674
Sean Christophersonfbc18002020-09-23 11:03:59 -0700675 for (i = 0; i < vmx->nr_uret_msrs; ++i)
Sean Christopherson802145c2020-09-23 11:04:09 -0700676 if (vmx_uret_msrs_list[vmx->guest_uret_msrs[i].slot] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300677 return i;
678 return -1;
679}
680
Sean Christophersond85a8032020-09-23 11:04:06 -0700681struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300682{
683 int i;
684
Sean Christopherson1e7a4832020-09-23 11:04:02 -0700685 i = __vmx_find_uret_msr(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300686 if (i >= 0)
Sean Christophersoneb3db1b2020-09-23 11:03:58 -0700687 return &vmx->guest_uret_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000688 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800689}
690
Sean Christopherson7bf662b2020-09-23 11:04:07 -0700691static int vmx_set_guest_uret_msr(struct vcpu_vmx *vmx,
692 struct vmx_uret_msr *msr, u64 data)
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500693{
694 int ret = 0;
695
696 u64 old_msr_data = msr->data;
697 msr->data = data;
Sean Christophersone9bb1ae2020-09-23 11:04:00 -0700698 if (msr - vmx->guest_uret_msrs < vmx->nr_active_uret_msrs) {
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500699 preempt_disable();
Sean Christopherson802145c2020-09-23 11:04:09 -0700700 ret = kvm_set_user_return_msr(msr->slot, msr->data, msr->mask);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500701 preempt_enable();
702 if (ret)
703 msr->data = old_msr_data;
704 }
705 return ret;
706}
707
Dave Young2965faa2015-09-09 15:38:55 -0700708#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800709static void crash_vmclear_local_loaded_vmcss(void)
710{
711 int cpu = raw_smp_processor_id();
712 struct loaded_vmcs *v;
713
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800714 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
715 loaded_vmcss_on_cpu_link)
716 vmcs_clear(v->vmcs);
717}
Dave Young2965faa2015-09-09 15:38:55 -0700718#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800719
Nadav Har'Eld462b812011-05-24 15:26:10 +0300720static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300722 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800723 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724
Nadav Har'Eld462b812011-05-24 15:26:10 +0300725 if (loaded_vmcs->cpu != cpu)
726 return; /* vcpu migration can race with cpu offline */
727 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 per_cpu(current_vmcs, cpu) = NULL;
Sean Christopherson31603d42020-03-21 12:37:49 -0700729
730 vmcs_clear(loaded_vmcs->vmcs);
731 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
732 vmcs_clear(loaded_vmcs->shadow_vmcs);
733
Nadav Har'Eld462b812011-05-24 15:26:10 +0300734 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800735
736 /*
Sean Christopherson31603d42020-03-21 12:37:49 -0700737 * Ensure all writes to loaded_vmcs, including deleting it from its
738 * current percpu list, complete before setting loaded_vmcs->vcpu to
739 * -1, otherwise a different cpu can see vcpu == -1 first and add
740 * loaded_vmcs to its percpu list before it's deleted from this cpu's
741 * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800742 */
743 smp_wmb();
744
Sean Christopherson31603d42020-03-21 12:37:49 -0700745 loaded_vmcs->cpu = -1;
746 loaded_vmcs->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747}
748
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800749void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800750{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800751 int cpu = loaded_vmcs->cpu;
752
753 if (cpu != -1)
754 smp_call_function_single(cpu,
755 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800756}
757
Avi Kivity2fb92db2011-04-27 19:42:18 +0300758static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
759 unsigned field)
760{
761 bool ret;
762 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
763
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700764 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) {
765 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS);
Avi Kivity2fb92db2011-04-27 19:42:18 +0300766 vmx->segment_cache.bitmask = 0;
767 }
768 ret = vmx->segment_cache.bitmask & mask;
769 vmx->segment_cache.bitmask |= mask;
770 return ret;
771}
772
773static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
774{
775 u16 *p = &vmx->segment_cache.seg[seg].selector;
776
777 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
778 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
779 return *p;
780}
781
782static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
783{
784 ulong *p = &vmx->segment_cache.seg[seg].base;
785
786 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
787 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
788 return *p;
789}
790
791static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
792{
793 u32 *p = &vmx->segment_cache.seg[seg].limit;
794
795 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
796 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
797 return *p;
798}
799
800static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
801{
802 u32 *p = &vmx->segment_cache.seg[seg].ar;
803
804 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
805 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
806 return *p;
807}
808
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800809void update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300810{
811 u32 eb;
812
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100813 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800814 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200815 /*
816 * Guest access to VMware backdoor ports could legitimately
817 * trigger #GP because of TSS I/O permission bitmap.
818 * We intercept those #GP and allow access to them anyway
819 * as VMware does.
820 */
821 if (enable_vmware_backdoor)
822 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100823 if ((vcpu->guest_debug &
824 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
825 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
826 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300827 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300828 eb = ~0;
Paolo Bonzinia0c13432020-07-10 17:48:08 +0200829 if (!vmx_need_pf_intercept(vcpu))
Miaohe Lin49f933d2020-02-27 11:20:54 +0800830 eb &= ~(1u << PF_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300831
832 /* When we are running a nested L2 guest and L1 specified for it a
833 * certain exception bitmap, we must trap the same exceptions and pass
834 * them to L1. When running L2, we will only handle the exceptions
835 * specified above if L1 did not want them.
836 */
837 if (is_guest_mode(vcpu))
838 eb |= get_vmcs12(vcpu)->exception_bitmap;
Paolo Bonzinib502e6e2020-09-29 08:31:32 -0400839 else {
840 /*
841 * If EPT is enabled, #PF is only trapped if MAXPHYADDR is mismatched
842 * between guest and host. In that case we only care about present
843 * faults. For vmcs02, however, PFEC_MASK and PFEC_MATCH are set in
844 * prepare_vmcs02_rare.
845 */
846 bool selective_pf_trap = enable_ept && (eb & (1u << PF_VECTOR));
847 int mask = selective_pf_trap ? PFERR_PRESENT_MASK : 0;
848 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, mask);
849 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, mask);
850 }
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300851
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300852 vmcs_write32(EXCEPTION_BITMAP, eb);
853}
854
Ashok Raj15d45072018-02-01 22:59:43 +0100855/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100856 * Check if MSR is intercepted for currently loaded MSR bitmap.
857 */
858static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
859{
860 unsigned long *msr_bitmap;
861 int f = sizeof(unsigned long);
862
863 if (!cpu_has_vmx_msr_bitmap())
864 return true;
865
866 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
867
868 if (msr <= 0x1fff) {
869 return !!test_bit(msr, msr_bitmap + 0x800 / f);
870 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
871 msr &= 0x1fff;
872 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
873 }
874
875 return true;
876}
877
Gleb Natapov2961e8762013-11-25 15:37:13 +0200878static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
879 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200880{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200881 vm_entry_controls_clearbit(vmx, entry);
882 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200883}
884
Sean Christophersona128a932020-09-23 11:03:57 -0700885int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr)
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400886{
887 unsigned int i;
888
889 for (i = 0; i < m->nr; ++i) {
890 if (m->val[i].index == msr)
891 return i;
892 }
893 return -ENOENT;
894}
895
Avi Kivity61d2ef22010-04-28 16:40:38 +0300896static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
897{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400898 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300899 struct msr_autoload *m = &vmx->msr_autoload;
900
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200901 switch (msr) {
902 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800903 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200904 clear_atomic_switch_msr_special(vmx,
905 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200906 VM_EXIT_LOAD_IA32_EFER);
907 return;
908 }
909 break;
910 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800911 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200912 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200913 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
914 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
915 return;
916 }
917 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200918 }
Sean Christophersona128a932020-09-23 11:03:57 -0700919 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400920 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400921 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400922 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400923 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400924 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200925
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400926skip_guest:
Sean Christophersona128a932020-09-23 11:03:57 -0700927 i = vmx_find_loadstore_msr_slot(&m->host, msr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400928 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300929 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400930
931 --m->host.nr;
932 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400933 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300934}
935
Gleb Natapov2961e8762013-11-25 15:37:13 +0200936static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
937 unsigned long entry, unsigned long exit,
938 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
939 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200940{
941 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700942 if (host_val_vmcs != HOST_IA32_EFER)
943 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200944 vm_entry_controls_setbit(vmx, entry);
945 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200946}
947
Avi Kivity61d2ef22010-04-28 16:40:38 +0300948static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400949 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300950{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400951 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300952 struct msr_autoload *m = &vmx->msr_autoload;
953
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200954 switch (msr) {
955 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800956 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200957 add_atomic_switch_msr_special(vmx,
958 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200959 VM_EXIT_LOAD_IA32_EFER,
960 GUEST_IA32_EFER,
961 HOST_IA32_EFER,
962 guest_val, host_val);
963 return;
964 }
965 break;
966 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800967 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200968 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200969 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
970 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
971 GUEST_IA32_PERF_GLOBAL_CTRL,
972 HOST_IA32_PERF_GLOBAL_CTRL,
973 guest_val, host_val);
974 return;
975 }
976 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +0100977 case MSR_IA32_PEBS_ENABLE:
978 /* PEBS needs a quiescent period after being disabled (to write
979 * a record). Disabling PEBS through VMX MSR swapping doesn't
980 * provide that period, so a CPU could write host's record into
981 * guest's memory.
982 */
983 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +0200984 }
985
Sean Christophersona128a932020-09-23 11:03:57 -0700986 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400987 if (!entry_only)
Sean Christophersona128a932020-09-23 11:03:57 -0700988 j = vmx_find_loadstore_msr_slot(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300989
Sean Christophersonce833b22020-09-23 11:03:56 -0700990 if ((i < 0 && m->guest.nr == MAX_NR_LOADSTORE_MSRS) ||
991 (j < 0 && m->host.nr == MAX_NR_LOADSTORE_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +0200992 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +0200993 "Can't add msr %x\n", msr);
994 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300995 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400996 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400997 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400998 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400999 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001000 m->guest.val[i].index = msr;
1001 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001002
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001003 if (entry_only)
1004 return;
1005
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001006 if (j < 0) {
1007 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001008 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03001009 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001010 m->host.val[j].index = msr;
1011 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001012}
1013
Sean Christopherson86e3e492020-09-23 11:04:04 -07001014static bool update_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +03001015{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001016 u64 guest_efer = vmx->vcpu.arch.efer;
1017 u64 ignore_bits = 0;
Sean Christopherson86e3e492020-09-23 11:04:04 -07001018 int i;
Eddie Dong2cc51562007-05-21 07:28:09 +03001019
Paolo Bonzini9167ab72019-10-27 16:23:23 +01001020 /* Shadow paging assumes NX to be available. */
1021 if (!enable_ept)
1022 guest_efer |= EFER_NX;
Roel Kluin3a34a882009-08-04 02:08:45 -07001023
Avi Kivity51c6cf62007-08-29 03:48:05 +03001024 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001025 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03001026 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001027 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001028#ifdef CONFIG_X86_64
1029 ignore_bits |= EFER_LMA | EFER_LME;
1030 /* SCE is meaningful only in long mode on Intel */
1031 if (guest_efer & EFER_LMA)
1032 ignore_bits &= ~(u64)EFER_SCE;
1033#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03001034
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001035 /*
1036 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1037 * On CPUs that support "load IA32_EFER", always switch EFER
1038 * atomically, since it's faster than switching it manually.
1039 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -08001040 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001041 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03001042 if (!(guest_efer & EFER_LMA))
1043 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001044 if (guest_efer != host_efer)
1045 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001046 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07001047 else
1048 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03001049 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001050 }
Sean Christopherson86e3e492020-09-23 11:04:04 -07001051
1052 i = __vmx_find_uret_msr(vmx, MSR_EFER);
1053 if (i < 0)
1054 return false;
1055
1056 clear_atomic_switch_msr(vmx, MSR_EFER);
1057
1058 guest_efer &= ~ignore_bits;
1059 guest_efer |= host_efer & ignore_bits;
1060
1061 vmx->guest_uret_msrs[i].data = guest_efer;
1062 vmx->guest_uret_msrs[i].mask = ~ignore_bits;
1063
1064 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001065}
1066
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001067#ifdef CONFIG_X86_32
1068/*
1069 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1070 * VMCS rather than the segment table. KVM uses this helper to figure
1071 * out the current bases to poke them into the VMCS before entry.
1072 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001073static unsigned long segment_base(u16 selector)
1074{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001075 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001076 unsigned long v;
1077
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001078 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001079 return 0;
1080
Thomas Garnier45fc8752017-03-14 10:05:08 -07001081 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001082
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001083 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001084 u16 ldt_selector = kvm_read_ldt();
1085
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001086 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001087 return 0;
1088
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001089 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001090 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001091 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001092 return v;
1093}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001094#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001095
Sean Christophersone348ac72019-12-10 15:24:33 -08001096static inline bool pt_can_write_msr(struct vcpu_vmx *vmx)
1097{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001098 return vmx_pt_mode_is_host_guest() &&
Sean Christophersone348ac72019-12-10 15:24:33 -08001099 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
1100}
1101
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001102static inline bool pt_output_base_valid(struct kvm_vcpu *vcpu, u64 base)
1103{
1104 /* The base must be 128-byte aligned and a legal physical address. */
Sean Christopherson7096cbf2020-09-24 12:42:50 -07001105 return !kvm_vcpu_is_illegal_gpa(vcpu, base) && !(base & 0x7f);
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001106}
1107
Chao Peng2ef444f2018-10-24 16:05:12 +08001108static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range)
1109{
1110 u32 i;
1111
1112 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1113 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1114 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1115 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1116 for (i = 0; i < addr_range; i++) {
1117 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1118 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1119 }
1120}
1121
1122static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range)
1123{
1124 u32 i;
1125
1126 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1127 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1128 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1129 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1130 for (i = 0; i < addr_range; i++) {
1131 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1132 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1133 }
1134}
1135
1136static void pt_guest_enter(struct vcpu_vmx *vmx)
1137{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001138 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001139 return;
1140
Chao Peng2ef444f2018-10-24 16:05:12 +08001141 /*
Chao Pengb08c2892018-10-24 16:05:15 +08001142 * GUEST_IA32_RTIT_CTL is already set in the VMCS.
1143 * Save host state before VM entry.
Chao Peng2ef444f2018-10-24 16:05:12 +08001144 */
Chao Pengb08c2892018-10-24 16:05:15 +08001145 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
Chao Peng2ef444f2018-10-24 16:05:12 +08001146 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1147 wrmsrl(MSR_IA32_RTIT_CTL, 0);
1148 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1149 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1150 }
1151}
1152
1153static void pt_guest_exit(struct vcpu_vmx *vmx)
1154{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001155 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001156 return;
1157
1158 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1159 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1160 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1161 }
1162
1163 /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */
1164 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
1165}
1166
Sean Christopherson13b964a2019-05-07 09:06:31 -07001167void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1168 unsigned long fs_base, unsigned long gs_base)
1169{
1170 if (unlikely(fs_sel != host->fs_sel)) {
1171 if (!(fs_sel & 7))
1172 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1173 else
1174 vmcs_write16(HOST_FS_SELECTOR, 0);
1175 host->fs_sel = fs_sel;
1176 }
1177 if (unlikely(gs_sel != host->gs_sel)) {
1178 if (!(gs_sel & 7))
1179 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1180 else
1181 vmcs_write16(HOST_GS_SELECTOR, 0);
1182 host->gs_sel = gs_sel;
1183 }
1184 if (unlikely(fs_base != host->fs_base)) {
1185 vmcs_writel(HOST_FS_BASE, fs_base);
1186 host->fs_base = fs_base;
1187 }
1188 if (unlikely(gs_base != host->gs_base)) {
1189 vmcs_writel(HOST_GS_BASE, gs_base);
1190 host->gs_base = gs_base;
1191 }
1192}
1193
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001194void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001195{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001196 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001197 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001198#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01001199 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001200#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001201 unsigned long fs_base, gs_base;
1202 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03001203 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001204
Sean Christophersond264ee02018-08-27 15:21:12 -07001205 vmx->req_immediate_exit = false;
1206
Liran Alonf48b4712018-11-20 18:03:25 +02001207 /*
1208 * Note that guest MSRs to be saved/restored can also be changed
1209 * when guest state is loaded. This happens when guest transitions
1210 * to/from long-mode by setting MSR_EFER.LMA.
1211 */
Sean Christopherson658ece82020-09-23 11:04:01 -07001212 if (!vmx->guest_uret_msrs_loaded) {
1213 vmx->guest_uret_msrs_loaded = true;
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001214 for (i = 0; i < vmx->nr_active_uret_msrs; ++i)
Sean Christopherson802145c2020-09-23 11:04:09 -07001215 kvm_set_user_return_msr(vmx->guest_uret_msrs[i].slot,
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001216 vmx->guest_uret_msrs[i].data,
1217 vmx->guest_uret_msrs[i].mask);
Liran Alonf48b4712018-11-20 18:03:25 +02001218
1219 }
wanpeng lic9dfd3f2020-02-17 18:37:43 +08001220
1221 if (vmx->nested.need_vmcs12_to_shadow_sync)
1222 nested_sync_vmcs12_to_shadow(vcpu);
1223
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001224 if (vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001225 return;
1226
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001227 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001228
Avi Kivity33ed6322007-05-02 16:54:03 +03001229 /*
1230 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1231 * allow segment selectors with cpl > 0 or ti == 1.
1232 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07001233 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01001234
1235#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001236 savesegment(ds, host_state->ds_sel);
1237 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07001238
1239 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001240 if (likely(is_64bit_mm(current->mm))) {
Thomas Gleixner67580342020-05-28 16:13:52 -04001241 current_save_fsgs();
Sean Christophersone368b872018-07-23 12:32:41 -07001242 fs_sel = current->thread.fsindex;
1243 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001244 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07001245 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001246 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07001247 savesegment(fs, fs_sel);
1248 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001249 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07001250 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03001251 }
1252
Paolo Bonzini4679b612018-09-24 17:23:01 +02001253 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03001254#else
Sean Christophersone368b872018-07-23 12:32:41 -07001255 savesegment(fs, fs_sel);
1256 savesegment(gs, gs_sel);
1257 fs_base = segment_base(fs_sel);
1258 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001259#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001260
Sean Christopherson13b964a2019-05-07 09:06:31 -07001261 vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001262 vmx->guest_state_loaded = true;
Avi Kivity33ed6322007-05-02 16:54:03 +03001263}
1264
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001265static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001266{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001267 struct vmcs_host_state *host_state;
1268
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001269 if (!vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001270 return;
1271
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001272 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001273
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001274 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001275
Avi Kivityc8770e72010-11-11 12:37:26 +02001276#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001277 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001278#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001279 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1280 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001281#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001282 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001283#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001284 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001285#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001286 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001287 if (host_state->fs_sel & 7)
1288 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001289#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001290 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1291 loadsegment(ds, host_state->ds_sel);
1292 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001293 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001294#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001295 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001296#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001297 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001298#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001299 load_fixmap_gdt(raw_smp_processor_id());
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001300 vmx->guest_state_loaded = false;
Sean Christopherson658ece82020-09-23 11:04:01 -07001301 vmx->guest_uret_msrs_loaded = false;
Avi Kivity33ed6322007-05-02 16:54:03 +03001302}
1303
Sean Christopherson678e3152018-07-23 12:32:43 -07001304#ifdef CONFIG_X86_64
1305static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001306{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001307 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001308 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001309 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1310 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001311 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001312}
1313
Sean Christopherson678e3152018-07-23 12:32:43 -07001314static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1315{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001316 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001317 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001318 wrmsrl(MSR_KERNEL_GS_BASE, data);
1319 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001320 vmx->msr_guest_kernel_gs_base = data;
1321}
1322#endif
1323
Sean Christopherson5c911be2020-05-01 09:31:17 -07001324void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
1325 struct loaded_vmcs *buddy)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001327 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001328 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Sean Christopherson5c911be2020-05-01 09:31:17 -07001329 struct vmcs *prev;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001331 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001332 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001333 local_irq_disable();
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001334
1335 /*
Sean Christopherson31603d42020-03-21 12:37:49 -07001336 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
1337 * this cpu's percpu list, otherwise it may not yet be deleted
1338 * from its previous cpu's percpu list. Pairs with the
1339 * smb_wmb() in __loaded_vmcs_clear().
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001340 */
1341 smp_rmb();
1342
Nadav Har'Eld462b812011-05-24 15:26:10 +03001343 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1344 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001345 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001346 }
1347
Sean Christopherson5c911be2020-05-01 09:31:17 -07001348 prev = per_cpu(current_vmcs, cpu);
1349 if (prev != vmx->loaded_vmcs->vmcs) {
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001350 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1351 vmcs_load(vmx->loaded_vmcs->vmcs);
Sean Christopherson5c911be2020-05-01 09:31:17 -07001352
1353 /*
1354 * No indirect branch prediction barrier needed when switching
1355 * the active VMCS within a guest, e.g. on nested VM-Enter.
1356 * The L1 VMM can protect itself with retpolines, IBPB or IBRS.
1357 */
1358 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev))
1359 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001360 }
1361
1362 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001363 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001364 unsigned long sysenter_esp;
1365
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001366 /*
1367 * Flush all EPTP/VPID contexts, the new pCPU may have stale
1368 * TLB entries from its previous association with the vCPU.
1369 */
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001370 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001371
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 /*
1373 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001374 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001376 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001377 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001378 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379
1380 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1381 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001382
Nadav Har'Eld462b812011-05-24 15:26:10 +03001383 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384 }
Feng Wu28b835d2015-09-18 22:29:54 +08001385
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001386 /* Setup TSC multiplier */
1387 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001388 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1389 decache_tsc_multiplier(vmx);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001390}
1391
1392/*
1393 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1394 * vcpu mutex is already taken.
1395 */
Sean Christopherson1af1bb02020-05-06 16:58:50 -07001396static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001397{
1398 struct vcpu_vmx *vmx = to_vmx(vcpu);
1399
Sean Christopherson5c911be2020-05-01 09:31:17 -07001400 vmx_vcpu_load_vmcs(vcpu, cpu, NULL);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001401
Feng Wu28b835d2015-09-18 22:29:54 +08001402 vmx_vcpu_pi_load(vcpu, cpu);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001403
Wanpeng Li74c55932017-11-29 01:31:20 -08001404 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001405}
1406
Sean Christopherson13b964a2019-05-07 09:06:31 -07001407static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408{
Feng Wu28b835d2015-09-18 22:29:54 +08001409 vmx_vcpu_pi_put(vcpu);
1410
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001411 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412}
1413
Wanpeng Lif244dee2017-07-20 01:11:54 -07001414static bool emulation_required(struct kvm_vcpu *vcpu)
1415{
Sean Christopherson2ba44932020-09-23 11:44:48 -07001416 return emulate_invalid_guest_state && !vmx_guest_state_valid(vcpu);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001417}
1418
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001419unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001421 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001422 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001423
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001424 if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) {
1425 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Avi Kivity6de12732011-03-07 12:51:22 +02001426 rflags = vmcs_readl(GUEST_RFLAGS);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001427 if (vmx->rmode.vm86_active) {
Avi Kivity6de12732011-03-07 12:51:22 +02001428 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
Sean Christophersone7bddc52019-09-27 14:45:18 -07001429 save_rflags = vmx->rmode.save_rflags;
Avi Kivity6de12732011-03-07 12:51:22 +02001430 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1431 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001432 vmx->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001433 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001434 return vmx->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435}
1436
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001437void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001439 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001440 unsigned long old_rflags;
Wanpeng Lif244dee2017-07-20 01:11:54 -07001441
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00001442 if (is_unrestricted_guest(vcpu)) {
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001443 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001444 vmx->rflags = rflags;
1445 vmcs_writel(GUEST_RFLAGS, rflags);
1446 return;
1447 }
1448
1449 old_rflags = vmx_get_rflags(vcpu);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001450 vmx->rflags = rflags;
1451 if (vmx->rmode.vm86_active) {
1452 vmx->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001453 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001454 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001456
Sean Christophersone7bddc52019-09-27 14:45:18 -07001457 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM)
1458 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459}
1460
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001461u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001462{
1463 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1464 int ret = 0;
1465
1466 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001467 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001468 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001469 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001470
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001471 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001472}
1473
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001474void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001475{
1476 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1477 u32 interruptibility = interruptibility_old;
1478
1479 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1480
Jan Kiszka48005f62010-02-19 19:38:07 +01001481 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001482 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001483 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001484 interruptibility |= GUEST_INTR_STATE_STI;
1485
1486 if ((interruptibility != interruptibility_old))
1487 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1488}
1489
Chao Pengbf8c55d2018-10-24 16:05:14 +08001490static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data)
1491{
1492 struct vcpu_vmx *vmx = to_vmx(vcpu);
1493 unsigned long value;
1494
1495 /*
1496 * Any MSR write that attempts to change bits marked reserved will
1497 * case a #GP fault.
1498 */
1499 if (data & vmx->pt_desc.ctl_bitmask)
1500 return 1;
1501
1502 /*
1503 * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will
1504 * result in a #GP unless the same write also clears TraceEn.
1505 */
1506 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) &&
1507 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN))
1508 return 1;
1509
1510 /*
1511 * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit
1512 * and FabricEn would cause #GP, if
1513 * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0
1514 */
1515 if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) &&
1516 !(data & RTIT_CTL_FABRIC_EN) &&
1517 !intel_pt_validate_cap(vmx->pt_desc.caps,
1518 PT_CAP_single_range_output))
1519 return 1;
1520
1521 /*
1522 * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that
1523 * utilize encodings marked reserved will casue a #GP fault.
1524 */
1525 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods);
1526 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) &&
1527 !test_bit((data & RTIT_CTL_MTC_RANGE) >>
1528 RTIT_CTL_MTC_RANGE_OFFSET, &value))
1529 return 1;
1530 value = intel_pt_validate_cap(vmx->pt_desc.caps,
1531 PT_CAP_cycle_thresholds);
1532 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1533 !test_bit((data & RTIT_CTL_CYC_THRESH) >>
1534 RTIT_CTL_CYC_THRESH_OFFSET, &value))
1535 return 1;
1536 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods);
1537 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1538 !test_bit((data & RTIT_CTL_PSB_FREQ) >>
1539 RTIT_CTL_PSB_FREQ_OFFSET, &value))
1540 return 1;
1541
1542 /*
1543 * If ADDRx_CFG is reserved or the encodings is >2 will
1544 * cause a #GP fault.
1545 */
1546 value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET;
1547 if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2))
1548 return 1;
1549 value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET;
1550 if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2))
1551 return 1;
1552 value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET;
1553 if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2))
1554 return 1;
1555 value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET;
1556 if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2))
1557 return 1;
1558
1559 return 0;
1560}
1561
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07001562static bool vmx_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
1563{
1564 return true;
1565}
1566
Sean Christopherson1957aa62019-08-27 14:40:39 -07001567static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568{
Paolo Bonzinifede8072020-04-27 11:55:59 -04001569 unsigned long rip, orig_rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570
Sean Christopherson1957aa62019-08-27 14:40:39 -07001571 /*
1572 * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on
1573 * undefined behavior: Intel's SDM doesn't mandate the VMCS field be
1574 * set when EPT misconfig occurs. In practice, real hardware updates
1575 * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors
1576 * (namely Hyper-V) don't set it due to it being undefined behavior,
1577 * i.e. we end up advancing IP with some random value.
1578 */
1579 if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
Sean Christopherson8e533242020-11-06 17:03:12 +08001580 to_vmx(vcpu)->exit_reason.basic != EXIT_REASON_EPT_MISCONFIG) {
Paolo Bonzinifede8072020-04-27 11:55:59 -04001581 orig_rip = kvm_rip_read(vcpu);
1582 rip = orig_rip + vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
1583#ifdef CONFIG_X86_64
1584 /*
1585 * We need to mask out the high 32 bits of RIP if not in 64-bit
1586 * mode, but just finding out that we are in 64-bit mode is
1587 * quite expensive. Only do it if there was a carry.
1588 */
1589 if (unlikely(((rip ^ orig_rip) >> 31) == 3) && !is_64_bit_mode(vcpu))
1590 rip = (u32)rip;
1591#endif
Sean Christopherson1957aa62019-08-27 14:40:39 -07001592 kvm_rip_write(vcpu, rip);
1593 } else {
1594 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
1595 return 0;
1596 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597
Glauber Costa2809f5d2009-05-12 16:21:05 -04001598 /* skipping an emulated instruction also counts */
1599 vmx_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001600
Sean Christopherson60fc3d02019-08-27 14:40:38 -07001601 return 1;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001602}
1603
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02001604/*
Oliver Upton5ef8acb2020-02-07 02:36:07 -08001605 * Recognizes a pending MTF VM-exit and records the nested state for later
1606 * delivery.
1607 */
1608static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)
1609{
1610 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1611 struct vcpu_vmx *vmx = to_vmx(vcpu);
1612
1613 if (!is_guest_mode(vcpu))
1614 return;
1615
1616 /*
1617 * Per the SDM, MTF takes priority over debug-trap exceptions besides
1618 * T-bit traps. As instruction emulation is completed (i.e. at the
1619 * instruction boundary), any #DB exception pending delivery must be a
1620 * debug-trap. Record the pending MTF state to be delivered in
1621 * vmx_check_nested_events().
1622 */
1623 if (nested_cpu_has_mtf(vmcs12) &&
1624 (!vcpu->arch.exception.pending ||
1625 vcpu->arch.exception.nr == DB_VECTOR))
1626 vmx->nested.mtf_pending = true;
1627 else
1628 vmx->nested.mtf_pending = false;
1629}
1630
1631static int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu)
1632{
1633 vmx_update_emulated_instruction(vcpu);
1634 return skip_emulated_instruction(vcpu);
1635}
1636
Wanpeng Licaa057a2018-03-12 04:53:03 -07001637static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1638{
1639 /*
1640 * Ensure that we clear the HLT state in the VMCS. We don't need to
1641 * explicitly skip the instruction because if the HLT state is set,
1642 * then the instruction is already executing and RIP has already been
1643 * advanced.
1644 */
1645 if (kvm_hlt_in_guest(vcpu->kvm) &&
1646 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1647 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1648}
1649
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001650static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001651{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001652 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001653 unsigned nr = vcpu->arch.exception.nr;
1654 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001655 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001656 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001657
Jim Mattsonda998b42018-10-16 14:29:22 -07001658 kvm_deliver_exception_payload(vcpu);
1659
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001660 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001661 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001662 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1663 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001664
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001665 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001666 int inc_eip = 0;
1667 if (kvm_exception_is_soft(nr))
1668 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07001669 kvm_inject_realmode_interrupt(vcpu, nr, inc_eip);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001670 return;
1671 }
1672
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001673 WARN_ON_ONCE(vmx->emulation_required);
1674
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001675 if (kvm_exception_is_soft(nr)) {
1676 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1677 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001678 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1679 } else
1680 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1681
1682 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001683
1684 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001685}
1686
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001687static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001688{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001689 struct vmx_uret_msr tmp;
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001690 int from, to;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001691
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001692 from = __vmx_find_uret_msr(vmx, msr);
1693 if (from < 0)
1694 return;
1695 to = vmx->nr_active_uret_msrs++;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001696
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001697 tmp = vmx->guest_uret_msrs[to];
1698 vmx->guest_uret_msrs[to] = vmx->guest_uret_msrs[from];
1699 vmx->guest_uret_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001700}
1701
1702/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001703 * Set up the vmcs to automatically save and restore system
1704 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1705 * mode, as fiddling with msrs is very expensive.
1706 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001707static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001708{
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001709 vmx->guest_uret_msrs_loaded = false;
1710 vmx->nr_active_uret_msrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001711#ifdef CONFIG_X86_64
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001712 /*
1713 * The SYSCALL MSRs are only needed on long mode guests, and only
1714 * when EFER.SCE is set.
1715 */
1716 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001717 vmx_setup_uret_msr(vmx, MSR_STAR);
1718 vmx_setup_uret_msr(vmx, MSR_LSTAR);
1719 vmx_setup_uret_msr(vmx, MSR_SYSCALL_MASK);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001720 }
Eddie Donga75beee2007-05-17 18:55:15 +03001721#endif
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001722 if (update_transition_efer(vmx))
1723 vmx_setup_uret_msr(vmx, MSR_EFER);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001724
Sean Christophersonbd65ba82020-09-23 11:04:05 -07001725 if (guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
1726 vmx_setup_uret_msr(vmx, MSR_TSC_AUX);
1727
1728 vmx_setup_uret_msr(vmx, MSR_IA32_TSX_CTRL);
Avi Kivity58972972009-02-24 22:26:47 +02001729
Yang Zhang8d146952013-01-25 10:18:50 +08001730 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001731 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001732}
1733
Leonid Shatz326e7422018-11-06 12:14:25 +02001734static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001736 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1737 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001738
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001739 /*
1740 * We're here if L1 chose not to trap WRMSR to TSC. According
1741 * to the spec, this should set L1's TSC; The offset that L1
1742 * set for L2 remains unchanged, and still needs to be added
1743 * to the newly set TSC to get L2's TSC.
1744 */
1745 if (is_guest_mode(vcpu) &&
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08001746 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001747 g_tsc_offset = vmcs12->tsc_offset;
1748
1749 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1750 vcpu->arch.tsc_offset - g_tsc_offset,
1751 offset);
1752 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1753 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001754}
1755
Nadav Har'El801d3422011-05-25 23:02:23 +03001756/*
1757 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1758 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1759 * all guests if the "nested" module option is off, and can also be disabled
1760 * for a single guest by disabling its VMX cpuid bit.
1761 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001762bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001763{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001764 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001765}
1766
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001767static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1768 uint64_t val)
1769{
1770 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1771
1772 return !(val & ~valid_bits);
1773}
1774
Tom Lendacky801e4592018-02-21 13:39:51 -06001775static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1776{
Paolo Bonzini13893092018-02-26 13:40:09 +01001777 switch (msr->index) {
1778 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1779 if (!nested)
1780 return 1;
1781 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
Like Xu27461da32020-05-29 15:43:45 +08001782 case MSR_IA32_PERF_CAPABILITIES:
1783 msr->data = vmx_get_perf_capabilities();
1784 return 0;
Paolo Bonzini13893092018-02-26 13:40:09 +01001785 default:
Peter Xu12bc2132020-06-22 18:04:42 -04001786 return KVM_MSR_RET_INVALID;
Paolo Bonzini13893092018-02-26 13:40:09 +01001787 }
Tom Lendacky801e4592018-02-21 13:39:51 -06001788}
1789
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001790/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791 * Reads an msr value (of 'msr_index') into 'pdata'.
1792 * Returns 0 on success, non-0 otherwise.
1793 * Assumes vcpu_load() was already called.
1794 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001795static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001797 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001798 struct vmx_uret_msr *msr;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001799 u32 index;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001801 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001802#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001804 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805 break;
1806 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001807 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001808 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001809 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001810 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001811 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001812#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001814 return kvm_get_msr_common(vcpu, msr_info);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001815 case MSR_IA32_TSX_CTRL:
1816 if (!msr_info->host_initiated &&
1817 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
1818 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001819 goto find_uret_msr;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001820 case MSR_IA32_UMWAIT_CONTROL:
1821 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
1822 return 1;
1823
1824 msr_info->data = vmx->msr_ia32_umwait_control;
1825 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001826 case MSR_IA32_SPEC_CTRL:
1827 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05001828 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001829 return 1;
1830
1831 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1832 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001834 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835 break;
1836 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001837 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838 break;
1839 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001840 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001842 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001843 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001844 (!msr_info->host_initiated &&
1845 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001846 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001847 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001848 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001849 case MSR_IA32_MCG_EXT_CTL:
1850 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001851 !(vmx->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001852 FEAT_CTL_LMCE_ENABLED))
Jan Kiszkacae50132014-01-04 18:47:22 +01001853 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001854 msr_info->data = vcpu->arch.mcg_ext_ctl;
1855 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001856 case MSR_IA32_FEAT_CTL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001857 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001858 break;
1859 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1860 if (!nested_vmx_allowed(vcpu))
1861 return 1;
Vitaly Kuznetsov31de3d22020-02-05 13:30:33 +01001862 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1863 &msr_info->data))
1864 return 1;
1865 /*
1866 * Enlightened VMCS v1 doesn't have certain fields, but buggy
1867 * Hyper-V versions are still trying to use corresponding
1868 * features when they are exposed. Filter out the essential
1869 * minimum.
1870 */
1871 if (!msr_info->host_initiated &&
1872 vmx->nested.enlightened_vmcs_enabled)
1873 nested_evmcs_filter_control_msr(msr_info->index,
1874 &msr_info->data);
1875 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001876 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001877 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001878 return 1;
1879 msr_info->data = vmx->pt_desc.guest.ctl;
1880 break;
1881 case MSR_IA32_RTIT_STATUS:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001882 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001883 return 1;
1884 msr_info->data = vmx->pt_desc.guest.status;
1885 break;
1886 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001887 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001888 !intel_pt_validate_cap(vmx->pt_desc.caps,
1889 PT_CAP_cr3_filtering))
1890 return 1;
1891 msr_info->data = vmx->pt_desc.guest.cr3_match;
1892 break;
1893 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001894 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001895 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1896 PT_CAP_topa_output) &&
1897 !intel_pt_validate_cap(vmx->pt_desc.caps,
1898 PT_CAP_single_range_output)))
1899 return 1;
1900 msr_info->data = vmx->pt_desc.guest.output_base;
1901 break;
1902 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001903 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001904 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1905 PT_CAP_topa_output) &&
1906 !intel_pt_validate_cap(vmx->pt_desc.caps,
1907 PT_CAP_single_range_output)))
1908 return 1;
1909 msr_info->data = vmx->pt_desc.guest.output_mask;
1910 break;
1911 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
1912 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christopherson2ef76192020-03-02 15:56:22 -08001913 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001914 (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
1915 PT_CAP_num_address_ranges)))
1916 return 1;
1917 if (index % 2)
1918 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
1919 else
1920 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
1921 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001922 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001923 if (!msr_info->host_initiated &&
1924 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001925 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001926 goto find_uret_msr;
Like Xud8550662021-01-08 09:36:55 +08001927 case MSR_IA32_DEBUGCTLMSR:
1928 msr_info->data = vmcs_read64(GUEST_IA32_DEBUGCTL);
1929 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001931 find_uret_msr:
Sean Christophersond85a8032020-09-23 11:04:06 -07001932 msr = vmx_find_uret_msr(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001933 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001934 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001935 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001937 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938 }
1939
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940 return 0;
1941}
1942
Sean Christopherson24085002020-04-28 16:10:24 -07001943static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu,
1944 u64 data)
1945{
1946#ifdef CONFIG_X86_64
1947 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
1948 return (u32)data;
1949#endif
1950 return (unsigned long)data;
1951}
1952
Like Xuc6462362021-02-01 13:10:31 +08001953static u64 vcpu_supported_debugctl(struct kvm_vcpu *vcpu)
1954{
1955 u64 debugctl = vmx_supported_debugctl();
1956
1957 if (!intel_pmu_lbr_is_enabled(vcpu))
1958 debugctl &= ~DEBUGCTLMSR_LBR;
1959
1960 return debugctl;
1961}
1962
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963/*
Miaohe Lin311497e2019-12-11 14:26:25 +08001964 * Writes msr value into the appropriate "register".
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965 * Returns 0 on success, non-0 otherwise.
1966 * Assumes vcpu_load() was already called.
1967 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001968static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001970 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001971 struct vmx_uret_msr *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001972 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08001973 u32 msr_index = msr_info->index;
1974 u64 data = msr_info->data;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001975 u32 index;
Eddie Dong2cc51562007-05-21 07:28:09 +03001976
Avi Kivity6aa8b732006-12-10 02:21:36 -08001977 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001978 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08001979 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03001980 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001981#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001983 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984 vmcs_writel(GUEST_FS_BASE, data);
1985 break;
1986 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001987 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 vmcs_writel(GUEST_GS_BASE, data);
1989 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001990 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001991 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001992 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001993#endif
1994 case MSR_IA32_SYSENTER_CS:
Sean Christophersonde70d272019-05-07 09:06:36 -07001995 if (is_guest_mode(vcpu))
1996 get_vmcs12(vcpu)->guest_sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 vmcs_write32(GUEST_SYSENTER_CS, data);
1998 break;
1999 case MSR_IA32_SYSENTER_EIP:
Sean Christopherson24085002020-04-28 16:10:24 -07002000 if (is_guest_mode(vcpu)) {
2001 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002002 get_vmcs12(vcpu)->guest_sysenter_eip = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002003 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002004 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005 break;
2006 case MSR_IA32_SYSENTER_ESP:
Sean Christopherson24085002020-04-28 16:10:24 -07002007 if (is_guest_mode(vcpu)) {
2008 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002009 get_vmcs12(vcpu)->guest_sysenter_esp = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002010 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002011 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 break;
Like Xud8550662021-01-08 09:36:55 +08002013 case MSR_IA32_DEBUGCTLMSR: {
Like Xuc6462362021-02-01 13:10:31 +08002014 u64 invalid = data & ~vcpu_supported_debugctl(vcpu);
Like Xud8550662021-01-08 09:36:55 +08002015 if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) {
2016 if (report_ignored_msrs)
2017 vcpu_unimpl(vcpu, "%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n",
2018 __func__, data);
2019 data &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR);
2020 invalid &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR);
2021 }
2022
2023 if (invalid)
2024 return 1;
2025
Sean Christopherson699a1ac2019-05-07 09:06:37 -07002026 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
2027 VM_EXIT_SAVE_DEBUG_CONTROLS)
2028 get_vmcs12(vcpu)->guest_ia32_debugctl = data;
2029
Like Xud8550662021-01-08 09:36:55 +08002030 vmcs_write64(GUEST_IA32_DEBUGCTL, data);
2031 return 0;
2032 }
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002033 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08002034 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02002035 (!msr_info->host_initiated &&
2036 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002037 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002038 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07002039 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002041 vmcs_write64(GUEST_BNDCFGS, data);
2042 break;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08002043 case MSR_IA32_UMWAIT_CONTROL:
2044 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
2045 return 1;
2046
2047 /* The reserved bit 1 and non-32 bit [63:32] should be zero */
2048 if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32)))
2049 return 1;
2050
2051 vmx->msr_ia32_umwait_control = data;
2052 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002053 case MSR_IA32_SPEC_CTRL:
2054 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002055 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002056 return 1;
2057
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002058 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002059 return 1;
2060
2061 vmx->spec_ctrl = data;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002062 if (!data)
2063 break;
2064
2065 /*
2066 * For non-nested:
2067 * When it's written (to non-zero) for the first time, pass
2068 * it through.
2069 *
2070 * For nested:
2071 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002072 * nested_vmx_prepare_msr_bitmap. We should not touch the
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002073 * vmcs02.msr_bitmap here since it gets completely overwritten
2074 * in the merging. We update the vmcs01 here for L1 as well
2075 * since it will end up touching the MSR anyway now.
2076 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002077 vmx_disable_intercept_for_msr(vcpu,
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002078 MSR_IA32_SPEC_CTRL,
2079 MSR_TYPE_RW);
2080 break;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002081 case MSR_IA32_TSX_CTRL:
2082 if (!msr_info->host_initiated &&
2083 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
2084 return 1;
2085 if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR))
2086 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002087 goto find_uret_msr;
Ashok Raj15d45072018-02-01 22:59:43 +01002088 case MSR_IA32_PRED_CMD:
2089 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002090 !guest_has_pred_cmd_msr(vcpu))
Ashok Raj15d45072018-02-01 22:59:43 +01002091 return 1;
2092
2093 if (data & ~PRED_CMD_IBPB)
2094 return 1;
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002095 if (!boot_cpu_has(X86_FEATURE_IBPB))
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002096 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002097 if (!data)
2098 break;
2099
2100 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2101
2102 /*
2103 * For non-nested:
2104 * When it's written (to non-zero) for the first time, pass
2105 * it through.
2106 *
2107 * For nested:
2108 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002109 * nested_vmx_prepare_msr_bitmap. We should not touch the
Ashok Raj15d45072018-02-01 22:59:43 +01002110 * vmcs02.msr_bitmap here since it gets completely overwritten
2111 * in the merging.
2112 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002113 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W);
Ashok Raj15d45072018-02-01 22:59:43 +01002114 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002115 case MSR_IA32_CR_PAT:
Sean Christophersond28f4292019-05-07 09:06:27 -07002116 if (!kvm_pat_valid(data))
2117 return 1;
2118
Sean Christopherson142e4be2019-05-07 09:06:35 -07002119 if (is_guest_mode(vcpu) &&
2120 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
2121 get_vmcs12(vcpu)->guest_ia32_pat = data;
2122
Sheng Yang468d4722008-10-09 16:01:55 +08002123 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2124 vmcs_write64(GUEST_IA32_PAT, data);
2125 vcpu->arch.pat = data;
2126 break;
2127 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002128 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002129 break;
Will Auldba904632012-11-29 12:42:50 -08002130 case MSR_IA32_TSC_ADJUST:
2131 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002132 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002133 case MSR_IA32_MCG_EXT_CTL:
2134 if ((!msr_info->host_initiated &&
2135 !(to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002136 FEAT_CTL_LMCE_ENABLED)) ||
Ashok Rajc45dcc72016-06-22 14:59:56 +08002137 (data & ~MCG_EXT_CTL_LMCE_EN))
2138 return 1;
2139 vcpu->arch.mcg_ext_ctl = data;
2140 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002141 case MSR_IA32_FEAT_CTL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002142 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08002143 (to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002144 FEAT_CTL_LOCKED && !msr_info->host_initiated))
Jan Kiszkacae50132014-01-04 18:47:22 +01002145 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08002146 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002147 if (msr_info->host_initiated && data == 0)
2148 vmx_leave_nested(vcpu);
2149 break;
2150 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08002151 if (!msr_info->host_initiated)
2152 return 1; /* they are read-only */
2153 if (!nested_vmx_allowed(vcpu))
2154 return 1;
2155 return vmx_set_vmx_msr(vcpu, msr_index, data);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002156 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08002157 if (!vmx_pt_mode_is_host_guest() ||
Luwei Kangee85dec2018-10-24 16:05:16 +08002158 vmx_rtit_ctl_check(vcpu, data) ||
2159 vmx->nested.vmxon)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002160 return 1;
2161 vmcs_write64(GUEST_IA32_RTIT_CTL, data);
2162 vmx->pt_desc.guest.ctl = data;
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002163 pt_update_intercept_for_msr(vcpu);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002164 break;
2165 case MSR_IA32_RTIT_STATUS:
Sean Christophersone348ac72019-12-10 15:24:33 -08002166 if (!pt_can_write_msr(vmx))
2167 return 1;
2168 if (data & MSR_IA32_RTIT_STATUS_MASK)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002169 return 1;
2170 vmx->pt_desc.guest.status = data;
2171 break;
2172 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christophersone348ac72019-12-10 15:24:33 -08002173 if (!pt_can_write_msr(vmx))
2174 return 1;
2175 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2176 PT_CAP_cr3_filtering))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002177 return 1;
2178 vmx->pt_desc.guest.cr3_match = data;
2179 break;
2180 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christophersone348ac72019-12-10 15:24:33 -08002181 if (!pt_can_write_msr(vmx))
2182 return 1;
2183 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2184 PT_CAP_topa_output) &&
2185 !intel_pt_validate_cap(vmx->pt_desc.caps,
2186 PT_CAP_single_range_output))
2187 return 1;
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07002188 if (!pt_output_base_valid(vcpu, data))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002189 return 1;
2190 vmx->pt_desc.guest.output_base = data;
2191 break;
2192 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christophersone348ac72019-12-10 15:24:33 -08002193 if (!pt_can_write_msr(vmx))
2194 return 1;
2195 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2196 PT_CAP_topa_output) &&
2197 !intel_pt_validate_cap(vmx->pt_desc.caps,
2198 PT_CAP_single_range_output))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002199 return 1;
2200 vmx->pt_desc.guest.output_mask = data;
2201 break;
2202 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christophersone348ac72019-12-10 15:24:33 -08002203 if (!pt_can_write_msr(vmx))
2204 return 1;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002205 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christophersone348ac72019-12-10 15:24:33 -08002206 if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
2207 PT_CAP_num_address_ranges))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002208 return 1;
Sean Christophersonfe6ed362019-12-10 15:24:32 -08002209 if (is_noncanonical_address(data, vcpu))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002210 return 1;
2211 if (index % 2)
2212 vmx->pt_desc.guest.addr_b[index / 2] = data;
2213 else
2214 vmx->pt_desc.guest.addr_a[index / 2] = data;
2215 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002216 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002217 if (!msr_info->host_initiated &&
2218 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002219 return 1;
2220 /* Check reserved bit, higher 32 bits should be zero */
2221 if ((data >> 32) != 0)
2222 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002223 goto find_uret_msr;
Paolo Bonzini9c9520c2021-02-02 09:36:08 -05002224 case MSR_IA32_PERF_CAPABILITIES:
2225 if (data && !vcpu_to_pmu(vcpu)->version)
2226 return 1;
2227 if (data & PMU_CAP_LBR_FMT) {
2228 if ((data & PMU_CAP_LBR_FMT) !=
2229 (vmx_get_perf_capabilities() & PMU_CAP_LBR_FMT))
2230 return 1;
2231 if (!intel_pmu_lbr_is_compatible(vcpu))
2232 return 1;
2233 }
2234 ret = kvm_set_msr_common(vcpu, msr_info);
2235 break;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002236
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002238 find_uret_msr:
Sean Christophersond85a8032020-09-23 11:04:06 -07002239 msr = vmx_find_uret_msr(vmx, msr_index);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05002240 if (msr)
Sean Christopherson7bf662b2020-09-23 11:04:07 -07002241 ret = vmx_set_guest_uret_msr(vmx, msr, data);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05002242 else
2243 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 }
2245
Eddie Dong2cc51562007-05-21 07:28:09 +03002246 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247}
2248
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002249static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250{
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002251 unsigned long guest_owned_bits;
2252
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002253 kvm_register_mark_available(vcpu, reg);
2254
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002255 switch (reg) {
2256 case VCPU_REGS_RSP:
2257 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2258 break;
2259 case VCPU_REGS_RIP:
2260 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2261 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002262 case VCPU_EXREG_PDPTR:
2263 if (enable_ept)
2264 ept_save_pdptrs(vcpu);
2265 break;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07002266 case VCPU_EXREG_CR0:
2267 guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2268
2269 vcpu->arch.cr0 &= ~guest_owned_bits;
2270 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits;
2271 break;
Sean Christopherson34059c22019-09-27 14:45:23 -07002272 case VCPU_EXREG_CR3:
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002273 if (is_unrestricted_guest(vcpu) ||
2274 (enable_ept && is_paging(vcpu)))
Sean Christopherson34059c22019-09-27 14:45:23 -07002275 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2276 break;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002277 case VCPU_EXREG_CR4:
2278 guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2279
2280 vcpu->arch.cr4 &= ~guest_owned_bits;
2281 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits;
2282 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002283 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07002284 WARN_ON_ONCE(1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002285 break;
2286 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287}
2288
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289static __init int cpu_has_kvm_support(void)
2290{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002291 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292}
2293
2294static __init int vmx_disabled_by_bios(void)
2295{
Sean Christophersona4d0b2f2019-12-20 20:45:09 -08002296 return !boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2297 !boot_cpu_has(X86_FEATURE_VMX);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298}
2299
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002300static int kvm_cpu_vmxon(u64 vmxon_pointer)
Dongxiao Xu7725b892010-05-11 18:29:38 +08002301{
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002302 u64 msr;
2303
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002304 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002305 intel_pt_handle_vmx(1);
2306
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002307 asm_volatile_goto("1: vmxon %[vmxon_pointer]\n\t"
2308 _ASM_EXTABLE(1b, %l[fault])
2309 : : [vmxon_pointer] "m"(vmxon_pointer)
2310 : : fault);
2311 return 0;
2312
2313fault:
2314 WARN_ONCE(1, "VMXON faulted, MSR_IA32_FEAT_CTL (0x3a) = 0x%llx\n",
2315 rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr) ? 0xdeadbeef : msr);
2316 intel_pt_handle_vmx(0);
2317 cr4_clear_bits(X86_CR4_VMXE);
2318
2319 return -EFAULT;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002320}
2321
Radim Krčmář13a34e02014-08-28 15:13:03 +02002322static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323{
2324 int cpu = raw_smp_processor_id();
2325 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002326 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002328 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02002329 return -EBUSY;
2330
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002331 /*
2332 * This can happen if we hot-added a CPU but failed to allocate
2333 * VP assist page for it.
2334 */
2335 if (static_branch_unlikely(&enable_evmcs) &&
2336 !hv_get_vp_assist_page(cpu))
2337 return -EFAULT;
2338
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002339 r = kvm_cpu_vmxon(phys_addr);
2340 if (r)
2341 return r;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002342
David Hildenbrandfdf288b2017-08-24 20:51:29 +02002343 if (enable_ept)
2344 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02002345
2346 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347}
2348
Nadav Har'Eld462b812011-05-24 15:26:10 +03002349static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002350{
2351 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002352 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002353
Nadav Har'Eld462b812011-05-24 15:26:10 +03002354 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2355 loaded_vmcss_on_cpu_link)
2356 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002357}
2358
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002359
2360/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2361 * tricks.
2362 */
2363static void kvm_cpu_vmxoff(void)
2364{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002365 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002366
2367 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002368 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002369}
2370
Radim Krčmář13a34e02014-08-28 15:13:03 +02002371static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002373 vmclear_local_loaded_vmcss();
2374 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375}
2376
Sean Christopherson7a57c092020-03-12 11:04:16 -07002377/*
2378 * There is no X86_FEATURE for SGX yet, but anyway we need to query CPUID
2379 * directly instead of going through cpu_has(), to ensure KVM is trapping
2380 * ENCLS whenever it's supported in hardware. It does not matter whether
2381 * the host OS supports or has enabled SGX.
2382 */
2383static bool cpu_has_sgx(void)
2384{
2385 return cpuid_eax(0) >= 0x12 && (cpuid_eax(0x12) & BIT(0));
2386}
2387
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002388static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002389 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390{
2391 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002392 u32 ctl = ctl_min | ctl_opt;
2393
2394 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2395
2396 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2397 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2398
2399 /* Ensure minimum (required) set of control bits are supported. */
2400 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002401 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002402
2403 *result = ctl;
2404 return 0;
2405}
2406
Sean Christopherson7caaa712018-12-03 13:53:01 -08002407static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
2408 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002409{
2410 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002411 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002412 u32 _pin_based_exec_control = 0;
2413 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002414 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002415 u32 _vmexit_control = 0;
2416 u32 _vmentry_control = 0;
2417
Paolo Bonzini13893092018-02-26 13:40:09 +01002418 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05302419 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002420#ifdef CONFIG_X86_64
2421 CPU_BASED_CR8_LOAD_EXITING |
2422 CPU_BASED_CR8_STORE_EXITING |
2423#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002424 CPU_BASED_CR3_LOAD_EXITING |
2425 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08002426 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002427 CPU_BASED_MOV_DR_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08002428 CPU_BASED_USE_TSC_OFFSETTING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002429 CPU_BASED_MWAIT_EXITING |
2430 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002431 CPU_BASED_INVLPG_EXITING |
2432 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002433
Sheng Yangf78e0e22007-10-29 09:40:42 +08002434 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002435 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002436 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002437 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2438 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002439 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002440#ifdef CONFIG_X86_64
2441 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2442 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2443 ~CPU_BASED_CR8_STORE_EXITING;
2444#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002445 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002446 min2 = 0;
2447 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002448 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002449 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002450 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002451 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002452 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002453 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02002454 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002455 SECONDARY_EXEC_ENABLE_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002456 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002457 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002458 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08002459 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08002460 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02002461 SECONDARY_EXEC_RDSEED_EXITING |
2462 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08002463 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04002464 SECONDARY_EXEC_TSC_SCALING |
Tao Xue69e72fa2019-07-16 14:55:49 +08002465 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Chao Pengf99e3da2018-10-24 16:05:10 +08002466 SECONDARY_EXEC_PT_USE_GPA |
2467 SECONDARY_EXEC_PT_CONCEAL_VMX |
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08002468 SECONDARY_EXEC_ENABLE_VMFUNC |
2469 SECONDARY_EXEC_BUS_LOCK_DETECTION;
Sean Christopherson7a57c092020-03-12 11:04:16 -07002470 if (cpu_has_sgx())
2471 opt2 |= SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08002472 if (adjust_vmx_controls(min2, opt2,
2473 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002474 &_cpu_based_2nd_exec_control) < 0)
2475 return -EIO;
2476 }
2477#ifndef CONFIG_X86_64
2478 if (!(_cpu_based_2nd_exec_control &
2479 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2480 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2481#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002482
2483 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2484 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002485 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002486 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2487 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002488
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002489 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08002490 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002491
Sheng Yangd56f5462008-04-25 10:13:16 +08002492 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002493 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2494 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002495 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2496 CPU_BASED_CR3_STORE_EXITING |
2497 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08002498 } else if (vmx_cap->ept) {
2499 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002500 pr_warn_once("EPT CAP should not exist if not support "
2501 "1-setting enable EPT VM-execution control\n");
2502 }
2503 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002504 vmx_cap->vpid) {
2505 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002506 pr_warn_once("VPID CAP should not exist if not support "
2507 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002508 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002509
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002510 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002511#ifdef CONFIG_X86_64
2512 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2513#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002514 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002515 VM_EXIT_LOAD_IA32_PAT |
2516 VM_EXIT_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002517 VM_EXIT_CLEAR_BNDCFGS |
2518 VM_EXIT_PT_CONCEAL_PIP |
2519 VM_EXIT_CLEAR_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002520 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2521 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002522 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002523
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002524 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2525 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2526 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002527 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2528 &_pin_based_exec_control) < 0)
2529 return -EIO;
2530
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002531 if (cpu_has_broken_vmx_preemption_timer())
2532 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002533 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002534 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002535 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2536
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002537 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002538 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2539 VM_ENTRY_LOAD_IA32_PAT |
2540 VM_ENTRY_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002541 VM_ENTRY_LOAD_BNDCFGS |
2542 VM_ENTRY_PT_CONCEAL_PIP |
2543 VM_ENTRY_LOAD_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002544 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2545 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002546 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002548 /*
2549 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2550 * can't be used due to an errata where VM Exit may incorrectly clear
2551 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2552 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2553 */
2554 if (boot_cpu_data.x86 == 0x6) {
2555 switch (boot_cpu_data.x86_model) {
2556 case 26: /* AAK155 */
2557 case 30: /* AAP115 */
2558 case 37: /* AAT100 */
2559 case 44: /* BC86,AAY89,BD102 */
2560 case 46: /* BA97 */
Sean Christopherson85ba2b12019-01-14 12:12:02 -08002561 _vmentry_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002562 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2563 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2564 "does not work properly. Using workaround\n");
2565 break;
2566 default:
2567 break;
2568 }
2569 }
2570
2571
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002572 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002573
2574 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2575 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002576 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002577
2578#ifdef CONFIG_X86_64
2579 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2580 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002581 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002582#endif
2583
2584 /* Require Write-Back (WB) memory type for VMCS accesses. */
2585 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002586 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002587
Yang, Sheng002c7f72007-07-31 14:23:01 +03002588 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002589 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002590 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002591
Liran Alon2307af12018-06-29 22:59:04 +03002592 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002593
Yang, Sheng002c7f72007-07-31 14:23:01 +03002594 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2595 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002596 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002597 vmcs_conf->vmexit_ctrl = _vmexit_control;
2598 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002599
Vitaly Kuznetsov064eedf2020-10-14 16:33:46 +02002600#if IS_ENABLED(CONFIG_HYPERV)
2601 if (enlightened_vmcs)
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002602 evmcs_sanitize_exec_ctrls(vmcs_conf);
Vitaly Kuznetsov064eedf2020-10-14 16:33:46 +02002603#endif
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002604
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002605 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002606}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607
Ben Gardon41836832019-02-11 11:02:52 -08002608struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609{
2610 int node = cpu_to_node(cpu);
2611 struct page *pages;
2612 struct vmcs *vmcs;
2613
Ben Gardon41836832019-02-11 11:02:52 -08002614 pages = __alloc_pages_node(node, flags, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615 if (!pages)
2616 return NULL;
2617 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002618 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002619
2620 /* KVM supports Enlightened VMCS v1 only */
2621 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002622 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002623 else
Liran Alon392b2f22018-06-23 02:35:01 +03002624 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002625
Liran Alon491a6032018-06-23 02:35:12 +03002626 if (shadow)
2627 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 return vmcs;
2629}
2630
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002631void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002633 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634}
2635
Nadav Har'Eld462b812011-05-24 15:26:10 +03002636/*
2637 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2638 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002639void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002640{
2641 if (!loaded_vmcs->vmcs)
2642 return;
2643 loaded_vmcs_clear(loaded_vmcs);
2644 free_vmcs(loaded_vmcs->vmcs);
2645 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002646 if (loaded_vmcs->msr_bitmap)
2647 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002648 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002649}
2650
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002651int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002652{
Liran Alon491a6032018-06-23 02:35:12 +03002653 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002654 if (!loaded_vmcs->vmcs)
2655 return -ENOMEM;
2656
Sean Christophersond260f9e2020-03-21 12:37:50 -07002657 vmcs_clear(loaded_vmcs->vmcs);
2658
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002659 loaded_vmcs->shadow_vmcs = NULL;
Sean Christopherson804939e2019-05-07 12:18:05 -07002660 loaded_vmcs->hv_timer_soft_disabled = false;
Sean Christophersond260f9e2020-03-21 12:37:50 -07002661 loaded_vmcs->cpu = -1;
2662 loaded_vmcs->launched = 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002663
2664 if (cpu_has_vmx_msr_bitmap()) {
Ben Gardon41836832019-02-11 11:02:52 -08002665 loaded_vmcs->msr_bitmap = (unsigned long *)
2666 __get_free_page(GFP_KERNEL_ACCOUNT);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002667 if (!loaded_vmcs->msr_bitmap)
2668 goto out_vmcs;
2669 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002670
Arnd Bergmann1f008e12018-05-25 17:36:17 +02002671 if (IS_ENABLED(CONFIG_HYPERV) &&
2672 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002673 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2674 struct hv_enlightened_vmcs *evmcs =
2675 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2676
2677 evmcs->hv_enlightenments_control.msr_bitmap = 1;
2678 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002679 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002680
2681 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002682 memset(&loaded_vmcs->controls_shadow, 0,
2683 sizeof(struct vmcs_controls_shadow));
Sean Christophersond7ee0392018-07-23 12:32:47 -07002684
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002685 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002686
2687out_vmcs:
2688 free_loaded_vmcs(loaded_vmcs);
2689 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002690}
2691
Sam Ravnborg39959582007-06-01 00:47:13 -07002692static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693{
2694 int cpu;
2695
Zachary Amsden3230bb42009-09-29 11:38:37 -10002696 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002698 per_cpu(vmxarea, cpu) = NULL;
2699 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700}
2701
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702static __init int alloc_kvm_area(void)
2703{
2704 int cpu;
2705
Zachary Amsden3230bb42009-09-29 11:38:37 -10002706 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 struct vmcs *vmcs;
2708
Ben Gardon41836832019-02-11 11:02:52 -08002709 vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 if (!vmcs) {
2711 free_kvm_area();
2712 return -ENOMEM;
2713 }
2714
Liran Alon2307af12018-06-29 22:59:04 +03002715 /*
2716 * When eVMCS is enabled, alloc_vmcs_cpu() sets
2717 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
2718 * revision_id reported by MSR_IA32_VMX_BASIC.
2719 *
Linus Torvalds312a4662018-12-26 17:03:51 -08002720 * However, even though not explicitly documented by
Liran Alon2307af12018-06-29 22:59:04 +03002721 * TLFS, VMXArea passed as VMXON argument should
2722 * still be marked with revision_id reported by
2723 * physical CPU.
2724 */
2725 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002726 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002727
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 per_cpu(vmxarea, cpu) = vmcs;
2729 }
2730 return 0;
2731}
2732
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002733static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002734 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002736 if (!emulate_invalid_guest_state) {
2737 /*
2738 * CS and SS RPL should be equal during guest entry according
2739 * to VMX spec, but in reality it is not always so. Since vcpu
2740 * is in the middle of the transition from real mode to
2741 * protected mode it is safe to assume that RPL 0 is a good
2742 * default value.
2743 */
2744 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03002745 save->selector &= ~SEGMENT_RPL_MASK;
2746 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02002747 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002749 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750}
2751
2752static void enter_pmode(struct kvm_vcpu *vcpu)
2753{
2754 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002755 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756
Gleb Natapovd99e4152012-12-20 16:57:45 +02002757 /*
2758 * Update real mode segment cache. It may be not up-to-date if sement
2759 * register was written while vcpu was in a guest mode.
2760 */
2761 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2762 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2763 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2764 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2765 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2766 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2767
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002768 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002770 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771
2772 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002773 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2774 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 vmcs_writel(GUEST_RFLAGS, flags);
2776
Rusty Russell66aee912007-07-17 23:34:16 +10002777 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2778 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779
2780 update_exception_bitmap(vcpu);
2781
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002782 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2783 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2784 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2785 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2786 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2787 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788}
2789
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002790static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791{
Mathias Krause772e0312012-08-30 01:30:19 +02002792 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002793 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794
Gleb Natapovd99e4152012-12-20 16:57:45 +02002795 var.dpl = 0x3;
2796 if (seg == VCPU_SREG_CS)
2797 var.type = 0x3;
2798
2799 if (!emulate_invalid_guest_state) {
2800 var.selector = var.base >> 4;
2801 var.base = var.base & 0xffff0;
2802 var.limit = 0xffff;
2803 var.g = 0;
2804 var.db = 0;
2805 var.present = 1;
2806 var.s = 1;
2807 var.l = 0;
2808 var.unusable = 0;
2809 var.type = 0x3;
2810 var.avl = 0;
2811 if (save->base & 0xf)
2812 printk_once(KERN_WARNING "kvm: segment base is not "
2813 "paragraph aligned when entering "
2814 "protected mode (seg=%d)", seg);
2815 }
2816
2817 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05002818 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002819 vmcs_write32(sf->limit, var.limit);
2820 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821}
2822
2823static void enter_rmode(struct kvm_vcpu *vcpu)
2824{
2825 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002826 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002827 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002829 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2830 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2831 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2832 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2833 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002834 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2835 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002836
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002837 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838
Gleb Natapov776e58e2011-03-13 12:34:27 +02002839 /*
2840 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002841 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002842 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002843 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002844 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2845 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002846
Avi Kivity2fb92db2011-04-27 19:42:18 +03002847 vmx_segment_cache_clear(vmx);
2848
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002849 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2852
2853 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002854 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002856 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857
2858 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002859 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 update_exception_bitmap(vcpu);
2861
Gleb Natapovd99e4152012-12-20 16:57:45 +02002862 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2863 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2864 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2865 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2866 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2867 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002868
Eddie Dong8668a3c2007-10-10 14:26:45 +08002869 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870}
2871
Maxim Levitsky72f211e2020-10-01 14:29:53 +03002872int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Amit Shah401d10d2009-02-20 22:53:37 +05302873{
2874 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond85a8032020-09-23 11:04:06 -07002875 struct vmx_uret_msr *msr = vmx_find_uret_msr(vmx, MSR_EFER);
Avi Kivity26bb0982009-09-07 11:14:12 +03002876
Maxim Levitsky72f211e2020-10-01 14:29:53 +03002877 /* Nothing to do if hardware doesn't support EFER. */
Avi Kivity26bb0982009-09-07 11:14:12 +03002878 if (!msr)
Maxim Levitsky72f211e2020-10-01 14:29:53 +03002879 return 0;
Amit Shah401d10d2009-02-20 22:53:37 +05302880
Avi Kivityf6801df2010-01-21 15:31:50 +02002881 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302882 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002883 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302884 msr->data = efer;
2885 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002886 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302887
2888 msr->data = efer & ~EFER_LME;
2889 }
2890 setup_msrs(vmx);
Maxim Levitsky72f211e2020-10-01 14:29:53 +03002891 return 0;
Amit Shah401d10d2009-02-20 22:53:37 +05302892}
2893
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002894#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895
2896static void enter_lmode(struct kvm_vcpu *vcpu)
2897{
2898 u32 guest_tr_ar;
2899
Avi Kivity2fb92db2011-04-27 19:42:18 +03002900 vmx_segment_cache_clear(to_vmx(vcpu));
2901
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002903 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002904 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2905 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002907 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
2908 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 }
Avi Kivityda38f432010-07-06 11:30:49 +03002910 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002911}
2912
2913static void exit_lmode(struct kvm_vcpu *vcpu)
2914{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002915 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002916 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917}
2918
2919#endif
2920
Sean Christopherson77809382020-03-20 14:28:18 -07002921static void vmx_flush_tlb_all(struct kvm_vcpu *vcpu)
Sean Christopherson5058b692020-03-20 14:28:14 -07002922{
2923 struct vcpu_vmx *vmx = to_vmx(vcpu);
2924
2925 /*
Sean Christopherson77809382020-03-20 14:28:18 -07002926 * INVEPT must be issued when EPT is enabled, irrespective of VPID, as
2927 * the CPU is not required to invalidate guest-physical mappings on
2928 * VM-Entry, even if VPID is disabled. Guest-physical mappings are
2929 * associated with the root EPT structure and not any particular VPID
2930 * (INVVPID also isn't required to invalidate guest-physical mappings).
Sean Christopherson5058b692020-03-20 14:28:14 -07002931 */
2932 if (enable_ept) {
2933 ept_sync_global();
2934 } else if (enable_vpid) {
2935 if (cpu_has_vmx_invvpid_global()) {
2936 vpid_sync_vcpu_global();
2937 } else {
2938 vpid_sync_vcpu_single(vmx->vpid);
2939 vpid_sync_vcpu_single(vmx->nested.vpid02);
2940 }
2941 }
2942}
2943
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002944static void vmx_flush_tlb_current(struct kvm_vcpu *vcpu)
2945{
Sean Christopherson2a40b902020-07-15 20:41:18 -07002946 struct kvm_mmu *mmu = vcpu->arch.mmu;
2947 u64 root_hpa = mmu->root_hpa;
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002948
2949 /* No flush required if the current context is invalid. */
2950 if (!VALID_PAGE(root_hpa))
2951 return;
2952
2953 if (enable_ept)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002954 ept_sync_context(construct_eptp(vcpu, root_hpa,
2955 mmu->shadow_root_level));
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002956 else if (!is_guest_mode(vcpu))
2957 vpid_sync_context(to_vmx(vcpu)->vpid);
2958 else
2959 vpid_sync_context(nested_get_vpid02(vcpu));
2960}
2961
Junaid Shahidfaff8752018-06-29 13:10:05 -07002962static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
2963{
Junaid Shahidfaff8752018-06-29 13:10:05 -07002964 /*
Sean Christophersonad104b52020-03-20 14:28:11 -07002965 * vpid_sync_vcpu_addr() is a nop if vmx->vpid==0, see the comment in
2966 * vmx_flush_tlb_guest() for an explanation of why this is ok.
Junaid Shahidfaff8752018-06-29 13:10:05 -07002967 */
Sean Christophersonad104b52020-03-20 14:28:11 -07002968 vpid_sync_vcpu_addr(to_vmx(vcpu)->vpid, addr);
Junaid Shahidfaff8752018-06-29 13:10:05 -07002969}
2970
Sean Christophersone64419d2020-03-20 14:28:10 -07002971static void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu)
2972{
2973 /*
2974 * vpid_sync_context() is a nop if vmx->vpid==0, e.g. if enable_vpid==0
2975 * or a vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit
2976 * are required to flush GVA->{G,H}PA mappings from the TLB if vpid is
2977 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed),
2978 * i.e. no explicit INVVPID is necessary.
2979 */
2980 vpid_sync_context(to_vmx(vcpu)->vpid);
2981}
2982
Peter Shier43fea4e2020-08-20 16:05:45 -07002983void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08002984{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002985 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2986
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002987 if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002988 return;
2989
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02002990 if (is_pae_paging(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002991 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
2992 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
2993 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
2994 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002995 }
2996}
2997
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002998void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03002999{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003000 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3001
Sean Christopherson9932b492020-04-15 13:34:50 -07003002 if (WARN_ON_ONCE(!is_pae_paging(vcpu)))
3003 return;
3004
3005 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3006 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3007 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3008 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003009
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003010 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003011}
3012
Sheng Yang14394422008-04-28 12:24:45 +08003013static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3014 unsigned long cr0,
3015 struct kvm_vcpu *vcpu)
3016{
Sean Christopherson2183f562019-05-07 12:17:56 -07003017 struct vcpu_vmx *vmx = to_vmx(vcpu);
3018
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003019 if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
Sean Christopherson34059c22019-09-27 14:45:23 -07003020 vmx_cache_reg(vcpu, VCPU_EXREG_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003021 if (!(cr0 & X86_CR0_PG)) {
3022 /* From paging/starting to nonpaging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003023 exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3024 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08003025 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003026 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003027 } else if (!is_paging(vcpu)) {
3028 /* From nonpaging to paging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003029 exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3030 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08003031 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003032 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003033 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003034
3035 if (!(cr0 & X86_CR0_WP))
3036 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003037}
3038
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003039void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003041 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003042 unsigned long hw_cr0;
3043
Sean Christopherson3de63472018-07-13 08:42:30 -07003044 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003045 if (is_unrestricted_guest(vcpu))
Gleb Natapov50378782013-02-04 16:00:28 +02003046 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003047 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003048 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003049
Gleb Natapov218e7632013-01-21 15:36:45 +02003050 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3051 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052
Gleb Natapov218e7632013-01-21 15:36:45 +02003053 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3054 enter_rmode(vcpu);
3055 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003057#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003058 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003059 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003061 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062 exit_lmode(vcpu);
3063 }
3064#endif
3065
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003066 if (enable_ept && !is_unrestricted_guest(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003067 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3068
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003070 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003071 vcpu->arch.cr0 = cr0;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07003072 kvm_register_mark_available(vcpu, VCPU_EXREG_CR0);
Gleb Natapov14168782013-01-21 15:36:49 +02003073
3074 /* depends on vcpu->arch.cr0 to be set to a new value */
3075 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076}
3077
Sean Christophersond468d942020-07-15 20:41:20 -07003078static int vmx_get_max_tdp_level(void)
Sean Christopherson0047fca2020-05-01 21:32:33 -07003079{
Sean Christophersond468d942020-07-15 20:41:20 -07003080 if (cpu_has_vmx_ept_5levels())
Sean Christopherson0047fca2020-05-01 21:32:33 -07003081 return 5;
3082 return 4;
3083}
3084
Sean Christopherson2a40b902020-07-15 20:41:18 -07003085u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,
3086 int root_level)
Sheng Yang14394422008-04-28 12:24:45 +08003087{
Yu Zhang855feb62017-08-24 20:27:55 +08003088 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08003089
Sean Christopherson2a40b902020-07-15 20:41:18 -07003090 eptp |= (root_level == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08003091
Peter Feiner995f00a2017-06-30 17:26:32 -07003092 if (enable_ept_ad_bits &&
3093 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02003094 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003095 eptp |= (root_hpa & PAGE_MASK);
3096
3097 return eptp;
3098}
3099
Sean Christopherson2a40b902020-07-15 20:41:18 -07003100static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,
3101 int pgd_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102{
Tianyu Lan877ad952018-07-19 08:40:23 +00003103 struct kvm *kvm = vcpu->kvm;
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003104 bool update_guest_cr3 = true;
Sheng Yang14394422008-04-28 12:24:45 +08003105 unsigned long guest_cr3;
3106 u64 eptp;
3107
Avi Kivity089d0342009-03-23 18:26:32 +02003108 if (enable_ept) {
Sean Christopherson2a40b902020-07-15 20:41:18 -07003109 eptp = construct_eptp(vcpu, pgd, pgd_level);
Sheng Yang14394422008-04-28 12:24:45 +08003110 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00003111
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003112 if (kvm_x86_ops.tlb_remote_flush) {
Tianyu Lan877ad952018-07-19 08:40:23 +00003113 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3114 to_vmx(vcpu)->ept_pointer = eptp;
3115 to_kvm_vmx(kvm)->ept_pointers_match
3116 = EPT_POINTERS_CHECK;
3117 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3118 }
3119
Paolo Bonzinidf7e0682020-05-20 08:37:37 -04003120 if (!enable_unrestricted_guest && !is_paging(vcpu))
Tianyu Lan877ad952018-07-19 08:40:23 +00003121 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Sean Christophersonb17b7432019-09-27 14:45:17 -07003122 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3123 guest_cr3 = vcpu->arch.cr3;
3124 else /* vmcs01.GUEST_CR3 is already up-to-date. */
3125 update_guest_cr3 = false;
Peter Shier43fea4e2020-08-20 16:05:45 -07003126 vmx_ept_load_pdptrs(vcpu);
Sean Christophersonbe100ef2020-03-20 14:28:33 -07003127 } else {
3128 guest_cr3 = pgd;
Sheng Yang14394422008-04-28 12:24:45 +08003129 }
3130
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003131 if (update_guest_cr3)
3132 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133}
3134
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07003135static bool vmx_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
3136{
3137 /*
3138 * We operate under the default treatment of SMM, so VMX cannot be
3139 * enabled under SMM. Note, whether or not VMXE is allowed at all is
Sean Christophersonee69c922020-10-06 18:44:16 -07003140 * handled by kvm_is_valid_cr4().
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07003141 */
3142 if ((cr4 & X86_CR4_VMXE) && is_smm(vcpu))
3143 return false;
3144
3145 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
3146 return false;
3147
3148 return true;
3149}
3150
3151void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152{
Jim Mattson2259c172020-10-29 10:06:48 -07003153 unsigned long old_cr4 = vcpu->arch.cr4;
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003154 struct vcpu_vmx *vmx = to_vmx(vcpu);
Ben Serebrin085e68e2015-04-16 11:58:05 -07003155 /*
3156 * Pass through host's Machine Check Enable value to hw_cr4, which
3157 * is in force while we are in guest mode. Do not let guests control
3158 * this bit, even if host CR4.MCE == 0.
3159 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003160 unsigned long hw_cr4;
3161
3162 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003163 if (is_unrestricted_guest(vcpu))
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003164 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003165 else if (vmx->rmode.vm86_active)
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003166 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3167 else
3168 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003169
Sean Christopherson64f7a112018-04-30 10:01:06 -07003170 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3171 if (cr4 & X86_CR4_UMIP) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003172 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07003173 hw_cr4 &= ~X86_CR4_UMIP;
3174 } else if (!is_guest_mode(vcpu) ||
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003175 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
3176 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
3177 }
Sean Christopherson64f7a112018-04-30 10:01:06 -07003178 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02003179
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003180 vcpu->arch.cr4 = cr4;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07003181 kvm_register_mark_available(vcpu, VCPU_EXREG_CR4);
Sheng Yang14394422008-04-28 12:24:45 +08003182
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003183 if (!is_unrestricted_guest(vcpu)) {
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003184 if (enable_ept) {
3185 if (!is_paging(vcpu)) {
3186 hw_cr4 &= ~X86_CR4_PAE;
3187 hw_cr4 |= X86_CR4_PSE;
3188 } else if (!(cr4 & X86_CR4_PAE)) {
3189 hw_cr4 &= ~X86_CR4_PAE;
3190 }
3191 }
3192
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003193 /*
Huaitong Handdba2622016-03-22 16:51:15 +08003194 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3195 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3196 * to be manually disabled when guest switches to non-paging
3197 * mode.
3198 *
3199 * If !enable_unrestricted_guest, the CPU is always running
3200 * with CR0.PG=1 and CR4 needs to be modified.
3201 * If enable_unrestricted_guest, the CPU automatically
3202 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003203 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003204 if (!is_paging(vcpu))
3205 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3206 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003207
Sheng Yang14394422008-04-28 12:24:45 +08003208 vmcs_writel(CR4_READ_SHADOW, cr4);
3209 vmcs_writel(GUEST_CR4, hw_cr4);
Jim Mattson2259c172020-10-29 10:06:48 -07003210
3211 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
3212 kvm_update_cpuid_runtime(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213}
3214
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003215void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216{
Avi Kivitya9179492011-01-03 14:28:52 +02003217 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 u32 ar;
3219
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003220 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003221 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003222 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003223 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003224 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003225 var->base = vmx_read_guest_seg_base(vmx, seg);
3226 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3227 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003228 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003229 var->base = vmx_read_guest_seg_base(vmx, seg);
3230 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3231 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3232 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003233 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234 var->type = ar & 15;
3235 var->s = (ar >> 4) & 1;
3236 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003237 /*
3238 * Some userspaces do not preserve unusable property. Since usable
3239 * segment has to be present according to VMX spec we can use present
3240 * property to amend userspace bug by making unusable segment always
3241 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3242 * segment as unusable.
3243 */
3244 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 var->avl = (ar >> 12) & 1;
3246 var->l = (ar >> 13) & 1;
3247 var->db = (ar >> 14) & 1;
3248 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249}
3250
Avi Kivitya9179492011-01-03 14:28:52 +02003251static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3252{
Avi Kivitya9179492011-01-03 14:28:52 +02003253 struct kvm_segment s;
3254
3255 if (to_vmx(vcpu)->rmode.vm86_active) {
3256 vmx_get_segment(vcpu, &s, seg);
3257 return s.base;
3258 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003259 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003260}
3261
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003262int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003263{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003264 struct vcpu_vmx *vmx = to_vmx(vcpu);
3265
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003266 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003267 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003268 else {
3269 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003270 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003271 }
Avi Kivity69c73022011-03-07 15:26:44 +02003272}
3273
Avi Kivity653e3102007-05-07 10:55:37 +03003274static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276 u32 ar;
3277
Avi Kivityf0495f92012-06-07 17:06:10 +03003278 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003279 ar = 1 << 16;
3280 else {
3281 ar = var->type & 15;
3282 ar |= (var->s & 1) << 4;
3283 ar |= (var->dpl & 3) << 5;
3284 ar |= (var->present & 1) << 7;
3285 ar |= (var->avl & 1) << 12;
3286 ar |= (var->l & 1) << 13;
3287 ar |= (var->db & 1) << 14;
3288 ar |= (var->g & 1) << 15;
3289 }
Avi Kivity653e3102007-05-07 10:55:37 +03003290
3291 return ar;
3292}
3293
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003294void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03003295{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003296 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003297 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003298
Avi Kivity2fb92db2011-04-27 19:42:18 +03003299 vmx_segment_cache_clear(vmx);
3300
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003301 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3302 vmx->rmode.segs[seg] = *var;
3303 if (seg == VCPU_SREG_TR)
3304 vmcs_write16(sf->selector, var->selector);
3305 else if (var->s)
3306 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003307 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003308 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003309
Avi Kivity653e3102007-05-07 10:55:37 +03003310 vmcs_writel(sf->base, var->base);
3311 vmcs_write32(sf->limit, var->limit);
3312 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003313
3314 /*
3315 * Fix the "Accessed" bit in AR field of segment registers for older
3316 * qemu binaries.
3317 * IA32 arch specifies that at the time of processor reset the
3318 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003319 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003320 * state vmexit when "unrestricted guest" mode is turned on.
3321 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3322 * tree. Newer qemu binaries with that qemu fix would not need this
3323 * kvm hack.
3324 */
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003325 if (is_unrestricted_guest(vcpu) && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003326 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003327
Gleb Natapovf924d662012-12-12 19:10:55 +02003328 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003329
3330out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003331 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332}
3333
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3335{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003336 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003337
3338 *db = (ar >> 14) & 1;
3339 *l = (ar >> 13) & 1;
3340}
3341
Gleb Natapov89a27f42010-02-16 10:51:48 +02003342static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003344 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3345 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003346}
3347
Gleb Natapov89a27f42010-02-16 10:51:48 +02003348static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003349{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003350 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3351 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003352}
3353
Gleb Natapov89a27f42010-02-16 10:51:48 +02003354static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003355{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003356 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3357 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003358}
3359
Gleb Natapov89a27f42010-02-16 10:51:48 +02003360static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003362 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3363 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364}
3365
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003366static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3367{
3368 struct kvm_segment var;
3369 u32 ar;
3370
3371 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003372 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003373 if (seg == VCPU_SREG_CS)
3374 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003375 ar = vmx_segment_access_rights(&var);
3376
3377 if (var.base != (var.selector << 4))
3378 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003379 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003380 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003381 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003382 return false;
3383
3384 return true;
3385}
3386
3387static bool code_segment_valid(struct kvm_vcpu *vcpu)
3388{
3389 struct kvm_segment cs;
3390 unsigned int cs_rpl;
3391
3392 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003393 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003394
Avi Kivity1872a3f2009-01-04 23:26:52 +02003395 if (cs.unusable)
3396 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003397 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003398 return false;
3399 if (!cs.s)
3400 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003401 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003402 if (cs.dpl > cs_rpl)
3403 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003404 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003405 if (cs.dpl != cs_rpl)
3406 return false;
3407 }
3408 if (!cs.present)
3409 return false;
3410
3411 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3412 return true;
3413}
3414
3415static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3416{
3417 struct kvm_segment ss;
3418 unsigned int ss_rpl;
3419
3420 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003421 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003422
Avi Kivity1872a3f2009-01-04 23:26:52 +02003423 if (ss.unusable)
3424 return true;
3425 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003426 return false;
3427 if (!ss.s)
3428 return false;
3429 if (ss.dpl != ss_rpl) /* DPL != RPL */
3430 return false;
3431 if (!ss.present)
3432 return false;
3433
3434 return true;
3435}
3436
3437static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3438{
3439 struct kvm_segment var;
3440 unsigned int rpl;
3441
3442 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03003443 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003444
Avi Kivity1872a3f2009-01-04 23:26:52 +02003445 if (var.unusable)
3446 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003447 if (!var.s)
3448 return false;
3449 if (!var.present)
3450 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003451 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003452 if (var.dpl < rpl) /* DPL < RPL */
3453 return false;
3454 }
3455
3456 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3457 * rights flags
3458 */
3459 return true;
3460}
3461
3462static bool tr_valid(struct kvm_vcpu *vcpu)
3463{
3464 struct kvm_segment tr;
3465
3466 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3467
Avi Kivity1872a3f2009-01-04 23:26:52 +02003468 if (tr.unusable)
3469 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03003470 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003471 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003472 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003473 return false;
3474 if (!tr.present)
3475 return false;
3476
3477 return true;
3478}
3479
3480static bool ldtr_valid(struct kvm_vcpu *vcpu)
3481{
3482 struct kvm_segment ldtr;
3483
3484 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3485
Avi Kivity1872a3f2009-01-04 23:26:52 +02003486 if (ldtr.unusable)
3487 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03003488 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003489 return false;
3490 if (ldtr.type != 2)
3491 return false;
3492 if (!ldtr.present)
3493 return false;
3494
3495 return true;
3496}
3497
3498static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3499{
3500 struct kvm_segment cs, ss;
3501
3502 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3503 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3504
Nadav Amitb32a9912015-03-29 16:33:04 +03003505 return ((cs.selector & SEGMENT_RPL_MASK) ==
3506 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003507}
3508
3509/*
3510 * Check if guest state is valid. Returns true if valid, false if
3511 * not.
3512 * We assume that registers are always usable
3513 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07003514bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003515{
3516 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003517 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003518 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3519 return false;
3520 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3521 return false;
3522 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3523 return false;
3524 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3525 return false;
3526 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3527 return false;
3528 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3529 return false;
3530 } else {
3531 /* protected mode guest state checks */
3532 if (!cs_ss_rpl_check(vcpu))
3533 return false;
3534 if (!code_segment_valid(vcpu))
3535 return false;
3536 if (!stack_segment_valid(vcpu))
3537 return false;
3538 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3539 return false;
3540 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3541 return false;
3542 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3543 return false;
3544 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3545 return false;
3546 if (!tr_valid(vcpu))
3547 return false;
3548 if (!ldtr_valid(vcpu))
3549 return false;
3550 }
3551 /* TODO:
3552 * - Add checks on RIP
3553 * - Add checks on RFLAGS
3554 */
3555
3556 return true;
3557}
3558
Peter Xuff5a9832020-09-30 21:20:33 -04003559static int init_rmode_tss(struct kvm *kvm, void __user *ua)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003560{
Peter Xuff5a9832020-09-30 21:20:33 -04003561 const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
3562 u16 data;
3563 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564
Peter Xuff5a9832020-09-30 21:20:33 -04003565 for (i = 0; i < 3; i++) {
3566 if (__copy_to_user(ua + PAGE_SIZE * i, zero_page, PAGE_SIZE))
3567 return -EFAULT;
3568 }
3569
Izik Eidus195aefd2007-10-01 22:14:18 +02003570 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Peter Xuff5a9832020-09-30 21:20:33 -04003571 if (__copy_to_user(ua + TSS_IOPB_BASE_OFFSET, &data, sizeof(u16)))
3572 return -EFAULT;
3573
Izik Eidus195aefd2007-10-01 22:14:18 +02003574 data = ~0;
Peter Xuff5a9832020-09-30 21:20:33 -04003575 if (__copy_to_user(ua + RMODE_TSS_SIZE - 1, &data, sizeof(u8)))
3576 return -EFAULT;
3577
3578 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003579}
3580
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003581static int init_rmode_identity_map(struct kvm *kvm)
3582{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003583 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Peter Xu2a5755b2020-01-09 09:57:14 -05003584 int i, r = 0;
Peter Xuff5a9832020-09-30 21:20:33 -04003585 void __user *uaddr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003586 u32 tmp;
3587
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003588 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003589 mutex_lock(&kvm->slots_lock);
3590
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003591 if (likely(kvm_vmx->ept_identity_pagetable_done))
Peter Xu2a5755b2020-01-09 09:57:14 -05003592 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003593
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003594 if (!kvm_vmx->ept_identity_map_addr)
3595 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chena255d472014-09-16 18:41:58 +08003596
Peter Xuff5a9832020-09-30 21:20:33 -04003597 uaddr = __x86_set_memory_region(kvm,
3598 IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
3599 kvm_vmx->ept_identity_map_addr,
3600 PAGE_SIZE);
3601 if (IS_ERR(uaddr)) {
3602 r = PTR_ERR(uaddr);
Peter Xu2a5755b2020-01-09 09:57:14 -05003603 goto out;
Peter Xuff5a9832020-09-30 21:20:33 -04003604 }
Tang Chena255d472014-09-16 18:41:58 +08003605
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003606 /* Set up identity-mapping pagetable for EPT in real mode */
3607 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3608 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3609 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
Peter Xuff5a9832020-09-30 21:20:33 -04003610 if (__copy_to_user(uaddr + i * sizeof(tmp), &tmp, sizeof(tmp))) {
3611 r = -EFAULT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003612 goto out;
Peter Xuff5a9832020-09-30 21:20:33 -04003613 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003614 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003615 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003616
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003617out:
Tang Chena255d472014-09-16 18:41:58 +08003618 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003619 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003620}
3621
Avi Kivity6aa8b732006-12-10 02:21:36 -08003622static void seg_setup(int seg)
3623{
Mathias Krause772e0312012-08-30 01:30:19 +02003624 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003625 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003626
3627 vmcs_write16(sf->selector, 0);
3628 vmcs_writel(sf->base, 0);
3629 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003630 ar = 0x93;
3631 if (seg == VCPU_SREG_CS)
3632 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003633
3634 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635}
3636
Sheng Yangf78e0e22007-10-29 09:40:42 +08003637static int alloc_apic_access_page(struct kvm *kvm)
3638{
Xiao Guangrong44841412012-09-07 14:14:20 +08003639 struct page *page;
Peter Xuff5a9832020-09-30 21:20:33 -04003640 void __user *hva;
3641 int ret = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003642
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003643 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003644 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003645 goto out;
Peter Xuff5a9832020-09-30 21:20:33 -04003646 hva = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3647 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
3648 if (IS_ERR(hva)) {
3649 ret = PTR_ERR(hva);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003650 goto out;
Peter Xuff5a9832020-09-30 21:20:33 -04003651 }
Izik Eidus72dc67a2008-02-10 18:04:15 +02003652
Tang Chen73a6d942014-09-11 13:38:00 +08003653 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003654 if (is_error_page(page)) {
Peter Xuff5a9832020-09-30 21:20:33 -04003655 ret = -EFAULT;
Xiao Guangrong44841412012-09-07 14:14:20 +08003656 goto out;
3657 }
3658
Tang Chenc24ae0d2014-09-24 15:57:58 +08003659 /*
3660 * Do not pin the page in memory, so that memory hot-unplug
3661 * is able to migrate it.
3662 */
3663 put_page(page);
3664 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003665out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003666 mutex_unlock(&kvm->slots_lock);
Peter Xuff5a9832020-09-30 21:20:33 -04003667 return ret;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003668}
3669
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003670int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003671{
3672 int vpid;
3673
Avi Kivity919818a2009-03-23 18:01:29 +02003674 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003675 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003676 spin_lock(&vmx_vpid_lock);
3677 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003678 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003679 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003680 else
3681 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003682 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003683 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003684}
3685
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003686void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003687{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003688 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003689 return;
3690 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003691 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003692 spin_unlock(&vmx_vpid_lock);
3693}
3694
Alexander Graf3eb90012020-09-25 16:34:20 +02003695static void vmx_clear_msr_bitmap_read(ulong *msr_bitmap, u32 msr)
3696{
3697 int f = sizeof(unsigned long);
3698
3699 if (msr <= 0x1fff)
3700 __clear_bit(msr, msr_bitmap + 0x000 / f);
3701 else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff))
3702 __clear_bit(msr & 0x1fff, msr_bitmap + 0x400 / f);
3703}
3704
3705static void vmx_clear_msr_bitmap_write(ulong *msr_bitmap, u32 msr)
3706{
3707 int f = sizeof(unsigned long);
3708
3709 if (msr <= 0x1fff)
3710 __clear_bit(msr, msr_bitmap + 0x800 / f);
3711 else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff))
3712 __clear_bit(msr & 0x1fff, msr_bitmap + 0xc00 / f);
3713}
3714
3715static void vmx_set_msr_bitmap_read(ulong *msr_bitmap, u32 msr)
3716{
3717 int f = sizeof(unsigned long);
3718
3719 if (msr <= 0x1fff)
3720 __set_bit(msr, msr_bitmap + 0x000 / f);
3721 else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff))
3722 __set_bit(msr & 0x1fff, msr_bitmap + 0x400 / f);
3723}
3724
3725static void vmx_set_msr_bitmap_write(ulong *msr_bitmap, u32 msr)
3726{
3727 int f = sizeof(unsigned long);
3728
3729 if (msr <= 0x1fff)
3730 __set_bit(msr, msr_bitmap + 0x800 / f);
3731 else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff))
3732 __set_bit(msr & 0x1fff, msr_bitmap + 0xc00 / f);
3733}
3734
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003735static __always_inline void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003736 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003737{
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003738 struct vcpu_vmx *vmx = to_vmx(vcpu);
3739 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
Sheng Yang25c5f222008-03-28 13:18:56 +08003740
3741 if (!cpu_has_vmx_msr_bitmap())
3742 return;
3743
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003744 if (static_branch_unlikely(&enable_evmcs))
3745 evmcs_touch_msr_bitmap();
3746
Sheng Yang25c5f222008-03-28 13:18:56 +08003747 /*
Alexander Graf3eb90012020-09-25 16:34:20 +02003748 * Mark the desired intercept state in shadow bitmap, this is needed
3749 * for resync when the MSR filters change.
3750 */
3751 if (is_valid_passthrough_msr(msr)) {
3752 int idx = possible_passthrough_msr_slot(msr);
Yang Zhang8d146952013-01-25 10:18:50 +08003753
Alexander Graf3eb90012020-09-25 16:34:20 +02003754 if (idx != -ENOENT) {
3755 if (type & MSR_TYPE_R)
3756 clear_bit(idx, vmx->shadow_msr_intercept.read);
3757 if (type & MSR_TYPE_W)
3758 clear_bit(idx, vmx->shadow_msr_intercept.write);
3759 }
Yang Zhang8d146952013-01-25 10:18:50 +08003760 }
Alexander Graf3eb90012020-09-25 16:34:20 +02003761
3762 if ((type & MSR_TYPE_R) &&
3763 !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) {
3764 vmx_set_msr_bitmap_read(msr_bitmap, msr);
3765 type &= ~MSR_TYPE_R;
3766 }
3767
3768 if ((type & MSR_TYPE_W) &&
3769 !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) {
3770 vmx_set_msr_bitmap_write(msr_bitmap, msr);
3771 type &= ~MSR_TYPE_W;
3772 }
3773
3774 if (type & MSR_TYPE_R)
3775 vmx_clear_msr_bitmap_read(msr_bitmap, msr);
3776
3777 if (type & MSR_TYPE_W)
3778 vmx_clear_msr_bitmap_write(msr_bitmap, msr);
Yang Zhang8d146952013-01-25 10:18:50 +08003779}
3780
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003781static __always_inline void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003782 u32 msr, int type)
3783{
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003784 struct vcpu_vmx *vmx = to_vmx(vcpu);
3785 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003786
3787 if (!cpu_has_vmx_msr_bitmap())
3788 return;
3789
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003790 if (static_branch_unlikely(&enable_evmcs))
3791 evmcs_touch_msr_bitmap();
3792
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003793 /*
Alexander Graf3eb90012020-09-25 16:34:20 +02003794 * Mark the desired intercept state in shadow bitmap, this is needed
3795 * for resync when the MSR filter changes.
3796 */
3797 if (is_valid_passthrough_msr(msr)) {
3798 int idx = possible_passthrough_msr_slot(msr);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003799
Alexander Graf3eb90012020-09-25 16:34:20 +02003800 if (idx != -ENOENT) {
3801 if (type & MSR_TYPE_R)
3802 set_bit(idx, vmx->shadow_msr_intercept.read);
3803 if (type & MSR_TYPE_W)
3804 set_bit(idx, vmx->shadow_msr_intercept.write);
3805 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003806 }
Alexander Graf3eb90012020-09-25 16:34:20 +02003807
3808 if (type & MSR_TYPE_R)
3809 vmx_set_msr_bitmap_read(msr_bitmap, msr);
3810
3811 if (type & MSR_TYPE_W)
3812 vmx_set_msr_bitmap_write(msr_bitmap, msr);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003813}
3814
Like Xu252e3652021-02-01 13:10:29 +08003815void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu,
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003816 u32 msr, int type, bool value)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003817{
3818 if (value)
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003819 vmx_enable_intercept_for_msr(vcpu, msr, type);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003820 else
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003821 vmx_disable_intercept_for_msr(vcpu, msr, type);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003822}
3823
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003824static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003825{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003826 u8 mode = 0;
3827
3828 if (cpu_has_secondary_exec_ctrls() &&
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003829 (secondary_exec_controls_get(to_vmx(vcpu)) &
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003830 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3831 mode |= MSR_BITMAP_MODE_X2APIC;
3832 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3833 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3834 }
3835
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003836 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003837}
3838
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003839static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003840{
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003841 unsigned long *msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
3842 unsigned long read_intercept;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003843 int msr;
3844
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003845 read_intercept = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003846
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003847 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3848 unsigned int read_idx = msr / BITS_PER_LONG;
3849 unsigned int write_idx = read_idx + (0x800 / sizeof(long));
3850
3851 msr_bitmap[read_idx] = read_intercept;
3852 msr_bitmap[write_idx] = ~0ul;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003853 }
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003854}
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003855
Sean Christopherson9389b9d2020-10-05 12:55:32 -07003856static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu, u8 mode)
3857{
3858 if (!cpu_has_vmx_msr_bitmap())
3859 return;
3860
3861 vmx_reset_x2apic_msrs(vcpu, mode);
3862
3863 /*
3864 * TPR reads and writes can be virtualized even if virtual interrupt
3865 * delivery is not in use.
3866 */
3867 vmx_set_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW,
3868 !(mode & MSR_BITMAP_MODE_X2APIC));
3869
3870 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
3871 vmx_enable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_RW);
3872 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3873 vmx_disable_intercept_for_msr(vcpu, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003874 }
3875}
3876
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003877void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003878{
3879 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003880 u8 mode = vmx_msr_bitmap_mode(vcpu);
3881 u8 changed = mode ^ vmx->msr_bitmap_mode;
3882
3883 if (!changed)
3884 return;
3885
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003886 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
Alexander Graf3eb90012020-09-25 16:34:20 +02003887 vmx_update_msr_bitmap_x2apic(vcpu, mode);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003888
3889 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003890}
3891
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003892void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu)
Chao Pengb08c2892018-10-24 16:05:15 +08003893{
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003894 struct vcpu_vmx *vmx = to_vmx(vcpu);
Chao Pengb08c2892018-10-24 16:05:15 +08003895 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
3896 u32 i;
3897
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003898 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_STATUS, MSR_TYPE_RW, flag);
3899 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_BASE, MSR_TYPE_RW, flag);
3900 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_OUTPUT_MASK, MSR_TYPE_RW, flag);
3901 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_CR3_MATCH, MSR_TYPE_RW, flag);
Chao Pengb08c2892018-10-24 16:05:15 +08003902 for (i = 0; i < vmx->pt_desc.addr_range; i++) {
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003903 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
3904 vmx_set_intercept_for_msr(vcpu, MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
Chao Pengb08c2892018-10-24 16:05:15 +08003905 }
3906}
3907
Liran Alone6c67d82018-09-04 10:56:52 +03003908static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3909{
3910 struct vcpu_vmx *vmx = to_vmx(vcpu);
3911 void *vapic_page;
3912 u32 vppr;
3913 int rvi;
3914
3915 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3916 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003917 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
Liran Alone6c67d82018-09-04 10:56:52 +03003918 return false;
3919
Paolo Bonzini7e712682018-10-03 13:44:26 +02003920 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003921
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003922 vapic_page = vmx->nested.virtual_apic_map.hva;
Liran Alone6c67d82018-09-04 10:56:52 +03003923 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
Liran Alone6c67d82018-09-04 10:56:52 +03003924
3925 return ((rvi & 0xf0) > (vppr & 0xf0));
3926}
3927
Alexander Graf3eb90012020-09-25 16:34:20 +02003928static void vmx_msr_filter_changed(struct kvm_vcpu *vcpu)
3929{
3930 struct vcpu_vmx *vmx = to_vmx(vcpu);
3931 u32 i;
3932
3933 /*
3934 * Set intercept permissions for all potentially passed through MSRs
3935 * again. They will automatically get filtered through the MSR filter,
3936 * so we are back in sync after this.
3937 */
3938 for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++) {
3939 u32 msr = vmx_possible_passthrough_msrs[i];
3940 bool read = test_bit(i, vmx->shadow_msr_intercept.read);
3941 bool write = test_bit(i, vmx->shadow_msr_intercept.write);
3942
3943 vmx_set_intercept_for_msr(vcpu, msr, MSR_TYPE_R, read);
3944 vmx_set_intercept_for_msr(vcpu, msr, MSR_TYPE_W, write);
3945 }
3946
3947 pt_update_intercept_for_msr(vcpu);
3948 vmx_update_msr_bitmap_x2apic(vcpu, vmx_msr_bitmap_mode(vcpu));
3949}
3950
Wincy Van06a55242017-04-28 13:13:59 +08003951static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
3952 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003953{
3954#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08003955 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
3956
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003957 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08003958 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003959 * The vector of interrupt to be delivered to vcpu had
3960 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08003961 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003962 * Following cases will be reached in this block, and
3963 * we always send a notification event in all cases as
3964 * explained below.
3965 *
3966 * Case 1: vcpu keeps in non-root mode. Sending a
3967 * notification event posts the interrupt to vcpu.
3968 *
3969 * Case 2: vcpu exits to root mode and is still
3970 * runnable. PIR will be synced to vIRR before the
3971 * next vcpu entry. Sending a notification event in
3972 * this case has no effect, as vcpu is not in root
3973 * mode.
3974 *
3975 * Case 3: vcpu exits to root mode and is blocked.
3976 * vcpu_block() has already synced PIR to vIRR and
3977 * never blocks vcpu if vIRR is not cleared. Therefore,
3978 * a blocked vcpu here does not wait for any requested
3979 * interrupts in PIR, and sending a notification event
3980 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08003981 */
Feng Wu28b835d2015-09-18 22:29:54 +08003982
Wincy Van06a55242017-04-28 13:13:59 +08003983 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003984 return true;
3985 }
3986#endif
3987 return false;
3988}
3989
Wincy Van705699a2015-02-03 23:58:17 +08003990static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
3991 int vector)
3992{
3993 struct vcpu_vmx *vmx = to_vmx(vcpu);
3994
3995 if (is_guest_mode(vcpu) &&
3996 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08003997 /*
3998 * If a posted intr is not recognized by hardware,
3999 * we will accomplish it in the next vmentry.
4000 */
4001 vmx->nested.pi_pending = true;
4002 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02004003 /* the PIR and ON have been set by L1. */
4004 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
4005 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004006 return 0;
4007 }
4008 return -1;
4009}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004010/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004011 * Send interrupt to vcpu via posted interrupt way.
4012 * 1. If target vcpu is running(non-root mode), send posted interrupt
4013 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4014 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4015 * interrupt from PIR in next vmentry.
4016 */
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004017static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
Yang Zhanga20ed542013-04-11 19:25:15 +08004018{
4019 struct vcpu_vmx *vmx = to_vmx(vcpu);
4020 int r;
4021
Wincy Van705699a2015-02-03 23:58:17 +08004022 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4023 if (!r)
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004024 return 0;
4025
4026 if (!vcpu->arch.apicv_active)
4027 return -1;
Wincy Van705699a2015-02-03 23:58:17 +08004028
Yang Zhanga20ed542013-04-11 19:25:15 +08004029 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004030 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08004031
Paolo Bonzinib95234c2016-12-19 13:57:33 +01004032 /* If a previous notification has sent the IPI, nothing to do. */
4033 if (pi_test_and_set_on(&vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004034 return 0;
Paolo Bonzinib95234c2016-12-19 13:57:33 +01004035
Wanpeng Li379a3c82020-04-28 14:23:27 +08004036 if (vcpu != kvm_get_running_vcpu() &&
4037 !kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08004038 kvm_vcpu_kick(vcpu);
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01004039
4040 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08004041}
4042
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004044 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4045 * will not change in the lifetime of the guest.
4046 * Note that host-state that does change is set elsewhere. E.g., host-state
4047 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4048 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004049void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004050{
4051 u32 low32, high32;
4052 unsigned long tmpl;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004053 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004054
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07004055 cr0 = read_cr0();
4056 WARN_ON(cr0 & X86_CR0_TS);
4057 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004058
4059 /*
4060 * Save the most likely value for this task's CR3 in the VMCS.
4061 * We can't use __get_current_cr3_fast() because we're not atomic.
4062 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07004063 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004064 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07004065 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004066
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004067 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004068 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004069 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07004070 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004071
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004072 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004073#ifdef CONFIG_X86_64
4074 /*
4075 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07004076 * vmx_prepare_switch_to_host(), in case userspace uses
4077 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03004078 */
4079 vmcs_write16(HOST_DS_SELECTOR, 0);
4080 vmcs_write16(HOST_ES_SELECTOR, 0);
4081#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004082 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4083 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004084#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004085 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4086 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4087
Sean Christopherson23420802019-04-19 22:50:57 -07004088 vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004089
Sean Christopherson453eafb2018-12-20 12:25:17 -08004090 vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004091
4092 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4093 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4094 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4095 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4096
4097 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4098 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4099 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4100 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004101
Sean Christophersonc73da3f2018-12-03 13:53:00 -08004102 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004103 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004104}
4105
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004106void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004107{
Sean Christopherson2ed41aa2020-09-29 21:16:58 -07004108 struct kvm_vcpu *vcpu = &vmx->vcpu;
4109
4110 vcpu->arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS &
4111 ~vcpu->arch.cr4_guest_rsvd_bits;
Sean Christophersonfa71e952020-07-02 21:04:22 -07004112 if (!enable_ept)
Sean Christopherson2ed41aa2020-09-29 21:16:58 -07004113 vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004114 if (is_guest_mode(&vmx->vcpu))
Sean Christopherson2ed41aa2020-09-29 21:16:58 -07004115 vcpu->arch.cr4_guest_owned_bits &=
4116 ~get_vmcs12(vcpu)->cr4_guest_host_mask;
4117 vmcs_writel(CR4_GUEST_HOST_MASK, ~vcpu->arch.cr4_guest_owned_bits);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004118}
4119
Sean Christophersonc075c3e2019-05-07 12:17:53 -07004120u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
Yang Zhang01e439b2013-04-11 19:25:12 +08004121{
4122 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4123
Andrey Smetanind62caab2015-11-10 15:36:33 +03004124 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004125 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004126
4127 if (!enable_vnmi)
4128 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
4129
Sean Christopherson804939e2019-05-07 12:18:05 -07004130 if (!enable_preemption_timer)
4131 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4132
Yang Zhang01e439b2013-04-11 19:25:12 +08004133 return pin_based_exec_ctrl;
4134}
4135
Andrey Smetanind62caab2015-11-10 15:36:33 +03004136static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4137{
4138 struct vcpu_vmx *vmx = to_vmx(vcpu);
4139
Sean Christophersonc5f2c762019-05-07 12:17:55 -07004140 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004141 if (cpu_has_secondary_exec_ctrls()) {
4142 if (kvm_vcpu_apicv_active(vcpu))
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004143 secondary_exec_controls_setbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004144 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4145 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4146 else
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004147 secondary_exec_controls_clearbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004148 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4149 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4150 }
4151
4152 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004153 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004154}
4155
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004156u32 vmx_exec_control(struct vcpu_vmx *vmx)
4157{
4158 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4159
4160 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4161 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4162
4163 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4164 exec_control &= ~CPU_BASED_TPR_SHADOW;
4165#ifdef CONFIG_X86_64
4166 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4167 CPU_BASED_CR8_LOAD_EXITING;
4168#endif
4169 }
4170 if (!enable_ept)
4171 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4172 CPU_BASED_CR3_LOAD_EXITING |
4173 CPU_BASED_INVLPG_EXITING;
4174 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4175 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4176 CPU_BASED_MONITOR_EXITING);
4177 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4178 exec_control &= ~CPU_BASED_HLT_EXITING;
4179 return exec_control;
4180}
4181
Sean Christopherson8b50b922020-09-24 17:30:11 -07004182/*
4183 * Adjust a single secondary execution control bit to intercept/allow an
4184 * instruction in the guest. This is usually done based on whether or not a
4185 * feature has been exposed to the guest in order to correctly emulate faults.
4186 */
4187static inline void
4188vmx_adjust_secondary_exec_control(struct vcpu_vmx *vmx, u32 *exec_control,
4189 u32 control, bool enabled, bool exiting)
4190{
4191 /*
4192 * If the control is for an opt-in feature, clear the control if the
4193 * feature is not exposed to the guest, i.e. not enabled. If the
4194 * control is opt-out, i.e. an exiting control, clear the control if
4195 * the feature _is_ exposed to the guest, i.e. exiting/interception is
4196 * disabled for the associated instruction. Note, the caller is
4197 * responsible presetting exec_control to set all supported bits.
4198 */
4199 if (enabled == exiting)
4200 *exec_control &= ~control;
4201
4202 /*
4203 * Update the nested MSR settings so that a nested VMM can/can't set
4204 * controls for features that are/aren't exposed to the guest.
4205 */
4206 if (nested) {
4207 if (enabled)
4208 vmx->nested.msrs.secondary_ctls_high |= control;
4209 else
4210 vmx->nested.msrs.secondary_ctls_high &= ~control;
4211 }
4212}
4213
4214/*
4215 * Wrapper macro for the common case of adjusting a secondary execution control
4216 * based on a single guest CPUID bit, with a dedicated feature bit. This also
4217 * verifies that the control is actually supported by KVM and hardware.
4218 */
4219#define vmx_adjust_sec_exec_control(vmx, exec_control, name, feat_name, ctrl_name, exiting) \
4220({ \
4221 bool __enabled; \
4222 \
4223 if (cpu_has_vmx_##name()) { \
4224 __enabled = guest_cpuid_has(&(vmx)->vcpu, \
4225 X86_FEATURE_##feat_name); \
4226 vmx_adjust_secondary_exec_control(vmx, exec_control, \
4227 SECONDARY_EXEC_##ctrl_name, __enabled, exiting); \
4228 } \
4229})
4230
4231/* More macro magic for ENABLE_/opt-in versus _EXITING/opt-out controls. */
4232#define vmx_adjust_sec_exec_feature(vmx, exec_control, lname, uname) \
4233 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, ENABLE_##uname, false)
4234
4235#define vmx_adjust_sec_exec_exiting(vmx, exec_control, lname, uname) \
4236 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, uname##_EXITING, true)
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004237
Paolo Bonzini80154d72017-08-24 13:55:35 +02004238static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004239{
Paolo Bonzini80154d72017-08-24 13:55:35 +02004240 struct kvm_vcpu *vcpu = &vmx->vcpu;
4241
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004242 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004243
Sean Christopherson2ef76192020-03-02 15:56:22 -08004244 if (vmx_pt_mode_is_system())
Chao Pengf99e3da2018-10-24 16:05:10 +08004245 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004246 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004247 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4248 if (vmx->vpid == 0)
4249 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4250 if (!enable_ept) {
4251 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4252 enable_unrestricted_guest = 0;
4253 }
4254 if (!enable_unrestricted_guest)
4255 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004256 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004257 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02004258 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004259 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4260 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004261 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004262
4263 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4264 * in vmx_set_cr4. */
4265 exec_control &= ~SECONDARY_EXEC_DESC;
4266
Abel Gordonabc4fc52013-04-18 14:35:25 +03004267 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4268 (handle_vmptrld).
4269 We can NOT enable shadow_vmcs here because we don't have yet
4270 a current VMCS12
4271 */
4272 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004273
4274 if (!enable_pml)
4275 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004276
Sean Christophersonbecdad82020-09-23 09:50:45 -07004277 if (cpu_has_vmx_xsaves()) {
Paolo Bonzini3db13482017-08-24 14:48:03 +02004278 /* Exposing XSAVES only when XSAVE is exposed */
4279 bool xsaves_enabled =
Sean Christopherson96be4e02019-12-10 14:44:15 -08004280 boot_cpu_has(X86_FEATURE_XSAVE) &&
Paolo Bonzini3db13482017-08-24 14:48:03 +02004281 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4282 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4283
Aaron Lewis72041602019-10-21 16:30:20 -07004284 vcpu->arch.xsaves_enabled = xsaves_enabled;
4285
Sean Christopherson8b50b922020-09-24 17:30:11 -07004286 vmx_adjust_secondary_exec_control(vmx, &exec_control,
4287 SECONDARY_EXEC_XSAVES,
4288 xsaves_enabled, false);
Paolo Bonzini3db13482017-08-24 14:48:03 +02004289 }
4290
Sean Christopherson8b50b922020-09-24 17:30:11 -07004291 vmx_adjust_sec_exec_feature(vmx, &exec_control, rdtscp, RDTSCP);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004292
Sean Christophersonb936d3e2020-09-23 09:50:46 -07004293 /*
4294 * Expose INVPCID if and only if PCID is also exposed to the guest.
4295 * INVPCID takes a #UD when it's disabled in the VMCS, but a #GP or #PF
4296 * if CR4.PCIDE=0. Enumerating CPUID.INVPCID=1 would lead to incorrect
4297 * behavior from the guest perspective (it would expect #GP or #PF).
4298 */
4299 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
4300 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
Sean Christopherson8b50b922020-09-24 17:30:11 -07004301 vmx_adjust_sec_exec_feature(vmx, &exec_control, invpcid, INVPCID);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004302
Paolo Bonzini80154d72017-08-24 13:55:35 +02004303
Sean Christopherson8b50b922020-09-24 17:30:11 -07004304 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdrand, RDRAND);
4305 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdseed, RDSEED);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004306
Sean Christopherson8b50b922020-09-24 17:30:11 -07004307 vmx_adjust_sec_exec_control(vmx, &exec_control, waitpkg, WAITPKG,
4308 ENABLE_USR_WAIT_PAUSE, false);
Tao Xue69e72fa2019-07-16 14:55:49 +08004309
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08004310 if (!vcpu->kvm->arch.bus_lock_detection_enabled)
4311 exec_control &= ~SECONDARY_EXEC_BUS_LOCK_DETECTION;
4312
Paolo Bonzini80154d72017-08-24 13:55:35 +02004313 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004314}
4315
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004316static void ept_set_mmio_spte_mask(void)
4317{
4318 /*
4319 * EPT Misconfigurations can be generated if the value of bits 2:0
4320 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004321 */
Paolo Bonzinie7581ca2020-05-19 05:04:49 -04004322 kvm_mmu_set_mmio_spte_mask(VMX_EPT_MISCONFIG_WX_VALUE, 0);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004323}
4324
Wanpeng Lif53cd632014-12-02 19:14:58 +08004325#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08004326
Sean Christopherson944c3462018-12-03 13:53:09 -08004327/*
Xiaoyao Li1b842922019-10-20 17:11:01 +08004328 * Noting that the initialization of Guest-state Area of VMCS is in
4329 * vmx_vcpu_reset().
Sean Christopherson944c3462018-12-03 13:53:09 -08004330 */
Xiaoyao Li1b842922019-10-20 17:11:01 +08004331static void init_vmcs(struct vcpu_vmx *vmx)
Sean Christopherson944c3462018-12-03 13:53:09 -08004332{
Sean Christopherson944c3462018-12-03 13:53:09 -08004333 if (nested)
Xiaoyao Li1b842922019-10-20 17:11:01 +08004334 nested_vmx_set_vmcs_shadowing_bitmap();
Sean Christopherson944c3462018-12-03 13:53:09 -08004335
Sheng Yang25c5f222008-03-28 13:18:56 +08004336 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004337 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08004338
Avi Kivity6aa8b732006-12-10 02:21:36 -08004339 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4340
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341 /* Control */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004342 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004343
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004344 exec_controls_set(vmx, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004345
Dan Williamsdfa169b2016-06-02 11:17:24 -07004346 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004347 vmx_compute_secondary_exec_control(vmx);
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004348 secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07004349 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004350
Andrey Smetanind62caab2015-11-10 15:36:33 +03004351 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004352 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4353 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4354 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4355 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4356
4357 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004358
Li RongQing0bcf2612015-12-03 13:29:34 +08004359 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004360 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004361 }
4362
Wanpeng Lib31c1142018-03-12 04:53:04 -07004363 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004364 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004365 vmx->ple_window = ple_window;
4366 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004367 }
4368
Xiao Guangrongc3707952011-07-12 03:28:04 +08004369 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4370 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4372
Avi Kivity9581d442010-10-19 16:46:55 +02004373 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4374 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004375 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004376 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4377 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378
Bandan Das2a499e42017-08-03 15:54:41 -04004379 if (cpu_has_vmx_vmfunc())
4380 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4381
Eddie Dong2cc51562007-05-21 07:28:09 +03004382 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4383 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004384 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03004385 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004386 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387
Radim Krčmář74545702015-04-27 15:11:25 +02004388 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4389 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004390
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004391 vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392
4393 /* 22.2.1, 20.8.1 */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004394 vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004395
Sean Christophersonfa71e952020-07-02 21:04:22 -07004396 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4397 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004398
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004399 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004400
Xiaoyao Li35fbe0d2019-10-20 17:10:58 +08004401 if (vmx->vpid != 0)
4402 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4403
Sean Christophersonbecdad82020-09-23 09:50:45 -07004404 if (cpu_has_vmx_xsaves())
Wanpeng Lif53cd632014-12-02 19:14:58 +08004405 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4406
Peter Feiner4e595162016-07-07 14:49:58 -07004407 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07004408 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4409 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4410 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07004411
4412 if (cpu_has_vmx_encls_vmexit())
4413 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Chao Peng2ef444f2018-10-24 16:05:12 +08004414
Sean Christopherson2ef76192020-03-02 15:56:22 -08004415 if (vmx_pt_mode_is_host_guest()) {
Chao Peng2ef444f2018-10-24 16:05:12 +08004416 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
4417 /* Bit[6~0] are forced to 1, writes are ignored. */
4418 vmx->pt_desc.guest.output_mask = 0x7F;
4419 vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
4420 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004421}
4422
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004423static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004424{
4425 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004426 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004427 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004428
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004429 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004430 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004431
Tao Xu6e3ba4a2019-07-16 14:55:50 +08004432 vmx->msr_ia32_umwait_control = 0;
4433
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004434 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Wanpeng Li95c06542019-09-05 14:26:28 +08004435 vmx->hv_deadline_tsc = -1;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004436 kvm_set_cr8(vcpu, 0);
4437
4438 if (!init_event) {
4439 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4440 MSR_IA32_APICBASE_ENABLE;
4441 if (kvm_vcpu_is_reset_bsp(vcpu))
4442 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4443 apic_base_msr.host_initiated = true;
4444 kvm_set_apic_base(vcpu, &apic_base_msr);
4445 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004446
Avi Kivity2fb92db2011-04-27 19:42:18 +03004447 vmx_segment_cache_clear(vmx);
4448
Avi Kivity5706be02008-08-20 15:07:31 +03004449 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004450 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004451 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004452
4453 seg_setup(VCPU_SREG_DS);
4454 seg_setup(VCPU_SREG_ES);
4455 seg_setup(VCPU_SREG_FS);
4456 seg_setup(VCPU_SREG_GS);
4457 seg_setup(VCPU_SREG_SS);
4458
4459 vmcs_write16(GUEST_TR_SELECTOR, 0);
4460 vmcs_writel(GUEST_TR_BASE, 0);
4461 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4462 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4463
4464 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4465 vmcs_writel(GUEST_LDTR_BASE, 0);
4466 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4467 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4468
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004469 if (!init_event) {
4470 vmcs_write32(GUEST_SYSENTER_CS, 0);
4471 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4472 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4473 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4474 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004475
Wanpeng Lic37c2872017-11-20 14:52:21 -08004476 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01004477 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004478
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004479 vmcs_writel(GUEST_GDTR_BASE, 0);
4480 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4481
4482 vmcs_writel(GUEST_IDTR_BASE, 0);
4483 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4484
Anthony Liguori443381a2010-12-06 10:53:38 -06004485 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004486 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004487 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07004488 if (kvm_mpx_supported())
4489 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004490
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004491 setup_msrs(vmx);
4492
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4494
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004495 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004496 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004497 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004498 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004499 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004500 vmcs_write32(TPR_THRESHOLD, 0);
4501 }
4502
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004503 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004504
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004505 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004506 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06004507 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004508 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004509 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004510
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004511 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004513 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004514 if (init_event)
4515 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004516}
4517
Jan Kiszkac9a79532014-03-07 20:03:15 +01004518static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004519{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08004520 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004521}
4522
Jan Kiszkac9a79532014-03-07 20:03:15 +01004523static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004524{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004525 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004526 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01004527 enable_irq_window(vcpu);
4528 return;
4529 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004530
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08004531 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004532}
4533
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004534static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004535{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004536 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004537 uint32_t intr;
4538 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004539
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004540 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004541
Avi Kivityfa89a812008-09-01 15:57:51 +03004542 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004543 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004544 int inc_eip = 0;
4545 if (vcpu->arch.interrupt.soft)
4546 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004547 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
Eddie Dong85f455f2007-07-06 12:20:49 +03004548 return;
4549 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004550 intr = irq | INTR_INFO_VALID_MASK;
4551 if (vcpu->arch.interrupt.soft) {
4552 intr |= INTR_TYPE_SOFT_INTR;
4553 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4554 vmx->vcpu.arch.event_exit_inst_len);
4555 } else
4556 intr |= INTR_TYPE_EXT_INTR;
4557 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004558
4559 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004560}
4561
Sheng Yangf08864b2008-05-15 18:23:25 +08004562static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4563{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004564 struct vcpu_vmx *vmx = to_vmx(vcpu);
4565
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004566 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004567 /*
4568 * Tracking the NMI-blocked state in software is built upon
4569 * finding the next open IRQ window. This, in turn, depends on
4570 * well-behaving guests: They have to keep IRQs disabled at
4571 * least as long as the NMI handler runs. Otherwise we may
4572 * cause NMI nesting, maybe breaking the guest. But as this is
4573 * highly unlikely, we can live with the residual risk.
4574 */
4575 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
4576 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4577 }
4578
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004579 ++vcpu->stat.nmi_injections;
4580 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004581
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004582 if (vmx->rmode.vm86_active) {
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004583 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004584 return;
4585 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08004586
Sheng Yangf08864b2008-05-15 18:23:25 +08004587 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4588 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004589
4590 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004591}
4592
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004593bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004594{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004595 struct vcpu_vmx *vmx = to_vmx(vcpu);
4596 bool masked;
4597
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004598 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004599 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004600 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02004601 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004602 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4603 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4604 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004605}
4606
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004607void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004608{
4609 struct vcpu_vmx *vmx = to_vmx(vcpu);
4610
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004611 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004612 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4613 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4614 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4615 }
4616 } else {
4617 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4618 if (masked)
4619 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4620 GUEST_INTR_STATE_NMI);
4621 else
4622 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4623 GUEST_INTR_STATE_NMI);
4624 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004625}
4626
Sean Christopherson1b660b62020-04-22 19:25:44 -07004627bool vmx_nmi_blocked(struct kvm_vcpu *vcpu)
4628{
4629 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
4630 return false;
4631
4632 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4633 return true;
4634
4635 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4636 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
4637 GUEST_INTR_STATE_NMI));
4638}
4639
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004640static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Jan Kiszka2505dc92013-04-14 12:12:47 +02004641{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004642 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004643 return -EBUSY;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004644
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004645 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
4646 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004647 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004648
Sean Christopherson1b660b62020-04-22 19:25:44 -07004649 return !vmx_nmi_blocked(vcpu);
4650}
Sean Christopherson429ab572020-04-22 19:25:42 -07004651
Sean Christopherson1b660b62020-04-22 19:25:44 -07004652bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)
4653{
4654 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Sean Christopherson88c604b2020-04-22 19:25:41 -07004655 return false;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004656
Sean Christopherson7ab0abd2020-04-22 19:25:50 -07004657 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) ||
Sean Christopherson1b660b62020-04-22 19:25:44 -07004658 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4659 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Jan Kiszka2505dc92013-04-14 12:12:47 +02004660}
4661
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004662static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Gleb Natapov78646122009-03-23 12:12:11 +02004663{
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004664 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004665 return -EBUSY;
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004666
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004667 /*
4668 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
4669 * e.g. if the IRQ arrived asynchronously after checking nested events.
4670 */
4671 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004672 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004673
Sean Christopherson1b660b62020-04-22 19:25:44 -07004674 return !vmx_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02004675}
4676
Izik Eiduscbc94022007-10-25 00:29:55 +02004677static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4678{
Peter Xuff5a9832020-09-30 21:20:33 -04004679 void __user *ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004680
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004681 if (enable_unrestricted_guest)
4682 return 0;
4683
Peter Xu6a3c6232020-01-09 09:57:16 -05004684 mutex_lock(&kvm->slots_lock);
4685 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4686 PAGE_SIZE * 3);
4687 mutex_unlock(&kvm->slots_lock);
4688
Peter Xuff5a9832020-09-30 21:20:33 -04004689 if (IS_ERR(ret))
4690 return PTR_ERR(ret);
4691
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004692 to_kvm_vmx(kvm)->tss_addr = addr;
Peter Xuff5a9832020-09-30 21:20:33 -04004693
4694 return init_rmode_tss(kvm, ret);
Izik Eiduscbc94022007-10-25 00:29:55 +02004695}
4696
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004697static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4698{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004699 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004700 return 0;
4701}
4702
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004703static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004705 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004706 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004707 /*
4708 * Update instruction length as we may reinject the exception
4709 * from user space while in guest debugging mode.
4710 */
4711 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4712 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004713 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004714 return false;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004715 fallthrough;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004716 case DB_VECTOR:
Miaohe Lina8cfbae2020-02-19 10:45:48 +08004717 return !(vcpu->guest_debug &
4718 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP));
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004719 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004720 case OF_VECTOR:
4721 case BR_VECTOR:
4722 case UD_VECTOR:
4723 case DF_VECTOR:
4724 case SS_VECTOR:
4725 case GP_VECTOR:
4726 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004727 return true;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004728 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004729 return false;
4730}
4731
4732static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4733 int vec, u32 err_code)
4734{
4735 /*
4736 * Instruction with address size override prefix opcode 0x67
4737 * Cause the #SS fault with 0 error code in VM86 mode.
4738 */
4739 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004740 if (kvm_emulate_instruction(vcpu, 0)) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004741 if (vcpu->arch.halt_request) {
4742 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004743 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004744 }
4745 return 1;
4746 }
4747 return 0;
4748 }
4749
4750 /*
4751 * Forward all other exceptions that are valid in real mode.
4752 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4753 * the required debugging infrastructure rework.
4754 */
4755 kvm_queue_exception(vcpu, vec);
4756 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757}
4758
Avi Kivity851ba692009-08-24 11:10:17 +03004759static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004760{
Sean Christopherson95b5a482019-04-19 22:50:59 -07004761 /* handled by vmx_vcpu_run() */
Andi Kleena0861c02009-06-08 17:37:09 +08004762 return 1;
4763}
4764
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004765/*
4766 * If the host has split lock detection disabled, then #AC is
4767 * unconditionally injected into the guest, which is the pre split lock
4768 * detection behaviour.
4769 *
4770 * If the host has split lock detection enabled then #AC is
4771 * only injected into the guest when:
4772 * - Guest CPL == 3 (user mode)
4773 * - Guest has #AC detection enabled in CR0
4774 * - Guest EFLAGS has AC bit set
4775 */
4776static inline bool guest_inject_ac(struct kvm_vcpu *vcpu)
4777{
4778 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
4779 return true;
4780
4781 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) &&
4782 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
4783}
4784
Sean Christopherson95b5a482019-04-19 22:50:59 -07004785static int handle_exception_nmi(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004786{
Avi Kivity1155f762007-11-22 11:30:47 +02004787 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004788 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004789 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004790 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791 u32 vect_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792
Avi Kivity1155f762007-11-22 11:30:47 +02004793 vect_info = vmx->idt_vectoring_info;
Sean Christophersonf27ad732020-04-27 10:18:37 -07004794 intr_info = vmx_get_intr_info(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004795
Paolo Bonzini2ea72032019-06-06 14:57:25 +02004796 if (is_machine_check(intr_info) || is_nmi(intr_info))
Sean Christopherson95b5a482019-04-19 22:50:59 -07004797 return 1; /* handled by handle_exception_nmi_irqoff() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004798
Wanpeng Li082d06e2018-04-03 16:28:48 -07004799 if (is_invalid_opcode(intr_info))
4800 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004801
Avi Kivity6aa8b732006-12-10 02:21:36 -08004802 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004803 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004805
Liran Alon9e869482018-03-12 13:12:51 +02004806 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4807 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christophersona6c6ed12019-08-27 14:40:30 -07004808
4809 /*
4810 * VMware backdoor emulation on #GP interception only handles
4811 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
4812 * error code on #GP.
4813 */
4814 if (error_code) {
4815 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4816 return 1;
4817 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004818 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon9e869482018-03-12 13:12:51 +02004819 }
4820
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004821 /*
4822 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4823 * MMIO, it is better to report an internal error.
4824 * See the comments in vmx_handle_exit.
4825 */
4826 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4827 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4828 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4829 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Jim Mattson1aa561b2020-06-03 16:56:21 -07004830 vcpu->run->internal.ndata = 4;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004831 vcpu->run->internal.data[0] = vect_info;
4832 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004833 vcpu->run->internal.data[2] = error_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07004834 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004835 return 0;
4836 }
4837
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838 if (is_page_fault(intr_info)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07004839 cr2 = vmx_get_exit_qual(vcpu);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004840 if (enable_ept && !vcpu->arch.apf.host_apf_flags) {
4841 /*
4842 * EPT will cause page fault only if we need to
4843 * detect illegal GPAs.
4844 */
Mohammed Gamalb96e6502020-09-03 16:11:22 +02004845 WARN_ON_ONCE(!allow_smaller_maxphyaddr);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004846 kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
4847 return 1;
4848 } else
4849 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 }
4851
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004852 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004853
4854 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4855 return handle_rmode_exception(vcpu, ex_no, error_code);
4856
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004857 switch (ex_no) {
4858 case DB_VECTOR:
Sean Christopherson5addc232020-04-15 13:34:53 -07004859 dr6 = vmx_get_exit_qual(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004860 if (!(vcpu->guest_debug &
4861 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004862 if (is_icebp(intr_info))
Sean Christopherson1957aa62019-08-27 14:40:39 -07004863 WARN_ON(!skip_emulated_instruction(vcpu));
Huw Daviesfd2a4452014-04-16 10:02:51 +01004864
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004865 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004866 return 1;
4867 }
Peter Xu13196632020-05-05 16:49:58 -04004868 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004869 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004870 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004871 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004872 /*
4873 * Update instruction length as we may reinject #BP from
4874 * user space while in guest debugging mode. Reading it for
4875 * #DB as well causes no harm, it is not used in that case.
4876 */
4877 vmx->vcpu.arch.event_exit_inst_len =
4878 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004880 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004881 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4882 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004883 break;
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004884 case AC_VECTOR:
4885 if (guest_inject_ac(vcpu)) {
4886 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4887 return 1;
4888 }
4889
4890 /*
4891 * Handle split lock. Depending on detection mode this will
4892 * either warn and disable split lock detection for this
4893 * task or force SIGBUS on it.
4894 */
4895 if (handle_guest_split_lock(kvm_rip_read(vcpu)))
4896 return 1;
4897 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004898 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004899 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4900 kvm_run->ex.exception = ex_no;
4901 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004902 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004903 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004904 return 0;
4905}
4906
Andrea Arcangelif399e602019-11-04 17:59:58 -05004907static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004908{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004909 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004910 return 1;
4911}
4912
Avi Kivity851ba692009-08-24 11:10:17 +03004913static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004914{
Avi Kivity851ba692009-08-24 11:10:17 +03004915 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004916 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004917 return 0;
4918}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004919
Avi Kivity851ba692009-08-24 11:10:17 +03004920static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004921{
He, Qingbfdaab02007-09-12 14:18:28 +08004922 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004923 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004924 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004925
Sean Christopherson5addc232020-04-15 13:34:53 -07004926 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02004927 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004928
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004929 ++vcpu->stat.io_exits;
4930
Sean Christopherson432baf62018-03-08 08:57:26 -08004931 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004932 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004933
4934 port = exit_qualification >> 16;
4935 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004936 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004937
Sean Christophersondca7f122018-03-08 08:57:27 -08004938 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939}
4940
Ingo Molnar102d8322007-02-19 14:37:47 +02004941static void
4942vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4943{
4944 /*
4945 * Patch in the VMCALL instruction:
4946 */
4947 hypercall[0] = 0x0f;
4948 hypercall[1] = 0x01;
4949 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004950}
4951
Guo Chao0fa06072012-06-28 15:16:19 +08004952/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004953static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4954{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004955 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004956 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4957 unsigned long orig_val = val;
4958
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004959 /*
4960 * We get here when L2 changed cr0 in a way that did not change
4961 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004962 * but did change L0 shadowed bits. So we first calculate the
4963 * effective cr0 value that L1 would like to write into the
4964 * hardware. It consists of the L2-owned bits from the new
4965 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004966 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004967 val = (val & ~vmcs12->cr0_guest_host_mask) |
4968 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4969
David Matlack38991522016-11-29 18:14:08 -08004970 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004971 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004972
4973 if (kvm_set_cr0(vcpu, val))
4974 return 1;
4975 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004976 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004977 } else {
4978 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08004979 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004980 return 1;
David Matlack38991522016-11-29 18:14:08 -08004981
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004982 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004983 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004984}
4985
4986static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4987{
4988 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004989 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4990 unsigned long orig_val = val;
4991
4992 /* analogously to handle_set_cr0 */
4993 val = (val & ~vmcs12->cr4_guest_host_mask) |
4994 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4995 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004996 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004997 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004998 return 0;
4999 } else
5000 return kvm_set_cr4(vcpu, val);
5001}
5002
Paolo Bonzini0367f202016-07-12 10:44:55 +02005003static int handle_desc(struct kvm_vcpu *vcpu)
5004{
5005 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005006 return kvm_emulate_instruction(vcpu, 0);
Paolo Bonzini0367f202016-07-12 10:44:55 +02005007}
5008
Avi Kivity851ba692009-08-24 11:10:17 +03005009static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005010{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005011 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005012 int cr;
5013 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005014 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005015 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005016
Sean Christopherson5addc232020-04-15 13:34:53 -07005017 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018 cr = exit_qualification & 15;
5019 reg = (exit_qualification >> 8) & 15;
5020 switch ((exit_qualification >> 4) & 3) {
5021 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005022 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005023 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005024 switch (cr) {
5025 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005026 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005027 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005028 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005029 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03005030 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005031 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005032 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005033 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005034 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005035 case 8: {
5036 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005037 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005038 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005039 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005040 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08005041 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005042 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005043 return ret;
5044 /*
5045 * TODO: we might be squashing a
5046 * KVM_GUESTDBG_SINGLESTEP-triggered
5047 * KVM_EXIT_DEBUG here.
5048 */
Avi Kivity851ba692009-08-24 11:10:17 +03005049 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005050 return 0;
5051 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005052 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005053 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005054 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005055 WARN_ONCE(1, "Guest should always own CR0.TS");
5056 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005057 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005058 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005059 case 1: /*mov from cr*/
5060 switch (cr) {
5061 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005062 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02005063 val = kvm_read_cr3(vcpu);
5064 kvm_register_write(vcpu, reg, val);
5065 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005066 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005067 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005068 val = kvm_get_cr8(vcpu);
5069 kvm_register_write(vcpu, reg, val);
5070 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005071 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005072 }
5073 break;
5074 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005075 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005076 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005077 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005078
Kyle Huey6affcbe2016-11-29 12:40:40 -08005079 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005080 default:
5081 break;
5082 }
Avi Kivity851ba692009-08-24 11:10:17 +03005083 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005084 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005085 (int)(exit_qualification >> 4) & 3, cr);
5086 return 0;
5087}
5088
Avi Kivity851ba692009-08-24 11:10:17 +03005089static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005090{
He, Qingbfdaab02007-09-12 14:18:28 +08005091 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005092 int dr, dr7, reg;
5093
Sean Christopherson5addc232020-04-15 13:34:53 -07005094 exit_qualification = vmx_get_exit_qual(vcpu);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005095 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5096
5097 /* First, if DR does not exist, trigger UD */
5098 if (!kvm_require_dr(vcpu, dr))
5099 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100
Jan Kiszkaf2483412010-01-20 18:20:20 +01005101 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005102 if (!kvm_require_cpl(vcpu, 0))
5103 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005104 dr7 = vmcs_readl(GUEST_DR7);
5105 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005106 /*
5107 * As the vm-exit takes precedence over the debug trap, we
5108 * need to emulate the latter, either for the host or the
5109 * guest debugging itself.
5110 */
5111 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Paolo Bonzini45981de2020-05-06 05:59:39 -04005112 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_RTM | DR6_FIXED_1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005113 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005114 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005115 vcpu->run->debug.arch.exception = DB_VECTOR;
5116 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005117 return 0;
5118 } else {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04005119 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005120 return 1;
5121 }
5122 }
5123
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005124 if (vcpu->guest_debug == 0) {
Sean Christopherson2183f562019-05-07 12:17:56 -07005125 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005126
5127 /*
5128 * No more DR vmexits; force a reload of the debug registers
5129 * and reenter on this instruction. The next vmexit will
5130 * retrieve the full state of the debug registers.
5131 */
5132 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5133 return 1;
5134 }
5135
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005136 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5137 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005138 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005139
5140 if (kvm_get_dr(vcpu, dr, &val))
5141 return 1;
5142 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005143 } else
Nadav Amit57773922014-06-18 17:19:23 +03005144 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005145 return 1;
5146
Kyle Huey6affcbe2016-11-29 12:40:40 -08005147 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005148}
5149
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005150static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5151{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005152 get_debugreg(vcpu->arch.db[0], 0);
5153 get_debugreg(vcpu->arch.db[1], 1);
5154 get_debugreg(vcpu->arch.db[2], 2);
5155 get_debugreg(vcpu->arch.db[3], 3);
5156 get_debugreg(vcpu->arch.dr6, 6);
5157 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5158
5159 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Sean Christopherson2183f562019-05-07 12:17:56 -07005160 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005161}
5162
Gleb Natapov020df072010-04-13 10:05:23 +03005163static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5164{
5165 vmcs_writel(GUEST_DR7, val);
5166}
5167
Avi Kivity851ba692009-08-24 11:10:17 +03005168static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005169{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01005170 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005171 return 1;
5172}
5173
Avi Kivity851ba692009-08-24 11:10:17 +03005174static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005175{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005176 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005177
Avi Kivity3842d132010-07-27 12:30:24 +03005178 kvm_make_request(KVM_REQ_EVENT, vcpu);
5179
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005180 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005181 return 1;
5182}
5183
Avi Kivity851ba692009-08-24 11:10:17 +03005184static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005185{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005186 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005187}
5188
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005189static int handle_invd(struct kvm_vcpu *vcpu)
5190{
Tom Lendacky28e2b2f2020-09-24 13:41:58 -05005191 /* Treat an INVD instruction as a NOP and just skip it. */
5192 return kvm_skip_emulated_instruction(vcpu);
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005193}
5194
Avi Kivity851ba692009-08-24 11:10:17 +03005195static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005196{
Sean Christopherson5addc232020-04-15 13:34:53 -07005197 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005198
5199 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005200 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005201}
5202
Avi Kivityfee84b02011-11-10 14:57:25 +02005203static int handle_rdpmc(struct kvm_vcpu *vcpu)
5204{
5205 int err;
5206
5207 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005208 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02005209}
5210
Avi Kivity851ba692009-08-24 11:10:17 +03005211static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005212{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005213 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005214}
5215
Dexuan Cui2acf9232010-06-10 11:27:12 +08005216static int handle_xsetbv(struct kvm_vcpu *vcpu)
5217{
5218 u64 new_bv = kvm_read_edx_eax(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07005219 u32 index = kvm_rcx_read(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005220
5221 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005222 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005223 return 1;
5224}
5225
Avi Kivity851ba692009-08-24 11:10:17 +03005226static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005227{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005228 if (likely(fasteoi)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07005229 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005230 int access_type, offset;
5231
5232 access_type = exit_qualification & APIC_ACCESS_TYPE;
5233 offset = exit_qualification & APIC_ACCESS_OFFSET;
5234 /*
5235 * Sane guest uses MOV to write EOI, with written value
5236 * not cared. So make a short-circuit here by avoiding
5237 * heavy instruction emulation.
5238 */
5239 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5240 (offset == APIC_EOI)) {
5241 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005242 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005243 }
5244 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005245 return kvm_emulate_instruction(vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005246}
5247
Yang Zhangc7c9c562013-01-25 10:18:51 +08005248static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5249{
Sean Christopherson5addc232020-04-15 13:34:53 -07005250 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005251 int vector = exit_qualification & 0xff;
5252
5253 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5254 kvm_apic_set_eoi_accelerated(vcpu, vector);
5255 return 1;
5256}
5257
Yang Zhang83d4c282013-01-25 10:18:49 +08005258static int handle_apic_write(struct kvm_vcpu *vcpu)
5259{
Sean Christopherson5addc232020-04-15 13:34:53 -07005260 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhang83d4c282013-01-25 10:18:49 +08005261 u32 offset = exit_qualification & 0xfff;
5262
5263 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5264 kvm_apic_write_nodecode(vcpu, offset);
5265 return 1;
5266}
5267
Avi Kivity851ba692009-08-24 11:10:17 +03005268static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005269{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005270 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005271 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005272 bool has_error_code = false;
5273 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005274 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005275 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005276
5277 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005278 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005279 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005280
Sean Christopherson5addc232020-04-15 13:34:53 -07005281 exit_qualification = vmx_get_exit_qual(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005282
5283 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005284 if (reason == TASK_SWITCH_GATE && idt_v) {
5285 switch (type) {
5286 case INTR_TYPE_NMI_INTR:
5287 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005288 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005289 break;
5290 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005291 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005292 kvm_clear_interrupt_queue(vcpu);
5293 break;
5294 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005295 if (vmx->idt_vectoring_info &
5296 VECTORING_INFO_DELIVER_CODE_MASK) {
5297 has_error_code = true;
5298 error_code =
5299 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5300 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005301 fallthrough;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005302 case INTR_TYPE_SOFT_EXCEPTION:
5303 kvm_clear_exception_queue(vcpu);
5304 break;
5305 default:
5306 break;
5307 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005308 }
Izik Eidus37817f22008-03-24 23:14:53 +02005309 tss_selector = exit_qualification;
5310
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005311 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5312 type != INTR_TYPE_EXT_INTR &&
5313 type != INTR_TYPE_NMI_INTR))
Sean Christopherson1957aa62019-08-27 14:40:39 -07005314 WARN_ON(!skip_emulated_instruction(vcpu));
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005315
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005316 /*
5317 * TODO: What about debug traps on tss switch?
5318 * Are we supposed to inject them and update dr6?
5319 */
Sean Christopherson10517782019-08-27 14:40:35 -07005320 return kvm_task_switch(vcpu, tss_selector,
5321 type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005322 reason, has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02005323}
5324
Avi Kivity851ba692009-08-24 11:10:17 +03005325static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005326{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005327 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005328 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01005329 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005330
Sean Christopherson5addc232020-04-15 13:34:53 -07005331 exit_qualification = vmx_get_exit_qual(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005332
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005333 /*
5334 * EPT violation happened while executing iret from NMI,
5335 * "blocked by NMI" bit has to be set before next VM entry.
5336 * There are errata that may cause this bit to not be set:
5337 * AAK134, BY25.
5338 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005339 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005340 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005341 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005342 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5343
Sheng Yang14394422008-04-28 12:24:45 +08005344 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005345 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005346
Junaid Shahid27959a42016-12-06 16:46:10 -08005347 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005348 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08005349 ? PFERR_USER_MASK : 0;
5350 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005351 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08005352 ? PFERR_WRITE_MASK : 0;
5353 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005354 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08005355 ? PFERR_FETCH_MASK : 0;
5356 /* ept page table entry is present? */
5357 error_code |= (exit_qualification &
5358 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5359 EPT_VIOLATION_EXECUTABLE))
5360 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005361
Paolo Bonzinieebed242016-11-28 14:39:58 +01005362 error_code |= (exit_qualification & 0x100) != 0 ?
5363 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005364
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005365 vcpu->arch.exit_qualification = exit_qualification;
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005366
5367 /*
5368 * Check that the GPA doesn't exceed physical memory limits, as that is
5369 * a guest page fault. We have to emulate the instruction here, because
5370 * if the illegal address is that of a paging structure, then
5371 * EPT_VIOLATION_ACC_WRITE bit is set. Alternatively, if supported we
5372 * would also use advanced VM-exit information for EPT violations to
5373 * reconstruct the page fault error code.
5374 */
Paolo Bonzinic0623f52020-10-21 18:05:58 -04005375 if (unlikely(allow_smaller_maxphyaddr && kvm_vcpu_is_illegal_gpa(vcpu, gpa)))
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005376 return kvm_emulate_instruction(vcpu, 0);
5377
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005378 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005379}
5380
Avi Kivity851ba692009-08-24 11:10:17 +03005381static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005382{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005383 gpa_t gpa;
5384
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005385 /*
5386 * A nested guest cannot optimize MMIO vmexits, because we have an
5387 * nGPA here instead of the required GPA.
5388 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005389 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005390 if (!is_guest_mode(vcpu) &&
5391 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08005392 trace_kvm_fast_mmio(gpa);
Sean Christopherson1957aa62019-08-27 14:40:39 -07005393 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005394 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005395
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07005396 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005397}
5398
Avi Kivity851ba692009-08-24 11:10:17 +03005399static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005400{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005401 WARN_ON_ONCE(!enable_vnmi);
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005402 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Sheng Yangf08864b2008-05-15 18:23:25 +08005403 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005404 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005405
5406 return 1;
5407}
5408
Mohammed Gamal80ced182009-09-01 12:48:18 +02005409static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005410{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005411 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity49e9d552010-09-19 14:34:08 +02005412 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005413 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005414
Sean Christopherson2183f562019-05-07 12:17:56 -07005415 intr_window_requested = exec_controls_get(vmx) &
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005416 CPU_BASED_INTR_WINDOW_EXITING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005417
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005418 while (vmx->emulation_required && count-- != 0) {
Sean Christophersondb438592020-04-22 19:25:48 -07005419 if (intr_window_requested && !vmx_interrupt_blocked(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005420 return handle_interrupt_window(&vmx->vcpu);
5421
Radim Krčmář72875d82017-04-26 22:32:19 +02005422 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005423 return 1;
5424
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005425 if (!kvm_emulate_instruction(vcpu, 0))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005426 return 0;
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005427
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005428 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
Sean Christopherson8fff2712019-08-27 14:40:37 -07005429 vcpu->arch.exception.pending) {
5430 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5431 vcpu->run->internal.suberror =
5432 KVM_INTERNAL_ERROR_EMULATION;
5433 vcpu->run->internal.ndata = 0;
5434 return 0;
5435 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005436
Gleb Natapov8d76c492013-05-08 18:38:44 +03005437 if (vcpu->arch.halt_request) {
5438 vcpu->arch.halt_request = 0;
Sean Christopherson8fff2712019-08-27 14:40:37 -07005439 return kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005440 }
5441
Sean Christopherson8fff2712019-08-27 14:40:37 -07005442 /*
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005443 * Note, return 1 and not 0, vcpu_run() will invoke
5444 * xfer_to_guest_mode() which will create a proper return
5445 * code.
Sean Christopherson8fff2712019-08-27 14:40:37 -07005446 */
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005447 if (__xfer_to_guest_mode_work_pending())
Sean Christopherson8fff2712019-08-27 14:40:37 -07005448 return 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005449 }
5450
Sean Christopherson8fff2712019-08-27 14:40:37 -07005451 return 1;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005452}
5453
5454static void grow_ple_window(struct kvm_vcpu *vcpu)
5455{
5456 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005457 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005458
Babu Mogerc8e88712018-03-16 16:37:24 -04005459 vmx->ple_window = __grow_ple_window(old, ple_window,
5460 ple_window_grow,
5461 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005462
Peter Xu4f75bcc2019-09-06 10:17:22 +08005463 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005464 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005465 trace_kvm_ple_window_update(vcpu->vcpu_id,
5466 vmx->ple_window, old);
5467 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005468}
5469
5470static void shrink_ple_window(struct kvm_vcpu *vcpu)
5471{
5472 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005473 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005474
Babu Mogerc8e88712018-03-16 16:37:24 -04005475 vmx->ple_window = __shrink_ple_window(old, ple_window,
5476 ple_window_shrink,
5477 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005478
Peter Xu4f75bcc2019-09-06 10:17:22 +08005479 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005480 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005481 trace_kvm_ple_window_update(vcpu->vcpu_id,
5482 vmx->ple_window, old);
5483 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005484}
5485
Peng Haoe01bca22018-04-07 05:47:32 +08005486static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005487{
5488 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
5489 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
5490 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
5491 0ull, VMX_EPT_EXECUTABLE_MASK,
5492 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005493 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005494
5495 ept_set_mmio_spte_mask();
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005496}
5497
Avi Kivity6aa8b732006-12-10 02:21:36 -08005498/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005499 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5500 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5501 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005502static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005503{
Wanpeng Lib31c1142018-03-12 04:53:04 -07005504 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005505 grow_ple_window(vcpu);
5506
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08005507 /*
5508 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
5509 * VM-execution control is ignored if CPL > 0. OTOH, KVM
5510 * never set PAUSE_EXITING and just set PLE if supported,
5511 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
5512 */
5513 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005514 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005515}
5516
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005517static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08005518{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005519 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08005520}
5521
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005522static int handle_mwait(struct kvm_vcpu *vcpu)
5523{
5524 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
5525 return handle_nop(vcpu);
5526}
5527
Jim Mattson45ec3682017-08-23 16:32:04 -07005528static int handle_invalid_op(struct kvm_vcpu *vcpu)
5529{
5530 kvm_queue_exception(vcpu, UD_VECTOR);
5531 return 1;
5532}
5533
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005534static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5535{
5536 return 1;
5537}
5538
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005539static int handle_monitor(struct kvm_vcpu *vcpu)
5540{
5541 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
5542 return handle_nop(vcpu);
5543}
5544
Junaid Shahideb4b2482018-06-27 14:59:14 -07005545static int handle_invpcid(struct kvm_vcpu *vcpu)
5546{
5547 u32 vmx_instruction_info;
5548 unsigned long type;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005549 gva_t gva;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005550 struct {
5551 u64 pcid;
5552 u64 gla;
5553 } operand;
5554
5555 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5556 kvm_queue_exception(vcpu, UD_VECTOR);
5557 return 1;
5558 }
5559
5560 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5561 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5562
5563 if (type > 3) {
5564 kvm_inject_gp(vcpu, 0);
5565 return 1;
5566 }
5567
5568 /* According to the Intel instruction reference, the memory operand
5569 * is read even if it isn't needed (e.g., for type==all)
5570 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005571 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005572 vmx_instruction_info, false,
5573 sizeof(operand), &gva))
Junaid Shahideb4b2482018-06-27 14:59:14 -07005574 return 1;
5575
Babu Moger97150922020-09-11 14:29:12 -05005576 return kvm_handle_invpcid(vcpu, type, gva);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005577}
5578
Kai Huang843e4332015-01-28 10:54:28 +08005579static int handle_pml_full(struct kvm_vcpu *vcpu)
5580{
5581 unsigned long exit_qualification;
5582
5583 trace_kvm_pml_full(vcpu->vcpu_id);
5584
Sean Christopherson5addc232020-04-15 13:34:53 -07005585 exit_qualification = vmx_get_exit_qual(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005586
5587 /*
5588 * PML buffer FULL happened while executing iret from NMI,
5589 * "blocked by NMI" bit has to be set before next VM entry.
5590 */
5591 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005592 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005593 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5594 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5595 GUEST_INTR_STATE_NMI);
5596
5597 /*
5598 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5599 * here.., and there's no userspace involvement needed for PML.
5600 */
5601 return 1;
5602}
5603
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005604static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07005605{
Sean Christopherson804939e2019-05-07 12:18:05 -07005606 struct vcpu_vmx *vmx = to_vmx(vcpu);
5607
5608 if (!vmx->req_immediate_exit &&
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005609 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) {
Sean Christophersond264ee02018-08-27 15:21:12 -07005610 kvm_lapic_expired_hv_timer(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005611 return EXIT_FASTPATH_REENTER_GUEST;
5612 }
Sean Christopherson804939e2019-05-07 12:18:05 -07005613
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005614 return EXIT_FASTPATH_NONE;
5615}
5616
5617static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5618{
5619 handle_fastpath_preemption_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005620 return 1;
5621}
5622
Sean Christophersone4027cf2018-12-03 13:53:12 -08005623/*
5624 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5625 * are overwritten by nested_vmx_setup() when nested=1.
5626 */
5627static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5628{
5629 kvm_queue_exception(vcpu, UD_VECTOR);
5630 return 1;
5631}
5632
Sean Christopherson0b665d32018-08-14 09:33:34 -07005633static int handle_encls(struct kvm_vcpu *vcpu)
5634{
5635 /*
5636 * SGX virtualization is not yet supported. There is no software
5637 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
5638 * to prevent the guest from executing ENCLS.
5639 */
5640 kvm_queue_exception(vcpu, UD_VECTOR);
5641 return 1;
5642}
5643
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005644static int handle_bus_lock_vmexit(struct kvm_vcpu *vcpu)
5645{
5646 vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK;
5647 vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK;
5648 return 0;
5649}
5650
Nadav Har'El0140cae2011-05-25 23:06:28 +03005651/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005652 * The exit handlers return 1 if the exit was handled fully and guest execution
5653 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5654 * to be done to userspace and return 0.
5655 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005656static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Sean Christopherson95b5a482019-04-19 22:50:59 -07005657 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005658 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005659 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005660 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005661 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005662 [EXIT_REASON_CR_ACCESS] = handle_cr,
5663 [EXIT_REASON_DR_ACCESS] = handle_dr,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005664 [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
5665 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
5666 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005667 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005668 [EXIT_REASON_HLT] = kvm_emulate_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005669 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005670 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005671 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005672 [EXIT_REASON_VMCALL] = handle_vmcall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005673 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5674 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5675 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5676 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5677 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5678 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5679 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5680 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5681 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005682 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5683 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005684 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005685 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005686 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005687 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005688 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005689 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005690 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5691 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005692 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5693 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005694 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005695 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005696 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005697 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005698 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5699 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Jim Mattson45ec3682017-08-23 16:32:04 -07005700 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07005701 [EXIT_REASON_RDSEED] = handle_invalid_op,
Kai Huang843e4332015-01-28 10:54:28 +08005702 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005703 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005704 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005705 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005706 [EXIT_REASON_ENCLS] = handle_encls,
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005707 [EXIT_REASON_BUS_LOCK] = handle_bus_lock_vmexit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005708};
5709
5710static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005711 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005712
Sean Christopherson235ba742020-09-23 13:13:46 -07005713static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
5714 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02005715{
Sean Christopherson235ba742020-09-23 13:13:46 -07005716 struct vcpu_vmx *vmx = to_vmx(vcpu);
5717
Sean Christopherson5addc232020-04-15 13:34:53 -07005718 *info1 = vmx_get_exit_qual(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08005719 if (!(vmx->exit_reason.failed_vmentry)) {
Sean Christopherson235ba742020-09-23 13:13:46 -07005720 *info2 = vmx->idt_vectoring_info;
5721 *intr_info = vmx_get_intr_info(vcpu);
5722 if (is_exception_with_error_code(*intr_info))
5723 *error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5724 else
5725 *error_code = 0;
5726 } else {
5727 *info2 = 0;
5728 *intr_info = 0;
5729 *error_code = 0;
5730 }
Avi Kivity586f9602010-11-18 13:09:54 +02005731}
5732
Kai Huanga3eaa862015-11-04 13:46:05 +08005733static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005734{
Kai Huanga3eaa862015-11-04 13:46:05 +08005735 if (vmx->pml_pg) {
5736 __free_page(vmx->pml_pg);
5737 vmx->pml_pg = NULL;
5738 }
Kai Huang843e4332015-01-28 10:54:28 +08005739}
5740
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005741static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005742{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005743 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005744 u64 *pml_buf;
5745 u16 pml_idx;
5746
5747 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5748
5749 /* Do nothing if PML buffer is empty */
5750 if (pml_idx == (PML_ENTITY_NUM - 1))
5751 return;
5752
5753 /* PML index always points to next available PML buffer entity */
5754 if (pml_idx >= PML_ENTITY_NUM)
5755 pml_idx = 0;
5756 else
5757 pml_idx++;
5758
5759 pml_buf = page_address(vmx->pml_pg);
5760 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5761 u64 gpa;
5762
5763 gpa = pml_buf[pml_idx];
5764 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005765 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005766 }
5767
5768 /* reset PML index */
5769 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5770}
5771
5772/*
5773 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
5774 * Called before reporting dirty_bitmap to userspace.
5775 */
5776static void kvm_flush_pml_buffers(struct kvm *kvm)
5777{
5778 int i;
5779 struct kvm_vcpu *vcpu;
5780 /*
5781 * We only need to kick vcpu out of guest mode here, as PML buffer
5782 * is flushed at beginning of all VMEXITs, and it's obvious that only
5783 * vcpus running in guest are possible to have unflushed GPAs in PML
5784 * buffer.
5785 */
5786 kvm_for_each_vcpu(i, vcpu, kvm)
5787 kvm_vcpu_kick(vcpu);
5788}
5789
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005790static void vmx_dump_sel(char *name, uint32_t sel)
5791{
5792 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005793 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005794 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5795 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5796 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5797}
5798
5799static void vmx_dump_dtsel(char *name, uint32_t limit)
5800{
5801 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5802 name, vmcs_read32(limit),
5803 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5804}
5805
Paolo Bonzini69090812019-04-15 15:16:17 +02005806void dump_vmcs(void)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005807{
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005808 u32 vmentry_ctl, vmexit_ctl;
5809 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
5810 unsigned long cr4;
5811 u64 efer;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005812
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005813 if (!dump_invalid_vmcs) {
5814 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
5815 return;
5816 }
5817
5818 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5819 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5820 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5821 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5822 cr4 = vmcs_readl(GUEST_CR4);
5823 efer = vmcs_read64(GUEST_IA32_EFER);
5824 secondary_exec_control = 0;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005825 if (cpu_has_secondary_exec_ctrls())
5826 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5827
5828 pr_err("*** Guest State ***\n");
5829 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5830 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5831 vmcs_readl(CR0_GUEST_HOST_MASK));
5832 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5833 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5834 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
5835 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
5836 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
5837 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005838 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5839 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5840 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5841 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005842 }
5843 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5844 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5845 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5846 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5847 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5848 vmcs_readl(GUEST_SYSENTER_ESP),
5849 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5850 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5851 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5852 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5853 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5854 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5855 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5856 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5857 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5858 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5859 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
5860 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
5861 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005862 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5863 efer, vmcs_read64(GUEST_IA32_PAT));
5864 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5865 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005866 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005867 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005868 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005869 pr_err("PerfGlobCtl = 0x%016llx\n",
5870 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005871 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005872 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005873 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5874 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5875 vmcs_read32(GUEST_ACTIVITY_STATE));
5876 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5877 pr_err("InterruptStatus = %04x\n",
5878 vmcs_read16(GUEST_INTR_STATUS));
5879
5880 pr_err("*** Host State ***\n");
5881 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5882 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5883 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5884 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5885 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5886 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5887 vmcs_read16(HOST_TR_SELECTOR));
5888 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5889 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5890 vmcs_readl(HOST_TR_BASE));
5891 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5892 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5893 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5894 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5895 vmcs_readl(HOST_CR4));
5896 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5897 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5898 vmcs_read32(HOST_IA32_SYSENTER_CS),
5899 vmcs_readl(HOST_IA32_SYSENTER_EIP));
5900 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005901 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5902 vmcs_read64(HOST_IA32_EFER),
5903 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005904 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005905 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005906 pr_err("PerfGlobCtl = 0x%016llx\n",
5907 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005908
5909 pr_err("*** Control State ***\n");
5910 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5911 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5912 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5913 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5914 vmcs_read32(EXCEPTION_BITMAP),
5915 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5916 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5917 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5918 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5919 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5920 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5921 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5922 vmcs_read32(VM_EXIT_INTR_INFO),
5923 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5924 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5925 pr_err(" reason=%08x qualification=%016lx\n",
5926 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5927 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5928 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5929 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005930 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005931 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005932 pr_err("TSC Multiplier = 0x%016llx\n",
5933 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005934 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
5935 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
5936 u16 status = vmcs_read16(GUEST_INTR_STATUS);
5937 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
5938 }
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005939 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005940 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
5941 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005942 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005943 }
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005944 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5945 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5946 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005947 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005948 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5949 pr_err("PLE Gap=%08x Window=%08x\n",
5950 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5951 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5952 pr_err("Virtual processor ID = 0x%04x\n",
5953 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5954}
5955
Avi Kivity6aa8b732006-12-10 02:21:36 -08005956/*
5957 * The guest has exited. See if we can fix it or if we need userspace
5958 * assistance.
5959 */
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08005960static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005961{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005962 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08005963 union vmx_exit_reason exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005964 u32 vectoring_info = vmx->idt_vectoring_info;
Sean Christopherson8e533242020-11-06 17:03:12 +08005965 u16 exit_handler_index;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005966
Kai Huang843e4332015-01-28 10:54:28 +08005967 /*
5968 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5969 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5970 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5971 * mode as if vcpus is in root mode, the PML buffer must has been
5972 * flushed already.
5973 */
5974 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005975 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005976
Sean Christophersondb438592020-04-22 19:25:48 -07005977 /*
5978 * We should never reach this point with a pending nested VM-Enter, and
5979 * more specifically emulation of L2 due to invalid guest state (see
5980 * below) should never happen as that means we incorrectly allowed a
5981 * nested VM-Enter with an invalid vmcs12.
5982 */
5983 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5984
Mohammed Gamal80ced182009-09-01 12:48:18 +02005985 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02005986 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02005987 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005988
Paolo Bonzini96b100c2020-03-17 18:32:50 +01005989 if (is_guest_mode(vcpu)) {
5990 /*
5991 * The host physical addresses of some pages of guest memory
5992 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
5993 * Page). The CPU may write to these pages via their host
5994 * physical address while L2 is running, bypassing any
5995 * address-translation-based dirty tracking (e.g. EPT write
5996 * protection).
5997 *
5998 * Mark them dirty on every exit from L2 to prevent them from
5999 * getting out of sync with dirty tracking.
6000 */
6001 nested_mark_vmcs12_pages_dirty(vcpu);
6002
Sean Christophersonf47baae2020-04-15 10:55:16 -07006003 if (nested_vmx_reflect_vmexit(vcpu))
Sean Christopherson789afc52020-04-15 10:55:10 -07006004 return 1;
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006005 }
Nadav Har'El644d7112011-05-25 23:12:35 +03006006
Sean Christopherson8e533242020-11-06 17:03:12 +08006007 if (exit_reason.failed_vmentry) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02006008 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03006009 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6010 vcpu->run->fail_entry.hardware_entry_failure_reason
Sean Christopherson8e533242020-11-06 17:03:12 +08006011 = exit_reason.full;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006012 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Mohammed Gamal51207022010-05-31 22:40:54 +03006013 return 0;
6014 }
6015
Avi Kivity29bd8a72007-09-10 17:27:03 +03006016 if (unlikely(vmx->fail)) {
Paolo Bonzini3b20e032019-07-19 18:15:08 +02006017 dump_vmcs();
Avi Kivity851ba692009-08-24 11:10:17 +03006018 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6019 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006020 = vmcs_read32(VM_INSTRUCTION_ERROR);
Jim Mattson8a14fe42020-06-03 16:56:22 -07006021 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006022 return 0;
6023 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006024
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006025 /*
6026 * Note:
6027 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6028 * delivery event since it indicates guest is accessing MMIO.
6029 * The vm-exit can be triggered again after return to guest that
6030 * will cause infinite loop.
6031 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006032 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sean Christopherson8e533242020-11-06 17:03:12 +08006033 (exit_reason.basic != EXIT_REASON_EXCEPTION_NMI &&
6034 exit_reason.basic != EXIT_REASON_EPT_VIOLATION &&
6035 exit_reason.basic != EXIT_REASON_PML_FULL &&
6036 exit_reason.basic != EXIT_REASON_APIC_ACCESS &&
6037 exit_reason.basic != EXIT_REASON_TASK_SWITCH)) {
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006038 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6039 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006040 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006041 vcpu->run->internal.data[0] = vectoring_info;
Sean Christopherson8e533242020-11-06 17:03:12 +08006042 vcpu->run->internal.data[1] = exit_reason.full;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006043 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
Sean Christopherson8e533242020-11-06 17:03:12 +08006044 if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG) {
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006045 vcpu->run->internal.ndata++;
6046 vcpu->run->internal.data[3] =
6047 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6048 }
Jim Mattson1aa561b2020-06-03 16:56:21 -07006049 vcpu->run->internal.data[vcpu->run->internal.ndata++] =
Jim Mattson8a14fe42020-06-03 16:56:22 -07006050 vcpu->arch.last_vmentry_cpu;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006051 return 0;
6052 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006053
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006054 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006055 vmx->loaded_vmcs->soft_vnmi_blocked)) {
Sean Christophersondb438592020-04-22 19:25:48 -07006056 if (!vmx_interrupt_blocked(vcpu)) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006057 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6058 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
6059 vcpu->arch.nmi_pending) {
6060 /*
6061 * This CPU don't support us in finding the end of an
6062 * NMI-blocked window if the guest runs with IRQs
6063 * disabled. So we pull the trigger after 1 s of
6064 * futile waiting, but inform the user about this.
6065 */
6066 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6067 "state on VCPU %d after 1 s timeout\n",
6068 __func__, vcpu->vcpu_id);
6069 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6070 }
6071 }
6072
Wanpeng Li404d5d72020-04-28 14:23:25 +08006073 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08006074 return 1;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006075
Sean Christopherson8e533242020-11-06 17:03:12 +08006076 if (exit_reason.basic >= kvm_vmx_max_exit_handlers)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006077 goto unexpected_vmexit;
6078#ifdef CONFIG_RETPOLINE
Sean Christopherson8e533242020-11-06 17:03:12 +08006079 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006080 return kvm_emulate_wrmsr(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006081 else if (exit_reason.basic == EXIT_REASON_PREEMPTION_TIMER)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006082 return handle_preemption_timer(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006083 else if (exit_reason.basic == EXIT_REASON_INTERRUPT_WINDOW)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006084 return handle_interrupt_window(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006085 else if (exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006086 return handle_external_interrupt(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006087 else if (exit_reason.basic == EXIT_REASON_HLT)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006088 return kvm_emulate_halt(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006089 else if (exit_reason.basic == EXIT_REASON_EPT_MISCONFIG)
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006090 return handle_ept_misconfig(vcpu);
6091#endif
6092
Sean Christopherson8e533242020-11-06 17:03:12 +08006093 exit_handler_index = array_index_nospec((u16)exit_reason.basic,
6094 kvm_vmx_max_exit_handlers);
6095 if (!kvm_vmx_exit_handlers[exit_handler_index])
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006096 goto unexpected_vmexit;
6097
Sean Christopherson8e533242020-11-06 17:03:12 +08006098 return kvm_vmx_exit_handlers[exit_handler_index](vcpu);
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006099
6100unexpected_vmexit:
Sean Christopherson8e533242020-11-06 17:03:12 +08006101 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
6102 exit_reason.full);
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006103 dump_vmcs();
6104 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6105 vcpu->run->internal.suberror =
6106 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
Jim Mattson1aa561b2020-06-03 16:56:21 -07006107 vcpu->run->internal.ndata = 2;
Sean Christopherson8e533242020-11-06 17:03:12 +08006108 vcpu->run->internal.data[0] = exit_reason.full;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006109 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006110 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006111}
6112
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08006113static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
6114{
6115 int ret = __vmx_handle_exit(vcpu, exit_fastpath);
6116
6117 /*
6118 * Even when current exit reason is handled by KVM internally, we
6119 * still need to exit to user space when bus lock detected to inform
6120 * that there is a bus lock in guest.
6121 */
6122 if (to_vmx(vcpu)->exit_reason.bus_lock_detected) {
6123 if (ret > 0)
6124 vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK;
6125
6126 vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK;
6127 return 0;
6128 }
6129 return ret;
6130}
6131
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006132/*
6133 * Software based L1D cache flush which is used when microcode providing
6134 * the cache control MSR is not loaded.
6135 *
6136 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
6137 * flush it is required to read in 64 KiB because the replacement algorithm
6138 * is not exactly LRU. This could be sized at runtime via topology
6139 * information but as all relevant affected CPUs have 32KiB L1D cache size
6140 * there is no point in doing so.
6141 */
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006142static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006143{
6144 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006145
6146 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02006147 * This code is only executed when the the flush mode is 'cond' or
6148 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006149 */
Nicolai Stange427362a2018-07-21 22:25:00 +02006150 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02006151 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006152
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006153 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02006154 * Clear the per-vcpu flush bit, it gets set again
6155 * either from vcpu_run() or from one of the unsafe
6156 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006157 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02006158 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02006159 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02006160
6161 /*
6162 * Clear the per-cpu flush bit, it gets set again from
6163 * the interrupt handlers.
6164 */
6165 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
6166 kvm_clear_cpu_l1tf_flush_l1d();
6167
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006168 if (!flush_l1d)
6169 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006170 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006171
6172 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006173
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006174 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006175 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006176 return;
6177 }
6178
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006179 asm volatile(
6180 /* First ensure the pages are in the TLB */
6181 "xorl %%eax, %%eax\n"
6182 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02006183 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006184 "addl $4096, %%eax\n\t"
6185 "cmpl %%eax, %[size]\n\t"
6186 "jne .Lpopulate_tlb\n\t"
6187 "xorl %%eax, %%eax\n\t"
6188 "cpuid\n\t"
6189 /* Now fill the cache */
6190 "xorl %%eax, %%eax\n"
6191 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006192 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006193 "addl $64, %%eax\n\t"
6194 "cmpl %%eax, %[size]\n\t"
6195 "jne .Lfill_cache\n\t"
6196 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006197 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006198 [size] "r" (size)
6199 : "eax", "ebx", "ecx", "edx");
6200}
6201
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006202static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006203{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006204 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Liran Alon132f4f72019-11-11 14:30:54 +02006205 int tpr_threshold;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006206
6207 if (is_guest_mode(vcpu) &&
6208 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6209 return;
6210
Liran Alon132f4f72019-11-11 14:30:54 +02006211 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
Liran Alon02d496cf2019-11-11 14:30:55 +02006212 if (is_guest_mode(vcpu))
6213 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6214 else
6215 vmcs_write32(TPR_THRESHOLD, tpr_threshold);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006216}
6217
Sean Christopherson97b7ead2018-12-03 13:53:16 -08006218void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08006219{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006220 struct vcpu_vmx *vmx = to_vmx(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006221 u32 sec_exec_control;
6222
Jim Mattson8d860bb2018-05-09 16:56:05 -04006223 if (!lapic_in_kernel(vcpu))
6224 return;
6225
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07006226 if (!flexpriority_enabled &&
6227 !cpu_has_vmx_virtualize_x2apic_mode())
6228 return;
6229
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006230 /* Postpone execution until vmcs01 is the current VMCS. */
6231 if (is_guest_mode(vcpu)) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006232 vmx->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006233 return;
6234 }
6235
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006236 sec_exec_control = secondary_exec_controls_get(vmx);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006237 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6238 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08006239
Jim Mattson8d860bb2018-05-09 16:56:05 -04006240 switch (kvm_get_apic_mode(vcpu)) {
6241 case LAPIC_MODE_INVALID:
6242 WARN_ONCE(true, "Invalid local APIC state");
6243 case LAPIC_MODE_DISABLED:
6244 break;
6245 case LAPIC_MODE_XAPIC:
6246 if (flexpriority_enabled) {
6247 sec_exec_control |=
6248 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006249 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6250
6251 /*
6252 * Flush the TLB, reloading the APIC access page will
6253 * only do so if its physical address has changed, but
6254 * the guest may have inserted a non-APIC mapping into
6255 * the TLB while the APIC access page was disabled.
6256 */
6257 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006258 }
6259 break;
6260 case LAPIC_MODE_X2APIC:
6261 if (cpu_has_vmx_virtualize_x2apic_mode())
6262 sec_exec_control |=
6263 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6264 break;
Yang Zhang8d146952013-01-25 10:18:50 +08006265 }
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006266 secondary_exec_controls_set(vmx, sec_exec_control);
Yang Zhang8d146952013-01-25 10:18:50 +08006267
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006268 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006269}
6270
Sean Christophersona4148b72020-03-20 14:28:24 -07006271static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
Tang Chen38b99172014-09-24 15:57:54 +08006272{
Sean Christophersona4148b72020-03-20 14:28:24 -07006273 struct page *page;
6274
Sean Christopherson1196cb92020-03-20 14:28:23 -07006275 /* Defer reload until vmcs01 is the current VMCS. */
6276 if (is_guest_mode(vcpu)) {
6277 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true;
6278 return;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07006279 }
Sean Christopherson1196cb92020-03-20 14:28:23 -07006280
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006281 if (!(secondary_exec_controls_get(to_vmx(vcpu)) &
6282 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
6283 return;
6284
Sean Christophersona4148b72020-03-20 14:28:24 -07006285 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
6286 if (is_error_page(page))
6287 return;
6288
6289 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
Sean Christopherson1196cb92020-03-20 14:28:23 -07006290 vmx_flush_tlb_current(vcpu);
Sean Christophersona4148b72020-03-20 14:28:24 -07006291
6292 /*
6293 * Do not pin apic access page in memory, the MMU notifier
6294 * will call us again if it is migrated or swapped out.
6295 */
6296 put_page(page);
Tang Chen38b99172014-09-24 15:57:54 +08006297}
6298
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006299static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006300{
6301 u16 status;
6302 u8 old;
6303
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006304 if (max_isr == -1)
6305 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006306
6307 status = vmcs_read16(GUEST_INTR_STATUS);
6308 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006309 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006310 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006311 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006312 vmcs_write16(GUEST_INTR_STATUS, status);
6313 }
6314}
6315
6316static void vmx_set_rvi(int vector)
6317{
6318 u16 status;
6319 u8 old;
6320
Wei Wang4114c272014-11-05 10:53:43 +08006321 if (vector == -1)
6322 vector = 0;
6323
Yang Zhangc7c9c562013-01-25 10:18:51 +08006324 status = vmcs_read16(GUEST_INTR_STATUS);
6325 old = (u8)status & 0xff;
6326 if ((u8)vector != old) {
6327 status &= ~0xff;
6328 status |= (u8)vector;
6329 vmcs_write16(GUEST_INTR_STATUS, status);
6330 }
6331}
6332
6333static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6334{
Liran Alon851c1a182017-12-24 18:12:56 +02006335 /*
6336 * When running L2, updating RVI is only relevant when
6337 * vmcs12 virtual-interrupt-delivery enabled.
6338 * However, it can be enabled only when L1 also
6339 * intercepts external-interrupts and in that case
6340 * we should not update vmcs02 RVI but instead intercept
6341 * interrupt. Therefore, do nothing when running L2.
6342 */
6343 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08006344 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006345}
6346
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006347static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006348{
6349 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006350 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02006351 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006352
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006353 WARN_ON(!vcpu->arch.apicv_active);
6354 if (pi_test_on(&vmx->pi_desc)) {
6355 pi_clear_on(&vmx->pi_desc);
6356 /*
Liran Alond9ff2742019-11-11 14:25:25 +02006357 * IOMMU can write to PID.ON, so the barrier matters even on UP.
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006358 * But on x86 this is just a compiler barrier anyway.
6359 */
6360 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02006361 max_irr_updated =
6362 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6363
6364 /*
6365 * If we are running L2 and L1 has a new pending interrupt
6366 * which can be injected, we should re-evaluate
6367 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02006368 * If L1 intercepts external-interrupts, we should
6369 * exit from L2 to L1. Otherwise, interrupt should be
6370 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02006371 */
Liran Alon851c1a182017-12-24 18:12:56 +02006372 if (is_guest_mode(vcpu) && max_irr_updated) {
6373 if (nested_exit_on_intr(vcpu))
6374 kvm_vcpu_exiting_guest_mode(vcpu);
6375 else
6376 kvm_make_request(KVM_REQ_EVENT, vcpu);
6377 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006378 } else {
6379 max_irr = kvm_lapic_find_highest_irr(vcpu);
6380 }
6381 vmx_hwapic_irr_update(vcpu, max_irr);
6382 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006383}
6384
Andrey Smetanin63086302015-11-10 15:36:32 +03006385static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006386{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006387 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08006388 return;
6389
Yang Zhangc7c9c562013-01-25 10:18:51 +08006390 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6391 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6392 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6393 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6394}
6395
Paolo Bonzini967235d2016-12-19 14:03:45 +01006396static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
6397{
6398 struct vcpu_vmx *vmx = to_vmx(vcpu);
6399
6400 pi_clear_on(&vmx->pi_desc);
6401 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
6402}
6403
Sean Christopherson535f7ef2020-09-15 12:15:04 -07006404void vmx_do_interrupt_nmi_irqoff(unsigned long entry);
6405
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006406static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, u32 intr_info)
6407{
6408 unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK;
6409 gate_desc *desc = (gate_desc *)host_idt_base + vector;
6410
6411 kvm_before_interrupt(vcpu);
6412 vmx_do_interrupt_nmi_irqoff(gate_offset(desc));
6413 kvm_after_interrupt(vcpu);
6414}
6415
Sean Christopherson95b5a482019-04-19 22:50:59 -07006416static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006417{
Sean Christopherson87915852020-04-15 13:34:54 -07006418 u32 intr_info = vmx_get_intr_info(&vmx->vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006419
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006420 /* if exit due to PF check for async PF */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006421 if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02006422 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags();
Andi Kleena0861c02009-06-08 17:37:09 +08006423 /* Handle machine checks before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006424 else if (is_machine_check(intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08006425 kvm_machine_check();
Gleb Natapov20f65982009-05-11 13:35:55 +03006426 /* We need to handle NMIs before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006427 else if (is_nmi(intr_info))
6428 handle_interrupt_nmi_irqoff(&vmx->vcpu, intr_info);
Avi Kivity51aa01d2010-07-20 14:31:20 +03006429}
Gleb Natapov20f65982009-05-11 13:35:55 +03006430
Sean Christopherson95b5a482019-04-19 22:50:59 -07006431static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006432{
Sean Christopherson87915852020-04-15 13:34:54 -07006433 u32 intr_info = vmx_get_intr_info(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006434
Sean Christopherson49def502019-04-19 22:50:56 -07006435 if (WARN_ONCE(!is_external_intr(intr_info),
6436 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
6437 return;
6438
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006439 handle_interrupt_nmi_irqoff(vcpu, intr_info);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006440}
Sean Christopherson95b5a482019-04-19 22:50:59 -07006441
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006442static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006443{
6444 struct vcpu_vmx *vmx = to_vmx(vcpu);
6445
Sean Christopherson8e533242020-11-06 17:03:12 +08006446 if (vmx->exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006447 handle_external_interrupt_irqoff(vcpu);
Sean Christopherson8e533242020-11-06 17:03:12 +08006448 else if (vmx->exit_reason.basic == EXIT_REASON_EXCEPTION_NMI)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006449 handle_exception_nmi_irqoff(vmx);
6450}
Yang Zhanga547c6d2013-04-11 19:25:10 +08006451
Tom Lendacky57194552020-12-10 11:10:00 -06006452/*
6453 * The kvm parameter can be NULL (module initialization, or invocation before
6454 * VM creation). Be sure to check the kvm parameter before using it.
6455 */
6456static bool vmx_has_emulated_msr(struct kvm *kvm, u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006457{
Tom Lendackybc226f02018-05-10 22:06:39 +02006458 switch (index) {
6459 case MSR_IA32_SMBASE:
6460 /*
6461 * We cannot do SMM unless we can run the guest in big
6462 * real mode.
6463 */
6464 return enable_unrestricted_guest || emulate_invalid_guest_state;
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02006465 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
6466 return nested;
Tom Lendackybc226f02018-05-10 22:06:39 +02006467 case MSR_AMD64_VIRT_SPEC_CTRL:
6468 /* This is AMD only. */
6469 return false;
6470 default:
6471 return true;
6472 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006473}
6474
Avi Kivity51aa01d2010-07-20 14:31:20 +03006475static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6476{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006477 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006478 bool unblock_nmi;
6479 u8 vector;
6480 bool idtv_info_valid;
6481
6482 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006483
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006484 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006485 if (vmx->loaded_vmcs->nmi_known_unmasked)
6486 return;
Sean Christopherson87915852020-04-15 13:34:54 -07006487
6488 exit_intr_info = vmx_get_intr_info(&vmx->vcpu);
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006489 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6490 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6491 /*
6492 * SDM 3: 27.7.1.2 (September 2008)
6493 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6494 * a guest IRET fault.
6495 * SDM 3: 23.2.2 (September 2008)
6496 * Bit 12 is undefined in any of the following cases:
6497 * If the VM exit sets the valid bit in the IDT-vectoring
6498 * information field.
6499 * If the VM exit is due to a double fault.
6500 */
6501 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6502 vector != DF_VECTOR && !idtv_info_valid)
6503 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6504 GUEST_INTR_STATE_NMI);
6505 else
6506 vmx->loaded_vmcs->nmi_known_unmasked =
6507 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6508 & GUEST_INTR_STATE_NMI);
6509 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
6510 vmx->loaded_vmcs->vnmi_blocked_time +=
6511 ktime_to_ns(ktime_sub(ktime_get(),
6512 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006513}
6514
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006515static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006516 u32 idt_vectoring_info,
6517 int instr_len_field,
6518 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006519{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006520 u8 vector;
6521 int type;
6522 bool idtv_info_valid;
6523
6524 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006525
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006526 vcpu->arch.nmi_injected = false;
6527 kvm_clear_exception_queue(vcpu);
6528 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006529
6530 if (!idtv_info_valid)
6531 return;
6532
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006533 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006534
Avi Kivity668f6122008-07-02 09:28:55 +03006535 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6536 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006537
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006538 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006539 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006540 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006541 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006542 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006543 * Clear bit "block by NMI" before VM entry if a NMI
6544 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006545 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006546 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006547 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006548 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006549 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006550 fallthrough;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006551 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006552 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006553 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03006554 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006555 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03006556 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006557 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006558 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006559 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006560 fallthrough;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006561 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006562 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006563 break;
6564 default:
6565 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006566 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006567}
6568
Avi Kivity83422e12010-07-20 14:43:23 +03006569static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6570{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006571 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006572 VM_EXIT_INSTRUCTION_LEN,
6573 IDT_VECTORING_ERROR_CODE);
6574}
6575
Avi Kivityb463a6f2010-07-20 15:06:17 +03006576static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6577{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006578 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006579 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6580 VM_ENTRY_INSTRUCTION_LEN,
6581 VM_ENTRY_EXCEPTION_ERROR_CODE);
6582
6583 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6584}
6585
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006586static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6587{
6588 int i, nr_msrs;
6589 struct perf_guest_switch_msr *msrs;
6590
6591 msrs = perf_guest_get_msrs(&nr_msrs);
6592
6593 if (!msrs)
6594 return;
6595
6596 for (i = 0; i < nr_msrs; i++)
6597 if (msrs[i].host == msrs[i].guest)
6598 clear_atomic_switch_msr(vmx, msrs[i].msr);
6599 else
6600 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006601 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006602}
6603
Sean Christophersonf459a702018-08-27 15:21:11 -07006604static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006605{
6606 struct vcpu_vmx *vmx = to_vmx(vcpu);
6607 u64 tscl;
6608 u32 delta_tsc;
6609
Sean Christophersond264ee02018-08-27 15:21:12 -07006610 if (vmx->req_immediate_exit) {
Sean Christopherson804939e2019-05-07 12:18:05 -07006611 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
6612 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6613 } else if (vmx->hv_deadline_tsc != -1) {
Sean Christophersonf459a702018-08-27 15:21:11 -07006614 tscl = rdtsc();
6615 if (vmx->hv_deadline_tsc > tscl)
6616 /* set_hv_timer ensures the delta fits in 32-bits */
6617 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6618 cpu_preemption_timer_multi);
6619 else
6620 delta_tsc = 0;
6621
Sean Christopherson804939e2019-05-07 12:18:05 -07006622 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
6623 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6624 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
6625 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
6626 vmx->loaded_vmcs->hv_timer_soft_disabled = true;
Sean Christophersonf459a702018-08-27 15:21:11 -07006627 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006628}
6629
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006630void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006631{
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006632 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
6633 vmx->loaded_vmcs->host_state.rsp = host_rsp;
6634 vmcs_writel(HOST_RSP, host_rsp);
6635 }
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006636}
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006637
Wanpeng Li404d5d72020-04-28 14:23:25 +08006638static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006639{
Sean Christopherson8e533242020-11-06 17:03:12 +08006640 switch (to_vmx(vcpu)->exit_reason.basic) {
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006641 case EXIT_REASON_MSR_WRITE:
6642 return handle_fastpath_set_msr_irqoff(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04006643 case EXIT_REASON_PREEMPTION_TIMER:
6644 return handle_fastpath_preemption_timer(vcpu);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006645 default:
6646 return EXIT_FASTPATH_NONE;
6647 }
6648}
6649
Sean Christophersonfc2ba5a2019-01-25 07:41:19 -08006650bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006651
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006652static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
6653 struct vcpu_vmx *vmx)
6654{
6655 /*
6656 * VMENTER enables interrupts (host state), but the kernel state is
6657 * interrupts disabled when this is invoked. Also tell RCU about
6658 * it. This is the same logic as for exit_to_user_mode().
6659 *
6660 * This ensures that e.g. latency analysis on the host observes
6661 * guest mode as interrupt enabled.
6662 *
6663 * guest_enter_irqoff() informs context tracking about the
6664 * transition to guest mode and if enabled adjusts RCU state
6665 * accordingly.
6666 */
6667 instrumentation_begin();
6668 trace_hardirqs_on_prepare();
6669 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
6670 instrumentation_end();
6671
6672 guest_enter_irqoff();
6673 lockdep_hardirqs_on(CALLER_ADDR0);
6674
6675 /* L1D Flush includes CPU buffer clear to mitigate MDS */
6676 if (static_branch_unlikely(&vmx_l1d_should_flush))
6677 vmx_l1d_flush(vcpu);
6678 else if (static_branch_unlikely(&mds_user_clear))
6679 mds_clear_cpu_buffers();
6680
Thomas Gleixner2245d392020-07-08 21:52:00 +02006681 if (vcpu->arch.cr2 != native_read_cr2())
6682 native_write_cr2(vcpu->arch.cr2);
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006683
6684 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
6685 vmx->loaded_vmcs->launched);
6686
Thomas Gleixner2245d392020-07-08 21:52:00 +02006687 vcpu->arch.cr2 = native_read_cr2();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006688
6689 /*
6690 * VMEXIT disables interrupts (host state), but tracing and lockdep
6691 * have them in state 'on' as recorded before entering guest mode.
6692 * Same as enter_from_user_mode().
6693 *
6694 * guest_exit_irqoff() restores host context and reinstates RCU if
6695 * enabled and required.
6696 *
6697 * This needs to be done before the below as native_read_msr()
6698 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
6699 * into world and some more.
6700 */
6701 lockdep_hardirqs_off(CALLER_ADDR0);
6702 guest_exit_irqoff();
6703
6704 instrumentation_begin();
6705 trace_hardirqs_off_finish();
6706 instrumentation_end();
6707}
6708
Wanpeng Li404d5d72020-04-28 14:23:25 +08006709static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006710{
Wanpeng Li404d5d72020-04-28 14:23:25 +08006711 fastpath_t exit_fastpath;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006712 struct vcpu_vmx *vmx = to_vmx(vcpu);
6713 unsigned long cr3, cr4;
6714
Wanpeng Li404d5d72020-04-28 14:23:25 +08006715reenter_guest:
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006716 /* Record the guest's net vcpu time for enforced NMI injections. */
6717 if (unlikely(!enable_vnmi &&
6718 vmx->loaded_vmcs->soft_vnmi_blocked))
6719 vmx->loaded_vmcs->entry_time = ktime_get();
6720
6721 /* Don't enter VMX if guest state is invalid, let the exit handler
6722 start emulation until we arrive back to a valid state */
6723 if (vmx->emulation_required)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006724 return EXIT_FASTPATH_NONE;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006725
Lorenzo Bresciad95df952020-12-23 14:45:07 +00006726 trace_kvm_entry(vcpu);
6727
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006728 if (vmx->ple_window_dirty) {
6729 vmx->ple_window_dirty = false;
6730 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6731 }
6732
wanpeng lic9dfd3f2020-02-17 18:37:43 +08006733 /*
6734 * We did this in prepare_switch_to_guest, because it needs to
6735 * be within srcu_read_lock.
6736 */
6737 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006738
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006739 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006740 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006741 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006742 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6743
6744 cr3 = __get_current_cr3_fast();
6745 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6746 vmcs_writel(HOST_CR3, cr3);
6747 vmx->loaded_vmcs->host_state.cr3 = cr3;
6748 }
6749
6750 cr4 = cr4_read_shadow();
6751 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
6752 vmcs_writel(HOST_CR4, cr4);
6753 vmx->loaded_vmcs->host_state.cr4 = cr4;
6754 }
6755
6756 /* When single-stepping over STI and MOV SS, we must clear the
6757 * corresponding interruptibility bits in the guest state. Otherwise
6758 * vmentry fails as it then expects bit 14 (BS) in pending debug
6759 * exceptions being set, but that's not correct for the guest debugging
6760 * case. */
6761 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6762 vmx_set_interrupt_shadow(vcpu, 0);
6763
Aaron Lewis139a12c2019-10-21 16:30:25 -07006764 kvm_load_guest_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006765
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006766 pt_guest_enter(vmx);
6767
Vitaly Kuznetsov49097762020-06-19 11:40:46 +02006768 atomic_switch_perf_msrs(vmx);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006769
Sean Christopherson804939e2019-05-07 12:18:05 -07006770 if (enable_preemption_timer)
6771 vmx_update_hv_timer(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006772
Wanpeng Li010fd372020-09-10 17:50:41 +08006773 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08006774
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006775 /*
6776 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6777 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6778 * is no need to worry about the conditional branch over the wrmsr
6779 * being speculatively taken.
6780 */
6781 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
6782
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006783 /* The actual VMENTER/EXIT is in the .noinstr.text section. */
6784 vmx_vcpu_enter_exit(vcpu, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006785
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006786 /*
6787 * We do not use IBRS in the kernel. If this vCPU has used the
6788 * SPEC_CTRL MSR it may have left it on; save the value and
6789 * turn it off. This is much more efficient than blindly adding
6790 * it to the atomic save/restore list. Especially as the former
6791 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6792 *
6793 * For non-nested case:
6794 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6795 * save it.
6796 *
6797 * For nested case:
6798 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6799 * save it.
6800 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006801 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006802 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006803
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006804 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006805
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006806 /* All fields are clean at this point */
6807 if (static_branch_unlikely(&enable_evmcs))
6808 current_evmcs->hv_clean_fields |=
6809 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6810
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08006811 if (static_branch_unlikely(&enable_evmcs))
6812 current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index;
6813
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006814 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006815 if (vmx->host_debugctlmsr)
6816 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006817
Avi Kivityaa67f602012-08-01 16:48:03 +03006818#ifndef CONFIG_X86_64
6819 /*
6820 * The sysexit path does not restore ds/es, so we must set them to
6821 * a reasonable value ourselves.
6822 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006823 * We can't defer this to vmx_prepare_switch_to_host() since that
6824 * function may be executed in interrupt context, which saves and
6825 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006826 */
6827 loadsegment(ds, __USER_DS);
6828 loadsegment(es, __USER_DS);
6829#endif
6830
Sean Christophersone5d03de2020-04-15 13:34:51 -07006831 vmx_register_cache_reset(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006832
Chao Peng2ef444f2018-10-24 16:05:12 +08006833 pt_guest_exit(vmx);
6834
Aaron Lewis139a12c2019-10-21 16:30:25 -07006835 kvm_load_host_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006836
Gleb Natapove0b890d2013-09-25 12:51:33 +03006837 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006838 vmx->idt_vectoring_info = 0;
6839
Sean Christopherson873e1da2020-04-10 10:47:02 -07006840 if (unlikely(vmx->fail)) {
Sean Christopherson8e533242020-11-06 17:03:12 +08006841 vmx->exit_reason.full = 0xdead;
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006842 return EXIT_FASTPATH_NONE;
Sean Christopherson873e1da2020-04-10 10:47:02 -07006843 }
6844
Sean Christopherson8e533242020-11-06 17:03:12 +08006845 vmx->exit_reason.full = vmcs_read32(VM_EXIT_REASON);
6846 if (unlikely((u16)vmx->exit_reason.basic == EXIT_REASON_MCE_DURING_VMENTRY))
Sean Christophersonbeb8d932019-04-19 22:50:55 -07006847 kvm_machine_check();
6848
Sean Christopherson8e533242020-11-06 17:03:12 +08006849 trace_kvm_exit(vmx->exit_reason.full, vcpu, KVM_ISA_VMX);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006850
Sean Christopherson8e533242020-11-06 17:03:12 +08006851 if (unlikely(vmx->exit_reason.failed_vmentry))
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006852 return EXIT_FASTPATH_NONE;
6853
Jim Mattsonb060ca32017-09-14 16:31:42 -07006854 vmx->loaded_vmcs->launched = 1;
6855 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03006856
Avi Kivity51aa01d2010-07-20 14:31:20 +03006857 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006858 vmx_complete_interrupts(vmx);
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006859
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006860 if (is_guest_mode(vcpu))
6861 return EXIT_FASTPATH_NONE;
6862
6863 exit_fastpath = vmx_exit_handlers_fastpath(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006864 if (exit_fastpath == EXIT_FASTPATH_REENTER_GUEST) {
6865 if (!kvm_vcpu_exit_request(vcpu)) {
6866 /*
6867 * FIXME: this goto should be a loop in vcpu_enter_guest,
6868 * but it would incur the cost of a retpoline for now.
6869 * Revisit once static calls are available.
6870 */
Wanpeng Li379a3c82020-04-28 14:23:27 +08006871 if (vcpu->arch.apicv_active)
6872 vmx_sync_pir_to_irr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006873 goto reenter_guest;
6874 }
6875 exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
6876 }
6877
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006878 return exit_fastpath;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006879}
6880
Avi Kivity6aa8b732006-12-10 02:21:36 -08006881static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6882{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006883 struct vcpu_vmx *vmx = to_vmx(vcpu);
6884
Kai Huang843e4332015-01-28 10:54:28 +08006885 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006886 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006887 free_vpid(vmx->vpid);
Sean Christopherson55d23752018-12-03 13:53:18 -08006888 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006889 free_loaded_vmcs(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006890}
6891
Sean Christopherson987b2592019-12-18 13:54:55 -08006892static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006893{
Ben Gardon41836832019-02-11 11:02:52 -08006894 struct vcpu_vmx *vmx;
Sean Christopherson34109c02019-12-18 13:54:50 -08006895 int i, cpu, err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006896
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006897 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0);
6898 vmx = to_vmx(vcpu);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006899
Peter Feiner4e595162016-07-07 14:49:58 -07006900 err = -ENOMEM;
6901
Sean Christopherson034d8e22019-12-18 13:54:49 -08006902 vmx->vpid = allocate_vpid();
6903
Peter Feiner4e595162016-07-07 14:49:58 -07006904 /*
6905 * If PML is turned on, failure on enabling PML just results in failure
6906 * of creating the vcpu, therefore we can simplify PML logic (by
6907 * avoiding dealing with cases, such as enabling PML partially on vcpus
Miaohe Lin67b0ae42019-12-11 14:26:22 +08006908 * for the guest), etc.
Peter Feiner4e595162016-07-07 14:49:58 -07006909 */
6910 if (enable_pml) {
Ben Gardon41836832019-02-11 11:02:52 -08006911 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Peter Feiner4e595162016-07-07 14:49:58 -07006912 if (!vmx->pml_pg)
Sean Christopherson987b2592019-12-18 13:54:55 -08006913 goto free_vpid;
Peter Feiner4e595162016-07-07 14:49:58 -07006914 }
6915
Sean Christopherson14a61b62020-09-23 11:04:08 -07006916 BUILD_BUG_ON(ARRAY_SIZE(vmx_uret_msrs_list) != MAX_NR_USER_RETURN_MSRS);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006917
Sean Christopherson14a61b62020-09-23 11:04:08 -07006918 for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i) {
6919 u32 index = vmx_uret_msrs_list[i];
Xiaoyao Li4be53412019-10-20 17:11:00 +08006920 u32 data_low, data_high;
Sean Christophersonfbc18002020-09-23 11:03:59 -07006921 int j = vmx->nr_uret_msrs;
Xiaoyao Li4be53412019-10-20 17:11:00 +08006922
6923 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6924 continue;
6925 if (wrmsr_safe(index, data_low, data_high) < 0)
6926 continue;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006927
Sean Christopherson802145c2020-09-23 11:04:09 -07006928 vmx->guest_uret_msrs[j].slot = i;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006929 vmx->guest_uret_msrs[j].data = 0;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006930 switch (index) {
6931 case MSR_IA32_TSX_CTRL:
6932 /*
Paolo Bonzini71316362021-01-28 11:45:00 -05006933 * TSX_CTRL_CPUID_CLEAR is handled in the CPUID
6934 * interception. Keep the host value unchanged to avoid
6935 * changing CPUID bits under the host kernel's feet.
6936 *
6937 * hle=0, rtm=0, tsx_ctrl=1 can be found with some
6938 * combinations of new kernel and old userspace. If
6939 * those guests run on a tsx=off host, do allow guests
6940 * to use TSX_CTRL, but do not change the value on the
6941 * host so that TSX remains always disabled.
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006942 */
Paolo Bonzini71316362021-01-28 11:45:00 -05006943 if (boot_cpu_has(X86_FEATURE_RTM))
6944 vmx->guest_uret_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
6945 else
6946 vmx->guest_uret_msrs[j].mask = 0;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006947 break;
6948 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006949 vmx->guest_uret_msrs[j].mask = -1ull;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006950 break;
6951 }
Sean Christophersonfbc18002020-09-23 11:03:59 -07006952 ++vmx->nr_uret_msrs;
Xiaoyao Li4be53412019-10-20 17:11:00 +08006953 }
6954
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006955 err = alloc_loaded_vmcs(&vmx->vmcs01);
6956 if (err < 0)
Jim Mattson7d737102019-12-03 16:24:42 -08006957 goto free_pml;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006958
Alexander Graf3eb90012020-09-25 16:34:20 +02006959 /* The MSR bitmap starts with all ones */
6960 bitmap_fill(vmx->shadow_msr_intercept.read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
6961 bitmap_fill(vmx->shadow_msr_intercept.write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
6962
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006963 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_TSC, MSR_TYPE_R);
6964 vmx_disable_intercept_for_msr(vcpu, MSR_FS_BASE, MSR_TYPE_RW);
6965 vmx_disable_intercept_for_msr(vcpu, MSR_GS_BASE, MSR_TYPE_RW);
6966 vmx_disable_intercept_for_msr(vcpu, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
6967 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6968 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6969 vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
Sean Christopherson987b2592019-12-18 13:54:55 -08006970 if (kvm_cstate_in_guest(vcpu->kvm)) {
Aaron Lewis476c9bd2020-09-25 16:34:18 +02006971 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C1_RES, MSR_TYPE_R);
6972 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
6973 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
6974 vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
Wanpeng Lib5170062019-05-21 14:06:53 +08006975 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006976 vmx->msr_bitmap_mode = 0;
6977
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006978 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03006979 cpu = get_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006980 vmx_vcpu_load(vcpu, cpu);
6981 vcpu->cpu = cpu;
Xiaoyao Li1b842922019-10-20 17:11:01 +08006982 init_vmcs(vmx);
Sean Christopherson34109c02019-12-18 13:54:50 -08006983 vmx_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006984 put_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006985 if (cpu_need_virtualize_apic_accesses(vcpu)) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006986 err = alloc_apic_access_page(vcpu->kvm);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006987 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006988 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02006989 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006990
Sean Christophersone90008d2018-03-05 12:04:37 -08006991 if (enable_ept && !enable_unrestricted_guest) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006992 err = init_rmode_identity_map(vcpu->kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08006993 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02006994 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006995 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006996
Roman Kagan63aff652018-07-19 21:59:07 +03006997 if (nested)
Chenyi Qiangb9757a42020-08-28 16:56:22 +08006998 memcpy(&vmx->nested.msrs, &vmcs_config.nested, sizeof(vmx->nested.msrs));
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006999 else
7000 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08007001
Wincy Van705699a2015-02-03 23:58:17 +08007002 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007003 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007004
Paolo Bonzinibab0c312020-02-11 18:40:58 +01007005 vcpu->arch.microcode_version = 0x100000000ULL;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007006 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007007
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02007008 /*
7009 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
7010 * or POSTED_INTR_WAKEUP_VECTOR.
7011 */
7012 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
7013 vmx->pi_desc.sn = 1;
7014
Lan Tianyu53963a72018-12-06 15:34:36 +08007015 vmx->ept_pointer = INVALID_PAGE;
7016
Sean Christophersona9dd6f02019-12-18 13:54:52 -08007017 return 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007018
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007019free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08007020 free_loaded_vmcs(vmx->loaded_vmcs);
Peter Feiner4e595162016-07-07 14:49:58 -07007021free_pml:
7022 vmx_destroy_pml_buffer(vmx);
Sean Christopherson987b2592019-12-18 13:54:55 -08007023free_vpid:
Wanpeng Li991e7a02015-09-16 17:30:05 +08007024 free_vpid(vmx->vpid);
Sean Christophersona9dd6f02019-12-18 13:54:52 -08007025 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007026}
7027
Thomas Gleixner65fd4cb2019-02-19 11:10:49 +01007028#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"
7029#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 -04007030
Wanpeng Lib31c1142018-03-12 04:53:04 -07007031static int vmx_vm_init(struct kvm *kvm)
7032{
Tianyu Lan877ad952018-07-19 08:40:23 +00007033 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
7034
Wanpeng Lib31c1142018-03-12 04:53:04 -07007035 if (!ple_gap)
7036 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007037
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007038 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
7039 switch (l1tf_mitigation) {
7040 case L1TF_MITIGATION_OFF:
7041 case L1TF_MITIGATION_FLUSH_NOWARN:
7042 /* 'I explicitly don't care' is set */
7043 break;
7044 case L1TF_MITIGATION_FLUSH:
7045 case L1TF_MITIGATION_FLUSH_NOSMT:
7046 case L1TF_MITIGATION_FULL:
7047 /*
7048 * Warn upon starting the first VM in a potentially
7049 * insecure environment.
7050 */
Josh Poimboeufb2849092019-01-30 07:13:58 -06007051 if (sched_smt_active())
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007052 pr_warn_once(L1TF_MSG_SMT);
7053 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
7054 pr_warn_once(L1TF_MSG_L1D);
7055 break;
7056 case L1TF_MITIGATION_FULL_FORCE:
7057 /* Flush is enforced */
7058 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007059 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007060 }
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007061 kvm_apicv_init(kvm, enable_apicv);
Wanpeng Lib31c1142018-03-12 04:53:04 -07007062 return 0;
7063}
7064
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007065static int __init vmx_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03007066{
7067 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08007068 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007069
Sean Christophersonff10e222019-12-20 20:45:10 -08007070 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
7071 !this_cpu_has(X86_FEATURE_VMX)) {
7072 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id());
7073 return -EIO;
7074 }
7075
Sean Christopherson7caaa712018-12-03 13:53:01 -08007076 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007077 return -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007078 if (nested)
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01007079 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept);
Yang, Sheng002c7f72007-07-31 14:23:01 +03007080 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7081 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7082 smp_processor_id());
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007083 return -EIO;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007084 }
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007085 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007086}
7087
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007088static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007089{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007090 u8 cache;
7091 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007092
Chia-I Wu222f06e2020-02-13 13:30:34 -08007093 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in
7094 * memory aliases with conflicting memory types and sometimes MCEs.
7095 * We have to be careful as to what are honored and when.
7096 *
7097 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to
7098 * UC. The effective memory type is UC or WC depending on guest PAT.
7099 * This was historically the source of MCEs and we want to be
7100 * conservative.
7101 *
7102 * When there is no need to deal with noncoherent DMA (e.g., no VT-d
7103 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The
7104 * EPT memory type is set to WB. The effective memory type is forced
7105 * WB.
7106 *
7107 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The
7108 * EPT memory type is used to emulate guest CD/MTRR.
Sheng Yang522c68c2009-04-27 20:35:43 +08007109 */
Chia-I Wu222f06e2020-02-13 13:30:34 -08007110
Paolo Bonzini606decd2015-10-01 13:12:47 +02007111 if (is_mmio) {
7112 cache = MTRR_TYPE_UNCACHABLE;
7113 goto exit;
7114 }
7115
7116 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007117 ipat = VMX_EPT_IPAT_BIT;
7118 cache = MTRR_TYPE_WRBACK;
7119 goto exit;
7120 }
7121
7122 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
7123 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02007124 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08007125 cache = MTRR_TYPE_WRBACK;
7126 else
7127 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007128 goto exit;
7129 }
7130
Xiao Guangrongff536042015-06-15 16:55:22 +08007131 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007132
7133exit:
7134 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08007135}
7136
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007137static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007138{
7139 /*
7140 * These bits in the secondary execution controls field
7141 * are dynamic, the others are mostly based on the hypervisor
7142 * architecture and the guest's CPUID. Do not touch the
7143 * dynamic bits.
7144 */
7145 u32 mask =
7146 SECONDARY_EXEC_SHADOW_VMCS |
7147 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02007148 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7149 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007150
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007151 u32 new_ctl = vmx->secondary_exec_control;
7152 u32 cur_ctl = secondary_exec_controls_get(vmx);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007153
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007154 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007155}
7156
David Matlack8322ebb2016-11-29 18:14:09 -08007157/*
7158 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
7159 * (indicating "allowed-1") if they are supported in the guest's CPUID.
7160 */
7161static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
7162{
7163 struct vcpu_vmx *vmx = to_vmx(vcpu);
7164 struct kvm_cpuid_entry2 *entry;
7165
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007166 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
7167 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08007168
7169#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
7170 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007171 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08007172} while (0)
7173
7174 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007175 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME));
7176 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME));
7177 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC));
7178 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE));
7179 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE));
7180 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE));
7181 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE));
7182 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE));
7183 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR));
7184 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM));
7185 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX));
7186 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX));
7187 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID));
7188 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE));
David Matlack8322ebb2016-11-29 18:14:09 -08007189
7190 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007191 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE));
7192 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP));
7193 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP));
7194 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU));
7195 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP));
7196 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57));
David Matlack8322ebb2016-11-29 18:14:09 -08007197
7198#undef cr4_fixed1_update
7199}
7200
Liran Alon5f76f6f2018-09-14 03:25:52 +03007201static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
7202{
7203 struct vcpu_vmx *vmx = to_vmx(vcpu);
7204
7205 if (kvm_mpx_supported()) {
7206 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
7207
7208 if (mpx_enabled) {
7209 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7210 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7211 } else {
7212 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7213 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7214 }
7215 }
7216}
7217
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007218static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7219{
7220 struct vcpu_vmx *vmx = to_vmx(vcpu);
7221 struct kvm_cpuid_entry2 *best = NULL;
7222 int i;
7223
7224 for (i = 0; i < PT_CPUID_LEAVES; i++) {
7225 best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7226 if (!best)
7227 return;
7228 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7229 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7230 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7231 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7232 }
7233
7234 /* Get the number of configurable Address Ranges for filtering */
7235 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7236 PT_CAP_num_address_ranges);
7237
7238 /* Initialize and clear the no dependency bits */
7239 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7240 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7241
7242 /*
7243 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7244 * will inject an #GP
7245 */
7246 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7247 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7248
7249 /*
7250 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7251 * PSBFreq can be set
7252 */
7253 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7254 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7255 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7256
7257 /*
7258 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7259 * MTCFreq can be set
7260 */
7261 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7262 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7263 RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7264
7265 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7266 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7267 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7268 RTIT_CTL_PTW_EN);
7269
7270 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7271 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7272 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7273
7274 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7275 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7276 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7277
7278 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */
7279 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7280 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7281
7282 /* unmask address range configure area */
7283 for (i = 0; i < vmx->pt_desc.addr_range; i++)
Gustavo A. R. Silvad14eff12018-12-26 14:40:59 -06007284 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007285}
7286
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007287static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08007288{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007289 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007290
Aaron Lewis72041602019-10-21 16:30:20 -07007291 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7292 vcpu->arch.xsaves_enabled = false;
7293
Paolo Bonzini80154d72017-08-24 13:55:35 +02007294 if (cpu_has_secondary_exec_ctrls()) {
7295 vmx_compute_secondary_exec_control(vmx);
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007296 vmcs_set_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007297 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007298
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007299 if (nested_vmx_allowed(vcpu))
7300 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007301 FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7302 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007303 else
7304 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007305 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7306 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX);
David Matlack8322ebb2016-11-29 18:14:09 -08007307
Liran Alon5f76f6f2018-09-14 03:25:52 +03007308 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08007309 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03007310 nested_vmx_entry_exit_ctls_update(vcpu);
7311 }
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007312
7313 if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7314 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7315 update_intel_pt_cfg(vcpu);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007316
7317 if (boot_cpu_has(X86_FEATURE_RTM)) {
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07007318 struct vmx_uret_msr *msr;
Sean Christophersond85a8032020-09-23 11:04:06 -07007319 msr = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007320 if (msr) {
7321 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
Sean Christopherson7bf662b2020-09-23 11:04:07 -07007322 vmx_set_guest_uret_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007323 }
7324 }
Sean Christophersona6337a32020-09-29 21:16:57 -07007325
Sean Christopherson2ed41aa2020-09-29 21:16:58 -07007326 set_cr4_guest_host_mask(vmx);
7327
Sean Christophersona6337a32020-09-29 21:16:57 -07007328 /* Refresh #PF interception to account for MAXPHYADDR changes. */
7329 update_exception_bitmap(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08007330}
7331
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007332static __init void vmx_set_cpu_caps(void)
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007333{
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007334 kvm_set_cpu_caps();
7335
7336 /* CPUID 0x1 */
7337 if (nested)
7338 kvm_cpu_cap_set(X86_FEATURE_VMX);
7339
7340 /* CPUID 0x7 */
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007341 if (kvm_mpx_supported())
7342 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX);
7343 if (cpu_has_vmx_invpcid())
7344 kvm_cpu_cap_check_and_set(X86_FEATURE_INVPCID);
7345 if (vmx_pt_mode_is_host_guest())
7346 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT);
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007347
Sean Christopherson90d2f602020-03-02 15:56:47 -08007348 if (vmx_umip_emulated())
7349 kvm_cpu_cap_set(X86_FEATURE_UMIP);
7350
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007351 /* CPUID 0xD.1 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01007352 supported_xss = 0;
Sean Christophersonbecdad82020-09-23 09:50:45 -07007353 if (!cpu_has_vmx_xsaves())
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007354 kvm_cpu_cap_clear(X86_FEATURE_XSAVES);
7355
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007356 /* CPUID 0x80000001 */
7357 if (!cpu_has_vmx_rdtscp())
7358 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007359
Sean Christophersonbecdad82020-09-23 09:50:45 -07007360 if (cpu_has_vmx_waitpkg())
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007361 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007362}
7363
Sean Christophersond264ee02018-08-27 15:21:12 -07007364static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7365{
7366 to_vmx(vcpu)->req_immediate_exit = true;
7367}
7368
Oliver Upton35a57132020-02-04 15:26:31 -08007369static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
7370 struct x86_instruction_info *info)
7371{
7372 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7373 unsigned short port;
7374 bool intercept;
7375 int size;
7376
7377 if (info->intercept == x86_intercept_in ||
7378 info->intercept == x86_intercept_ins) {
7379 port = info->src_val;
7380 size = info->dst_bytes;
7381 } else {
7382 port = info->dst_val;
7383 size = info->src_bytes;
7384 }
7385
7386 /*
7387 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
7388 * VM-exits depend on the 'unconditional IO exiting' VM-execution
7389 * control.
7390 *
7391 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
7392 */
7393 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7394 intercept = nested_cpu_has(vmcs12,
7395 CPU_BASED_UNCOND_IO_EXITING);
7396 else
7397 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
7398
Oliver Upton86f7e902020-02-29 11:30:14 -08007399 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
Oliver Upton35a57132020-02-04 15:26:31 -08007400 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
7401}
7402
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007403static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7404 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007405 enum x86_intercept_stage stage,
7406 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007407{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007408 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007409
Oliver Upton35a57132020-02-04 15:26:31 -08007410 switch (info->intercept) {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007411 /*
7412 * RDPID causes #UD if disabled through secondary execution controls.
7413 * Because it is marked as EmulateOnUD, we need to intercept it here.
7414 */
Oliver Upton35a57132020-02-04 15:26:31 -08007415 case x86_intercept_rdtscp:
Sean Christopherson7f3603b2020-09-23 09:50:47 -07007416 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_RDTSCP)) {
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007417 exception->vector = UD_VECTOR;
7418 exception->error_code_valid = false;
Oliver Upton35a57132020-02-04 15:26:31 -08007419 return X86EMUL_PROPAGATE_FAULT;
7420 }
7421 break;
7422
7423 case x86_intercept_in:
7424 case x86_intercept_ins:
7425 case x86_intercept_out:
7426 case x86_intercept_outs:
7427 return vmx_check_intercept_io(vcpu, info);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007428
Oliver Upton86f7e902020-02-29 11:30:14 -08007429 case x86_intercept_lgdt:
7430 case x86_intercept_lidt:
7431 case x86_intercept_lldt:
7432 case x86_intercept_ltr:
7433 case x86_intercept_sgdt:
7434 case x86_intercept_sidt:
7435 case x86_intercept_sldt:
7436 case x86_intercept_str:
7437 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
7438 return X86EMUL_CONTINUE;
7439
7440 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
7441 break;
7442
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007443 /* TODO: check more intercepts... */
Oliver Upton35a57132020-02-04 15:26:31 -08007444 default:
7445 break;
7446 }
7447
Paolo Bonzini07721fe2020-02-04 15:26:29 -08007448 return X86EMUL_UNHANDLEABLE;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007449}
7450
Yunhong Jiang64672c92016-06-13 14:19:59 -07007451#ifdef CONFIG_X86_64
7452/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
7453static inline int u64_shl_div_u64(u64 a, unsigned int shift,
7454 u64 divisor, u64 *result)
7455{
7456 u64 low = a << shift, high = a >> (64 - shift);
7457
7458 /* To avoid the overflow on divq */
7459 if (high >= divisor)
7460 return 1;
7461
7462 /* Low hold the result, high hold rem which is discarded */
7463 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
7464 "rm" (divisor), "0" (low), "1" (high));
7465 *result = low;
7466
7467 return 0;
7468}
7469
Sean Christophersonf9927982019-04-16 13:32:46 -07007470static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
7471 bool *expired)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007472{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007473 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007474 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
Sean Christopherson39497d72019-04-17 10:15:32 -07007475 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007476
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007477 vmx = to_vmx(vcpu);
7478 tscl = rdtsc();
7479 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
7480 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Sean Christopherson39497d72019-04-17 10:15:32 -07007481 lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
7482 ktimer->timer_advance_ns);
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007483
7484 if (delta_tsc > lapic_timer_advance_cycles)
7485 delta_tsc -= lapic_timer_advance_cycles;
7486 else
7487 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007488
7489 /* Convert to host delta tsc if tsc scaling is enabled */
7490 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
Sean Christopherson0967fa12019-04-16 13:32:48 -07007491 delta_tsc && u64_shl_div_u64(delta_tsc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007492 kvm_tsc_scaling_ratio_frac_bits,
Sean Christopherson0967fa12019-04-16 13:32:48 -07007493 vcpu->arch.tsc_scaling_ratio, &delta_tsc))
Yunhong Jiang64672c92016-06-13 14:19:59 -07007494 return -ERANGE;
7495
7496 /*
7497 * If the delta tsc can't fit in the 32 bit after the multi shift,
7498 * we can't use the preemption timer.
7499 * It's possible that it fits on later vmentries, but checking
7500 * on every vmentry is costly so we just use an hrtimer.
7501 */
7502 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
7503 return -ERANGE;
7504
7505 vmx->hv_deadline_tsc = tscl + delta_tsc;
Sean Christophersonf9927982019-04-16 13:32:46 -07007506 *expired = !delta_tsc;
7507 return 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007508}
7509
7510static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
7511{
Sean Christophersonf459a702018-08-27 15:21:11 -07007512 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007513}
7514#endif
7515
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007516static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007517{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007518 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007519 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007520}
7521
Kai Huang843e4332015-01-28 10:54:28 +08007522static void vmx_slot_enable_log_dirty(struct kvm *kvm,
7523 struct kvm_memory_slot *slot)
7524{
Jay Zhou3c9bd402020-02-27 09:32:27 +08007525 if (!kvm_dirty_log_manual_protect_and_init_set(kvm))
7526 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
Kai Huang843e4332015-01-28 10:54:28 +08007527 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
7528}
7529
7530static void vmx_slot_disable_log_dirty(struct kvm *kvm,
7531 struct kvm_memory_slot *slot)
7532{
7533 kvm_mmu_slot_set_dirty(kvm, slot);
7534}
7535
7536static void vmx_flush_log_dirty(struct kvm *kvm)
7537{
7538 kvm_flush_pml_buffers(kvm);
7539}
7540
7541static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
7542 struct kvm_memory_slot *memslot,
7543 gfn_t offset, unsigned long mask)
7544{
7545 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
7546}
7547
Yunhong Jiangbc225122016-06-13 14:19:58 -07007548static int vmx_pre_block(struct kvm_vcpu *vcpu)
7549{
7550 if (pi_pre_block(vcpu))
7551 return 1;
7552
Yunhong Jiang64672c92016-06-13 14:19:59 -07007553 if (kvm_lapic_hv_timer_in_use(vcpu))
7554 kvm_lapic_switch_to_sw_timer(vcpu);
7555
Yunhong Jiangbc225122016-06-13 14:19:58 -07007556 return 0;
7557}
7558
Yunhong Jiangbc225122016-06-13 14:19:58 -07007559static void vmx_post_block(struct kvm_vcpu *vcpu)
7560{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007561 if (kvm_x86_ops.set_hv_timer)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007562 kvm_lapic_switch_to_hv_timer(vcpu);
7563
Yunhong Jiangbc225122016-06-13 14:19:58 -07007564 pi_post_block(vcpu);
7565}
7566
Ashok Rajc45dcc72016-06-22 14:59:56 +08007567static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7568{
7569 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7570 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007571 FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007572 else
7573 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007574 ~FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007575}
7576
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007577static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Ladi Prosek72d7b372017-10-11 16:54:41 +02007578{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007579 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7580 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007581 return -EBUSY;
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -04007582 return !is_smm(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02007583}
7584
Ladi Prosek0234bf82017-10-11 16:54:40 +02007585static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7586{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007587 struct vcpu_vmx *vmx = to_vmx(vcpu);
7588
7589 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7590 if (vmx->nested.smm.guest_mode)
7591 nested_vmx_vmexit(vcpu, -1, 0, 0);
7592
7593 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7594 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007595 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007596 return 0;
7597}
7598
Sean Christophersoned193212019-04-02 08:03:09 -07007599static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007600{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007601 struct vcpu_vmx *vmx = to_vmx(vcpu);
7602 int ret;
7603
7604 if (vmx->nested.smm.vmxon) {
7605 vmx->nested.vmxon = true;
7606 vmx->nested.smm.vmxon = false;
7607 }
7608
7609 if (vmx->nested.smm.guest_mode) {
Sean Christophersona633e412018-09-26 09:23:47 -07007610 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007611 if (ret)
7612 return ret;
7613
7614 vmx->nested.smm.guest_mode = false;
7615 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007616 return 0;
7617}
7618
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007619static void enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007620{
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007621 /* RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007622}
7623
Liran Alon4b9852f2019-08-26 13:24:49 +03007624static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7625{
Paolo Bonzini1c96dcc2020-11-05 11:20:49 -05007626 return to_vmx(vcpu)->nested.vmxon && !is_guest_mode(vcpu);
Liran Alon4b9852f2019-08-26 13:24:49 +03007627}
7628
Jim Mattson93dff2f2020-05-08 13:36:43 -07007629static void vmx_migrate_timers(struct kvm_vcpu *vcpu)
7630{
7631 if (is_guest_mode(vcpu)) {
7632 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer;
7633
7634 if (hrtimer_try_to_cancel(timer) == 1)
7635 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
7636 }
7637}
7638
Sean Christopherson6e4fd062020-03-21 13:26:01 -07007639static void hardware_unsetup(void)
Sean Christophersona3203382018-12-03 13:53:11 -08007640{
7641 if (nested)
7642 nested_vmx_hardware_unsetup();
7643
7644 free_kvm_area();
7645}
7646
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007647static bool vmx_check_apicv_inhibit_reasons(ulong bit)
7648{
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -06007649 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
7650 BIT(APICV_INHIBIT_REASON_HYPERV);
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007651
7652 return supported & BIT(bit);
7653}
7654
Peter Xufb04a1e2020-09-30 21:22:22 -04007655static int vmx_cpu_dirty_log_size(void)
7656{
7657 return enable_pml ? PML_ENTITY_NUM : 0;
7658}
7659
Sean Christophersone286ac02020-03-21 13:26:02 -07007660static struct kvm_x86_ops vmx_x86_ops __initdata = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007661 .hardware_unsetup = hardware_unsetup,
Sean Christopherson484014f2020-03-21 13:25:57 -07007662
Avi Kivity6aa8b732006-12-10 02:21:36 -08007663 .hardware_enable = hardware_enable,
7664 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007665 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007666 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007667
Sean Christopherson484014f2020-03-21 13:25:57 -07007668 .vm_size = sizeof(struct kvm_vmx),
Wanpeng Lib31c1142018-03-12 04:53:04 -07007669 .vm_init = vmx_vm_init,
7670
Avi Kivity6aa8b732006-12-10 02:21:36 -08007671 .vcpu_create = vmx_create_vcpu,
7672 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007673 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007674
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007675 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007676 .vcpu_load = vmx_vcpu_load,
7677 .vcpu_put = vmx_vcpu_put,
7678
Paolo Bonzini69869822020-07-10 17:48:06 +02007679 .update_exception_bitmap = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007680 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007681 .get_msr = vmx_get_msr,
7682 .set_msr = vmx_set_msr,
7683 .get_segment_base = vmx_get_segment_base,
7684 .get_segment = vmx_get_segment,
7685 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007686 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007687 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
7688 .set_cr0 = vmx_set_cr0,
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07007689 .is_valid_cr4 = vmx_is_valid_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007690 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007691 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007692 .get_idt = vmx_get_idt,
7693 .set_idt = vmx_set_idt,
7694 .get_gdt = vmx_get_gdt,
7695 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007696 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007697 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007698 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007699 .get_rflags = vmx_get_rflags,
7700 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007701
Sean Christopherson77809382020-03-20 14:28:18 -07007702 .tlb_flush_all = vmx_flush_tlb_all,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07007703 .tlb_flush_current = vmx_flush_tlb_current,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007704 .tlb_flush_gva = vmx_flush_tlb_gva,
Sean Christophersone64419d2020-03-20 14:28:10 -07007705 .tlb_flush_guest = vmx_flush_tlb_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007706
Avi Kivity6aa8b732006-12-10 02:21:36 -08007707 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007708 .handle_exit = vmx_handle_exit,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08007709 .skip_emulated_instruction = vmx_skip_emulated_instruction,
7710 .update_emulated_instruction = vmx_update_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007711 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7712 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007713 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007714 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007715 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007716 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007717 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007718 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007719 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007720 .get_nmi_mask = vmx_get_nmi_mask,
7721 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007722 .enable_nmi_window = enable_nmi_window,
7723 .enable_irq_window = enable_irq_window,
7724 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007725 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007726 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007727 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007728 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007729 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007730 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007731 .hwapic_irr_update = vmx_hwapic_irr_update,
7732 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007733 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007734 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7735 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007736 .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007737
Izik Eiduscbc94022007-10-25 00:29:55 +02007738 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007739 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007740 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007741
Avi Kivity586f9602010-11-18 13:09:54 +02007742 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007743
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007744 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007745
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007746 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007747
Leonid Shatz326e7422018-11-06 12:14:25 +02007748 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007749
Sean Christopherson484014f2020-03-21 13:25:57 -07007750 .load_mmu_pgd = vmx_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007751
7752 .check_intercept = vmx_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007753 .handle_exit_irqoff = vmx_handle_exit_irqoff,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007754
Sean Christophersond264ee02018-08-27 15:21:12 -07007755 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007756
7757 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007758
7759 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
7760 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
7761 .flush_log_dirty = vmx_flush_log_dirty,
7762 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +02007763
Feng Wubf9f6ac2015-09-18 22:29:55 +08007764 .pre_block = vmx_pre_block,
7765 .post_block = vmx_post_block,
7766
Wei Huang25462f72015-06-19 15:45:05 +02007767 .pmu_ops = &intel_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04007768 .nested_ops = &vmx_nested_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007769
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007770 .update_pi_irte = pi_update_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007771
7772#ifdef CONFIG_X86_64
7773 .set_hv_timer = vmx_set_hv_timer,
7774 .cancel_hv_timer = vmx_cancel_hv_timer,
7775#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007776
7777 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007778
Ladi Prosek72d7b372017-10-11 16:54:41 +02007779 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007780 .pre_enter_smm = vmx_pre_enter_smm,
7781 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007782 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007783
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007784 .can_emulate_instruction = vmx_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03007785 .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
Jim Mattson93dff2f2020-05-08 13:36:43 -07007786 .migrate_timers = vmx_migrate_timers,
Alexander Graf3eb90012020-09-25 16:34:20 +02007787
7788 .msr_filter_changed = vmx_msr_filter_changed,
Paolo Bonzinif9a4d622020-12-14 10:26:51 -05007789 .complete_emulated_msr = kvm_complete_insn_gp,
Peter Xufb04a1e2020-09-30 21:22:22 -04007790 .cpu_dirty_log_size = vmx_cpu_dirty_log_size,
Tom Lendacky647daca2021-01-04 14:20:01 -06007791
7792 .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007793};
7794
Avi Kivity6aa8b732006-12-10 02:21:36 -08007795static __init int hardware_setup(void)
7796{
7797 unsigned long host_bndcfgs;
7798 struct desc_ptr dt;
Sean Christopherson703c3352020-03-02 15:57:03 -08007799 int r, i, ept_lpage_level;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007800
Avi Kivity6aa8b732006-12-10 02:21:36 -08007801 store_idt(&dt);
7802 host_idt_base = dt.address;
7803
Sean Christopherson14a61b62020-09-23 11:04:08 -07007804 for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i)
7805 kvm_define_user_return_msr(i, vmx_uret_msrs_list[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007806
7807 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
7808 return -EIO;
7809
7810 if (boot_cpu_has(X86_FEATURE_NX))
7811 kvm_enable_efer_bits(EFER_NX);
7812
7813 if (boot_cpu_has(X86_FEATURE_MPX)) {
7814 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7815 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7816 }
7817
Sean Christopherson7f5581f2020-03-02 15:56:24 -08007818 if (!cpu_has_vmx_mpx())
Sean Christophersoncfc48182020-03-02 15:56:23 -08007819 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
7820 XFEATURE_MASK_BNDCSR);
7821
Avi Kivity6aa8b732006-12-10 02:21:36 -08007822 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7823 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7824 enable_vpid = 0;
7825
7826 if (!cpu_has_vmx_ept() ||
7827 !cpu_has_vmx_ept_4levels() ||
7828 !cpu_has_vmx_ept_mt_wb() ||
7829 !cpu_has_vmx_invept_global())
7830 enable_ept = 0;
7831
7832 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7833 enable_ept_ad_bits = 0;
7834
7835 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Avi Kivity873a7c42006-12-13 00:34:14 -08007836 enable_unrestricted_guest = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007837
7838 if (!cpu_has_vmx_flexpriority())
7839 flexpriority_enabled = 0;
7840
7841 if (!cpu_has_virtual_nmis())
7842 enable_vnmi = 0;
7843
7844 /*
7845 * set_apic_access_page_addr() is used to reload apic access
7846 * page upon invalidation. No need to do anything if not
7847 * using the APIC_ACCESS_ADDR VMCS field.
7848 */
7849 if (!flexpriority_enabled)
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007850 vmx_x86_ops.set_apic_access_page_addr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007851
7852 if (!cpu_has_vmx_tpr_shadow())
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007853 vmx_x86_ops.update_cr8_intercept = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007854
Avi Kivity6aa8b732006-12-10 02:21:36 -08007855#if IS_ENABLED(CONFIG_HYPERV)
7856 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7857 && enable_ept) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007858 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
7859 vmx_x86_ops.tlb_remote_flush_with_range =
Avi Kivity6aa8b732006-12-10 02:21:36 -08007860 hv_remote_flush_tlb_with_range;
7861 }
7862#endif
7863
7864 if (!cpu_has_vmx_ple()) {
7865 ple_gap = 0;
7866 ple_window = 0;
7867 ple_window_grow = 0;
7868 ple_window_max = 0;
7869 ple_window_shrink = 0;
7870 }
7871
7872 if (!cpu_has_vmx_apicv()) {
7873 enable_apicv = 0;
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007874 vmx_x86_ops.sync_pir_to_irr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007875 }
7876
7877 if (cpu_has_vmx_tsc_scaling()) {
7878 kvm_has_tsc_control = true;
7879 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7880 kvm_tsc_scaling_ratio_frac_bits = 48;
7881 }
7882
Chenyi Qiangfe6b6bc2020-11-06 17:03:14 +08007883 kvm_has_bus_lock_exit = cpu_has_vmx_bus_lock_detection();
7884
Avi Kivity6aa8b732006-12-10 02:21:36 -08007885 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7886
7887 if (enable_ept)
7888 vmx_enable_tdp();
Sean Christopherson703c3352020-03-02 15:57:03 -08007889
7890 if (!enable_ept)
7891 ept_lpage_level = 0;
7892 else if (cpu_has_vmx_ept_1g_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007893 ept_lpage_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08007894 else if (cpu_has_vmx_ept_2m_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007895 ept_lpage_level = PG_LEVEL_2M;
Sean Christopherson703c3352020-03-02 15:57:03 -08007896 else
Sean Christopherson3bae0452020-04-27 17:54:22 -07007897 ept_lpage_level = PG_LEVEL_4K;
Sean Christopherson83013052020-07-15 20:41:22 -07007898 kvm_configure_mmu(enable_ept, vmx_get_max_tdp_level(), ept_lpage_level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007899
7900 /*
7901 * Only enable PML when hardware supports PML feature, and both EPT
7902 * and EPT A/D bit features are enabled -- PML depends on them to work.
7903 */
7904 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7905 enable_pml = 0;
7906
7907 if (!enable_pml) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007908 vmx_x86_ops.slot_enable_log_dirty = NULL;
7909 vmx_x86_ops.slot_disable_log_dirty = NULL;
7910 vmx_x86_ops.flush_log_dirty = NULL;
7911 vmx_x86_ops.enable_log_dirty_pt_masked = NULL;
Peter Xufb04a1e2020-09-30 21:22:22 -04007912 vmx_x86_ops.cpu_dirty_log_size = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007913 }
7914
7915 if (!cpu_has_vmx_preemption_timer())
7916 enable_preemption_timer = false;
7917
7918 if (enable_preemption_timer) {
7919 u64 use_timer_freq = 5000ULL * 1000 * 1000;
7920 u64 vmx_msr;
7921
7922 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7923 cpu_preemption_timer_multi =
7924 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7925
7926 if (tsc_khz)
7927 use_timer_freq = (u64)tsc_khz * 1000;
7928 use_timer_freq >>= cpu_preemption_timer_multi;
7929
7930 /*
7931 * KVM "disables" the preemption timer by setting it to its max
7932 * value. Don't use the timer if it might cause spurious exits
7933 * at a rate faster than 0.1 Hz (of uninterrupted guest time).
7934 */
7935 if (use_timer_freq > 0xffffffffu / 10)
7936 enable_preemption_timer = false;
7937 }
7938
7939 if (!enable_preemption_timer) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007940 vmx_x86_ops.set_hv_timer = NULL;
7941 vmx_x86_ops.cancel_hv_timer = NULL;
7942 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007943 }
7944
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007945 kvm_set_posted_intr_wakeup_handler(pi_wakeup_handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007946
7947 kvm_mce_cap_supported |= MCG_LMCE_P;
7948
7949 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
7950 return -EINVAL;
7951 if (!enable_ept || !cpu_has_vmx_intel_pt())
7952 pt_mode = PT_MODE_SYSTEM;
7953
7954 if (nested) {
7955 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
7956 vmx_capability.ept);
7957
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07007958 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007959 if (r)
7960 return r;
7961 }
7962
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007963 vmx_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08007964
Avi Kivity6aa8b732006-12-10 02:21:36 -08007965 r = alloc_kvm_area();
7966 if (r)
7967 nested_vmx_hardware_unsetup();
7968 return r;
7969}
7970
Sean Christophersond008dfd2020-03-21 13:25:56 -07007971static struct kvm_x86_init_ops vmx_init_ops __initdata = {
7972 .cpu_has_kvm_support = cpu_has_kvm_support,
7973 .disabled_by_bios = vmx_disabled_by_bios,
7974 .check_processor_compatibility = vmx_check_processor_compat,
7975 .hardware_setup = hardware_setup,
7976
7977 .runtime_ops = &vmx_x86_ops,
7978};
7979
Avi Kivity6aa8b732006-12-10 02:21:36 -08007980static void vmx_cleanup_l1d_flush(void)
7981{
7982 if (vmx_l1d_flush_pages) {
7983 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
7984 vmx_l1d_flush_pages = NULL;
7985 }
7986 /* Restore state so sysfs ignores VMX */
7987 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
7988}
7989
7990static void vmx_exit(void)
7991{
7992#ifdef CONFIG_KEXEC_CORE
7993 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
7994 synchronize_rcu();
7995#endif
7996
7997 kvm_exit();
7998
7999#if IS_ENABLED(CONFIG_HYPERV)
8000 if (static_branch_unlikely(&enable_evmcs)) {
8001 int cpu;
8002 struct hv_vp_assist_page *vp_ap;
8003 /*
8004 * Reset everything to support using non-enlightened VMCS
8005 * access later (e.g. when we reload the module with
8006 * enlightened_vmcs=0)
8007 */
8008 for_each_online_cpu(cpu) {
8009 vp_ap = hv_get_vp_assist_page(cpu);
8010
8011 if (!vp_ap)
8012 continue;
8013
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008014 vp_ap->nested_control.features.directhypercall = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008015 vp_ap->current_nested_vmcs = 0;
8016 vp_ap->enlighten_vmentry = 0;
8017 }
8018
8019 static_branch_disable(&enable_evmcs);
8020 }
8021#endif
8022 vmx_cleanup_l1d_flush();
8023}
8024module_exit(vmx_exit);
8025
8026static int __init vmx_init(void)
8027{
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008028 int r, cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008029
8030#if IS_ENABLED(CONFIG_HYPERV)
8031 /*
8032 * Enlightened VMCS usage should be recommended and the host needs
8033 * to support eVMCS v1 or above. We can also disable eVMCS support
8034 * with module parameter.
8035 */
8036 if (enlightened_vmcs &&
8037 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
8038 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
8039 KVM_EVMCS_VERSION) {
8040 int cpu;
8041
8042 /* Check that we have assist pages on all online CPUs */
8043 for_each_online_cpu(cpu) {
8044 if (!hv_get_vp_assist_page(cpu)) {
8045 enlightened_vmcs = false;
8046 break;
8047 }
8048 }
8049
8050 if (enlightened_vmcs) {
8051 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
8052 static_branch_enable(&enable_evmcs);
8053 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008054
8055 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
8056 vmx_x86_ops.enable_direct_tlbflush
8057 = hv_enable_direct_tlbflush;
8058
Avi Kivity6aa8b732006-12-10 02:21:36 -08008059 } else {
8060 enlightened_vmcs = false;
8061 }
8062#endif
8063
Sean Christophersond008dfd2020-03-21 13:25:56 -07008064 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008065 __alignof__(struct vcpu_vmx), THIS_MODULE);
8066 if (r)
8067 return r;
8068
8069 /*
8070 * Must be called after kvm_init() so enable_ept is properly set
8071 * up. Hand the parameter mitigation value in which was stored in
8072 * the pre module init parser. If no parameter was given, it will
8073 * contain 'auto' which will be turned into the default 'cond'
8074 * mitigation mode.
8075 */
Waiman Long19a36d32019-08-26 15:30:23 -04008076 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
8077 if (r) {
8078 vmx_exit();
8079 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008080 }
8081
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008082 for_each_possible_cpu(cpu) {
8083 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07008084
Paolo Bonzinia3ff25f2020-10-24 04:08:37 -04008085 pi_init_cpu(cpu);
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008086 }
8087
Avi Kivity6aa8b732006-12-10 02:21:36 -08008088#ifdef CONFIG_KEXEC_CORE
8089 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8090 crash_vmclear_local_loaded_vmcss);
8091#endif
8092 vmx_check_vmcs12_offsets();
8093
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008094 /*
Mohammed Gamalb96e6502020-09-03 16:11:22 +02008095 * Shadow paging doesn't have a (further) performance penalty
8096 * from GUEST_MAXPHYADDR < HOST_MAXPHYADDR so enable it
8097 * by default
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008098 */
Mohammed Gamalb96e6502020-09-03 16:11:22 +02008099 if (!enable_ept)
8100 allow_smaller_maxphyaddr = true;
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008101
Avi Kivity6aa8b732006-12-10 02:21:36 -08008102 return 0;
8103}
8104module_init(vmx_init);