blob: f4e9c310032a7db631592694b1b2763a07104e69 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity6aa8b732006-12-10 02:21:36 -08002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * This module enables machines with Intel VT-x extensions to run virtual
6 * machines without emulation or binary translation.
7 *
8 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Avi Kivity6aa8b732006-12-10 02:21:36 -080014 */
15
Sean Christopherson199b1182018-12-03 13:52:53 -080016#include <linux/frame.h>
17#include <linux/highmem.h>
18#include <linux/hrtimer.h>
19#include <linux/kernel.h>
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/module.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020022#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070023#include <linux/mod_devicetable.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080024#include <linux/mm.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080025#include <linux/sched.h>
Josh Poimboeufb2849092019-01-30 07:13:58 -060026#include <linux/sched/smt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040028#include <linux/tboot.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080029#include <linux/trace_events.h>
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +020030#include <linux/entry-kvm.h>
Avi Kivitye4956062007-06-28 14:15:57 -040031
Sean Christopherson199b1182018-12-03 13:52:53 -080032#include <asm/apic.h>
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020033#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080034#include <asm/cpu.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010035#include <asm/cpu_device_id.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010036#include <asm/debugreg.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080037#include <asm/desc.h>
38#include <asm/fpu/internal.h>
39#include <asm/io.h>
Feng Wuefc64402015-09-18 22:29:51 +080040#include <asm/irq_remapping.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080041#include <asm/kexec.h>
42#include <asm/perf_event.h>
43#include <asm/mce.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070044#include <asm/mmu_context.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010045#include <asm/mshyperv.h>
Benjamin Thielb10c3072020-01-23 18:29:45 +010046#include <asm/mwait.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080047#include <asm/spec-ctrl.h>
48#include <asm/virtext.h>
49#include <asm/vmx.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Sean Christopherson3077c192018-12-03 13:53:02 -080051#include "capabilities.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080052#include "cpuid.h"
Sean Christopherson4cebd742018-12-03 13:52:58 -080053#include "evmcs.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080054#include "irq.h"
55#include "kvm_cache_regs.h"
56#include "lapic.h"
57#include "mmu.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080058#include "nested.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080059#include "ops.h"
Wei Huang25462f72015-06-19 15:45:05 +020060#include "pmu.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080061#include "trace.h"
Sean Christophersoncb1d4742018-12-03 13:53:04 -080062#include "vmcs.h"
Sean Christopherson609363c2018-12-03 13:53:05 -080063#include "vmcs12.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080064#include "vmx.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080065#include "x86.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030066
Avi Kivity6aa8b732006-12-10 02:21:36 -080067MODULE_AUTHOR("Qumranet");
68MODULE_LICENSE("GPL");
69
Valdis Klētnieks575b2552020-02-27 21:49:52 -050070#ifdef MODULE
Josh Triplette9bda3b2012-03-20 23:33:51 -070071static const struct x86_cpu_id vmx_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010072 X86_MATCH_FEATURE(X86_FEATURE_VMX, NULL),
Josh Triplette9bda3b2012-03-20 23:33:51 -070073 {}
74};
75MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050076#endif
Josh Triplette9bda3b2012-03-20 23:33:51 -070077
Sean Christopherson2c4fd912018-12-03 13:53:03 -080078bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020079module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080080
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010081static bool __read_mostly enable_vnmi = 1;
82module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
83
Sean Christopherson2c4fd912018-12-03 13:53:03 -080084bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020086
Sean Christopherson2c4fd912018-12-03 13:53:03 -080087bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020088module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080089
Sean Christopherson2c4fd912018-12-03 13:53:03 -080090bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070091module_param_named(unrestricted_guest,
92 enable_unrestricted_guest, bool, S_IRUGO);
93
Sean Christopherson2c4fd912018-12-03 13:53:03 -080094bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080095module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
96
Avi Kivitya27685c2012-06-12 20:30:18 +030097static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020098module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030099
Rusty Russell476bc002012-01-13 09:32:18 +1030100static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +0300101module_param(fasteoi, bool, S_IRUGO);
102
Vitaly Kuznetsova4443262020-02-20 18:22:04 +0100103bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800104module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800105
Nadav Har'El801d3422011-05-25 23:02:23 +0300106/*
107 * If nested=1, nested virtualization is supported, i.e., guests may use
108 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
109 * use VMX instructions.
110 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200111static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300112module_param(nested, bool, S_IRUGO);
113
Sean Christopherson2c4fd912018-12-03 13:53:03 -0800114bool __read_mostly enable_pml = 1;
Kai Huang843e4332015-01-28 10:54:28 +0800115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200117static bool __read_mostly dump_invalid_vmcs = 0;
118module_param(dump_invalid_vmcs, bool, 0644);
119
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100120#define MSR_BITMAP_MODE_X2APIC 1
121#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100122
Haozhong Zhang64903d62015-10-20 15:39:09 +0800123#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
124
Yunhong Jiang64672c92016-06-13 14:19:59 -0700125/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
126static int __read_mostly cpu_preemption_timer_multi;
127static bool __read_mostly enable_preemption_timer = 1;
128#ifdef CONFIG_X86_64
129module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
130#endif
131
Mohammed Gamalb96e6502020-09-03 16:11:22 +0200132extern bool __read_mostly allow_smaller_maxphyaddr;
133module_param(allow_smaller_maxphyaddr, bool, S_IRUGO);
134
Sean Christopherson3de63472018-07-13 08:42:30 -0700135#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800136#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
137#define KVM_VM_CR0_ALWAYS_ON \
138 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
139 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200140
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800141#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200142#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
143#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
144
Avi Kivity78ac8b42010-04-08 18:19:35 +0300145#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
146
Chao Pengbf8c55d2018-10-24 16:05:14 +0800147#define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \
148 RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \
149 RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \
150 RTIT_STATUS_BYTECNT))
151
152#define MSR_IA32_RTIT_OUTPUT_BASE_MASK \
153 (~((1UL << cpuid_query_maxphyaddr(vcpu)) - 1) | 0x7f)
154
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800155/*
156 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
157 * ple_gap: upper bound on the amount of time between two successive
158 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500159 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800160 * ple_window: upper bound on the amount of time a guest is allowed to execute
161 * in a PAUSE loop. Tests indicate that most spinlocks are held for
162 * less than 2^12 cycles
163 * Time is measured based on a counter that runs at the same rate as the TSC,
164 * refer SDM volume 3b section 21.6.13 & 22.1.3.
165 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400166static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500167module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200168
Babu Moger7fbc85a2018-03-16 16:37:22 -0400169static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
170module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800171
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200172/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400173static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200175
176/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400177static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400178module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200179
180/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400181static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
182module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200183
Chao Pengf99e3da2018-10-24 16:05:10 +0800184/* Default is SYSTEM mode, 1 for host-guest mode */
185int __read_mostly pt_mode = PT_MODE_SYSTEM;
186module_param(pt_mode, int, S_IRUGO);
187
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200188static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200189static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200190static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200191
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200192/* Storage for pre module init parameter parsing */
193static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194
195static const struct {
196 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200197 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200198} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200199 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
200 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
201 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
202 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
203 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
204 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200205};
206
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200207#define L1D_CACHE_ORDER 4
208static void *vmx_l1d_flush_pages;
209
210static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
211{
212 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200213 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200214
Waiman Long19a36d32019-08-26 15:30:23 -0400215 if (!boot_cpu_has_bug(X86_BUG_L1TF)) {
216 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
217 return 0;
218 }
219
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200220 if (!enable_ept) {
221 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
222 return 0;
223 }
224
Yi Wangd806afa2018-08-16 13:42:39 +0800225 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
226 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200227
Yi Wangd806afa2018-08-16 13:42:39 +0800228 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
229 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
230 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
231 return 0;
232 }
233 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200234
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200235 /* If set to auto use the default l1tf mitigation method */
236 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
237 switch (l1tf_mitigation) {
238 case L1TF_MITIGATION_OFF:
239 l1tf = VMENTER_L1D_FLUSH_NEVER;
240 break;
241 case L1TF_MITIGATION_FLUSH_NOWARN:
242 case L1TF_MITIGATION_FLUSH:
243 case L1TF_MITIGATION_FLUSH_NOSMT:
244 l1tf = VMENTER_L1D_FLUSH_COND;
245 break;
246 case L1TF_MITIGATION_FULL:
247 case L1TF_MITIGATION_FULL_FORCE:
248 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
249 break;
250 }
251 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
252 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
253 }
254
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200255 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
256 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Ben Gardon41836832019-02-11 11:02:52 -0800257 /*
258 * This allocation for vmx_l1d_flush_pages is not tied to a VM
259 * lifetime and so should not be charged to a memcg.
260 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200261 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
262 if (!page)
263 return -ENOMEM;
264 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200265
266 /*
267 * Initialize each page with a different pattern in
268 * order to protect against KSM in the nested
269 * virtualization case.
270 */
271 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
272 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
273 PAGE_SIZE);
274 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200275 }
276
277 l1tf_vmx_mitigation = l1tf;
278
Thomas Gleixner895ae472018-07-13 16:23:22 +0200279 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
280 static_branch_enable(&vmx_l1d_should_flush);
281 else
282 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200283
Nicolai Stange427362a2018-07-21 22:25:00 +0200284 if (l1tf == VMENTER_L1D_FLUSH_COND)
285 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200286 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200287 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200288 return 0;
289}
290
291static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200292{
293 unsigned int i;
294
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200295 if (s) {
296 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200297 if (vmentry_l1d_param[i].for_parse &&
298 sysfs_streq(s, vmentry_l1d_param[i].option))
299 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200300 }
301 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200302 return -EINVAL;
303}
304
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200305static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
306{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200307 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200308
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200309 l1tf = vmentry_l1d_flush_parse(s);
310 if (l1tf < 0)
311 return l1tf;
312
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200313 if (!boot_cpu_has(X86_BUG_L1TF))
314 return 0;
315
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200316 /*
317 * Has vmx_init() run already? If not then this is the pre init
318 * parameter parsing. In that case just store the value and let
319 * vmx_init() do the proper setup after enable_ept has been
320 * established.
321 */
322 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
323 vmentry_l1d_flush_param = l1tf;
324 return 0;
325 }
326
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200327 mutex_lock(&vmx_l1d_flush_mutex);
328 ret = vmx_setup_l1d_flush(l1tf);
329 mutex_unlock(&vmx_l1d_flush_mutex);
330 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200331}
332
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200333static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
334{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200335 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
336 return sprintf(s, "???\n");
337
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200338 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200339}
340
341static const struct kernel_param_ops vmentry_l1d_flush_ops = {
342 .set = vmentry_l1d_flush_set,
343 .get = vmentry_l1d_flush_get,
344};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200345module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200346
Gleb Natapovd99e4152012-12-20 16:57:45 +0200347static bool guest_state_valid(struct kvm_vcpu *vcpu);
348static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yi Wang1e4329ee2018-11-08 11:22:21 +0800349static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj15d45072018-02-01 22:59:43 +0100350 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300351
Sean Christopherson453eafb2018-12-20 12:25:17 -0800352void vmx_vmexit(void);
353
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700354#define vmx_insn_failed(fmt...) \
355do { \
356 WARN_ONCE(1, fmt); \
357 pr_warn_ratelimited(fmt); \
358} while (0)
359
Sean Christopherson6e202092019-07-19 13:41:08 -0700360asmlinkage void vmread_error(unsigned long field, bool fault)
361{
362 if (fault)
363 kvm_spurious_fault();
364 else
365 vmx_insn_failed("kvm: vmread failed: field=%lx\n", field);
366}
367
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700368noinline void vmwrite_error(unsigned long field, unsigned long value)
369{
370 vmx_insn_failed("kvm: vmwrite failed: field=%lx val=%lx err=%d\n",
371 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
372}
373
374noinline void vmclear_error(struct vmcs *vmcs, u64 phys_addr)
375{
376 vmx_insn_failed("kvm: vmclear failed: %p/%llx\n", vmcs, phys_addr);
377}
378
379noinline void vmptrld_error(struct vmcs *vmcs, u64 phys_addr)
380{
381 vmx_insn_failed("kvm: vmptrld failed: %p/%llx\n", vmcs, phys_addr);
382}
383
384noinline void invvpid_error(unsigned long ext, u16 vpid, gva_t gva)
385{
386 vmx_insn_failed("kvm: invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n",
387 ext, vpid, gva);
388}
389
390noinline void invept_error(unsigned long ext, u64 eptp, gpa_t gpa)
391{
392 vmx_insn_failed("kvm: invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n",
393 ext, eptp, gpa);
394}
395
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800397DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300398/*
399 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
400 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
401 */
402static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403
Feng Wubf9f6ac2015-09-18 22:29:55 +0800404/*
405 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
406 * can find which vCPU should be waken up.
407 */
408static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
409static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
410
Sheng Yang2384d2b2008-01-17 15:14:33 +0800411static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
412static DEFINE_SPINLOCK(vmx_vpid_lock);
413
Sean Christopherson3077c192018-12-03 13:53:02 -0800414struct vmcs_config vmcs_config;
415struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800416
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417#define VMX_SEGMENT_FIELD(seg) \
418 [VCPU_SREG_##seg] = { \
419 .selector = GUEST_##seg##_SELECTOR, \
420 .base = GUEST_##seg##_BASE, \
421 .limit = GUEST_##seg##_LIMIT, \
422 .ar_bytes = GUEST_##seg##_AR_BYTES, \
423 }
424
Mathias Krause772e0312012-08-30 01:30:19 +0200425static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426 unsigned selector;
427 unsigned base;
428 unsigned limit;
429 unsigned ar_bytes;
430} kvm_vmx_segment_fields[] = {
431 VMX_SEGMENT_FIELD(CS),
432 VMX_SEGMENT_FIELD(DS),
433 VMX_SEGMENT_FIELD(ES),
434 VMX_SEGMENT_FIELD(FS),
435 VMX_SEGMENT_FIELD(GS),
436 VMX_SEGMENT_FIELD(SS),
437 VMX_SEGMENT_FIELD(TR),
438 VMX_SEGMENT_FIELD(LDTR),
439};
440
Sean Christophersonec0241f2020-04-15 13:34:52 -0700441static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
442{
443 vmx->segment_cache.bitmask = 0;
444}
445
Sean Christopherson23420802019-04-19 22:50:57 -0700446static unsigned long host_idt_base;
Avi Kivity26bb0982009-09-07 11:14:12 +0300447
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300448/*
Jim Mattson898a8112018-12-05 15:28:59 -0800449 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
450 * will emulate SYSCALL in legacy mode if the vendor string in guest
451 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
452 * support this emulation, IA32_STAR must always be included in
453 * vmx_msr_index[], even in i386 builds.
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300454 */
Sean Christophersoncf3646e2018-12-03 13:53:15 -0800455const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800456#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300457 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400459 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Paolo Bonzinic11f83e2019-11-18 12:23:00 -0500460 MSR_IA32_TSX_CTRL,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100463#if IS_ENABLED(CONFIG_HYPERV)
464static bool __read_mostly enlightened_vmcs = true;
465module_param(enlightened_vmcs, bool, 0444);
466
Tianyu Lan877ad952018-07-19 08:40:23 +0000467/* check_ept_pointer() should be under protection of ept_pointer_lock. */
468static void check_ept_pointer_match(struct kvm *kvm)
469{
470 struct kvm_vcpu *vcpu;
471 u64 tmp_eptp = INVALID_PAGE;
472 int i;
473
474 kvm_for_each_vcpu(i, vcpu, kvm) {
475 if (!VALID_PAGE(tmp_eptp)) {
476 tmp_eptp = to_vmx(vcpu)->ept_pointer;
477 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
478 to_kvm_vmx(kvm)->ept_pointers_match
479 = EPT_POINTERS_MISMATCH;
480 return;
481 }
482 }
483
484 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
485}
486
Yi Wang8997f652019-01-21 15:27:05 +0800487static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800488 void *data)
489{
490 struct kvm_tlb_range *range = data;
491
492 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
493 range->pages);
494}
495
496static inline int __hv_remote_flush_tlb_with_range(struct kvm *kvm,
497 struct kvm_vcpu *vcpu, struct kvm_tlb_range *range)
498{
499 u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
500
501 /*
502 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs address
503 * of the base of EPT PML4 table, strip off EPT configuration
504 * information.
505 */
506 if (range)
507 return hyperv_flush_guest_mapping_range(ept_pointer & PAGE_MASK,
508 kvm_fill_hv_flush_list_func, (void *)range);
509 else
510 return hyperv_flush_guest_mapping(ept_pointer & PAGE_MASK);
511}
512
513static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
514 struct kvm_tlb_range *range)
Tianyu Lan877ad952018-07-19 08:40:23 +0000515{
Lan Tianyua5c214d2018-10-13 22:54:05 +0800516 struct kvm_vcpu *vcpu;
Lan Tianyub7c1c222019-01-04 15:20:44 +0800517 int ret = 0, i;
Tianyu Lan877ad952018-07-19 08:40:23 +0000518
519 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
520
521 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
522 check_ept_pointer_match(kvm);
523
524 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyu53963a72018-12-06 15:34:36 +0800525 kvm_for_each_vcpu(i, vcpu, kvm) {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800526 /* If ept_pointer is invalid pointer, bypass flush request. */
527 if (VALID_PAGE(to_vmx(vcpu)->ept_pointer))
528 ret |= __hv_remote_flush_tlb_with_range(
529 kvm, vcpu, range);
Lan Tianyu53963a72018-12-06 15:34:36 +0800530 }
Lan Tianyua5c214d2018-10-13 22:54:05 +0800531 } else {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800532 ret = __hv_remote_flush_tlb_with_range(kvm,
533 kvm_get_vcpu(kvm, 0), range);
Tianyu Lan877ad952018-07-19 08:40:23 +0000534 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000535
Tianyu Lan877ad952018-07-19 08:40:23 +0000536 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
537 return ret;
538}
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800539static int hv_remote_flush_tlb(struct kvm *kvm)
540{
541 return hv_remote_flush_tlb_with_range(kvm, NULL);
542}
543
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800544static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)
545{
546 struct hv_enlightened_vmcs *evmcs;
547 struct hv_partition_assist_pg **p_hv_pa_pg =
548 &vcpu->kvm->arch.hyperv.hv_pa_pg;
549 /*
550 * Synthetic VM-Exit is not enabled in current code and so All
551 * evmcs in singe VM shares same assist page.
552 */
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200553 if (!*p_hv_pa_pg)
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800554 *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200555
556 if (!*p_hv_pa_pg)
557 return -ENOMEM;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800558
559 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs;
560
561 evmcs->partition_assist_page =
562 __pa(*p_hv_pa_pg);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200563 evmcs->hv_vm_id = (unsigned long)vcpu->kvm;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800564 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;
565
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800566 return 0;
567}
568
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100569#endif /* IS_ENABLED(CONFIG_HYPERV) */
570
Yunhong Jiang64672c92016-06-13 14:19:59 -0700571/*
572 * Comment's format: document - errata name - stepping - processor name.
573 * Refer from
574 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
575 */
576static u32 vmx_preemption_cpu_tfms[] = {
577/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
5780x000206E6,
579/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
580/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
581/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
5820x00020652,
583/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
5840x00020655,
585/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
586/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
587/*
588 * 320767.pdf - AAP86 - B1 -
589 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
590 */
5910x000106E5,
592/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
5930x000106A0,
594/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
5950x000106A1,
596/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
5970x000106A4,
598 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
599 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
600 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
6010x000106A5,
Wei Huang3d82c562018-12-03 14:13:32 -0600602 /* Xeon E3-1220 V2 */
6030x000306A8,
Yunhong Jiang64672c92016-06-13 14:19:59 -0700604};
605
606static inline bool cpu_has_broken_vmx_preemption_timer(void)
607{
608 u32 eax = cpuid_eax(0x00000001), i;
609
610 /* Clear the reserved bits */
611 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000612 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700613 if (eax == vmx_preemption_cpu_tfms[i])
614 return true;
615
616 return false;
617}
618
Paolo Bonzini35754c92015-07-29 12:05:37 +0200619static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800620{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200621 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800622}
623
Sheng Yang04547152009-04-01 15:52:31 +0800624static inline bool report_flexpriority(void)
625{
626 return flexpriority_enabled;
627}
628
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800629static inline int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800630{
631 int i;
632
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400633 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300634 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300635 return i;
636 return -1;
637}
638
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800639struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300640{
641 int i;
642
Rusty Russell8b9cf982007-07-30 16:31:43 +1000643 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300644 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400645 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000646 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800647}
648
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500649static int vmx_set_guest_msr(struct vcpu_vmx *vmx, struct shared_msr_entry *msr, u64 data)
650{
651 int ret = 0;
652
653 u64 old_msr_data = msr->data;
654 msr->data = data;
655 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
656 preempt_disable();
657 ret = kvm_set_shared_msr(msr->index, msr->data,
658 msr->mask);
659 preempt_enable();
660 if (ret)
661 msr->data = old_msr_data;
662 }
663 return ret;
664}
665
Dave Young2965faa2015-09-09 15:38:55 -0700666#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800667static void crash_vmclear_local_loaded_vmcss(void)
668{
669 int cpu = raw_smp_processor_id();
670 struct loaded_vmcs *v;
671
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800672 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
673 loaded_vmcss_on_cpu_link)
674 vmcs_clear(v->vmcs);
675}
Dave Young2965faa2015-09-09 15:38:55 -0700676#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800677
Nadav Har'Eld462b812011-05-24 15:26:10 +0300678static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300680 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800681 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
Nadav Har'Eld462b812011-05-24 15:26:10 +0300683 if (loaded_vmcs->cpu != cpu)
684 return; /* vcpu migration can race with cpu offline */
685 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686 per_cpu(current_vmcs, cpu) = NULL;
Sean Christopherson31603d42020-03-21 12:37:49 -0700687
688 vmcs_clear(loaded_vmcs->vmcs);
689 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
690 vmcs_clear(loaded_vmcs->shadow_vmcs);
691
Nadav Har'Eld462b812011-05-24 15:26:10 +0300692 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800693
694 /*
Sean Christopherson31603d42020-03-21 12:37:49 -0700695 * Ensure all writes to loaded_vmcs, including deleting it from its
696 * current percpu list, complete before setting loaded_vmcs->vcpu to
697 * -1, otherwise a different cpu can see vcpu == -1 first and add
698 * loaded_vmcs to its percpu list before it's deleted from this cpu's
699 * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800700 */
701 smp_wmb();
702
Sean Christopherson31603d42020-03-21 12:37:49 -0700703 loaded_vmcs->cpu = -1;
704 loaded_vmcs->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705}
706
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800707void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800708{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800709 int cpu = loaded_vmcs->cpu;
710
711 if (cpu != -1)
712 smp_call_function_single(cpu,
713 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800714}
715
Avi Kivity2fb92db2011-04-27 19:42:18 +0300716static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
717 unsigned field)
718{
719 bool ret;
720 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
721
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700722 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) {
723 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS);
Avi Kivity2fb92db2011-04-27 19:42:18 +0300724 vmx->segment_cache.bitmask = 0;
725 }
726 ret = vmx->segment_cache.bitmask & mask;
727 vmx->segment_cache.bitmask |= mask;
728 return ret;
729}
730
731static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
732{
733 u16 *p = &vmx->segment_cache.seg[seg].selector;
734
735 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
736 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
737 return *p;
738}
739
740static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
741{
742 ulong *p = &vmx->segment_cache.seg[seg].base;
743
744 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
745 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
746 return *p;
747}
748
749static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
750{
751 u32 *p = &vmx->segment_cache.seg[seg].limit;
752
753 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
754 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
755 return *p;
756}
757
758static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
759{
760 u32 *p = &vmx->segment_cache.seg[seg].ar;
761
762 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
763 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
764 return *p;
765}
766
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800767void update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300768{
769 u32 eb;
770
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100771 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800772 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200773 /*
774 * Guest access to VMware backdoor ports could legitimately
775 * trigger #GP because of TSS I/O permission bitmap.
776 * We intercept those #GP and allow access to them anyway
777 * as VMware does.
778 */
779 if (enable_vmware_backdoor)
780 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100781 if ((vcpu->guest_debug &
782 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
783 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
784 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300785 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300786 eb = ~0;
Paolo Bonzinia0c13432020-07-10 17:48:08 +0200787 if (!vmx_need_pf_intercept(vcpu))
Miaohe Lin49f933d2020-02-27 11:20:54 +0800788 eb &= ~(1u << PF_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300789
790 /* When we are running a nested L2 guest and L1 specified for it a
791 * certain exception bitmap, we must trap the same exceptions and pass
792 * them to L1. When running L2, we will only handle the exceptions
793 * specified above if L1 did not want them.
794 */
795 if (is_guest_mode(vcpu))
796 eb |= get_vmcs12(vcpu)->exception_bitmap;
Paolo Bonzinib502e6e2020-09-29 08:31:32 -0400797 else {
798 /*
799 * If EPT is enabled, #PF is only trapped if MAXPHYADDR is mismatched
800 * between guest and host. In that case we only care about present
801 * faults. For vmcs02, however, PFEC_MASK and PFEC_MATCH are set in
802 * prepare_vmcs02_rare.
803 */
804 bool selective_pf_trap = enable_ept && (eb & (1u << PF_VECTOR));
805 int mask = selective_pf_trap ? PFERR_PRESENT_MASK : 0;
806 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, mask);
807 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, mask);
808 }
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300809
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300810 vmcs_write32(EXCEPTION_BITMAP, eb);
811}
812
Ashok Raj15d45072018-02-01 22:59:43 +0100813/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100814 * Check if MSR is intercepted for currently loaded MSR bitmap.
815 */
816static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
817{
818 unsigned long *msr_bitmap;
819 int f = sizeof(unsigned long);
820
821 if (!cpu_has_vmx_msr_bitmap())
822 return true;
823
824 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
825
826 if (msr <= 0x1fff) {
827 return !!test_bit(msr, msr_bitmap + 0x800 / f);
828 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
829 msr &= 0x1fff;
830 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
831 }
832
833 return true;
834}
835
Gleb Natapov2961e8762013-11-25 15:37:13 +0200836static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
837 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200838{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200839 vm_entry_controls_clearbit(vmx, entry);
840 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200841}
842
Aaron Lewis662f1d12019-11-07 21:14:39 -0800843int vmx_find_msr_index(struct vmx_msrs *m, u32 msr)
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400844{
845 unsigned int i;
846
847 for (i = 0; i < m->nr; ++i) {
848 if (m->val[i].index == msr)
849 return i;
850 }
851 return -ENOENT;
852}
853
Avi Kivity61d2ef22010-04-28 16:40:38 +0300854static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
855{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400856 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300857 struct msr_autoload *m = &vmx->msr_autoload;
858
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200859 switch (msr) {
860 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800861 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200862 clear_atomic_switch_msr_special(vmx,
863 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200864 VM_EXIT_LOAD_IA32_EFER);
865 return;
866 }
867 break;
868 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800869 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200870 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200871 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
872 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
873 return;
874 }
875 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200876 }
Aaron Lewisef0fbca2019-11-07 21:14:38 -0800877 i = vmx_find_msr_index(&m->guest, msr);
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400878 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400879 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400880 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400881 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400882 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200883
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400884skip_guest:
Aaron Lewisef0fbca2019-11-07 21:14:38 -0800885 i = vmx_find_msr_index(&m->host, msr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400886 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300887 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400888
889 --m->host.nr;
890 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400891 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300892}
893
Gleb Natapov2961e8762013-11-25 15:37:13 +0200894static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
895 unsigned long entry, unsigned long exit,
896 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
897 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200898{
899 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700900 if (host_val_vmcs != HOST_IA32_EFER)
901 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200902 vm_entry_controls_setbit(vmx, entry);
903 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200904}
905
Avi Kivity61d2ef22010-04-28 16:40:38 +0300906static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400907 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300908{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400909 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300910 struct msr_autoload *m = &vmx->msr_autoload;
911
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200912 switch (msr) {
913 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800914 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200915 add_atomic_switch_msr_special(vmx,
916 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200917 VM_EXIT_LOAD_IA32_EFER,
918 GUEST_IA32_EFER,
919 HOST_IA32_EFER,
920 guest_val, host_val);
921 return;
922 }
923 break;
924 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800925 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200926 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200927 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
928 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
929 GUEST_IA32_PERF_GLOBAL_CTRL,
930 HOST_IA32_PERF_GLOBAL_CTRL,
931 guest_val, host_val);
932 return;
933 }
934 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +0100935 case MSR_IA32_PEBS_ENABLE:
936 /* PEBS needs a quiescent period after being disabled (to write
937 * a record). Disabling PEBS through VMX MSR swapping doesn't
938 * provide that period, so a CPU could write host's record into
939 * guest's memory.
940 */
941 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +0200942 }
943
Aaron Lewisef0fbca2019-11-07 21:14:38 -0800944 i = vmx_find_msr_index(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400945 if (!entry_only)
Aaron Lewisef0fbca2019-11-07 21:14:38 -0800946 j = vmx_find_msr_index(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300947
Aaron Lewis7cfe0522019-11-07 21:14:37 -0800948 if ((i < 0 && m->guest.nr == NR_LOADSTORE_MSRS) ||
949 (j < 0 && m->host.nr == NR_LOADSTORE_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +0200950 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +0200951 "Can't add msr %x\n", msr);
952 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300953 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400954 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400955 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400956 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400957 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400958 m->guest.val[i].index = msr;
959 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300960
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400961 if (entry_only)
962 return;
963
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400964 if (j < 0) {
965 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400966 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300967 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400968 m->host.val[j].index = msr;
969 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300970}
971
Avi Kivity92c0d902009-10-29 11:00:16 +0200972static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300973{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100974 u64 guest_efer = vmx->vcpu.arch.efer;
975 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +0300976
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100977 /* Shadow paging assumes NX to be available. */
978 if (!enable_ept)
979 guest_efer |= EFER_NX;
Roel Kluin3a34a882009-08-04 02:08:45 -0700980
Avi Kivity51c6cf62007-08-29 03:48:05 +0300981 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100982 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +0300983 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100984 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300985#ifdef CONFIG_X86_64
986 ignore_bits |= EFER_LMA | EFER_LME;
987 /* SCE is meaningful only in long mode on Intel */
988 if (guest_efer & EFER_LMA)
989 ignore_bits &= ~(u64)EFER_SCE;
990#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +0300991
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800992 /*
993 * On EPT, we can't emulate NX, so we must switch EFER atomically.
994 * On CPUs that support "load IA32_EFER", always switch EFER
995 * atomically, since it's faster than switching it manually.
996 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800997 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800998 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +0300999 if (!(guest_efer & EFER_LMA))
1000 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001001 if (guest_efer != host_efer)
1002 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001003 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07001004 else
1005 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03001006 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001007 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07001008 clear_atomic_switch_msr(vmx, MSR_EFER);
1009
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001010 guest_efer &= ~ignore_bits;
1011 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001012
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001013 vmx->guest_msrs[efer_offset].data = guest_efer;
1014 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
1015
1016 return true;
1017 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03001018}
1019
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001020#ifdef CONFIG_X86_32
1021/*
1022 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1023 * VMCS rather than the segment table. KVM uses this helper to figure
1024 * out the current bases to poke them into the VMCS before entry.
1025 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001026static unsigned long segment_base(u16 selector)
1027{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001028 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001029 unsigned long v;
1030
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001031 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001032 return 0;
1033
Thomas Garnier45fc8752017-03-14 10:05:08 -07001034 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001035
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001036 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001037 u16 ldt_selector = kvm_read_ldt();
1038
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001039 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001040 return 0;
1041
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001042 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001043 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001044 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001045 return v;
1046}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001047#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001048
Sean Christophersone348ac72019-12-10 15:24:33 -08001049static inline bool pt_can_write_msr(struct vcpu_vmx *vmx)
1050{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001051 return vmx_pt_mode_is_host_guest() &&
Sean Christophersone348ac72019-12-10 15:24:33 -08001052 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
1053}
1054
Chao Peng2ef444f2018-10-24 16:05:12 +08001055static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range)
1056{
1057 u32 i;
1058
1059 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1060 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1061 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1062 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1063 for (i = 0; i < addr_range; i++) {
1064 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1065 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1066 }
1067}
1068
1069static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range)
1070{
1071 u32 i;
1072
1073 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1074 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1075 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1076 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1077 for (i = 0; i < addr_range; i++) {
1078 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1079 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1080 }
1081}
1082
1083static void pt_guest_enter(struct vcpu_vmx *vmx)
1084{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001085 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001086 return;
1087
Chao Peng2ef444f2018-10-24 16:05:12 +08001088 /*
Chao Pengb08c2892018-10-24 16:05:15 +08001089 * GUEST_IA32_RTIT_CTL is already set in the VMCS.
1090 * Save host state before VM entry.
Chao Peng2ef444f2018-10-24 16:05:12 +08001091 */
Chao Pengb08c2892018-10-24 16:05:15 +08001092 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
Chao Peng2ef444f2018-10-24 16:05:12 +08001093 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1094 wrmsrl(MSR_IA32_RTIT_CTL, 0);
1095 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1096 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1097 }
1098}
1099
1100static void pt_guest_exit(struct vcpu_vmx *vmx)
1101{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001102 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001103 return;
1104
1105 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1106 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1107 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1108 }
1109
1110 /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */
1111 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
1112}
1113
Sean Christopherson13b964a2019-05-07 09:06:31 -07001114void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1115 unsigned long fs_base, unsigned long gs_base)
1116{
1117 if (unlikely(fs_sel != host->fs_sel)) {
1118 if (!(fs_sel & 7))
1119 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1120 else
1121 vmcs_write16(HOST_FS_SELECTOR, 0);
1122 host->fs_sel = fs_sel;
1123 }
1124 if (unlikely(gs_sel != host->gs_sel)) {
1125 if (!(gs_sel & 7))
1126 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1127 else
1128 vmcs_write16(HOST_GS_SELECTOR, 0);
1129 host->gs_sel = gs_sel;
1130 }
1131 if (unlikely(fs_base != host->fs_base)) {
1132 vmcs_writel(HOST_FS_BASE, fs_base);
1133 host->fs_base = fs_base;
1134 }
1135 if (unlikely(gs_base != host->gs_base)) {
1136 vmcs_writel(HOST_GS_BASE, gs_base);
1137 host->gs_base = gs_base;
1138 }
1139}
1140
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001141void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001142{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001143 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001144 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001145#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01001146 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001147#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001148 unsigned long fs_base, gs_base;
1149 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03001150 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001151
Sean Christophersond264ee02018-08-27 15:21:12 -07001152 vmx->req_immediate_exit = false;
1153
Liran Alonf48b4712018-11-20 18:03:25 +02001154 /*
1155 * Note that guest MSRs to be saved/restored can also be changed
1156 * when guest state is loaded. This happens when guest transitions
1157 * to/from long-mode by setting MSR_EFER.LMA.
1158 */
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001159 if (!vmx->guest_msrs_ready) {
1160 vmx->guest_msrs_ready = true;
Liran Alonf48b4712018-11-20 18:03:25 +02001161 for (i = 0; i < vmx->save_nmsrs; ++i)
1162 kvm_set_shared_msr(vmx->guest_msrs[i].index,
1163 vmx->guest_msrs[i].data,
1164 vmx->guest_msrs[i].mask);
1165
1166 }
wanpeng lic9dfd3f2020-02-17 18:37:43 +08001167
1168 if (vmx->nested.need_vmcs12_to_shadow_sync)
1169 nested_sync_vmcs12_to_shadow(vcpu);
1170
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001171 if (vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001172 return;
1173
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001174 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001175
Avi Kivity33ed6322007-05-02 16:54:03 +03001176 /*
1177 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1178 * allow segment selectors with cpl > 0 or ti == 1.
1179 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07001180 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01001181
1182#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001183 savesegment(ds, host_state->ds_sel);
1184 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07001185
1186 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001187 if (likely(is_64bit_mm(current->mm))) {
Thomas Gleixner67580342020-05-28 16:13:52 -04001188 current_save_fsgs();
Sean Christophersone368b872018-07-23 12:32:41 -07001189 fs_sel = current->thread.fsindex;
1190 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001191 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07001192 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001193 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07001194 savesegment(fs, fs_sel);
1195 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001196 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07001197 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03001198 }
1199
Paolo Bonzini4679b612018-09-24 17:23:01 +02001200 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03001201#else
Sean Christophersone368b872018-07-23 12:32:41 -07001202 savesegment(fs, fs_sel);
1203 savesegment(gs, gs_sel);
1204 fs_base = segment_base(fs_sel);
1205 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001206#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001207
Sean Christopherson13b964a2019-05-07 09:06:31 -07001208 vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001209 vmx->guest_state_loaded = true;
Avi Kivity33ed6322007-05-02 16:54:03 +03001210}
1211
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001212static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001213{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001214 struct vmcs_host_state *host_state;
1215
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001216 if (!vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001217 return;
1218
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001219 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001220
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001221 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001222
Avi Kivityc8770e72010-11-11 12:37:26 +02001223#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001224 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001225#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001226 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1227 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001228#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001229 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001230#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001231 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001232#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001233 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001234 if (host_state->fs_sel & 7)
1235 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001236#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001237 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1238 loadsegment(ds, host_state->ds_sel);
1239 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001240 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001241#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001242 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001243#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001244 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001245#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001246 load_fixmap_gdt(raw_smp_processor_id());
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001247 vmx->guest_state_loaded = false;
1248 vmx->guest_msrs_ready = false;
Avi Kivity33ed6322007-05-02 16:54:03 +03001249}
1250
Sean Christopherson678e3152018-07-23 12:32:43 -07001251#ifdef CONFIG_X86_64
1252static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001253{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001254 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001255 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001256 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1257 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001258 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001259}
1260
Sean Christopherson678e3152018-07-23 12:32:43 -07001261static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1262{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001263 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001264 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001265 wrmsrl(MSR_KERNEL_GS_BASE, data);
1266 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001267 vmx->msr_guest_kernel_gs_base = data;
1268}
1269#endif
1270
Feng Wu28b835d2015-09-18 22:29:54 +08001271static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
1272{
1273 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1274 struct pi_desc old, new;
1275 unsigned int dest;
1276
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001277 /*
1278 * In case of hot-plug or hot-unplug, we may have to undo
1279 * vmx_vcpu_pi_put even if there is no assigned device. And we
1280 * always keep PI.NDST up to date for simplicity: it makes the
1281 * code easier, and CPU migration is not a fast path.
1282 */
1283 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08001284 return;
1285
Joao Martins132194f2019-11-11 17:20:11 +00001286 /*
1287 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
1288 * PI.NDST: pi_post_block is the one expected to change PID.NDST and the
1289 * wakeup handler expects the vCPU to be on the blocked_vcpu_list that
1290 * matches PI.NDST. Otherwise, a vcpu may not be able to be woken up
1291 * correctly.
1292 */
1293 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || vcpu->cpu == cpu) {
1294 pi_clear_sn(pi_desc);
1295 goto after_clear_sn;
1296 }
1297
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001298 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08001299 do {
1300 old.control = new.control = pi_desc->control;
1301
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001302 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08001303
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001304 if (x2apic_enabled())
1305 new.ndst = dest;
1306 else
1307 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08001308
Feng Wu28b835d2015-09-18 22:29:54 +08001309 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02001310 } while (cmpxchg64(&pi_desc->control, old.control,
1311 new.control) != old.control);
Luwei Kangc112b5f2019-02-14 10:48:07 +08001312
Joao Martins132194f2019-11-11 17:20:11 +00001313after_clear_sn:
1314
Luwei Kangc112b5f2019-02-14 10:48:07 +08001315 /*
1316 * Clear SN before reading the bitmap. The VT-d firmware
1317 * writes the bitmap and reads SN atomically (5.2.3 in the
1318 * spec), so it doesn't really have a memory barrier that
1319 * pairs with this, but we cannot do that and we need one.
1320 */
1321 smp_mb__after_atomic();
1322
Joao Martins29881b62019-11-11 17:20:12 +00001323 if (!pi_is_pir_empty(pi_desc))
Luwei Kangc112b5f2019-02-14 10:48:07 +08001324 pi_set_on(pi_desc);
Feng Wu28b835d2015-09-18 22:29:54 +08001325}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001326
Sean Christopherson5c911be2020-05-01 09:31:17 -07001327void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
1328 struct loaded_vmcs *buddy)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001330 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001331 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Sean Christopherson5c911be2020-05-01 09:31:17 -07001332 struct vmcs *prev;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001334 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001335 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001336 local_irq_disable();
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001337
1338 /*
Sean Christopherson31603d42020-03-21 12:37:49 -07001339 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
1340 * this cpu's percpu list, otherwise it may not yet be deleted
1341 * from its previous cpu's percpu list. Pairs with the
1342 * smb_wmb() in __loaded_vmcs_clear().
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001343 */
1344 smp_rmb();
1345
Nadav Har'Eld462b812011-05-24 15:26:10 +03001346 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1347 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001348 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001349 }
1350
Sean Christopherson5c911be2020-05-01 09:31:17 -07001351 prev = per_cpu(current_vmcs, cpu);
1352 if (prev != vmx->loaded_vmcs->vmcs) {
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001353 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1354 vmcs_load(vmx->loaded_vmcs->vmcs);
Sean Christopherson5c911be2020-05-01 09:31:17 -07001355
1356 /*
1357 * No indirect branch prediction barrier needed when switching
1358 * the active VMCS within a guest, e.g. on nested VM-Enter.
1359 * The L1 VMM can protect itself with retpolines, IBPB or IBRS.
1360 */
1361 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev))
1362 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001363 }
1364
1365 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001366 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001367 unsigned long sysenter_esp;
1368
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001369 /*
1370 * Flush all EPTP/VPID contexts, the new pCPU may have stale
1371 * TLB entries from its previous association with the vCPU.
1372 */
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001373 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001374
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 /*
1376 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001377 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001379 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001380 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001381 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382
1383 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1384 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001385
Nadav Har'Eld462b812011-05-24 15:26:10 +03001386 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 }
Feng Wu28b835d2015-09-18 22:29:54 +08001388
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001389 /* Setup TSC multiplier */
1390 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001391 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1392 decache_tsc_multiplier(vmx);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001393}
1394
1395/*
1396 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1397 * vcpu mutex is already taken.
1398 */
Sean Christopherson1af1bb02020-05-06 16:58:50 -07001399static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001400{
1401 struct vcpu_vmx *vmx = to_vmx(vcpu);
1402
Sean Christopherson5c911be2020-05-01 09:31:17 -07001403 vmx_vcpu_load_vmcs(vcpu, cpu, NULL);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001404
Feng Wu28b835d2015-09-18 22:29:54 +08001405 vmx_vcpu_pi_load(vcpu, cpu);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001406
Wanpeng Li74c55932017-11-29 01:31:20 -08001407 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001408}
1409
1410static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
1411{
1412 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1413
1414 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08001415 !irq_remapping_cap(IRQ_POSTING_CAP) ||
1416 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08001417 return;
1418
1419 /* Set SN when the vCPU is preempted */
1420 if (vcpu->preempted)
1421 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422}
1423
Sean Christopherson13b964a2019-05-07 09:06:31 -07001424static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425{
Feng Wu28b835d2015-09-18 22:29:54 +08001426 vmx_vcpu_pi_put(vcpu);
1427
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001428 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429}
1430
Wanpeng Lif244dee2017-07-20 01:11:54 -07001431static bool emulation_required(struct kvm_vcpu *vcpu)
1432{
1433 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
1434}
1435
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001436unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001438 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001439 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001440
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001441 if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) {
1442 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Avi Kivity6de12732011-03-07 12:51:22 +02001443 rflags = vmcs_readl(GUEST_RFLAGS);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001444 if (vmx->rmode.vm86_active) {
Avi Kivity6de12732011-03-07 12:51:22 +02001445 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
Sean Christophersone7bddc52019-09-27 14:45:18 -07001446 save_rflags = vmx->rmode.save_rflags;
Avi Kivity6de12732011-03-07 12:51:22 +02001447 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1448 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001449 vmx->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001450 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001451 return vmx->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452}
1453
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001454void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001456 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001457 unsigned long old_rflags;
Wanpeng Lif244dee2017-07-20 01:11:54 -07001458
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001459 if (enable_unrestricted_guest) {
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001460 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001461 vmx->rflags = rflags;
1462 vmcs_writel(GUEST_RFLAGS, rflags);
1463 return;
1464 }
1465
1466 old_rflags = vmx_get_rflags(vcpu);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001467 vmx->rflags = rflags;
1468 if (vmx->rmode.vm86_active) {
1469 vmx->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001470 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001471 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001473
Sean Christophersone7bddc52019-09-27 14:45:18 -07001474 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM)
1475 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476}
1477
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001478u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001479{
1480 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1481 int ret = 0;
1482
1483 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001484 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001485 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001486 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001487
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001488 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001489}
1490
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001491void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001492{
1493 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1494 u32 interruptibility = interruptibility_old;
1495
1496 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1497
Jan Kiszka48005f62010-02-19 19:38:07 +01001498 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001499 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001500 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001501 interruptibility |= GUEST_INTR_STATE_STI;
1502
1503 if ((interruptibility != interruptibility_old))
1504 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1505}
1506
Chao Pengbf8c55d2018-10-24 16:05:14 +08001507static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data)
1508{
1509 struct vcpu_vmx *vmx = to_vmx(vcpu);
1510 unsigned long value;
1511
1512 /*
1513 * Any MSR write that attempts to change bits marked reserved will
1514 * case a #GP fault.
1515 */
1516 if (data & vmx->pt_desc.ctl_bitmask)
1517 return 1;
1518
1519 /*
1520 * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will
1521 * result in a #GP unless the same write also clears TraceEn.
1522 */
1523 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) &&
1524 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN))
1525 return 1;
1526
1527 /*
1528 * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit
1529 * and FabricEn would cause #GP, if
1530 * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0
1531 */
1532 if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) &&
1533 !(data & RTIT_CTL_FABRIC_EN) &&
1534 !intel_pt_validate_cap(vmx->pt_desc.caps,
1535 PT_CAP_single_range_output))
1536 return 1;
1537
1538 /*
1539 * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that
1540 * utilize encodings marked reserved will casue a #GP fault.
1541 */
1542 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods);
1543 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) &&
1544 !test_bit((data & RTIT_CTL_MTC_RANGE) >>
1545 RTIT_CTL_MTC_RANGE_OFFSET, &value))
1546 return 1;
1547 value = intel_pt_validate_cap(vmx->pt_desc.caps,
1548 PT_CAP_cycle_thresholds);
1549 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1550 !test_bit((data & RTIT_CTL_CYC_THRESH) >>
1551 RTIT_CTL_CYC_THRESH_OFFSET, &value))
1552 return 1;
1553 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods);
1554 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1555 !test_bit((data & RTIT_CTL_PSB_FREQ) >>
1556 RTIT_CTL_PSB_FREQ_OFFSET, &value))
1557 return 1;
1558
1559 /*
1560 * If ADDRx_CFG is reserved or the encodings is >2 will
1561 * cause a #GP fault.
1562 */
1563 value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET;
1564 if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2))
1565 return 1;
1566 value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET;
1567 if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2))
1568 return 1;
1569 value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET;
1570 if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2))
1571 return 1;
1572 value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET;
1573 if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2))
1574 return 1;
1575
1576 return 0;
1577}
1578
Sean Christopherson1957aa62019-08-27 14:40:39 -07001579static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580{
Paolo Bonzinifede8072020-04-27 11:55:59 -04001581 unsigned long rip, orig_rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582
Sean Christopherson1957aa62019-08-27 14:40:39 -07001583 /*
1584 * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on
1585 * undefined behavior: Intel's SDM doesn't mandate the VMCS field be
1586 * set when EPT misconfig occurs. In practice, real hardware updates
1587 * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors
1588 * (namely Hyper-V) don't set it due to it being undefined behavior,
1589 * i.e. we end up advancing IP with some random value.
1590 */
1591 if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
1592 to_vmx(vcpu)->exit_reason != EXIT_REASON_EPT_MISCONFIG) {
Paolo Bonzinifede8072020-04-27 11:55:59 -04001593 orig_rip = kvm_rip_read(vcpu);
1594 rip = orig_rip + vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
1595#ifdef CONFIG_X86_64
1596 /*
1597 * We need to mask out the high 32 bits of RIP if not in 64-bit
1598 * mode, but just finding out that we are in 64-bit mode is
1599 * quite expensive. Only do it if there was a carry.
1600 */
1601 if (unlikely(((rip ^ orig_rip) >> 31) == 3) && !is_64_bit_mode(vcpu))
1602 rip = (u32)rip;
1603#endif
Sean Christopherson1957aa62019-08-27 14:40:39 -07001604 kvm_rip_write(vcpu, rip);
1605 } else {
1606 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
1607 return 0;
1608 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609
Glauber Costa2809f5d2009-05-12 16:21:05 -04001610 /* skipping an emulated instruction also counts */
1611 vmx_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001612
Sean Christopherson60fc3d02019-08-27 14:40:38 -07001613 return 1;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001614}
1615
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02001616/*
1617 * Handles kvm_read/write_guest_virt*() result and either injects #PF or returns
1618 * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value
1619 * indicates whether exit to userspace is needed.
1620 */
1621int vmx_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
1622 struct x86_exception *e)
1623{
1624 if (r == X86EMUL_PROPAGATE_FAULT) {
1625 kvm_inject_emulated_page_fault(vcpu, e);
1626 return 1;
1627 }
1628
1629 /*
1630 * In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED
1631 * while handling a VMX instruction KVM could've handled the request
1632 * correctly by exiting to userspace and performing I/O but there
1633 * doesn't seem to be a real use-case behind such requests, just return
1634 * KVM_EXIT_INTERNAL_ERROR for now.
1635 */
1636 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1637 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1638 vcpu->run->internal.ndata = 0;
1639
1640 return 0;
1641}
Oliver Upton5ef8acb2020-02-07 02:36:07 -08001642
1643/*
1644 * Recognizes a pending MTF VM-exit and records the nested state for later
1645 * delivery.
1646 */
1647static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)
1648{
1649 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1650 struct vcpu_vmx *vmx = to_vmx(vcpu);
1651
1652 if (!is_guest_mode(vcpu))
1653 return;
1654
1655 /*
1656 * Per the SDM, MTF takes priority over debug-trap exceptions besides
1657 * T-bit traps. As instruction emulation is completed (i.e. at the
1658 * instruction boundary), any #DB exception pending delivery must be a
1659 * debug-trap. Record the pending MTF state to be delivered in
1660 * vmx_check_nested_events().
1661 */
1662 if (nested_cpu_has_mtf(vmcs12) &&
1663 (!vcpu->arch.exception.pending ||
1664 vcpu->arch.exception.nr == DB_VECTOR))
1665 vmx->nested.mtf_pending = true;
1666 else
1667 vmx->nested.mtf_pending = false;
1668}
1669
1670static int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu)
1671{
1672 vmx_update_emulated_instruction(vcpu);
1673 return skip_emulated_instruction(vcpu);
1674}
1675
Wanpeng Licaa057a2018-03-12 04:53:03 -07001676static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1677{
1678 /*
1679 * Ensure that we clear the HLT state in the VMCS. We don't need to
1680 * explicitly skip the instruction because if the HLT state is set,
1681 * then the instruction is already executing and RIP has already been
1682 * advanced.
1683 */
1684 if (kvm_hlt_in_guest(vcpu->kvm) &&
1685 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1686 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1687}
1688
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001689static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001690{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001691 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001692 unsigned nr = vcpu->arch.exception.nr;
1693 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001694 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001695 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001696
Jim Mattsonda998b42018-10-16 14:29:22 -07001697 kvm_deliver_exception_payload(vcpu);
1698
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001699 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001700 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001701 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1702 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001703
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001704 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001705 int inc_eip = 0;
1706 if (kvm_exception_is_soft(nr))
1707 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07001708 kvm_inject_realmode_interrupt(vcpu, nr, inc_eip);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001709 return;
1710 }
1711
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001712 WARN_ON_ONCE(vmx->emulation_required);
1713
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001714 if (kvm_exception_is_soft(nr)) {
1715 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1716 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001717 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1718 } else
1719 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1720
1721 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001722
1723 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001724}
1725
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726/*
Eddie Donga75beee2007-05-17 18:55:15 +03001727 * Swap MSR entry in host/guest MSR entry array.
1728 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001729static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001730{
Avi Kivity26bb0982009-09-07 11:14:12 +03001731 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001732
1733 tmp = vmx->guest_msrs[to];
1734 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1735 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001736}
1737
1738/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001739 * Set up the vmcs to automatically save and restore system
1740 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1741 * mode, as fiddling with msrs is very expensive.
1742 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001743static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001744{
Avi Kivity26bb0982009-09-07 11:14:12 +03001745 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001746
Eddie Donga75beee2007-05-17 18:55:15 +03001747 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001748#ifdef CONFIG_X86_64
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001749 /*
1750 * The SYSCALL MSRs are only needed on long mode guests, and only
1751 * when EFER.SCE is set.
1752 */
1753 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
1754 index = __find_msr_index(vmx, MSR_STAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001755 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001756 move_msr_up(vmx, index, save_nmsrs++);
1757 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001758 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001759 move_msr_up(vmx, index, save_nmsrs++);
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001760 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
1761 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001762 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001763 }
Eddie Donga75beee2007-05-17 18:55:15 +03001764#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001765 index = __find_msr_index(vmx, MSR_EFER);
1766 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001767 move_msr_up(vmx, index, save_nmsrs++);
Jim Mattson0023ef32018-12-05 15:28:58 -08001768 index = __find_msr_index(vmx, MSR_TSC_AUX);
1769 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
1770 move_msr_up(vmx, index, save_nmsrs++);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001771 index = __find_msr_index(vmx, MSR_IA32_TSX_CTRL);
1772 if (index >= 0)
1773 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001774
Avi Kivity26bb0982009-09-07 11:14:12 +03001775 vmx->save_nmsrs = save_nmsrs;
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001776 vmx->guest_msrs_ready = false;
Avi Kivity58972972009-02-24 22:26:47 +02001777
Yang Zhang8d146952013-01-25 10:18:50 +08001778 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001779 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001780}
1781
Leonid Shatz326e7422018-11-06 12:14:25 +02001782static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001784 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1785 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001786
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001787 /*
1788 * We're here if L1 chose not to trap WRMSR to TSC. According
1789 * to the spec, this should set L1's TSC; The offset that L1
1790 * set for L2 remains unchanged, and still needs to be added
1791 * to the newly set TSC to get L2's TSC.
1792 */
1793 if (is_guest_mode(vcpu) &&
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08001794 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001795 g_tsc_offset = vmcs12->tsc_offset;
1796
1797 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1798 vcpu->arch.tsc_offset - g_tsc_offset,
1799 offset);
1800 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1801 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802}
1803
Nadav Har'El801d3422011-05-25 23:02:23 +03001804/*
1805 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1806 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1807 * all guests if the "nested" module option is off, and can also be disabled
1808 * for a single guest by disabling its VMX cpuid bit.
1809 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001810bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001811{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001812 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001813}
1814
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001815static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1816 uint64_t val)
1817{
1818 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1819
1820 return !(val & ~valid_bits);
1821}
1822
Tom Lendacky801e4592018-02-21 13:39:51 -06001823static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1824{
Paolo Bonzini13893092018-02-26 13:40:09 +01001825 switch (msr->index) {
1826 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1827 if (!nested)
1828 return 1;
1829 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
Like Xu27461da32020-05-29 15:43:45 +08001830 case MSR_IA32_PERF_CAPABILITIES:
1831 msr->data = vmx_get_perf_capabilities();
1832 return 0;
Paolo Bonzini13893092018-02-26 13:40:09 +01001833 default:
Peter Xu12bc2132020-06-22 18:04:42 -04001834 return KVM_MSR_RET_INVALID;
Paolo Bonzini13893092018-02-26 13:40:09 +01001835 }
Tom Lendacky801e4592018-02-21 13:39:51 -06001836}
1837
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001838/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839 * Reads an msr value (of 'msr_index') into 'pdata'.
1840 * Returns 0 on success, non-0 otherwise.
1841 * Assumes vcpu_load() was already called.
1842 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001843static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001845 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001846 struct shared_msr_entry *msr;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001847 u32 index;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001849 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001850#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001852 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853 break;
1854 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001855 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001857 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001858 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001859 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001860#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001862 return kvm_get_msr_common(vcpu, msr_info);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001863 case MSR_IA32_TSX_CTRL:
1864 if (!msr_info->host_initiated &&
1865 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
1866 return 1;
1867 goto find_shared_msr;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001868 case MSR_IA32_UMWAIT_CONTROL:
1869 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
1870 return 1;
1871
1872 msr_info->data = vmx->msr_ia32_umwait_control;
1873 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001874 case MSR_IA32_SPEC_CTRL:
1875 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001876 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1877 return 1;
1878
1879 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1880 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001882 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883 break;
1884 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001885 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001886 break;
1887 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001888 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001890 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001891 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001892 (!msr_info->host_initiated &&
1893 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001894 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001895 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001896 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001897 case MSR_IA32_MCG_EXT_CTL:
1898 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001899 !(vmx->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001900 FEAT_CTL_LMCE_ENABLED))
Jan Kiszkacae50132014-01-04 18:47:22 +01001901 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001902 msr_info->data = vcpu->arch.mcg_ext_ctl;
1903 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001904 case MSR_IA32_FEAT_CTL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001905 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001906 break;
1907 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1908 if (!nested_vmx_allowed(vcpu))
1909 return 1;
Vitaly Kuznetsov31de3d22020-02-05 13:30:33 +01001910 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1911 &msr_info->data))
1912 return 1;
1913 /*
1914 * Enlightened VMCS v1 doesn't have certain fields, but buggy
1915 * Hyper-V versions are still trying to use corresponding
1916 * features when they are exposed. Filter out the essential
1917 * minimum.
1918 */
1919 if (!msr_info->host_initiated &&
1920 vmx->nested.enlightened_vmcs_enabled)
1921 nested_evmcs_filter_control_msr(msr_info->index,
1922 &msr_info->data);
1923 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001924 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001925 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001926 return 1;
1927 msr_info->data = vmx->pt_desc.guest.ctl;
1928 break;
1929 case MSR_IA32_RTIT_STATUS:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001930 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001931 return 1;
1932 msr_info->data = vmx->pt_desc.guest.status;
1933 break;
1934 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001935 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001936 !intel_pt_validate_cap(vmx->pt_desc.caps,
1937 PT_CAP_cr3_filtering))
1938 return 1;
1939 msr_info->data = vmx->pt_desc.guest.cr3_match;
1940 break;
1941 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001942 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001943 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1944 PT_CAP_topa_output) &&
1945 !intel_pt_validate_cap(vmx->pt_desc.caps,
1946 PT_CAP_single_range_output)))
1947 return 1;
1948 msr_info->data = vmx->pt_desc.guest.output_base;
1949 break;
1950 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001951 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001952 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1953 PT_CAP_topa_output) &&
1954 !intel_pt_validate_cap(vmx->pt_desc.caps,
1955 PT_CAP_single_range_output)))
1956 return 1;
1957 msr_info->data = vmx->pt_desc.guest.output_mask;
1958 break;
1959 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
1960 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christopherson2ef76192020-03-02 15:56:22 -08001961 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001962 (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
1963 PT_CAP_num_address_ranges)))
1964 return 1;
1965 if (index % 2)
1966 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
1967 else
1968 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
1969 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001970 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001971 if (!msr_info->host_initiated &&
1972 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001973 return 1;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001974 goto find_shared_msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 default:
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001976 find_shared_msr:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001977 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001978 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001979 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001980 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001982 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983 }
1984
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 return 0;
1986}
1987
Sean Christopherson24085002020-04-28 16:10:24 -07001988static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu,
1989 u64 data)
1990{
1991#ifdef CONFIG_X86_64
1992 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
1993 return (u32)data;
1994#endif
1995 return (unsigned long)data;
1996}
1997
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998/*
Miaohe Lin311497e2019-12-11 14:26:25 +08001999 * Writes msr value into the appropriate "register".
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000 * Returns 0 on success, non-0 otherwise.
2001 * Assumes vcpu_load() was already called.
2002 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002003static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002005 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002006 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002007 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002008 u32 msr_index = msr_info->index;
2009 u64 data = msr_info->data;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002010 u32 index;
Eddie Dong2cc51562007-05-21 07:28:09 +03002011
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002013 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002014 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002015 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002016#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002018 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019 vmcs_writel(GUEST_FS_BASE, data);
2020 break;
2021 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002022 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023 vmcs_writel(GUEST_GS_BASE, data);
2024 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002025 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07002026 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002027 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028#endif
2029 case MSR_IA32_SYSENTER_CS:
Sean Christophersonde70d272019-05-07 09:06:36 -07002030 if (is_guest_mode(vcpu))
2031 get_vmcs12(vcpu)->guest_sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032 vmcs_write32(GUEST_SYSENTER_CS, data);
2033 break;
2034 case MSR_IA32_SYSENTER_EIP:
Sean Christopherson24085002020-04-28 16:10:24 -07002035 if (is_guest_mode(vcpu)) {
2036 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002037 get_vmcs12(vcpu)->guest_sysenter_eip = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002038 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002039 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040 break;
2041 case MSR_IA32_SYSENTER_ESP:
Sean Christopherson24085002020-04-28 16:10:24 -07002042 if (is_guest_mode(vcpu)) {
2043 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002044 get_vmcs12(vcpu)->guest_sysenter_esp = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002045 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002046 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047 break;
Sean Christopherson699a1ac2019-05-07 09:06:37 -07002048 case MSR_IA32_DEBUGCTLMSR:
2049 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
2050 VM_EXIT_SAVE_DEBUG_CONTROLS)
2051 get_vmcs12(vcpu)->guest_ia32_debugctl = data;
2052
2053 ret = kvm_set_msr_common(vcpu, msr_info);
2054 break;
2055
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002056 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08002057 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02002058 (!msr_info->host_initiated &&
2059 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002060 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002061 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07002062 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002064 vmcs_write64(GUEST_BNDCFGS, data);
2065 break;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08002066 case MSR_IA32_UMWAIT_CONTROL:
2067 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
2068 return 1;
2069
2070 /* The reserved bit 1 and non-32 bit [63:32] should be zero */
2071 if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32)))
2072 return 1;
2073
2074 vmx->msr_ia32_umwait_control = data;
2075 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002076 case MSR_IA32_SPEC_CTRL:
2077 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002078 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2079 return 1;
2080
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002081 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002082 return 1;
2083
2084 vmx->spec_ctrl = data;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002085 if (!data)
2086 break;
2087
2088 /*
2089 * For non-nested:
2090 * When it's written (to non-zero) for the first time, pass
2091 * it through.
2092 *
2093 * For nested:
2094 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002095 * nested_vmx_prepare_msr_bitmap. We should not touch the
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002096 * vmcs02.msr_bitmap here since it gets completely overwritten
2097 * in the merging. We update the vmcs01 here for L1 as well
2098 * since it will end up touching the MSR anyway now.
2099 */
2100 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
2101 MSR_IA32_SPEC_CTRL,
2102 MSR_TYPE_RW);
2103 break;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002104 case MSR_IA32_TSX_CTRL:
2105 if (!msr_info->host_initiated &&
2106 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
2107 return 1;
2108 if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR))
2109 return 1;
2110 goto find_shared_msr;
Ashok Raj15d45072018-02-01 22:59:43 +01002111 case MSR_IA32_PRED_CMD:
2112 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01002113 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2114 return 1;
2115
2116 if (data & ~PRED_CMD_IBPB)
2117 return 1;
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002118 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL))
2119 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002120 if (!data)
2121 break;
2122
2123 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2124
2125 /*
2126 * For non-nested:
2127 * When it's written (to non-zero) for the first time, pass
2128 * it through.
2129 *
2130 * For nested:
2131 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002132 * nested_vmx_prepare_msr_bitmap. We should not touch the
Ashok Raj15d45072018-02-01 22:59:43 +01002133 * vmcs02.msr_bitmap here since it gets completely overwritten
2134 * in the merging.
2135 */
2136 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
2137 MSR_TYPE_W);
2138 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002139 case MSR_IA32_CR_PAT:
Sean Christophersond28f4292019-05-07 09:06:27 -07002140 if (!kvm_pat_valid(data))
2141 return 1;
2142
Sean Christopherson142e4be2019-05-07 09:06:35 -07002143 if (is_guest_mode(vcpu) &&
2144 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
2145 get_vmcs12(vcpu)->guest_ia32_pat = data;
2146
Sheng Yang468d4722008-10-09 16:01:55 +08002147 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2148 vmcs_write64(GUEST_IA32_PAT, data);
2149 vcpu->arch.pat = data;
2150 break;
2151 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002152 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002153 break;
Will Auldba904632012-11-29 12:42:50 -08002154 case MSR_IA32_TSC_ADJUST:
2155 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002156 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002157 case MSR_IA32_MCG_EXT_CTL:
2158 if ((!msr_info->host_initiated &&
2159 !(to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002160 FEAT_CTL_LMCE_ENABLED)) ||
Ashok Rajc45dcc72016-06-22 14:59:56 +08002161 (data & ~MCG_EXT_CTL_LMCE_EN))
2162 return 1;
2163 vcpu->arch.mcg_ext_ctl = data;
2164 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002165 case MSR_IA32_FEAT_CTL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002166 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08002167 (to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002168 FEAT_CTL_LOCKED && !msr_info->host_initiated))
Jan Kiszkacae50132014-01-04 18:47:22 +01002169 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08002170 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002171 if (msr_info->host_initiated && data == 0)
2172 vmx_leave_nested(vcpu);
2173 break;
2174 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08002175 if (!msr_info->host_initiated)
2176 return 1; /* they are read-only */
2177 if (!nested_vmx_allowed(vcpu))
2178 return 1;
2179 return vmx_set_vmx_msr(vcpu, msr_index, data);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002180 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08002181 if (!vmx_pt_mode_is_host_guest() ||
Luwei Kangee85dec2018-10-24 16:05:16 +08002182 vmx_rtit_ctl_check(vcpu, data) ||
2183 vmx->nested.vmxon)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002184 return 1;
2185 vmcs_write64(GUEST_IA32_RTIT_CTL, data);
2186 vmx->pt_desc.guest.ctl = data;
Chao Pengb08c2892018-10-24 16:05:15 +08002187 pt_update_intercept_for_msr(vmx);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002188 break;
2189 case MSR_IA32_RTIT_STATUS:
Sean Christophersone348ac72019-12-10 15:24:33 -08002190 if (!pt_can_write_msr(vmx))
2191 return 1;
2192 if (data & MSR_IA32_RTIT_STATUS_MASK)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002193 return 1;
2194 vmx->pt_desc.guest.status = data;
2195 break;
2196 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christophersone348ac72019-12-10 15:24:33 -08002197 if (!pt_can_write_msr(vmx))
2198 return 1;
2199 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2200 PT_CAP_cr3_filtering))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002201 return 1;
2202 vmx->pt_desc.guest.cr3_match = data;
2203 break;
2204 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christophersone348ac72019-12-10 15:24:33 -08002205 if (!pt_can_write_msr(vmx))
2206 return 1;
2207 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2208 PT_CAP_topa_output) &&
2209 !intel_pt_validate_cap(vmx->pt_desc.caps,
2210 PT_CAP_single_range_output))
2211 return 1;
2212 if (data & MSR_IA32_RTIT_OUTPUT_BASE_MASK)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002213 return 1;
2214 vmx->pt_desc.guest.output_base = data;
2215 break;
2216 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christophersone348ac72019-12-10 15:24:33 -08002217 if (!pt_can_write_msr(vmx))
2218 return 1;
2219 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2220 PT_CAP_topa_output) &&
2221 !intel_pt_validate_cap(vmx->pt_desc.caps,
2222 PT_CAP_single_range_output))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002223 return 1;
2224 vmx->pt_desc.guest.output_mask = data;
2225 break;
2226 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christophersone348ac72019-12-10 15:24:33 -08002227 if (!pt_can_write_msr(vmx))
2228 return 1;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002229 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christophersone348ac72019-12-10 15:24:33 -08002230 if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
2231 PT_CAP_num_address_ranges))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002232 return 1;
Sean Christophersonfe6ed362019-12-10 15:24:32 -08002233 if (is_noncanonical_address(data, vcpu))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002234 return 1;
2235 if (index % 2)
2236 vmx->pt_desc.guest.addr_b[index / 2] = data;
2237 else
2238 vmx->pt_desc.guest.addr_a[index / 2] = data;
2239 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002240 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002241 if (!msr_info->host_initiated &&
2242 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002243 return 1;
2244 /* Check reserved bit, higher 32 bits should be zero */
2245 if ((data >> 32) != 0)
2246 return 1;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002247 goto find_shared_msr;
2248
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 default:
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002250 find_shared_msr:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002251 msr = find_msr_entry(vmx, msr_index);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05002252 if (msr)
2253 ret = vmx_set_guest_msr(vmx, msr, data);
2254 else
2255 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 }
2257
Eddie Dong2cc51562007-05-21 07:28:09 +03002258 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259}
2260
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002261static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262{
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002263 unsigned long guest_owned_bits;
2264
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002265 kvm_register_mark_available(vcpu, reg);
2266
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002267 switch (reg) {
2268 case VCPU_REGS_RSP:
2269 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2270 break;
2271 case VCPU_REGS_RIP:
2272 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2273 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002274 case VCPU_EXREG_PDPTR:
2275 if (enable_ept)
2276 ept_save_pdptrs(vcpu);
2277 break;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07002278 case VCPU_EXREG_CR0:
2279 guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2280
2281 vcpu->arch.cr0 &= ~guest_owned_bits;
2282 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits;
2283 break;
Sean Christopherson34059c22019-09-27 14:45:23 -07002284 case VCPU_EXREG_CR3:
2285 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
2286 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2287 break;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002288 case VCPU_EXREG_CR4:
2289 guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2290
2291 vcpu->arch.cr4 &= ~guest_owned_bits;
2292 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits;
2293 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002294 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07002295 WARN_ON_ONCE(1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002296 break;
2297 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298}
2299
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300static __init int cpu_has_kvm_support(void)
2301{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002302 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303}
2304
2305static __init int vmx_disabled_by_bios(void)
2306{
Sean Christophersona4d0b2f2019-12-20 20:45:09 -08002307 return !boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2308 !boot_cpu_has(X86_FEATURE_VMX);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309}
2310
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002311static int kvm_cpu_vmxon(u64 vmxon_pointer)
Dongxiao Xu7725b892010-05-11 18:29:38 +08002312{
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002313 u64 msr;
2314
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002315 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002316 intel_pt_handle_vmx(1);
2317
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002318 asm_volatile_goto("1: vmxon %[vmxon_pointer]\n\t"
2319 _ASM_EXTABLE(1b, %l[fault])
2320 : : [vmxon_pointer] "m"(vmxon_pointer)
2321 : : fault);
2322 return 0;
2323
2324fault:
2325 WARN_ONCE(1, "VMXON faulted, MSR_IA32_FEAT_CTL (0x3a) = 0x%llx\n",
2326 rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr) ? 0xdeadbeef : msr);
2327 intel_pt_handle_vmx(0);
2328 cr4_clear_bits(X86_CR4_VMXE);
2329
2330 return -EFAULT;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002331}
2332
Radim Krčmář13a34e02014-08-28 15:13:03 +02002333static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334{
2335 int cpu = raw_smp_processor_id();
2336 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002337 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002339 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02002340 return -EBUSY;
2341
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002342 /*
2343 * This can happen if we hot-added a CPU but failed to allocate
2344 * VP assist page for it.
2345 */
2346 if (static_branch_unlikely(&enable_evmcs) &&
2347 !hv_get_vp_assist_page(cpu))
2348 return -EFAULT;
2349
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002350 r = kvm_cpu_vmxon(phys_addr);
2351 if (r)
2352 return r;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002353
David Hildenbrandfdf288b2017-08-24 20:51:29 +02002354 if (enable_ept)
2355 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02002356
2357 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358}
2359
Nadav Har'Eld462b812011-05-24 15:26:10 +03002360static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002361{
2362 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002363 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002364
Nadav Har'Eld462b812011-05-24 15:26:10 +03002365 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2366 loaded_vmcss_on_cpu_link)
2367 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002368}
2369
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002370
2371/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2372 * tricks.
2373 */
2374static void kvm_cpu_vmxoff(void)
2375{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002376 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002377
2378 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002379 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002380}
2381
Radim Krčmář13a34e02014-08-28 15:13:03 +02002382static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002384 vmclear_local_loaded_vmcss();
2385 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
Sean Christopherson7a57c092020-03-12 11:04:16 -07002388/*
2389 * There is no X86_FEATURE for SGX yet, but anyway we need to query CPUID
2390 * directly instead of going through cpu_has(), to ensure KVM is trapping
2391 * ENCLS whenever it's supported in hardware. It does not matter whether
2392 * the host OS supports or has enabled SGX.
2393 */
2394static bool cpu_has_sgx(void)
2395{
2396 return cpuid_eax(0) >= 0x12 && (cpuid_eax(0x12) & BIT(0));
2397}
2398
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002399static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002400 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401{
2402 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002403 u32 ctl = ctl_min | ctl_opt;
2404
2405 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2406
2407 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2408 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2409
2410 /* Ensure minimum (required) set of control bits are supported. */
2411 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002412 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002413
2414 *result = ctl;
2415 return 0;
2416}
2417
Sean Christopherson7caaa712018-12-03 13:53:01 -08002418static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
2419 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002420{
2421 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002422 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002423 u32 _pin_based_exec_control = 0;
2424 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002425 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002426 u32 _vmexit_control = 0;
2427 u32 _vmentry_control = 0;
2428
Paolo Bonzini13893092018-02-26 13:40:09 +01002429 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05302430 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002431#ifdef CONFIG_X86_64
2432 CPU_BASED_CR8_LOAD_EXITING |
2433 CPU_BASED_CR8_STORE_EXITING |
2434#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002435 CPU_BASED_CR3_LOAD_EXITING |
2436 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08002437 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002438 CPU_BASED_MOV_DR_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08002439 CPU_BASED_USE_TSC_OFFSETTING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002440 CPU_BASED_MWAIT_EXITING |
2441 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002442 CPU_BASED_INVLPG_EXITING |
2443 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002444
Sheng Yangf78e0e22007-10-29 09:40:42 +08002445 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002446 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002447 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002448 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2449 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002450 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002451#ifdef CONFIG_X86_64
2452 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2453 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2454 ~CPU_BASED_CR8_STORE_EXITING;
2455#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002456 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002457 min2 = 0;
2458 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002459 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002460 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002461 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002462 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002463 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002464 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02002465 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00002466 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002467 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002468 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002469 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08002470 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08002471 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02002472 SECONDARY_EXEC_RDSEED_EXITING |
2473 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08002474 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04002475 SECONDARY_EXEC_TSC_SCALING |
Tao Xue69e72fa2019-07-16 14:55:49 +08002476 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Chao Pengf99e3da2018-10-24 16:05:10 +08002477 SECONDARY_EXEC_PT_USE_GPA |
2478 SECONDARY_EXEC_PT_CONCEAL_VMX |
Sean Christopherson7a57c092020-03-12 11:04:16 -07002479 SECONDARY_EXEC_ENABLE_VMFUNC;
2480 if (cpu_has_sgx())
2481 opt2 |= SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08002482 if (adjust_vmx_controls(min2, opt2,
2483 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002484 &_cpu_based_2nd_exec_control) < 0)
2485 return -EIO;
2486 }
2487#ifndef CONFIG_X86_64
2488 if (!(_cpu_based_2nd_exec_control &
2489 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2490 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2491#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002492
2493 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2494 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002495 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002496 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2497 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002498
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002499 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08002500 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002501
Sheng Yangd56f5462008-04-25 10:13:16 +08002502 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002503 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2504 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002505 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2506 CPU_BASED_CR3_STORE_EXITING |
2507 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08002508 } else if (vmx_cap->ept) {
2509 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002510 pr_warn_once("EPT CAP should not exist if not support "
2511 "1-setting enable EPT VM-execution control\n");
2512 }
2513 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002514 vmx_cap->vpid) {
2515 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002516 pr_warn_once("VPID CAP should not exist if not support "
2517 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002518 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002519
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002520 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002521#ifdef CONFIG_X86_64
2522 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2523#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002524 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002525 VM_EXIT_LOAD_IA32_PAT |
2526 VM_EXIT_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002527 VM_EXIT_CLEAR_BNDCFGS |
2528 VM_EXIT_PT_CONCEAL_PIP |
2529 VM_EXIT_CLEAR_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002530 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2531 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002532 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002533
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002534 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2535 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2536 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002537 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2538 &_pin_based_exec_control) < 0)
2539 return -EIO;
2540
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002541 if (cpu_has_broken_vmx_preemption_timer())
2542 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002543 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002544 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002545 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2546
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002547 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002548 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2549 VM_ENTRY_LOAD_IA32_PAT |
2550 VM_ENTRY_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002551 VM_ENTRY_LOAD_BNDCFGS |
2552 VM_ENTRY_PT_CONCEAL_PIP |
2553 VM_ENTRY_LOAD_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002554 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2555 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002556 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002558 /*
2559 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2560 * can't be used due to an errata where VM Exit may incorrectly clear
2561 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2562 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2563 */
2564 if (boot_cpu_data.x86 == 0x6) {
2565 switch (boot_cpu_data.x86_model) {
2566 case 26: /* AAK155 */
2567 case 30: /* AAP115 */
2568 case 37: /* AAT100 */
2569 case 44: /* BC86,AAY89,BD102 */
2570 case 46: /* BA97 */
Sean Christopherson85ba2b12019-01-14 12:12:02 -08002571 _vmentry_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002572 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2573 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2574 "does not work properly. Using workaround\n");
2575 break;
2576 default:
2577 break;
2578 }
2579 }
2580
2581
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002582 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002583
2584 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2585 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002586 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002587
2588#ifdef CONFIG_X86_64
2589 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2590 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002591 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002592#endif
2593
2594 /* Require Write-Back (WB) memory type for VMCS accesses. */
2595 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002596 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002597
Yang, Sheng002c7f72007-07-31 14:23:01 +03002598 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002599 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002600 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002601
Liran Alon2307af12018-06-29 22:59:04 +03002602 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002603
Yang, Sheng002c7f72007-07-31 14:23:01 +03002604 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2605 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002606 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002607 vmcs_conf->vmexit_ctrl = _vmexit_control;
2608 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002609
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002610 if (static_branch_unlikely(&enable_evmcs))
2611 evmcs_sanitize_exec_ctrls(vmcs_conf);
2612
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002613 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002614}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615
Ben Gardon41836832019-02-11 11:02:52 -08002616struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002617{
2618 int node = cpu_to_node(cpu);
2619 struct page *pages;
2620 struct vmcs *vmcs;
2621
Ben Gardon41836832019-02-11 11:02:52 -08002622 pages = __alloc_pages_node(node, flags, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 if (!pages)
2624 return NULL;
2625 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002626 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002627
2628 /* KVM supports Enlightened VMCS v1 only */
2629 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002630 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002631 else
Liran Alon392b2f22018-06-23 02:35:01 +03002632 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002633
Liran Alon491a6032018-06-23 02:35:12 +03002634 if (shadow)
2635 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636 return vmcs;
2637}
2638
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002639void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002641 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642}
2643
Nadav Har'Eld462b812011-05-24 15:26:10 +03002644/*
2645 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2646 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002647void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002648{
2649 if (!loaded_vmcs->vmcs)
2650 return;
2651 loaded_vmcs_clear(loaded_vmcs);
2652 free_vmcs(loaded_vmcs->vmcs);
2653 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002654 if (loaded_vmcs->msr_bitmap)
2655 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002656 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002657}
2658
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002659int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002660{
Liran Alon491a6032018-06-23 02:35:12 +03002661 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002662 if (!loaded_vmcs->vmcs)
2663 return -ENOMEM;
2664
Sean Christophersond260f9e2020-03-21 12:37:50 -07002665 vmcs_clear(loaded_vmcs->vmcs);
2666
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002667 loaded_vmcs->shadow_vmcs = NULL;
Sean Christopherson804939e2019-05-07 12:18:05 -07002668 loaded_vmcs->hv_timer_soft_disabled = false;
Sean Christophersond260f9e2020-03-21 12:37:50 -07002669 loaded_vmcs->cpu = -1;
2670 loaded_vmcs->launched = 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002671
2672 if (cpu_has_vmx_msr_bitmap()) {
Ben Gardon41836832019-02-11 11:02:52 -08002673 loaded_vmcs->msr_bitmap = (unsigned long *)
2674 __get_free_page(GFP_KERNEL_ACCOUNT);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002675 if (!loaded_vmcs->msr_bitmap)
2676 goto out_vmcs;
2677 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002678
Arnd Bergmann1f008e12018-05-25 17:36:17 +02002679 if (IS_ENABLED(CONFIG_HYPERV) &&
2680 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002681 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2682 struct hv_enlightened_vmcs *evmcs =
2683 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2684
2685 evmcs->hv_enlightenments_control.msr_bitmap = 1;
2686 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002687 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002688
2689 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002690 memset(&loaded_vmcs->controls_shadow, 0,
2691 sizeof(struct vmcs_controls_shadow));
Sean Christophersond7ee0392018-07-23 12:32:47 -07002692
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002693 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002694
2695out_vmcs:
2696 free_loaded_vmcs(loaded_vmcs);
2697 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002698}
2699
Sam Ravnborg39959582007-06-01 00:47:13 -07002700static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701{
2702 int cpu;
2703
Zachary Amsden3230bb42009-09-29 11:38:37 -10002704 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002705 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002706 per_cpu(vmxarea, cpu) = NULL;
2707 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708}
2709
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710static __init int alloc_kvm_area(void)
2711{
2712 int cpu;
2713
Zachary Amsden3230bb42009-09-29 11:38:37 -10002714 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 struct vmcs *vmcs;
2716
Ben Gardon41836832019-02-11 11:02:52 -08002717 vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718 if (!vmcs) {
2719 free_kvm_area();
2720 return -ENOMEM;
2721 }
2722
Liran Alon2307af12018-06-29 22:59:04 +03002723 /*
2724 * When eVMCS is enabled, alloc_vmcs_cpu() sets
2725 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
2726 * revision_id reported by MSR_IA32_VMX_BASIC.
2727 *
Linus Torvalds312a4662018-12-26 17:03:51 -08002728 * However, even though not explicitly documented by
Liran Alon2307af12018-06-29 22:59:04 +03002729 * TLFS, VMXArea passed as VMXON argument should
2730 * still be marked with revision_id reported by
2731 * physical CPU.
2732 */
2733 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002734 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002735
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 per_cpu(vmxarea, cpu) = vmcs;
2737 }
2738 return 0;
2739}
2740
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002741static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002742 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002744 if (!emulate_invalid_guest_state) {
2745 /*
2746 * CS and SS RPL should be equal during guest entry according
2747 * to VMX spec, but in reality it is not always so. Since vcpu
2748 * is in the middle of the transition from real mode to
2749 * protected mode it is safe to assume that RPL 0 is a good
2750 * default value.
2751 */
2752 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03002753 save->selector &= ~SEGMENT_RPL_MASK;
2754 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02002755 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002757 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758}
2759
2760static void enter_pmode(struct kvm_vcpu *vcpu)
2761{
2762 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002763 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764
Gleb Natapovd99e4152012-12-20 16:57:45 +02002765 /*
2766 * Update real mode segment cache. It may be not up-to-date if sement
2767 * register was written while vcpu was in a guest mode.
2768 */
2769 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2770 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2771 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2772 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2773 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2774 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2775
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002776 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002778 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779
2780 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002781 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2782 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 vmcs_writel(GUEST_RFLAGS, flags);
2784
Rusty Russell66aee912007-07-17 23:34:16 +10002785 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2786 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787
2788 update_exception_bitmap(vcpu);
2789
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002790 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2791 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2792 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2793 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2794 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2795 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796}
2797
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002798static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799{
Mathias Krause772e0312012-08-30 01:30:19 +02002800 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002801 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802
Gleb Natapovd99e4152012-12-20 16:57:45 +02002803 var.dpl = 0x3;
2804 if (seg == VCPU_SREG_CS)
2805 var.type = 0x3;
2806
2807 if (!emulate_invalid_guest_state) {
2808 var.selector = var.base >> 4;
2809 var.base = var.base & 0xffff0;
2810 var.limit = 0xffff;
2811 var.g = 0;
2812 var.db = 0;
2813 var.present = 1;
2814 var.s = 1;
2815 var.l = 0;
2816 var.unusable = 0;
2817 var.type = 0x3;
2818 var.avl = 0;
2819 if (save->base & 0xf)
2820 printk_once(KERN_WARNING "kvm: segment base is not "
2821 "paragraph aligned when entering "
2822 "protected mode (seg=%d)", seg);
2823 }
2824
2825 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05002826 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002827 vmcs_write32(sf->limit, var.limit);
2828 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829}
2830
2831static void enter_rmode(struct kvm_vcpu *vcpu)
2832{
2833 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002834 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002835 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002837 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2838 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2839 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2840 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2841 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002842 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2843 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002844
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002845 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846
Gleb Natapov776e58e2011-03-13 12:34:27 +02002847 /*
2848 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002849 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002850 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002851 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002852 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2853 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002854
Avi Kivity2fb92db2011-04-27 19:42:18 +03002855 vmx_segment_cache_clear(vmx);
2856
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002857 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2860
2861 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002862 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002864 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865
2866 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002867 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 update_exception_bitmap(vcpu);
2869
Gleb Natapovd99e4152012-12-20 16:57:45 +02002870 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2871 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2872 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2873 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2874 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2875 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002876
Eddie Dong8668a3c2007-10-10 14:26:45 +08002877 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878}
2879
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002880void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Amit Shah401d10d2009-02-20 22:53:37 +05302881{
2882 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002883 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2884
2885 if (!msr)
2886 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302887
Avi Kivityf6801df2010-01-21 15:31:50 +02002888 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302889 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002890 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302891 msr->data = efer;
2892 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002893 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302894
2895 msr->data = efer & ~EFER_LME;
2896 }
2897 setup_msrs(vmx);
2898}
2899
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002900#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901
2902static void enter_lmode(struct kvm_vcpu *vcpu)
2903{
2904 u32 guest_tr_ar;
2905
Avi Kivity2fb92db2011-04-27 19:42:18 +03002906 vmx_segment_cache_clear(to_vmx(vcpu));
2907
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002909 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002910 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2911 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002913 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
2914 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915 }
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
2919static void exit_lmode(struct kvm_vcpu *vcpu)
2920{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002921 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002922 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923}
2924
2925#endif
2926
Sean Christopherson77809382020-03-20 14:28:18 -07002927static void vmx_flush_tlb_all(struct kvm_vcpu *vcpu)
Sean Christopherson5058b692020-03-20 14:28:14 -07002928{
2929 struct vcpu_vmx *vmx = to_vmx(vcpu);
2930
2931 /*
Sean Christopherson77809382020-03-20 14:28:18 -07002932 * INVEPT must be issued when EPT is enabled, irrespective of VPID, as
2933 * the CPU is not required to invalidate guest-physical mappings on
2934 * VM-Entry, even if VPID is disabled. Guest-physical mappings are
2935 * associated with the root EPT structure and not any particular VPID
2936 * (INVVPID also isn't required to invalidate guest-physical mappings).
Sean Christopherson5058b692020-03-20 14:28:14 -07002937 */
2938 if (enable_ept) {
2939 ept_sync_global();
2940 } else if (enable_vpid) {
2941 if (cpu_has_vmx_invvpid_global()) {
2942 vpid_sync_vcpu_global();
2943 } else {
2944 vpid_sync_vcpu_single(vmx->vpid);
2945 vpid_sync_vcpu_single(vmx->nested.vpid02);
2946 }
2947 }
2948}
2949
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002950static void vmx_flush_tlb_current(struct kvm_vcpu *vcpu)
2951{
Sean Christopherson2a40b902020-07-15 20:41:18 -07002952 struct kvm_mmu *mmu = vcpu->arch.mmu;
2953 u64 root_hpa = mmu->root_hpa;
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002954
2955 /* No flush required if the current context is invalid. */
2956 if (!VALID_PAGE(root_hpa))
2957 return;
2958
2959 if (enable_ept)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002960 ept_sync_context(construct_eptp(vcpu, root_hpa,
2961 mmu->shadow_root_level));
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002962 else if (!is_guest_mode(vcpu))
2963 vpid_sync_context(to_vmx(vcpu)->vpid);
2964 else
2965 vpid_sync_context(nested_get_vpid02(vcpu));
2966}
2967
Junaid Shahidfaff8752018-06-29 13:10:05 -07002968static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
2969{
Junaid Shahidfaff8752018-06-29 13:10:05 -07002970 /*
Sean Christophersonad104b52020-03-20 14:28:11 -07002971 * vpid_sync_vcpu_addr() is a nop if vmx->vpid==0, see the comment in
2972 * vmx_flush_tlb_guest() for an explanation of why this is ok.
Junaid Shahidfaff8752018-06-29 13:10:05 -07002973 */
Sean Christophersonad104b52020-03-20 14:28:11 -07002974 vpid_sync_vcpu_addr(to_vmx(vcpu)->vpid, addr);
Junaid Shahidfaff8752018-06-29 13:10:05 -07002975}
2976
Sean Christophersone64419d2020-03-20 14:28:10 -07002977static void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu)
2978{
2979 /*
2980 * vpid_sync_context() is a nop if vmx->vpid==0, e.g. if enable_vpid==0
2981 * or a vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit
2982 * are required to flush GVA->{G,H}PA mappings from the TLB if vpid is
2983 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed),
2984 * i.e. no explicit INVVPID is necessary.
2985 */
2986 vpid_sync_context(to_vmx(vcpu)->vpid);
2987}
2988
Peter Shier43fea4e2020-08-20 16:05:45 -07002989void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08002990{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002991 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2992
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002993 if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002994 return;
2995
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02002996 if (is_pae_paging(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002997 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
2998 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
2999 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3000 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003001 }
3002}
3003
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003004void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03003005{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003006 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3007
Sean Christopherson9932b492020-04-15 13:34:50 -07003008 if (WARN_ON_ONCE(!is_pae_paging(vcpu)))
3009 return;
3010
3011 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3012 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3013 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3014 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003015
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003016 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003017}
3018
Sheng Yang14394422008-04-28 12:24:45 +08003019static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3020 unsigned long cr0,
3021 struct kvm_vcpu *vcpu)
3022{
Sean Christopherson2183f562019-05-07 12:17:56 -07003023 struct vcpu_vmx *vmx = to_vmx(vcpu);
3024
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003025 if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
Sean Christopherson34059c22019-09-27 14:45:23 -07003026 vmx_cache_reg(vcpu, VCPU_EXREG_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003027 if (!(cr0 & X86_CR0_PG)) {
3028 /* From paging/starting to nonpaging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003029 exec_controls_setbit(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 } else if (!is_paging(vcpu)) {
3034 /* From nonpaging to paging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003035 exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3036 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08003037 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003038 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003039 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003040
3041 if (!(cr0 & X86_CR0_WP))
3042 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003043}
3044
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003045void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003047 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003048 unsigned long hw_cr0;
3049
Sean Christopherson3de63472018-07-13 08:42:30 -07003050 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003051 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003052 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003053 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003054 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003055
Gleb Natapov218e7632013-01-21 15:36:45 +02003056 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3057 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058
Gleb Natapov218e7632013-01-21 15:36:45 +02003059 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3060 enter_rmode(vcpu);
3061 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003063#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003064 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003065 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003067 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068 exit_lmode(vcpu);
3069 }
3070#endif
3071
Sean Christophersonb4d18512018-03-05 12:04:40 -08003072 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08003073 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3074
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003076 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003077 vcpu->arch.cr0 = cr0;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07003078 kvm_register_mark_available(vcpu, VCPU_EXREG_CR0);
Gleb Natapov14168782013-01-21 15:36:49 +02003079
3080 /* depends on vcpu->arch.cr0 to be set to a new value */
3081 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003082}
3083
Sean Christophersond468d942020-07-15 20:41:20 -07003084static int vmx_get_max_tdp_level(void)
Sean Christopherson0047fca2020-05-01 21:32:33 -07003085{
Sean Christophersond468d942020-07-15 20:41:20 -07003086 if (cpu_has_vmx_ept_5levels())
Sean Christopherson0047fca2020-05-01 21:32:33 -07003087 return 5;
3088 return 4;
3089}
3090
Sean Christopherson2a40b902020-07-15 20:41:18 -07003091u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,
3092 int root_level)
Sheng Yang14394422008-04-28 12:24:45 +08003093{
Yu Zhang855feb62017-08-24 20:27:55 +08003094 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08003095
Sean Christopherson2a40b902020-07-15 20:41:18 -07003096 eptp |= (root_level == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08003097
Peter Feiner995f00a2017-06-30 17:26:32 -07003098 if (enable_ept_ad_bits &&
3099 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02003100 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003101 eptp |= (root_hpa & PAGE_MASK);
3102
3103 return eptp;
3104}
3105
Sean Christopherson2a40b902020-07-15 20:41:18 -07003106static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,
3107 int pgd_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108{
Tianyu Lan877ad952018-07-19 08:40:23 +00003109 struct kvm *kvm = vcpu->kvm;
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003110 bool update_guest_cr3 = true;
Sheng Yang14394422008-04-28 12:24:45 +08003111 unsigned long guest_cr3;
3112 u64 eptp;
3113
Avi Kivity089d0342009-03-23 18:26:32 +02003114 if (enable_ept) {
Sean Christopherson2a40b902020-07-15 20:41:18 -07003115 eptp = construct_eptp(vcpu, pgd, pgd_level);
Sheng Yang14394422008-04-28 12:24:45 +08003116 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00003117
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003118 if (kvm_x86_ops.tlb_remote_flush) {
Tianyu Lan877ad952018-07-19 08:40:23 +00003119 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3120 to_vmx(vcpu)->ept_pointer = eptp;
3121 to_kvm_vmx(kvm)->ept_pointers_match
3122 = EPT_POINTERS_CHECK;
3123 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3124 }
3125
Paolo Bonzinidf7e0682020-05-20 08:37:37 -04003126 if (!enable_unrestricted_guest && !is_paging(vcpu))
Tianyu Lan877ad952018-07-19 08:40:23 +00003127 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Sean Christophersonb17b7432019-09-27 14:45:17 -07003128 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3129 guest_cr3 = vcpu->arch.cr3;
3130 else /* vmcs01.GUEST_CR3 is already up-to-date. */
3131 update_guest_cr3 = false;
Peter Shier43fea4e2020-08-20 16:05:45 -07003132 vmx_ept_load_pdptrs(vcpu);
Sean Christophersonbe100ef2020-03-20 14:28:33 -07003133 } else {
3134 guest_cr3 = pgd;
Sheng Yang14394422008-04-28 12:24:45 +08003135 }
3136
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003137 if (update_guest_cr3)
3138 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139}
3140
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003141int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003143 struct vcpu_vmx *vmx = to_vmx(vcpu);
Ben Serebrin085e68e2015-04-16 11:58:05 -07003144 /*
3145 * Pass through host's Machine Check Enable value to hw_cr4, which
3146 * is in force while we are in guest mode. Do not let guests control
3147 * this bit, even if host CR4.MCE == 0.
3148 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003149 unsigned long hw_cr4;
3150
3151 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
3152 if (enable_unrestricted_guest)
3153 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003154 else if (vmx->rmode.vm86_active)
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003155 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3156 else
3157 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003158
Sean Christopherson64f7a112018-04-30 10:01:06 -07003159 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3160 if (cr4 & X86_CR4_UMIP) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003161 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07003162 hw_cr4 &= ~X86_CR4_UMIP;
3163 } else if (!is_guest_mode(vcpu) ||
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003164 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
3165 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
3166 }
Sean Christopherson64f7a112018-04-30 10:01:06 -07003167 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02003168
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003169 if (cr4 & X86_CR4_VMXE) {
3170 /*
3171 * To use VMXON (and later other VMX instructions), a guest
3172 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3173 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003174 * is here. We operate under the default treatment of SMM,
3175 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003176 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003177 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003178 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003179 }
David Matlack38991522016-11-29 18:14:08 -08003180
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003181 if (vmx->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003182 return 1;
3183
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003184 vcpu->arch.cr4 = cr4;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07003185 kvm_register_mark_available(vcpu, VCPU_EXREG_CR4);
Sheng Yang14394422008-04-28 12:24:45 +08003186
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003187 if (!enable_unrestricted_guest) {
3188 if (enable_ept) {
3189 if (!is_paging(vcpu)) {
3190 hw_cr4 &= ~X86_CR4_PAE;
3191 hw_cr4 |= X86_CR4_PSE;
3192 } else if (!(cr4 & X86_CR4_PAE)) {
3193 hw_cr4 &= ~X86_CR4_PAE;
3194 }
3195 }
3196
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003197 /*
Huaitong Handdba2622016-03-22 16:51:15 +08003198 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3199 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3200 * to be manually disabled when guest switches to non-paging
3201 * mode.
3202 *
3203 * If !enable_unrestricted_guest, the CPU is always running
3204 * with CR0.PG=1 and CR4 needs to be modified.
3205 * If enable_unrestricted_guest, the CPU automatically
3206 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003207 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003208 if (!is_paging(vcpu))
3209 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3210 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003211
Sheng Yang14394422008-04-28 12:24:45 +08003212 vmcs_writel(CR4_READ_SHADOW, cr4);
3213 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003214 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215}
3216
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003217void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218{
Avi Kivitya9179492011-01-03 14:28:52 +02003219 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 u32 ar;
3221
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003222 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003223 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003224 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003225 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003226 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003227 var->base = vmx_read_guest_seg_base(vmx, seg);
3228 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3229 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003230 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003231 var->base = vmx_read_guest_seg_base(vmx, seg);
3232 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3233 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3234 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003235 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236 var->type = ar & 15;
3237 var->s = (ar >> 4) & 1;
3238 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003239 /*
3240 * Some userspaces do not preserve unusable property. Since usable
3241 * segment has to be present according to VMX spec we can use present
3242 * property to amend userspace bug by making unusable segment always
3243 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3244 * segment as unusable.
3245 */
3246 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247 var->avl = (ar >> 12) & 1;
3248 var->l = (ar >> 13) & 1;
3249 var->db = (ar >> 14) & 1;
3250 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251}
3252
Avi Kivitya9179492011-01-03 14:28:52 +02003253static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3254{
Avi Kivitya9179492011-01-03 14:28:52 +02003255 struct kvm_segment s;
3256
3257 if (to_vmx(vcpu)->rmode.vm86_active) {
3258 vmx_get_segment(vcpu, &s, seg);
3259 return s.base;
3260 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003261 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003262}
3263
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003264int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003265{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003266 struct vcpu_vmx *vmx = to_vmx(vcpu);
3267
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003268 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003269 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003270 else {
3271 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003272 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003273 }
Avi Kivity69c73022011-03-07 15:26:44 +02003274}
3275
Avi Kivity653e3102007-05-07 10:55:37 +03003276static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278 u32 ar;
3279
Avi Kivityf0495f92012-06-07 17:06:10 +03003280 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 ar = 1 << 16;
3282 else {
3283 ar = var->type & 15;
3284 ar |= (var->s & 1) << 4;
3285 ar |= (var->dpl & 3) << 5;
3286 ar |= (var->present & 1) << 7;
3287 ar |= (var->avl & 1) << 12;
3288 ar |= (var->l & 1) << 13;
3289 ar |= (var->db & 1) << 14;
3290 ar |= (var->g & 1) << 15;
3291 }
Avi Kivity653e3102007-05-07 10:55:37 +03003292
3293 return ar;
3294}
3295
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003296void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03003297{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003298 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003299 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003300
Avi Kivity2fb92db2011-04-27 19:42:18 +03003301 vmx_segment_cache_clear(vmx);
3302
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003303 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3304 vmx->rmode.segs[seg] = *var;
3305 if (seg == VCPU_SREG_TR)
3306 vmcs_write16(sf->selector, var->selector);
3307 else if (var->s)
3308 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003309 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003310 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003311
Avi Kivity653e3102007-05-07 10:55:37 +03003312 vmcs_writel(sf->base, var->base);
3313 vmcs_write32(sf->limit, var->limit);
3314 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003315
3316 /*
3317 * Fix the "Accessed" bit in AR field of segment registers for older
3318 * qemu binaries.
3319 * IA32 arch specifies that at the time of processor reset the
3320 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003321 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003322 * state vmexit when "unrestricted guest" mode is turned on.
3323 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3324 * tree. Newer qemu binaries with that qemu fix would not need this
3325 * kvm hack.
3326 */
3327 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003328 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003329
Gleb Natapovf924d662012-12-12 19:10:55 +02003330 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003331
3332out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003333 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334}
3335
Avi Kivity6aa8b732006-12-10 02:21:36 -08003336static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3337{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003338 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339
3340 *db = (ar >> 14) & 1;
3341 *l = (ar >> 13) & 1;
3342}
3343
Gleb Natapov89a27f42010-02-16 10:51:48 +02003344static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003346 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3347 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348}
3349
Gleb Natapov89a27f42010-02-16 10:51:48 +02003350static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003351{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003352 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3353 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354}
3355
Gleb Natapov89a27f42010-02-16 10:51:48 +02003356static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003357{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003358 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3359 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003360}
3361
Gleb Natapov89a27f42010-02-16 10:51:48 +02003362static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003364 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3365 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366}
3367
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003368static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3369{
3370 struct kvm_segment var;
3371 u32 ar;
3372
3373 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003374 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003375 if (seg == VCPU_SREG_CS)
3376 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003377 ar = vmx_segment_access_rights(&var);
3378
3379 if (var.base != (var.selector << 4))
3380 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003381 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003382 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003383 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003384 return false;
3385
3386 return true;
3387}
3388
3389static bool code_segment_valid(struct kvm_vcpu *vcpu)
3390{
3391 struct kvm_segment cs;
3392 unsigned int cs_rpl;
3393
3394 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003395 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003396
Avi Kivity1872a3f2009-01-04 23:26:52 +02003397 if (cs.unusable)
3398 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003399 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003400 return false;
3401 if (!cs.s)
3402 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003403 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003404 if (cs.dpl > cs_rpl)
3405 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003406 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003407 if (cs.dpl != cs_rpl)
3408 return false;
3409 }
3410 if (!cs.present)
3411 return false;
3412
3413 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3414 return true;
3415}
3416
3417static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3418{
3419 struct kvm_segment ss;
3420 unsigned int ss_rpl;
3421
3422 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003423 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003424
Avi Kivity1872a3f2009-01-04 23:26:52 +02003425 if (ss.unusable)
3426 return true;
3427 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003428 return false;
3429 if (!ss.s)
3430 return false;
3431 if (ss.dpl != ss_rpl) /* DPL != RPL */
3432 return false;
3433 if (!ss.present)
3434 return false;
3435
3436 return true;
3437}
3438
3439static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3440{
3441 struct kvm_segment var;
3442 unsigned int rpl;
3443
3444 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03003445 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003446
Avi Kivity1872a3f2009-01-04 23:26:52 +02003447 if (var.unusable)
3448 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003449 if (!var.s)
3450 return false;
3451 if (!var.present)
3452 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003453 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003454 if (var.dpl < rpl) /* DPL < RPL */
3455 return false;
3456 }
3457
3458 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3459 * rights flags
3460 */
3461 return true;
3462}
3463
3464static bool tr_valid(struct kvm_vcpu *vcpu)
3465{
3466 struct kvm_segment tr;
3467
3468 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3469
Avi Kivity1872a3f2009-01-04 23:26:52 +02003470 if (tr.unusable)
3471 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03003472 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003473 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003474 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003475 return false;
3476 if (!tr.present)
3477 return false;
3478
3479 return true;
3480}
3481
3482static bool ldtr_valid(struct kvm_vcpu *vcpu)
3483{
3484 struct kvm_segment ldtr;
3485
3486 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3487
Avi Kivity1872a3f2009-01-04 23:26:52 +02003488 if (ldtr.unusable)
3489 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03003490 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003491 return false;
3492 if (ldtr.type != 2)
3493 return false;
3494 if (!ldtr.present)
3495 return false;
3496
3497 return true;
3498}
3499
3500static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3501{
3502 struct kvm_segment cs, ss;
3503
3504 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3505 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3506
Nadav Amitb32a9912015-03-29 16:33:04 +03003507 return ((cs.selector & SEGMENT_RPL_MASK) ==
3508 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003509}
3510
3511/*
3512 * Check if guest state is valid. Returns true if valid, false if
3513 * not.
3514 * We assume that registers are always usable
3515 */
3516static bool guest_state_valid(struct kvm_vcpu *vcpu)
3517{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003518 if (enable_unrestricted_guest)
3519 return true;
3520
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003521 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003522 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003523 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3524 return false;
3525 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3526 return false;
3527 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3528 return false;
3529 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3530 return false;
3531 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3532 return false;
3533 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3534 return false;
3535 } else {
3536 /* protected mode guest state checks */
3537 if (!cs_ss_rpl_check(vcpu))
3538 return false;
3539 if (!code_segment_valid(vcpu))
3540 return false;
3541 if (!stack_segment_valid(vcpu))
3542 return false;
3543 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3544 return false;
3545 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3546 return false;
3547 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3548 return false;
3549 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3550 return false;
3551 if (!tr_valid(vcpu))
3552 return false;
3553 if (!ldtr_valid(vcpu))
3554 return false;
3555 }
3556 /* TODO:
3557 * - Add checks on RIP
3558 * - Add checks on RFLAGS
3559 */
3560
3561 return true;
3562}
3563
Mike Dayd77c26f2007-10-08 09:02:08 -04003564static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003565{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003566 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003567 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003568 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003569
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003570 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003571 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003572 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3573 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003574 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003575 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003576 r = kvm_write_guest_page(kvm, fn++, &data,
3577 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003578 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003579 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003580 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3581 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003582 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003583 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3584 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003585 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003586 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003587 r = kvm_write_guest_page(kvm, fn, &data,
3588 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3589 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003590out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003591 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003592 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003593}
3594
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003595static int init_rmode_identity_map(struct kvm *kvm)
3596{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003597 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Peter Xu2a5755b2020-01-09 09:57:14 -05003598 int i, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08003599 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003600 u32 tmp;
3601
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003602 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003603 mutex_lock(&kvm->slots_lock);
3604
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003605 if (likely(kvm_vmx->ept_identity_pagetable_done))
Peter Xu2a5755b2020-01-09 09:57:14 -05003606 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003607
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003608 if (!kvm_vmx->ept_identity_map_addr)
3609 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
3610 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08003611
David Hildenbrandd8a6e362017-08-24 20:51:34 +02003612 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003613 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08003614 if (r < 0)
Peter Xu2a5755b2020-01-09 09:57:14 -05003615 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003616
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003617 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3618 if (r < 0)
3619 goto out;
3620 /* Set up identity-mapping pagetable for EPT in real mode */
3621 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3622 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3623 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3624 r = kvm_write_guest_page(kvm, identity_map_pfn,
3625 &tmp, i * sizeof(tmp), sizeof(tmp));
3626 if (r < 0)
3627 goto out;
3628 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003629 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003630
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003631out:
Tang Chena255d472014-09-16 18:41:58 +08003632 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003633 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003634}
3635
Avi Kivity6aa8b732006-12-10 02:21:36 -08003636static void seg_setup(int seg)
3637{
Mathias Krause772e0312012-08-30 01:30:19 +02003638 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003639 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003640
3641 vmcs_write16(sf->selector, 0);
3642 vmcs_writel(sf->base, 0);
3643 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003644 ar = 0x93;
3645 if (seg == VCPU_SREG_CS)
3646 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003647
3648 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003649}
3650
Sheng Yangf78e0e22007-10-29 09:40:42 +08003651static int alloc_apic_access_page(struct kvm *kvm)
3652{
Xiao Guangrong44841412012-09-07 14:14:20 +08003653 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003654 int r = 0;
3655
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003656 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003657 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003658 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02003659 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3660 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003661 if (r)
3662 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003663
Tang Chen73a6d942014-09-11 13:38:00 +08003664 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003665 if (is_error_page(page)) {
3666 r = -EFAULT;
3667 goto out;
3668 }
3669
Tang Chenc24ae0d2014-09-24 15:57:58 +08003670 /*
3671 * Do not pin the page in memory, so that memory hot-unplug
3672 * is able to migrate it.
3673 */
3674 put_page(page);
3675 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003676out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003677 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003678 return r;
3679}
3680
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003681int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003682{
3683 int vpid;
3684
Avi Kivity919818a2009-03-23 18:01:29 +02003685 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003686 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003687 spin_lock(&vmx_vpid_lock);
3688 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003689 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003690 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003691 else
3692 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003693 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003694 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003695}
3696
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003697void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003698{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003699 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003700 return;
3701 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003702 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003703 spin_unlock(&vmx_vpid_lock);
3704}
3705
Yi Wang1e4329ee2018-11-08 11:22:21 +08003706static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003707 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003708{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003709 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003710
3711 if (!cpu_has_vmx_msr_bitmap())
3712 return;
3713
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003714 if (static_branch_unlikely(&enable_evmcs))
3715 evmcs_touch_msr_bitmap();
3716
Sheng Yang25c5f222008-03-28 13:18:56 +08003717 /*
3718 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3719 * have the write-low and read-high bitmap offsets the wrong way round.
3720 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3721 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003722 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003723 if (type & MSR_TYPE_R)
3724 /* read-low */
3725 __clear_bit(msr, msr_bitmap + 0x000 / f);
3726
3727 if (type & MSR_TYPE_W)
3728 /* write-low */
3729 __clear_bit(msr, msr_bitmap + 0x800 / f);
3730
Sheng Yang25c5f222008-03-28 13:18:56 +08003731 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3732 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003733 if (type & MSR_TYPE_R)
3734 /* read-high */
3735 __clear_bit(msr, msr_bitmap + 0x400 / f);
3736
3737 if (type & MSR_TYPE_W)
3738 /* write-high */
3739 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3740
3741 }
3742}
3743
Yi Wang1e4329ee2018-11-08 11:22:21 +08003744static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003745 u32 msr, int type)
3746{
3747 int f = sizeof(unsigned long);
3748
3749 if (!cpu_has_vmx_msr_bitmap())
3750 return;
3751
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003752 if (static_branch_unlikely(&enable_evmcs))
3753 evmcs_touch_msr_bitmap();
3754
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003755 /*
3756 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3757 * have the write-low and read-high bitmap offsets the wrong way round.
3758 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3759 */
3760 if (msr <= 0x1fff) {
3761 if (type & MSR_TYPE_R)
3762 /* read-low */
3763 __set_bit(msr, msr_bitmap + 0x000 / f);
3764
3765 if (type & MSR_TYPE_W)
3766 /* write-low */
3767 __set_bit(msr, msr_bitmap + 0x800 / f);
3768
3769 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3770 msr &= 0x1fff;
3771 if (type & MSR_TYPE_R)
3772 /* read-high */
3773 __set_bit(msr, msr_bitmap + 0x400 / f);
3774
3775 if (type & MSR_TYPE_W)
3776 /* write-high */
3777 __set_bit(msr, msr_bitmap + 0xc00 / f);
3778
3779 }
3780}
3781
Yi Wang1e4329ee2018-11-08 11:22:21 +08003782static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003783 u32 msr, int type, bool value)
3784{
3785 if (value)
3786 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
3787 else
3788 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
3789}
3790
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003791static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003792{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003793 u8 mode = 0;
3794
3795 if (cpu_has_secondary_exec_ctrls() &&
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003796 (secondary_exec_controls_get(to_vmx(vcpu)) &
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003797 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3798 mode |= MSR_BITMAP_MODE_X2APIC;
3799 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3800 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3801 }
3802
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003803 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003804}
3805
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003806static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
3807 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003808{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003809 int msr;
3810
3811 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3812 unsigned word = msr / BITS_PER_LONG;
3813 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
3814 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003815 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003816
3817 if (mode & MSR_BITMAP_MODE_X2APIC) {
3818 /*
3819 * TPR reads and writes can be virtualized even if virtual interrupt
3820 * delivery is not in use.
3821 */
3822 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
3823 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
3824 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
3825 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3826 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
3827 }
3828 }
3829}
3830
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003831void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003832{
3833 struct vcpu_vmx *vmx = to_vmx(vcpu);
3834 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3835 u8 mode = vmx_msr_bitmap_mode(vcpu);
3836 u8 changed = mode ^ vmx->msr_bitmap_mode;
3837
3838 if (!changed)
3839 return;
3840
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003841 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
3842 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
3843
3844 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003845}
3846
Chao Pengb08c2892018-10-24 16:05:15 +08003847void pt_update_intercept_for_msr(struct vcpu_vmx *vmx)
3848{
3849 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3850 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
3851 u32 i;
3852
3853 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_STATUS,
3854 MSR_TYPE_RW, flag);
3855 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_BASE,
3856 MSR_TYPE_RW, flag);
3857 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_MASK,
3858 MSR_TYPE_RW, flag);
3859 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_CR3_MATCH,
3860 MSR_TYPE_RW, flag);
3861 for (i = 0; i < vmx->pt_desc.addr_range; i++) {
3862 vmx_set_intercept_for_msr(msr_bitmap,
3863 MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
3864 vmx_set_intercept_for_msr(msr_bitmap,
3865 MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
3866 }
3867}
3868
Liran Alone6c67d82018-09-04 10:56:52 +03003869static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3870{
3871 struct vcpu_vmx *vmx = to_vmx(vcpu);
3872 void *vapic_page;
3873 u32 vppr;
3874 int rvi;
3875
3876 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3877 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003878 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
Liran Alone6c67d82018-09-04 10:56:52 +03003879 return false;
3880
Paolo Bonzini7e712682018-10-03 13:44:26 +02003881 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003882
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003883 vapic_page = vmx->nested.virtual_apic_map.hva;
Liran Alone6c67d82018-09-04 10:56:52 +03003884 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
Liran Alone6c67d82018-09-04 10:56:52 +03003885
3886 return ((rvi & 0xf0) > (vppr & 0xf0));
3887}
3888
Wincy Van06a55242017-04-28 13:13:59 +08003889static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
3890 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003891{
3892#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08003893 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
3894
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003895 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08003896 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003897 * The vector of interrupt to be delivered to vcpu had
3898 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08003899 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003900 * Following cases will be reached in this block, and
3901 * we always send a notification event in all cases as
3902 * explained below.
3903 *
3904 * Case 1: vcpu keeps in non-root mode. Sending a
3905 * notification event posts the interrupt to vcpu.
3906 *
3907 * Case 2: vcpu exits to root mode and is still
3908 * runnable. PIR will be synced to vIRR before the
3909 * next vcpu entry. Sending a notification event in
3910 * this case has no effect, as vcpu is not in root
3911 * mode.
3912 *
3913 * Case 3: vcpu exits to root mode and is blocked.
3914 * vcpu_block() has already synced PIR to vIRR and
3915 * never blocks vcpu if vIRR is not cleared. Therefore,
3916 * a blocked vcpu here does not wait for any requested
3917 * interrupts in PIR, and sending a notification event
3918 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08003919 */
Feng Wu28b835d2015-09-18 22:29:54 +08003920
Wincy Van06a55242017-04-28 13:13:59 +08003921 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003922 return true;
3923 }
3924#endif
3925 return false;
3926}
3927
Wincy Van705699a2015-02-03 23:58:17 +08003928static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
3929 int vector)
3930{
3931 struct vcpu_vmx *vmx = to_vmx(vcpu);
3932
3933 if (is_guest_mode(vcpu) &&
3934 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08003935 /*
3936 * If a posted intr is not recognized by hardware,
3937 * we will accomplish it in the next vmentry.
3938 */
3939 vmx->nested.pi_pending = true;
3940 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02003941 /* the PIR and ON have been set by L1. */
3942 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
3943 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08003944 return 0;
3945 }
3946 return -1;
3947}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003948/*
Yang Zhanga20ed542013-04-11 19:25:15 +08003949 * Send interrupt to vcpu via posted interrupt way.
3950 * 1. If target vcpu is running(non-root mode), send posted interrupt
3951 * notification to vcpu and hardware will sync PIR to vIRR atomically.
3952 * 2. If target vcpu isn't running(root mode), kick it to pick up the
3953 * interrupt from PIR in next vmentry.
3954 */
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003955static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
Yang Zhanga20ed542013-04-11 19:25:15 +08003956{
3957 struct vcpu_vmx *vmx = to_vmx(vcpu);
3958 int r;
3959
Wincy Van705699a2015-02-03 23:58:17 +08003960 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
3961 if (!r)
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003962 return 0;
3963
3964 if (!vcpu->arch.apicv_active)
3965 return -1;
Wincy Van705699a2015-02-03 23:58:17 +08003966
Yang Zhanga20ed542013-04-11 19:25:15 +08003967 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003968 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003969
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003970 /* If a previous notification has sent the IPI, nothing to do. */
3971 if (pi_test_and_set_on(&vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003972 return 0;
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003973
Wanpeng Li379a3c82020-04-28 14:23:27 +08003974 if (vcpu != kvm_get_running_vcpu() &&
3975 !kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08003976 kvm_vcpu_kick(vcpu);
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003977
3978 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003979}
3980
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003982 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3983 * will not change in the lifetime of the guest.
3984 * Note that host-state that does change is set elsewhere. E.g., host-state
3985 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3986 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003987void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003988{
3989 u32 low32, high32;
3990 unsigned long tmpl;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003991 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003992
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07003993 cr0 = read_cr0();
3994 WARN_ON(cr0 & X86_CR0_TS);
3995 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003996
3997 /*
3998 * Save the most likely value for this task's CR3 in the VMCS.
3999 * We can't use __get_current_cr3_fast() because we're not atomic.
4000 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07004001 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004002 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07004003 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004004
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004005 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004006 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004007 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07004008 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004009
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004010 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004011#ifdef CONFIG_X86_64
4012 /*
4013 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07004014 * vmx_prepare_switch_to_host(), in case userspace uses
4015 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03004016 */
4017 vmcs_write16(HOST_DS_SELECTOR, 0);
4018 vmcs_write16(HOST_ES_SELECTOR, 0);
4019#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004020 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4021 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004022#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004023 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4024 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4025
Sean Christopherson23420802019-04-19 22:50:57 -07004026 vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004027
Sean Christopherson453eafb2018-12-20 12:25:17 -08004028 vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004029
4030 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4031 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4032 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4033 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4034
4035 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4036 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4037 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4038 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004039
Sean Christophersonc73da3f2018-12-03 13:53:00 -08004040 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004041 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004042}
4043
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004044void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004045{
Sean Christophersonfa71e952020-07-02 21:04:22 -07004046 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS;
4047 if (!enable_ept)
4048 vmx->vcpu.arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004049 if (is_guest_mode(&vmx->vcpu))
4050 vmx->vcpu.arch.cr4_guest_owned_bits &=
4051 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004052 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4053}
4054
Sean Christophersonc075c3e2019-05-07 12:17:53 -07004055u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
Yang Zhang01e439b2013-04-11 19:25:12 +08004056{
4057 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4058
Andrey Smetanind62caab2015-11-10 15:36:33 +03004059 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004060 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004061
4062 if (!enable_vnmi)
4063 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
4064
Sean Christopherson804939e2019-05-07 12:18:05 -07004065 if (!enable_preemption_timer)
4066 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4067
Yang Zhang01e439b2013-04-11 19:25:12 +08004068 return pin_based_exec_ctrl;
4069}
4070
Andrey Smetanind62caab2015-11-10 15:36:33 +03004071static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4072{
4073 struct vcpu_vmx *vmx = to_vmx(vcpu);
4074
Sean Christophersonc5f2c762019-05-07 12:17:55 -07004075 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004076 if (cpu_has_secondary_exec_ctrls()) {
4077 if (kvm_vcpu_apicv_active(vcpu))
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004078 secondary_exec_controls_setbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004079 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4080 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4081 else
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004082 secondary_exec_controls_clearbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004083 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4084 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4085 }
4086
4087 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004088 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004089}
4090
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004091u32 vmx_exec_control(struct vcpu_vmx *vmx)
4092{
4093 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4094
4095 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4096 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4097
4098 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4099 exec_control &= ~CPU_BASED_TPR_SHADOW;
4100#ifdef CONFIG_X86_64
4101 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4102 CPU_BASED_CR8_LOAD_EXITING;
4103#endif
4104 }
4105 if (!enable_ept)
4106 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4107 CPU_BASED_CR3_LOAD_EXITING |
4108 CPU_BASED_INVLPG_EXITING;
4109 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4110 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4111 CPU_BASED_MONITOR_EXITING);
4112 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4113 exec_control &= ~CPU_BASED_HLT_EXITING;
4114 return exec_control;
4115}
4116
4117
Paolo Bonzini80154d72017-08-24 13:55:35 +02004118static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004119{
Paolo Bonzini80154d72017-08-24 13:55:35 +02004120 struct kvm_vcpu *vcpu = &vmx->vcpu;
4121
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004122 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004123
Sean Christopherson2ef76192020-03-02 15:56:22 -08004124 if (vmx_pt_mode_is_system())
Chao Pengf99e3da2018-10-24 16:05:10 +08004125 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004126 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004127 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4128 if (vmx->vpid == 0)
4129 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4130 if (!enable_ept) {
4131 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4132 enable_unrestricted_guest = 0;
4133 }
4134 if (!enable_unrestricted_guest)
4135 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004136 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004137 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02004138 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004139 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4140 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004141 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004142
4143 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4144 * in vmx_set_cr4. */
4145 exec_control &= ~SECONDARY_EXEC_DESC;
4146
Abel Gordonabc4fc52013-04-18 14:35:25 +03004147 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4148 (handle_vmptrld).
4149 We can NOT enable shadow_vmcs here because we don't have yet
4150 a current VMCS12
4151 */
4152 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004153
4154 if (!enable_pml)
4155 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004156
Paolo Bonzini3db13482017-08-24 14:48:03 +02004157 if (vmx_xsaves_supported()) {
4158 /* Exposing XSAVES only when XSAVE is exposed */
4159 bool xsaves_enabled =
Sean Christopherson96be4e02019-12-10 14:44:15 -08004160 boot_cpu_has(X86_FEATURE_XSAVE) &&
Paolo Bonzini3db13482017-08-24 14:48:03 +02004161 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4162 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4163
Aaron Lewis72041602019-10-21 16:30:20 -07004164 vcpu->arch.xsaves_enabled = xsaves_enabled;
4165
Paolo Bonzini3db13482017-08-24 14:48:03 +02004166 if (!xsaves_enabled)
4167 exec_control &= ~SECONDARY_EXEC_XSAVES;
4168
4169 if (nested) {
4170 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004171 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02004172 SECONDARY_EXEC_XSAVES;
4173 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004174 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02004175 ~SECONDARY_EXEC_XSAVES;
4176 }
4177 }
4178
Sean Christophersona7a200e2020-03-02 15:56:58 -08004179 if (cpu_has_vmx_rdtscp()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004180 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
4181 if (!rdtscp_enabled)
4182 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4183
4184 if (nested) {
4185 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004186 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004187 SECONDARY_EXEC_RDTSCP;
4188 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004189 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004190 ~SECONDARY_EXEC_RDTSCP;
4191 }
4192 }
4193
Sean Christopherson5ffec6f2020-03-02 15:56:34 -08004194 if (cpu_has_vmx_invpcid()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004195 /* Exposing INVPCID only when PCID is exposed */
4196 bool invpcid_enabled =
4197 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
4198 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
4199
4200 if (!invpcid_enabled) {
4201 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
4202 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
4203 }
4204
4205 if (nested) {
4206 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004207 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004208 SECONDARY_EXEC_ENABLE_INVPCID;
4209 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004210 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004211 ~SECONDARY_EXEC_ENABLE_INVPCID;
4212 }
4213 }
4214
Jim Mattson45ec3682017-08-23 16:32:04 -07004215 if (vmx_rdrand_supported()) {
4216 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
4217 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02004218 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004219
4220 if (nested) {
4221 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004222 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004223 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004224 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004225 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004226 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004227 }
4228 }
4229
Jim Mattson75f4fc82017-08-23 16:32:03 -07004230 if (vmx_rdseed_supported()) {
4231 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
4232 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02004233 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004234
4235 if (nested) {
4236 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004237 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004238 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004239 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004240 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004241 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004242 }
4243 }
4244
Tao Xue69e72fa2019-07-16 14:55:49 +08004245 if (vmx_waitpkg_supported()) {
4246 bool waitpkg_enabled =
4247 guest_cpuid_has(vcpu, X86_FEATURE_WAITPKG);
4248
4249 if (!waitpkg_enabled)
4250 exec_control &= ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4251
4252 if (nested) {
4253 if (waitpkg_enabled)
4254 vmx->nested.msrs.secondary_ctls_high |=
4255 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4256 else
4257 vmx->nested.msrs.secondary_ctls_high &=
4258 ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4259 }
4260 }
4261
Paolo Bonzini80154d72017-08-24 13:55:35 +02004262 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004263}
4264
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004265static void ept_set_mmio_spte_mask(void)
4266{
4267 /*
4268 * EPT Misconfigurations can be generated if the value of bits 2:0
4269 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004270 */
Paolo Bonzinie7581ca2020-05-19 05:04:49 -04004271 kvm_mmu_set_mmio_spte_mask(VMX_EPT_MISCONFIG_WX_VALUE, 0);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004272}
4273
Wanpeng Lif53cd632014-12-02 19:14:58 +08004274#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08004275
Sean Christopherson944c3462018-12-03 13:53:09 -08004276/*
Xiaoyao Li1b842922019-10-20 17:11:01 +08004277 * Noting that the initialization of Guest-state Area of VMCS is in
4278 * vmx_vcpu_reset().
Sean Christopherson944c3462018-12-03 13:53:09 -08004279 */
Xiaoyao Li1b842922019-10-20 17:11:01 +08004280static void init_vmcs(struct vcpu_vmx *vmx)
Sean Christopherson944c3462018-12-03 13:53:09 -08004281{
Sean Christopherson944c3462018-12-03 13:53:09 -08004282 if (nested)
Xiaoyao Li1b842922019-10-20 17:11:01 +08004283 nested_vmx_set_vmcs_shadowing_bitmap();
Sean Christopherson944c3462018-12-03 13:53:09 -08004284
Sheng Yang25c5f222008-03-28 13:18:56 +08004285 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004286 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08004287
Avi Kivity6aa8b732006-12-10 02:21:36 -08004288 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4289
Avi Kivity6aa8b732006-12-10 02:21:36 -08004290 /* Control */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004291 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004292
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004293 exec_controls_set(vmx, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004294
Dan Williamsdfa169b2016-06-02 11:17:24 -07004295 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004296 vmx_compute_secondary_exec_control(vmx);
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004297 secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07004298 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004299
Andrey Smetanind62caab2015-11-10 15:36:33 +03004300 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004301 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4302 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4303 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4304 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4305
4306 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004307
Li RongQing0bcf2612015-12-03 13:29:34 +08004308 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004309 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004310 }
4311
Wanpeng Lib31c1142018-03-12 04:53:04 -07004312 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004313 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004314 vmx->ple_window = ple_window;
4315 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004316 }
4317
Xiao Guangrongc3707952011-07-12 03:28:04 +08004318 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4319 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004320 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4321
Avi Kivity9581d442010-10-19 16:46:55 +02004322 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4323 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004324 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004325 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4326 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004327
Bandan Das2a499e42017-08-03 15:54:41 -04004328 if (cpu_has_vmx_vmfunc())
4329 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4330
Eddie Dong2cc51562007-05-21 07:28:09 +03004331 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4332 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004333 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03004334 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004335 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336
Radim Krčmář74545702015-04-27 15:11:25 +02004337 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4338 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004339
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004340 vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341
4342 /* 22.2.1, 20.8.1 */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004343 vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004344
Sean Christophersonfa71e952020-07-02 21:04:22 -07004345 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4346 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004347
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004348 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004349
Xiaoyao Li35fbe0d2019-10-20 17:10:58 +08004350 if (vmx->vpid != 0)
4351 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4352
Wanpeng Lif53cd632014-12-02 19:14:58 +08004353 if (vmx_xsaves_supported())
4354 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4355
Peter Feiner4e595162016-07-07 14:49:58 -07004356 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07004357 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4358 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4359 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07004360
4361 if (cpu_has_vmx_encls_vmexit())
4362 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Chao Peng2ef444f2018-10-24 16:05:12 +08004363
Sean Christopherson2ef76192020-03-02 15:56:22 -08004364 if (vmx_pt_mode_is_host_guest()) {
Chao Peng2ef444f2018-10-24 16:05:12 +08004365 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
4366 /* Bit[6~0] are forced to 1, writes are ignored. */
4367 vmx->pt_desc.guest.output_mask = 0x7F;
4368 vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
4369 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004370}
4371
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004372static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004373{
4374 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004375 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004376 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004377
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004378 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004379 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004380
Tao Xu6e3ba4a2019-07-16 14:55:50 +08004381 vmx->msr_ia32_umwait_control = 0;
4382
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004383 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Wanpeng Li95c06542019-09-05 14:26:28 +08004384 vmx->hv_deadline_tsc = -1;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004385 kvm_set_cr8(vcpu, 0);
4386
4387 if (!init_event) {
4388 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4389 MSR_IA32_APICBASE_ENABLE;
4390 if (kvm_vcpu_is_reset_bsp(vcpu))
4391 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4392 apic_base_msr.host_initiated = true;
4393 kvm_set_apic_base(vcpu, &apic_base_msr);
4394 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004395
Avi Kivity2fb92db2011-04-27 19:42:18 +03004396 vmx_segment_cache_clear(vmx);
4397
Avi Kivity5706be02008-08-20 15:07:31 +03004398 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004399 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004400 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004401
4402 seg_setup(VCPU_SREG_DS);
4403 seg_setup(VCPU_SREG_ES);
4404 seg_setup(VCPU_SREG_FS);
4405 seg_setup(VCPU_SREG_GS);
4406 seg_setup(VCPU_SREG_SS);
4407
4408 vmcs_write16(GUEST_TR_SELECTOR, 0);
4409 vmcs_writel(GUEST_TR_BASE, 0);
4410 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4411 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4412
4413 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4414 vmcs_writel(GUEST_LDTR_BASE, 0);
4415 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4416 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4417
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004418 if (!init_event) {
4419 vmcs_write32(GUEST_SYSENTER_CS, 0);
4420 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4421 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4422 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4423 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004424
Wanpeng Lic37c2872017-11-20 14:52:21 -08004425 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01004426 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004427
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004428 vmcs_writel(GUEST_GDTR_BASE, 0);
4429 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4430
4431 vmcs_writel(GUEST_IDTR_BASE, 0);
4432 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4433
Anthony Liguori443381a2010-12-06 10:53:38 -06004434 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004435 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004436 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07004437 if (kvm_mpx_supported())
4438 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004439
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004440 setup_msrs(vmx);
4441
Avi Kivity6aa8b732006-12-10 02:21:36 -08004442 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4443
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004444 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004445 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004446 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004447 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004448 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004449 vmcs_write32(TPR_THRESHOLD, 0);
4450 }
4451
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004452 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004453
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004454 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004455 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06004456 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004457 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004458 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004459
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004460 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004461
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004462 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004463 if (init_event)
4464 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465}
4466
Jan Kiszkac9a79532014-03-07 20:03:15 +01004467static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004468{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08004469 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004470}
4471
Jan Kiszkac9a79532014-03-07 20:03:15 +01004472static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004473{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004474 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004475 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01004476 enable_irq_window(vcpu);
4477 return;
4478 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004479
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08004480 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004481}
4482
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004483static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004484{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004485 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004486 uint32_t intr;
4487 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004488
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004489 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004490
Avi Kivityfa89a812008-09-01 15:57:51 +03004491 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004492 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004493 int inc_eip = 0;
4494 if (vcpu->arch.interrupt.soft)
4495 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004496 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
Eddie Dong85f455f2007-07-06 12:20:49 +03004497 return;
4498 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004499 intr = irq | INTR_INFO_VALID_MASK;
4500 if (vcpu->arch.interrupt.soft) {
4501 intr |= INTR_TYPE_SOFT_INTR;
4502 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4503 vmx->vcpu.arch.event_exit_inst_len);
4504 } else
4505 intr |= INTR_TYPE_EXT_INTR;
4506 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004507
4508 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004509}
4510
Sheng Yangf08864b2008-05-15 18:23:25 +08004511static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4512{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004513 struct vcpu_vmx *vmx = to_vmx(vcpu);
4514
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004515 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004516 /*
4517 * Tracking the NMI-blocked state in software is built upon
4518 * finding the next open IRQ window. This, in turn, depends on
4519 * well-behaving guests: They have to keep IRQs disabled at
4520 * least as long as the NMI handler runs. Otherwise we may
4521 * cause NMI nesting, maybe breaking the guest. But as this is
4522 * highly unlikely, we can live with the residual risk.
4523 */
4524 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
4525 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4526 }
4527
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004528 ++vcpu->stat.nmi_injections;
4529 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004530
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004531 if (vmx->rmode.vm86_active) {
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004532 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004533 return;
4534 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08004535
Sheng Yangf08864b2008-05-15 18:23:25 +08004536 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4537 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004538
4539 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004540}
4541
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004542bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004543{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004544 struct vcpu_vmx *vmx = to_vmx(vcpu);
4545 bool masked;
4546
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004547 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004548 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004549 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02004550 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004551 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4552 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4553 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004554}
4555
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004556void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004557{
4558 struct vcpu_vmx *vmx = to_vmx(vcpu);
4559
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004560 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004561 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4562 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4563 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4564 }
4565 } else {
4566 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4567 if (masked)
4568 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4569 GUEST_INTR_STATE_NMI);
4570 else
4571 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4572 GUEST_INTR_STATE_NMI);
4573 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004574}
4575
Sean Christopherson1b660b62020-04-22 19:25:44 -07004576bool vmx_nmi_blocked(struct kvm_vcpu *vcpu)
4577{
4578 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
4579 return false;
4580
4581 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4582 return true;
4583
4584 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4585 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
4586 GUEST_INTR_STATE_NMI));
4587}
4588
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004589static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Jan Kiszka2505dc92013-04-14 12:12:47 +02004590{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004591 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004592 return -EBUSY;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004593
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004594 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
4595 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004596 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004597
Sean Christopherson1b660b62020-04-22 19:25:44 -07004598 return !vmx_nmi_blocked(vcpu);
4599}
Sean Christopherson429ab572020-04-22 19:25:42 -07004600
Sean Christopherson1b660b62020-04-22 19:25:44 -07004601bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)
4602{
4603 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Sean Christopherson88c604b2020-04-22 19:25:41 -07004604 return false;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004605
Sean Christopherson7ab0abd2020-04-22 19:25:50 -07004606 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) ||
Sean Christopherson1b660b62020-04-22 19:25:44 -07004607 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4608 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Jan Kiszka2505dc92013-04-14 12:12:47 +02004609}
4610
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004611static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Gleb Natapov78646122009-03-23 12:12:11 +02004612{
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004613 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004614 return -EBUSY;
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004615
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004616 /*
4617 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
4618 * e.g. if the IRQ arrived asynchronously after checking nested events.
4619 */
4620 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004621 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004622
Sean Christopherson1b660b62020-04-22 19:25:44 -07004623 return !vmx_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02004624}
4625
Izik Eiduscbc94022007-10-25 00:29:55 +02004626static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4627{
4628 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004629
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004630 if (enable_unrestricted_guest)
4631 return 0;
4632
Peter Xu6a3c6232020-01-09 09:57:16 -05004633 mutex_lock(&kvm->slots_lock);
4634 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4635 PAGE_SIZE * 3);
4636 mutex_unlock(&kvm->slots_lock);
4637
Izik Eiduscbc94022007-10-25 00:29:55 +02004638 if (ret)
4639 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004640 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004641 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004642}
4643
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004644static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4645{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004646 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004647 return 0;
4648}
4649
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004650static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004651{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004652 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004653 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004654 /*
4655 * Update instruction length as we may reinject the exception
4656 * from user space while in guest debugging mode.
4657 */
4658 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4659 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004660 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004661 return false;
4662 /* fall through */
4663 case DB_VECTOR:
Miaohe Lina8cfbae2020-02-19 10:45:48 +08004664 return !(vcpu->guest_debug &
4665 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP));
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004666 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004667 case OF_VECTOR:
4668 case BR_VECTOR:
4669 case UD_VECTOR:
4670 case DF_VECTOR:
4671 case SS_VECTOR:
4672 case GP_VECTOR:
4673 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004674 return true;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004675 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004676 return false;
4677}
4678
4679static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4680 int vec, u32 err_code)
4681{
4682 /*
4683 * Instruction with address size override prefix opcode 0x67
4684 * Cause the #SS fault with 0 error code in VM86 mode.
4685 */
4686 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004687 if (kvm_emulate_instruction(vcpu, 0)) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004688 if (vcpu->arch.halt_request) {
4689 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004690 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004691 }
4692 return 1;
4693 }
4694 return 0;
4695 }
4696
4697 /*
4698 * Forward all other exceptions that are valid in real mode.
4699 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4700 * the required debugging infrastructure rework.
4701 */
4702 kvm_queue_exception(vcpu, vec);
4703 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704}
4705
Andi Kleena0861c02009-06-08 17:37:09 +08004706/*
4707 * Trigger machine check on the host. We assume all the MSRs are already set up
4708 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4709 * We pass a fake environment to the machine check handler because we want
4710 * the guest to be always treated like user space, no matter what context
4711 * it used internally.
4712 */
4713static void kvm_machine_check(void)
4714{
Uros Bizjakfb56baa2020-04-14 09:14:14 +02004715#if defined(CONFIG_X86_MCE)
Andi Kleena0861c02009-06-08 17:37:09 +08004716 struct pt_regs regs = {
4717 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4718 .flags = X86_EFLAGS_IF,
4719 };
4720
Thomas Gleixner8cd501c2020-02-25 23:33:23 +01004721 do_machine_check(&regs);
Andi Kleena0861c02009-06-08 17:37:09 +08004722#endif
4723}
4724
Avi Kivity851ba692009-08-24 11:10:17 +03004725static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004726{
Sean Christopherson95b5a482019-04-19 22:50:59 -07004727 /* handled by vmx_vcpu_run() */
Andi Kleena0861c02009-06-08 17:37:09 +08004728 return 1;
4729}
4730
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004731/*
4732 * If the host has split lock detection disabled, then #AC is
4733 * unconditionally injected into the guest, which is the pre split lock
4734 * detection behaviour.
4735 *
4736 * If the host has split lock detection enabled then #AC is
4737 * only injected into the guest when:
4738 * - Guest CPL == 3 (user mode)
4739 * - Guest has #AC detection enabled in CR0
4740 * - Guest EFLAGS has AC bit set
4741 */
4742static inline bool guest_inject_ac(struct kvm_vcpu *vcpu)
4743{
4744 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
4745 return true;
4746
4747 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) &&
4748 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
4749}
4750
Sean Christopherson95b5a482019-04-19 22:50:59 -07004751static int handle_exception_nmi(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752{
Avi Kivity1155f762007-11-22 11:30:47 +02004753 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004754 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004755 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004756 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757 u32 vect_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004758
Avi Kivity1155f762007-11-22 11:30:47 +02004759 vect_info = vmx->idt_vectoring_info;
Sean Christophersonf27ad732020-04-27 10:18:37 -07004760 intr_info = vmx_get_intr_info(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761
Paolo Bonzini2ea72032019-06-06 14:57:25 +02004762 if (is_machine_check(intr_info) || is_nmi(intr_info))
Sean Christopherson95b5a482019-04-19 22:50:59 -07004763 return 1; /* handled by handle_exception_nmi_irqoff() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004764
Wanpeng Li082d06e2018-04-03 16:28:48 -07004765 if (is_invalid_opcode(intr_info))
4766 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004767
Avi Kivity6aa8b732006-12-10 02:21:36 -08004768 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004769 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004771
Liran Alon9e869482018-03-12 13:12:51 +02004772 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4773 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christophersona6c6ed12019-08-27 14:40:30 -07004774
4775 /*
4776 * VMware backdoor emulation on #GP interception only handles
4777 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
4778 * error code on #GP.
4779 */
4780 if (error_code) {
4781 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4782 return 1;
4783 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004784 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon9e869482018-03-12 13:12:51 +02004785 }
4786
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004787 /*
4788 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4789 * MMIO, it is better to report an internal error.
4790 * See the comments in vmx_handle_exit.
4791 */
4792 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4793 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4794 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4795 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Jim Mattson1aa561b2020-06-03 16:56:21 -07004796 vcpu->run->internal.ndata = 4;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004797 vcpu->run->internal.data[0] = vect_info;
4798 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004799 vcpu->run->internal.data[2] = error_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07004800 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004801 return 0;
4802 }
4803
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804 if (is_page_fault(intr_info)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07004805 cr2 = vmx_get_exit_qual(vcpu);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004806 if (enable_ept && !vcpu->arch.apf.host_apf_flags) {
4807 /*
4808 * EPT will cause page fault only if we need to
4809 * detect illegal GPAs.
4810 */
Mohammed Gamalb96e6502020-09-03 16:11:22 +02004811 WARN_ON_ONCE(!allow_smaller_maxphyaddr);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004812 kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
4813 return 1;
4814 } else
4815 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004816 }
4817
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004818 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004819
4820 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4821 return handle_rmode_exception(vcpu, ex_no, error_code);
4822
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004823 switch (ex_no) {
4824 case DB_VECTOR:
Sean Christopherson5addc232020-04-15 13:34:53 -07004825 dr6 = vmx_get_exit_qual(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004826 if (!(vcpu->guest_debug &
4827 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004828 if (is_icebp(intr_info))
Sean Christopherson1957aa62019-08-27 14:40:39 -07004829 WARN_ON(!skip_emulated_instruction(vcpu));
Huw Daviesfd2a4452014-04-16 10:02:51 +01004830
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004831 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004832 return 1;
4833 }
Peter Xu13196632020-05-05 16:49:58 -04004834 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004835 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4836 /* fall through */
4837 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004838 /*
4839 * Update instruction length as we may reinject #BP from
4840 * user space while in guest debugging mode. Reading it for
4841 * #DB as well causes no harm, it is not used in that case.
4842 */
4843 vmx->vcpu.arch.event_exit_inst_len =
4844 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004845 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004846 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004847 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4848 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004849 break;
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004850 case AC_VECTOR:
4851 if (guest_inject_ac(vcpu)) {
4852 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4853 return 1;
4854 }
4855
4856 /*
4857 * Handle split lock. Depending on detection mode this will
4858 * either warn and disable split lock detection for this
4859 * task or force SIGBUS on it.
4860 */
4861 if (handle_guest_split_lock(kvm_rip_read(vcpu)))
4862 return 1;
4863 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004864 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004865 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4866 kvm_run->ex.exception = ex_no;
4867 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004868 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 return 0;
4871}
4872
Andrea Arcangelif399e602019-11-04 17:59:58 -05004873static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004875 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004876 return 1;
4877}
4878
Avi Kivity851ba692009-08-24 11:10:17 +03004879static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004880{
Avi Kivity851ba692009-08-24 11:10:17 +03004881 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004882 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004883 return 0;
4884}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004885
Avi Kivity851ba692009-08-24 11:10:17 +03004886static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004887{
He, Qingbfdaab02007-09-12 14:18:28 +08004888 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004889 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004890 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891
Sean Christopherson5addc232020-04-15 13:34:53 -07004892 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02004893 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004894
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004895 ++vcpu->stat.io_exits;
4896
Sean Christopherson432baf62018-03-08 08:57:26 -08004897 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004898 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004899
4900 port = exit_qualification >> 16;
4901 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004902 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004903
Sean Christophersondca7f122018-03-08 08:57:27 -08004904 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004905}
4906
Ingo Molnar102d8322007-02-19 14:37:47 +02004907static void
4908vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4909{
4910 /*
4911 * Patch in the VMCALL instruction:
4912 */
4913 hypercall[0] = 0x0f;
4914 hypercall[1] = 0x01;
4915 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004916}
4917
Guo Chao0fa06072012-06-28 15:16:19 +08004918/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004919static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4920{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004921 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004922 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4923 unsigned long orig_val = val;
4924
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004925 /*
4926 * We get here when L2 changed cr0 in a way that did not change
4927 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004928 * but did change L0 shadowed bits. So we first calculate the
4929 * effective cr0 value that L1 would like to write into the
4930 * hardware. It consists of the L2-owned bits from the new
4931 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004932 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004933 val = (val & ~vmcs12->cr0_guest_host_mask) |
4934 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4935
David Matlack38991522016-11-29 18:14:08 -08004936 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004937 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004938
4939 if (kvm_set_cr0(vcpu, val))
4940 return 1;
4941 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004942 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004943 } else {
4944 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08004945 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004946 return 1;
David Matlack38991522016-11-29 18:14:08 -08004947
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004948 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004949 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004950}
4951
4952static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4953{
4954 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004955 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4956 unsigned long orig_val = val;
4957
4958 /* analogously to handle_set_cr0 */
4959 val = (val & ~vmcs12->cr4_guest_host_mask) |
4960 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4961 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004962 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004963 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004964 return 0;
4965 } else
4966 return kvm_set_cr4(vcpu, val);
4967}
4968
Paolo Bonzini0367f202016-07-12 10:44:55 +02004969static int handle_desc(struct kvm_vcpu *vcpu)
4970{
4971 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004972 return kvm_emulate_instruction(vcpu, 0);
Paolo Bonzini0367f202016-07-12 10:44:55 +02004973}
4974
Avi Kivity851ba692009-08-24 11:10:17 +03004975static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004976{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004977 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978 int cr;
4979 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004980 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004981 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982
Sean Christopherson5addc232020-04-15 13:34:53 -07004983 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984 cr = exit_qualification & 15;
4985 reg = (exit_qualification >> 8) & 15;
4986 switch ((exit_qualification >> 4) & 3) {
4987 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03004988 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004989 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004990 switch (cr) {
4991 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004992 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004993 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004994 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004995 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03004996 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004997 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004999 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005000 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005001 case 8: {
5002 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005003 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005004 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005005 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005006 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08005007 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005008 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005009 return ret;
5010 /*
5011 * TODO: we might be squashing a
5012 * KVM_GUESTDBG_SINGLESTEP-triggered
5013 * KVM_EXIT_DEBUG here.
5014 */
Avi Kivity851ba692009-08-24 11:10:17 +03005015 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005016 return 0;
5017 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005018 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005019 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005020 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005021 WARN_ONCE(1, "Guest should always own CR0.TS");
5022 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005023 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005024 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005025 case 1: /*mov from cr*/
5026 switch (cr) {
5027 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005028 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02005029 val = kvm_read_cr3(vcpu);
5030 kvm_register_write(vcpu, reg, val);
5031 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005032 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005034 val = kvm_get_cr8(vcpu);
5035 kvm_register_write(vcpu, reg, val);
5036 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005037 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005038 }
5039 break;
5040 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005041 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005042 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005043 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005044
Kyle Huey6affcbe2016-11-29 12:40:40 -08005045 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 default:
5047 break;
5048 }
Avi Kivity851ba692009-08-24 11:10:17 +03005049 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005050 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051 (int)(exit_qualification >> 4) & 3, cr);
5052 return 0;
5053}
5054
Avi Kivity851ba692009-08-24 11:10:17 +03005055static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005056{
He, Qingbfdaab02007-09-12 14:18:28 +08005057 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005058 int dr, dr7, reg;
5059
Sean Christopherson5addc232020-04-15 13:34:53 -07005060 exit_qualification = vmx_get_exit_qual(vcpu);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005061 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5062
5063 /* First, if DR does not exist, trigger UD */
5064 if (!kvm_require_dr(vcpu, dr))
5065 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005066
Jan Kiszkaf2483412010-01-20 18:20:20 +01005067 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005068 if (!kvm_require_cpl(vcpu, 0))
5069 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005070 dr7 = vmcs_readl(GUEST_DR7);
5071 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005072 /*
5073 * As the vm-exit takes precedence over the debug trap, we
5074 * need to emulate the latter, either for the host or the
5075 * guest debugging itself.
5076 */
5077 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Paolo Bonzini45981de2020-05-06 05:59:39 -04005078 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_RTM | DR6_FIXED_1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005079 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005080 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005081 vcpu->run->debug.arch.exception = DB_VECTOR;
5082 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005083 return 0;
5084 } else {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04005085 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005086 return 1;
5087 }
5088 }
5089
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005090 if (vcpu->guest_debug == 0) {
Sean Christopherson2183f562019-05-07 12:17:56 -07005091 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005092
5093 /*
5094 * No more DR vmexits; force a reload of the debug registers
5095 * and reenter on this instruction. The next vmexit will
5096 * retrieve the full state of the debug registers.
5097 */
5098 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5099 return 1;
5100 }
5101
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005102 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5103 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005104 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005105
5106 if (kvm_get_dr(vcpu, dr, &val))
5107 return 1;
5108 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005109 } else
Nadav Amit57773922014-06-18 17:19:23 +03005110 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005111 return 1;
5112
Kyle Huey6affcbe2016-11-29 12:40:40 -08005113 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005114}
5115
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005116static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5117{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005118 get_debugreg(vcpu->arch.db[0], 0);
5119 get_debugreg(vcpu->arch.db[1], 1);
5120 get_debugreg(vcpu->arch.db[2], 2);
5121 get_debugreg(vcpu->arch.db[3], 3);
5122 get_debugreg(vcpu->arch.dr6, 6);
5123 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5124
5125 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Sean Christopherson2183f562019-05-07 12:17:56 -07005126 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005127}
5128
Gleb Natapov020df072010-04-13 10:05:23 +03005129static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5130{
5131 vmcs_writel(GUEST_DR7, val);
5132}
5133
Avi Kivity851ba692009-08-24 11:10:17 +03005134static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005135{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01005136 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005137 return 1;
5138}
5139
Avi Kivity851ba692009-08-24 11:10:17 +03005140static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005141{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005142 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005143
Avi Kivity3842d132010-07-27 12:30:24 +03005144 kvm_make_request(KVM_REQ_EVENT, vcpu);
5145
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005146 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005147 return 1;
5148}
5149
Avi Kivity851ba692009-08-24 11:10:17 +03005150static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005151{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005152 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005153}
5154
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005155static int handle_invd(struct kvm_vcpu *vcpu)
5156{
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005157 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005158}
5159
Avi Kivity851ba692009-08-24 11:10:17 +03005160static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005161{
Sean Christopherson5addc232020-04-15 13:34:53 -07005162 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005163
5164 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005165 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005166}
5167
Avi Kivityfee84b02011-11-10 14:57:25 +02005168static int handle_rdpmc(struct kvm_vcpu *vcpu)
5169{
5170 int err;
5171
5172 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005173 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02005174}
5175
Avi Kivity851ba692009-08-24 11:10:17 +03005176static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005177{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005178 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005179}
5180
Dexuan Cui2acf9232010-06-10 11:27:12 +08005181static int handle_xsetbv(struct kvm_vcpu *vcpu)
5182{
5183 u64 new_bv = kvm_read_edx_eax(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07005184 u32 index = kvm_rcx_read(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005185
5186 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005187 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005188 return 1;
5189}
5190
Avi Kivity851ba692009-08-24 11:10:17 +03005191static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005192{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005193 if (likely(fasteoi)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07005194 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005195 int access_type, offset;
5196
5197 access_type = exit_qualification & APIC_ACCESS_TYPE;
5198 offset = exit_qualification & APIC_ACCESS_OFFSET;
5199 /*
5200 * Sane guest uses MOV to write EOI, with written value
5201 * not cared. So make a short-circuit here by avoiding
5202 * heavy instruction emulation.
5203 */
5204 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5205 (offset == APIC_EOI)) {
5206 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005207 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005208 }
5209 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005210 return kvm_emulate_instruction(vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005211}
5212
Yang Zhangc7c9c562013-01-25 10:18:51 +08005213static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5214{
Sean Christopherson5addc232020-04-15 13:34:53 -07005215 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005216 int vector = exit_qualification & 0xff;
5217
5218 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5219 kvm_apic_set_eoi_accelerated(vcpu, vector);
5220 return 1;
5221}
5222
Yang Zhang83d4c282013-01-25 10:18:49 +08005223static int handle_apic_write(struct kvm_vcpu *vcpu)
5224{
Sean Christopherson5addc232020-04-15 13:34:53 -07005225 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhang83d4c282013-01-25 10:18:49 +08005226 u32 offset = exit_qualification & 0xfff;
5227
5228 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5229 kvm_apic_write_nodecode(vcpu, offset);
5230 return 1;
5231}
5232
Avi Kivity851ba692009-08-24 11:10:17 +03005233static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005234{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005235 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005236 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005237 bool has_error_code = false;
5238 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005239 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005240 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005241
5242 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005243 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005244 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005245
Sean Christopherson5addc232020-04-15 13:34:53 -07005246 exit_qualification = vmx_get_exit_qual(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005247
5248 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005249 if (reason == TASK_SWITCH_GATE && idt_v) {
5250 switch (type) {
5251 case INTR_TYPE_NMI_INTR:
5252 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005253 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005254 break;
5255 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005256 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005257 kvm_clear_interrupt_queue(vcpu);
5258 break;
5259 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005260 if (vmx->idt_vectoring_info &
5261 VECTORING_INFO_DELIVER_CODE_MASK) {
5262 has_error_code = true;
5263 error_code =
5264 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5265 }
5266 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005267 case INTR_TYPE_SOFT_EXCEPTION:
5268 kvm_clear_exception_queue(vcpu);
5269 break;
5270 default:
5271 break;
5272 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005273 }
Izik Eidus37817f22008-03-24 23:14:53 +02005274 tss_selector = exit_qualification;
5275
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005276 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5277 type != INTR_TYPE_EXT_INTR &&
5278 type != INTR_TYPE_NMI_INTR))
Sean Christopherson1957aa62019-08-27 14:40:39 -07005279 WARN_ON(!skip_emulated_instruction(vcpu));
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005280
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005281 /*
5282 * TODO: What about debug traps on tss switch?
5283 * Are we supposed to inject them and update dr6?
5284 */
Sean Christopherson10517782019-08-27 14:40:35 -07005285 return kvm_task_switch(vcpu, tss_selector,
5286 type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005287 reason, has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02005288}
5289
Avi Kivity851ba692009-08-24 11:10:17 +03005290static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005291{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005292 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005293 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01005294 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005295
Sean Christopherson5addc232020-04-15 13:34:53 -07005296 exit_qualification = vmx_get_exit_qual(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005297
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005298 /*
5299 * EPT violation happened while executing iret from NMI,
5300 * "blocked by NMI" bit has to be set before next VM entry.
5301 * There are errata that may cause this bit to not be set:
5302 * AAK134, BY25.
5303 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005304 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005305 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005306 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005307 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5308
Sheng Yang14394422008-04-28 12:24:45 +08005309 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005310 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005311
Junaid Shahid27959a42016-12-06 16:46:10 -08005312 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005313 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08005314 ? PFERR_USER_MASK : 0;
5315 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005316 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08005317 ? PFERR_WRITE_MASK : 0;
5318 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005319 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08005320 ? PFERR_FETCH_MASK : 0;
5321 /* ept page table entry is present? */
5322 error_code |= (exit_qualification &
5323 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5324 EPT_VIOLATION_EXECUTABLE))
5325 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005326
Paolo Bonzinieebed242016-11-28 14:39:58 +01005327 error_code |= (exit_qualification & 0x100) != 0 ?
5328 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005329
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005330 vcpu->arch.exit_qualification = exit_qualification;
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005331
5332 /*
5333 * Check that the GPA doesn't exceed physical memory limits, as that is
5334 * a guest page fault. We have to emulate the instruction here, because
5335 * if the illegal address is that of a paging structure, then
5336 * EPT_VIOLATION_ACC_WRITE bit is set. Alternatively, if supported we
5337 * would also use advanced VM-exit information for EPT violations to
5338 * reconstruct the page fault error code.
5339 */
Mohammed Gamalb96e6502020-09-03 16:11:22 +02005340 if (unlikely(allow_smaller_maxphyaddr && kvm_mmu_is_illegal_gpa(vcpu, gpa)))
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005341 return kvm_emulate_instruction(vcpu, 0);
5342
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005343 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005344}
5345
Avi Kivity851ba692009-08-24 11:10:17 +03005346static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005347{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005348 gpa_t gpa;
5349
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005350 /*
5351 * A nested guest cannot optimize MMIO vmexits, because we have an
5352 * nGPA here instead of the required GPA.
5353 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005354 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005355 if (!is_guest_mode(vcpu) &&
5356 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08005357 trace_kvm_fast_mmio(gpa);
Sean Christopherson1957aa62019-08-27 14:40:39 -07005358 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005359 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005360
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07005361 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005362}
5363
Avi Kivity851ba692009-08-24 11:10:17 +03005364static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005365{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005366 WARN_ON_ONCE(!enable_vnmi);
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005367 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Sheng Yangf08864b2008-05-15 18:23:25 +08005368 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005369 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005370
5371 return 1;
5372}
5373
Mohammed Gamal80ced182009-09-01 12:48:18 +02005374static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005375{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005376 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity49e9d552010-09-19 14:34:08 +02005377 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005378 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005379
Sean Christopherson2183f562019-05-07 12:17:56 -07005380 intr_window_requested = exec_controls_get(vmx) &
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005381 CPU_BASED_INTR_WINDOW_EXITING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005382
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005383 while (vmx->emulation_required && count-- != 0) {
Sean Christophersondb438592020-04-22 19:25:48 -07005384 if (intr_window_requested && !vmx_interrupt_blocked(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005385 return handle_interrupt_window(&vmx->vcpu);
5386
Radim Krčmář72875d82017-04-26 22:32:19 +02005387 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005388 return 1;
5389
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005390 if (!kvm_emulate_instruction(vcpu, 0))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005391 return 0;
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005392
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005393 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
Sean Christopherson8fff2712019-08-27 14:40:37 -07005394 vcpu->arch.exception.pending) {
5395 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5396 vcpu->run->internal.suberror =
5397 KVM_INTERNAL_ERROR_EMULATION;
5398 vcpu->run->internal.ndata = 0;
5399 return 0;
5400 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005401
Gleb Natapov8d76c492013-05-08 18:38:44 +03005402 if (vcpu->arch.halt_request) {
5403 vcpu->arch.halt_request = 0;
Sean Christopherson8fff2712019-08-27 14:40:37 -07005404 return kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005405 }
5406
Sean Christopherson8fff2712019-08-27 14:40:37 -07005407 /*
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005408 * Note, return 1 and not 0, vcpu_run() will invoke
5409 * xfer_to_guest_mode() which will create a proper return
5410 * code.
Sean Christopherson8fff2712019-08-27 14:40:37 -07005411 */
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005412 if (__xfer_to_guest_mode_work_pending())
Sean Christopherson8fff2712019-08-27 14:40:37 -07005413 return 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005414 }
5415
Sean Christopherson8fff2712019-08-27 14:40:37 -07005416 return 1;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005417}
5418
5419static void grow_ple_window(struct kvm_vcpu *vcpu)
5420{
5421 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005422 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005423
Babu Mogerc8e88712018-03-16 16:37:24 -04005424 vmx->ple_window = __grow_ple_window(old, ple_window,
5425 ple_window_grow,
5426 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005427
Peter Xu4f75bcc2019-09-06 10:17:22 +08005428 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005429 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005430 trace_kvm_ple_window_update(vcpu->vcpu_id,
5431 vmx->ple_window, old);
5432 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005433}
5434
5435static void shrink_ple_window(struct kvm_vcpu *vcpu)
5436{
5437 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005438 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005439
Babu Mogerc8e88712018-03-16 16:37:24 -04005440 vmx->ple_window = __shrink_ple_window(old, ple_window,
5441 ple_window_shrink,
5442 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005443
Peter Xu4f75bcc2019-09-06 10:17:22 +08005444 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005445 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005446 trace_kvm_ple_window_update(vcpu->vcpu_id,
5447 vmx->ple_window, old);
5448 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005449}
5450
5451/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08005452 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
5453 */
5454static void wakeup_handler(void)
5455{
5456 struct kvm_vcpu *vcpu;
5457 int cpu = smp_processor_id();
5458
5459 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
5460 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
5461 blocked_vcpu_list) {
5462 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
5463
5464 if (pi_test_on(pi_desc) == 1)
5465 kvm_vcpu_kick(vcpu);
5466 }
5467 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
5468}
5469
Peng Haoe01bca22018-04-07 05:47:32 +08005470static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005471{
5472 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
5473 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
5474 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
5475 0ull, VMX_EPT_EXECUTABLE_MASK,
5476 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005477 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005478
5479 ept_set_mmio_spte_mask();
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005480}
5481
Avi Kivity6aa8b732006-12-10 02:21:36 -08005482/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005483 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5484 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5485 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005486static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005487{
Wanpeng Lib31c1142018-03-12 04:53:04 -07005488 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005489 grow_ple_window(vcpu);
5490
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08005491 /*
5492 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
5493 * VM-execution control is ignored if CPL > 0. OTOH, KVM
5494 * never set PAUSE_EXITING and just set PLE if supported,
5495 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
5496 */
5497 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005498 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005499}
5500
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005501static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08005502{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005503 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08005504}
5505
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005506static int handle_mwait(struct kvm_vcpu *vcpu)
5507{
5508 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
5509 return handle_nop(vcpu);
5510}
5511
Jim Mattson45ec3682017-08-23 16:32:04 -07005512static int handle_invalid_op(struct kvm_vcpu *vcpu)
5513{
5514 kvm_queue_exception(vcpu, UD_VECTOR);
5515 return 1;
5516}
5517
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005518static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5519{
5520 return 1;
5521}
5522
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005523static int handle_monitor(struct kvm_vcpu *vcpu)
5524{
5525 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
5526 return handle_nop(vcpu);
5527}
5528
Junaid Shahideb4b2482018-06-27 14:59:14 -07005529static int handle_invpcid(struct kvm_vcpu *vcpu)
5530{
5531 u32 vmx_instruction_info;
5532 unsigned long type;
5533 bool pcid_enabled;
5534 gva_t gva;
5535 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005536 unsigned i;
5537 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005538 struct {
5539 u64 pcid;
5540 u64 gla;
5541 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005542 int r;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005543
5544 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5545 kvm_queue_exception(vcpu, UD_VECTOR);
5546 return 1;
5547 }
5548
5549 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5550 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5551
5552 if (type > 3) {
5553 kvm_inject_gp(vcpu, 0);
5554 return 1;
5555 }
5556
5557 /* According to the Intel instruction reference, the memory operand
5558 * is read even if it isn't needed (e.g., for type==all)
5559 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005560 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005561 vmx_instruction_info, false,
5562 sizeof(operand), &gva))
Junaid Shahideb4b2482018-06-27 14:59:14 -07005563 return 1;
5564
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005565 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5566 if (r != X86EMUL_CONTINUE)
5567 return vmx_handle_memory_failure(vcpu, r, &e);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005568
5569 if (operand.pcid >> 12 != 0) {
5570 kvm_inject_gp(vcpu, 0);
5571 return 1;
5572 }
5573
5574 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
5575
5576 switch (type) {
5577 case INVPCID_TYPE_INDIV_ADDR:
5578 if ((!pcid_enabled && (operand.pcid != 0)) ||
5579 is_noncanonical_address(operand.gla, vcpu)) {
5580 kvm_inject_gp(vcpu, 0);
5581 return 1;
5582 }
5583 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
5584 return kvm_skip_emulated_instruction(vcpu);
5585
5586 case INVPCID_TYPE_SINGLE_CTXT:
5587 if (!pcid_enabled && (operand.pcid != 0)) {
5588 kvm_inject_gp(vcpu, 0);
5589 return 1;
5590 }
5591
5592 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
5593 kvm_mmu_sync_roots(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07005594 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005595 }
5596
Junaid Shahidb94742c2018-06-27 14:59:20 -07005597 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005598 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd)
Junaid Shahidb94742c2018-06-27 14:59:20 -07005599 == operand.pcid)
5600 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07005601
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02005602 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005603 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07005604 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07005605 * given PCID, then nothing needs to be done here because a
5606 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07005607 */
5608
5609 return kvm_skip_emulated_instruction(vcpu);
5610
5611 case INVPCID_TYPE_ALL_NON_GLOBAL:
5612 /*
5613 * Currently, KVM doesn't mark global entries in the shadow
5614 * page tables, so a non-global flush just degenerates to a
5615 * global flush. If needed, we could optimize this later by
5616 * keeping track of global entries in shadow page tables.
5617 */
5618
5619 /* fall-through */
5620 case INVPCID_TYPE_ALL_INCL_GLOBAL:
5621 kvm_mmu_unload(vcpu);
5622 return kvm_skip_emulated_instruction(vcpu);
5623
5624 default:
5625 BUG(); /* We have already checked above that type <= 3 */
5626 }
5627}
5628
Kai Huang843e4332015-01-28 10:54:28 +08005629static int handle_pml_full(struct kvm_vcpu *vcpu)
5630{
5631 unsigned long exit_qualification;
5632
5633 trace_kvm_pml_full(vcpu->vcpu_id);
5634
Sean Christopherson5addc232020-04-15 13:34:53 -07005635 exit_qualification = vmx_get_exit_qual(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005636
5637 /*
5638 * PML buffer FULL happened while executing iret from NMI,
5639 * "blocked by NMI" bit has to be set before next VM entry.
5640 */
5641 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005642 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005643 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5644 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5645 GUEST_INTR_STATE_NMI);
5646
5647 /*
5648 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5649 * here.., and there's no userspace involvement needed for PML.
5650 */
5651 return 1;
5652}
5653
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005654static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07005655{
Sean Christopherson804939e2019-05-07 12:18:05 -07005656 struct vcpu_vmx *vmx = to_vmx(vcpu);
5657
5658 if (!vmx->req_immediate_exit &&
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005659 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) {
Sean Christophersond264ee02018-08-27 15:21:12 -07005660 kvm_lapic_expired_hv_timer(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005661 return EXIT_FASTPATH_REENTER_GUEST;
5662 }
Sean Christopherson804939e2019-05-07 12:18:05 -07005663
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005664 return EXIT_FASTPATH_NONE;
5665}
5666
5667static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5668{
5669 handle_fastpath_preemption_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005670 return 1;
5671}
5672
Sean Christophersone4027cf2018-12-03 13:53:12 -08005673/*
5674 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5675 * are overwritten by nested_vmx_setup() when nested=1.
5676 */
5677static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5678{
5679 kvm_queue_exception(vcpu, UD_VECTOR);
5680 return 1;
5681}
5682
Sean Christopherson0b665d32018-08-14 09:33:34 -07005683static int handle_encls(struct kvm_vcpu *vcpu)
5684{
5685 /*
5686 * SGX virtualization is not yet supported. There is no software
5687 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
5688 * to prevent the guest from executing ENCLS.
5689 */
5690 kvm_queue_exception(vcpu, UD_VECTOR);
5691 return 1;
5692}
5693
Nadav Har'El0140cae2011-05-25 23:06:28 +03005694/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005695 * The exit handlers return 1 if the exit was handled fully and guest execution
5696 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5697 * to be done to userspace and return 0.
5698 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005699static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Sean Christopherson95b5a482019-04-19 22:50:59 -07005700 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005701 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005702 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005703 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005704 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005705 [EXIT_REASON_CR_ACCESS] = handle_cr,
5706 [EXIT_REASON_DR_ACCESS] = handle_dr,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005707 [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
5708 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
5709 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005710 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005711 [EXIT_REASON_HLT] = kvm_emulate_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005712 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005713 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005714 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005715 [EXIT_REASON_VMCALL] = handle_vmcall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005716 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5717 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5718 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5719 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5720 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5721 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5722 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5723 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5724 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005725 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5726 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005727 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005728 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005729 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005730 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005731 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005732 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005733 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5734 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005735 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5736 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005737 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005738 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005739 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005740 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005741 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5742 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Jim Mattson45ec3682017-08-23 16:32:04 -07005743 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07005744 [EXIT_REASON_RDSEED] = handle_invalid_op,
Kai Huang843e4332015-01-28 10:54:28 +08005745 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005746 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005747 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005748 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005749 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005750};
5751
5752static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005753 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005754
Avi Kivity586f9602010-11-18 13:09:54 +02005755static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
5756{
Sean Christopherson5addc232020-04-15 13:34:53 -07005757 *info1 = vmx_get_exit_qual(vcpu);
Sean Christopherson87915852020-04-15 13:34:54 -07005758 *info2 = vmx_get_intr_info(vcpu);
Avi Kivity586f9602010-11-18 13:09:54 +02005759}
5760
Kai Huanga3eaa862015-11-04 13:46:05 +08005761static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005762{
Kai Huanga3eaa862015-11-04 13:46:05 +08005763 if (vmx->pml_pg) {
5764 __free_page(vmx->pml_pg);
5765 vmx->pml_pg = NULL;
5766 }
Kai Huang843e4332015-01-28 10:54:28 +08005767}
5768
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005769static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005770{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005771 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005772 u64 *pml_buf;
5773 u16 pml_idx;
5774
5775 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5776
5777 /* Do nothing if PML buffer is empty */
5778 if (pml_idx == (PML_ENTITY_NUM - 1))
5779 return;
5780
5781 /* PML index always points to next available PML buffer entity */
5782 if (pml_idx >= PML_ENTITY_NUM)
5783 pml_idx = 0;
5784 else
5785 pml_idx++;
5786
5787 pml_buf = page_address(vmx->pml_pg);
5788 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5789 u64 gpa;
5790
5791 gpa = pml_buf[pml_idx];
5792 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005793 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005794 }
5795
5796 /* reset PML index */
5797 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5798}
5799
5800/*
5801 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
5802 * Called before reporting dirty_bitmap to userspace.
5803 */
5804static void kvm_flush_pml_buffers(struct kvm *kvm)
5805{
5806 int i;
5807 struct kvm_vcpu *vcpu;
5808 /*
5809 * We only need to kick vcpu out of guest mode here, as PML buffer
5810 * is flushed at beginning of all VMEXITs, and it's obvious that only
5811 * vcpus running in guest are possible to have unflushed GPAs in PML
5812 * buffer.
5813 */
5814 kvm_for_each_vcpu(i, vcpu, kvm)
5815 kvm_vcpu_kick(vcpu);
5816}
5817
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005818static void vmx_dump_sel(char *name, uint32_t sel)
5819{
5820 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005821 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005822 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5823 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5824 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5825}
5826
5827static void vmx_dump_dtsel(char *name, uint32_t limit)
5828{
5829 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5830 name, vmcs_read32(limit),
5831 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5832}
5833
Paolo Bonzini69090812019-04-15 15:16:17 +02005834void dump_vmcs(void)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005835{
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005836 u32 vmentry_ctl, vmexit_ctl;
5837 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
5838 unsigned long cr4;
5839 u64 efer;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005840
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005841 if (!dump_invalid_vmcs) {
5842 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
5843 return;
5844 }
5845
5846 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5847 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5848 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5849 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5850 cr4 = vmcs_readl(GUEST_CR4);
5851 efer = vmcs_read64(GUEST_IA32_EFER);
5852 secondary_exec_control = 0;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005853 if (cpu_has_secondary_exec_ctrls())
5854 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5855
5856 pr_err("*** Guest State ***\n");
5857 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5858 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5859 vmcs_readl(CR0_GUEST_HOST_MASK));
5860 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5861 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5862 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
5863 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
5864 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
5865 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005866 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5867 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5868 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5869 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005870 }
5871 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5872 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5873 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5874 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5875 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5876 vmcs_readl(GUEST_SYSENTER_ESP),
5877 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5878 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5879 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5880 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5881 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5882 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5883 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5884 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5885 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5886 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5887 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
5888 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
5889 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005890 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5891 efer, vmcs_read64(GUEST_IA32_PAT));
5892 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5893 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005894 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005895 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005896 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005897 pr_err("PerfGlobCtl = 0x%016llx\n",
5898 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005899 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005900 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005901 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5902 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5903 vmcs_read32(GUEST_ACTIVITY_STATE));
5904 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5905 pr_err("InterruptStatus = %04x\n",
5906 vmcs_read16(GUEST_INTR_STATUS));
5907
5908 pr_err("*** Host State ***\n");
5909 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5910 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5911 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5912 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5913 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5914 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5915 vmcs_read16(HOST_TR_SELECTOR));
5916 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5917 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5918 vmcs_readl(HOST_TR_BASE));
5919 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5920 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5921 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5922 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5923 vmcs_readl(HOST_CR4));
5924 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5925 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5926 vmcs_read32(HOST_IA32_SYSENTER_CS),
5927 vmcs_readl(HOST_IA32_SYSENTER_EIP));
5928 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005929 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5930 vmcs_read64(HOST_IA32_EFER),
5931 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005932 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005933 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005934 pr_err("PerfGlobCtl = 0x%016llx\n",
5935 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005936
5937 pr_err("*** Control State ***\n");
5938 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5939 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5940 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5941 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5942 vmcs_read32(EXCEPTION_BITMAP),
5943 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5944 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5945 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5946 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5947 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5948 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5949 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5950 vmcs_read32(VM_EXIT_INTR_INFO),
5951 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5952 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5953 pr_err(" reason=%08x qualification=%016lx\n",
5954 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5955 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5956 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5957 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005958 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005959 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005960 pr_err("TSC Multiplier = 0x%016llx\n",
5961 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005962 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
5963 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
5964 u16 status = vmcs_read16(GUEST_INTR_STATUS);
5965 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
5966 }
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005967 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005968 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
5969 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005970 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005971 }
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005972 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5973 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5974 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005975 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005976 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5977 pr_err("PLE Gap=%08x Window=%08x\n",
5978 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5979 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5980 pr_err("Virtual processor ID = 0x%04x\n",
5981 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5982}
5983
Avi Kivity6aa8b732006-12-10 02:21:36 -08005984/*
5985 * The guest has exited. See if we can fix it or if we need userspace
5986 * assistance.
5987 */
Wanpeng Li404d5d72020-04-28 14:23:25 +08005988static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005989{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005990 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005991 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005992 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005993
Kai Huang843e4332015-01-28 10:54:28 +08005994 /*
5995 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5996 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5997 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5998 * mode as if vcpus is in root mode, the PML buffer must has been
5999 * flushed already.
6000 */
6001 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006002 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08006003
Sean Christophersondb438592020-04-22 19:25:48 -07006004 /*
6005 * We should never reach this point with a pending nested VM-Enter, and
6006 * more specifically emulation of L2 due to invalid guest state (see
6007 * below) should never happen as that means we incorrectly allowed a
6008 * nested VM-Enter with an invalid vmcs12.
6009 */
6010 WARN_ON_ONCE(vmx->nested.nested_run_pending);
6011
Mohammed Gamal80ced182009-09-01 12:48:18 +02006012 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006013 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006014 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006015
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006016 if (is_guest_mode(vcpu)) {
6017 /*
6018 * The host physical addresses of some pages of guest memory
6019 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
6020 * Page). The CPU may write to these pages via their host
6021 * physical address while L2 is running, bypassing any
6022 * address-translation-based dirty tracking (e.g. EPT write
6023 * protection).
6024 *
6025 * Mark them dirty on every exit from L2 to prevent them from
6026 * getting out of sync with dirty tracking.
6027 */
6028 nested_mark_vmcs12_pages_dirty(vcpu);
6029
Sean Christophersonf47baae2020-04-15 10:55:16 -07006030 if (nested_vmx_reflect_vmexit(vcpu))
Sean Christopherson789afc52020-04-15 10:55:10 -07006031 return 1;
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006032 }
Nadav Har'El644d7112011-05-25 23:12:35 +03006033
Mohammed Gamal51207022010-05-31 22:40:54 +03006034 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02006035 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03006036 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6037 vcpu->run->fail_entry.hardware_entry_failure_reason
6038 = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006039 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Mohammed Gamal51207022010-05-31 22:40:54 +03006040 return 0;
6041 }
6042
Avi Kivity29bd8a72007-09-10 17:27:03 +03006043 if (unlikely(vmx->fail)) {
Paolo Bonzini3b20e032019-07-19 18:15:08 +02006044 dump_vmcs();
Avi Kivity851ba692009-08-24 11:10:17 +03006045 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6046 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006047 = vmcs_read32(VM_INSTRUCTION_ERROR);
Jim Mattson8a14fe42020-06-03 16:56:22 -07006048 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006049 return 0;
6050 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006051
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006052 /*
6053 * Note:
6054 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6055 * delivery event since it indicates guest is accessing MMIO.
6056 * The vm-exit can be triggered again after return to guest that
6057 * will cause infinite loop.
6058 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006059 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08006060 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02006061 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00006062 exit_reason != EXIT_REASON_PML_FULL &&
Wanpeng Li99b82a12020-08-19 16:55:27 +08006063 exit_reason != EXIT_REASON_APIC_ACCESS &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006064 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6065 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6066 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006067 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006068 vcpu->run->internal.data[0] = vectoring_info;
6069 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006070 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
6071 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
6072 vcpu->run->internal.ndata++;
6073 vcpu->run->internal.data[3] =
6074 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6075 }
Jim Mattson1aa561b2020-06-03 16:56:21 -07006076 vcpu->run->internal.data[vcpu->run->internal.ndata++] =
Jim Mattson8a14fe42020-06-03 16:56:22 -07006077 vcpu->arch.last_vmentry_cpu;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006078 return 0;
6079 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006080
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006081 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006082 vmx->loaded_vmcs->soft_vnmi_blocked)) {
Sean Christophersondb438592020-04-22 19:25:48 -07006083 if (!vmx_interrupt_blocked(vcpu)) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006084 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6085 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
6086 vcpu->arch.nmi_pending) {
6087 /*
6088 * This CPU don't support us in finding the end of an
6089 * NMI-blocked window if the guest runs with IRQs
6090 * disabled. So we pull the trigger after 1 s of
6091 * futile waiting, but inform the user about this.
6092 */
6093 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6094 "state on VCPU %d after 1 s timeout\n",
6095 __func__, vcpu->vcpu_id);
6096 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6097 }
6098 }
6099
Wanpeng Li404d5d72020-04-28 14:23:25 +08006100 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08006101 return 1;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006102
6103 if (exit_reason >= kvm_vmx_max_exit_handlers)
6104 goto unexpected_vmexit;
6105#ifdef CONFIG_RETPOLINE
6106 if (exit_reason == EXIT_REASON_MSR_WRITE)
6107 return kvm_emulate_wrmsr(vcpu);
6108 else if (exit_reason == EXIT_REASON_PREEMPTION_TIMER)
6109 return handle_preemption_timer(vcpu);
6110 else if (exit_reason == EXIT_REASON_INTERRUPT_WINDOW)
6111 return handle_interrupt_window(vcpu);
6112 else if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6113 return handle_external_interrupt(vcpu);
6114 else if (exit_reason == EXIT_REASON_HLT)
6115 return kvm_emulate_halt(vcpu);
6116 else if (exit_reason == EXIT_REASON_EPT_MISCONFIG)
6117 return handle_ept_misconfig(vcpu);
6118#endif
6119
6120 exit_reason = array_index_nospec(exit_reason,
6121 kvm_vmx_max_exit_handlers);
6122 if (!kvm_vmx_exit_handlers[exit_reason])
6123 goto unexpected_vmexit;
6124
6125 return kvm_vmx_exit_handlers[exit_reason](vcpu);
6126
6127unexpected_vmexit:
6128 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", exit_reason);
6129 dump_vmcs();
6130 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6131 vcpu->run->internal.suberror =
6132 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
Jim Mattson1aa561b2020-06-03 16:56:21 -07006133 vcpu->run->internal.ndata = 2;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006134 vcpu->run->internal.data[0] = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006135 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006136 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006137}
6138
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006139/*
6140 * Software based L1D cache flush which is used when microcode providing
6141 * the cache control MSR is not loaded.
6142 *
6143 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
6144 * flush it is required to read in 64 KiB because the replacement algorithm
6145 * is not exactly LRU. This could be sized at runtime via topology
6146 * information but as all relevant affected CPUs have 32KiB L1D cache size
6147 * there is no point in doing so.
6148 */
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006149static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006150{
6151 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006152
6153 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02006154 * This code is only executed when the the flush mode is 'cond' or
6155 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006156 */
Nicolai Stange427362a2018-07-21 22:25:00 +02006157 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02006158 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006159
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006160 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02006161 * Clear the per-vcpu flush bit, it gets set again
6162 * either from vcpu_run() or from one of the unsafe
6163 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006164 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02006165 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02006166 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02006167
6168 /*
6169 * Clear the per-cpu flush bit, it gets set again from
6170 * the interrupt handlers.
6171 */
6172 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
6173 kvm_clear_cpu_l1tf_flush_l1d();
6174
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006175 if (!flush_l1d)
6176 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006177 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006178
6179 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006180
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006181 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006182 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006183 return;
6184 }
6185
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006186 asm volatile(
6187 /* First ensure the pages are in the TLB */
6188 "xorl %%eax, %%eax\n"
6189 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02006190 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006191 "addl $4096, %%eax\n\t"
6192 "cmpl %%eax, %[size]\n\t"
6193 "jne .Lpopulate_tlb\n\t"
6194 "xorl %%eax, %%eax\n\t"
6195 "cpuid\n\t"
6196 /* Now fill the cache */
6197 "xorl %%eax, %%eax\n"
6198 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006199 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006200 "addl $64, %%eax\n\t"
6201 "cmpl %%eax, %[size]\n\t"
6202 "jne .Lfill_cache\n\t"
6203 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006204 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006205 [size] "r" (size)
6206 : "eax", "ebx", "ecx", "edx");
6207}
6208
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006209static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006210{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006211 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Liran Alon132f4f72019-11-11 14:30:54 +02006212 int tpr_threshold;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006213
6214 if (is_guest_mode(vcpu) &&
6215 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6216 return;
6217
Liran Alon132f4f72019-11-11 14:30:54 +02006218 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
Liran Alon02d496cf2019-11-11 14:30:55 +02006219 if (is_guest_mode(vcpu))
6220 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6221 else
6222 vmcs_write32(TPR_THRESHOLD, tpr_threshold);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006223}
6224
Sean Christopherson97b7ead2018-12-03 13:53:16 -08006225void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08006226{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006227 struct vcpu_vmx *vmx = to_vmx(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006228 u32 sec_exec_control;
6229
Jim Mattson8d860bb2018-05-09 16:56:05 -04006230 if (!lapic_in_kernel(vcpu))
6231 return;
6232
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07006233 if (!flexpriority_enabled &&
6234 !cpu_has_vmx_virtualize_x2apic_mode())
6235 return;
6236
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006237 /* Postpone execution until vmcs01 is the current VMCS. */
6238 if (is_guest_mode(vcpu)) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006239 vmx->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006240 return;
6241 }
6242
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006243 sec_exec_control = secondary_exec_controls_get(vmx);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006244 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6245 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08006246
Jim Mattson8d860bb2018-05-09 16:56:05 -04006247 switch (kvm_get_apic_mode(vcpu)) {
6248 case LAPIC_MODE_INVALID:
6249 WARN_ONCE(true, "Invalid local APIC state");
6250 case LAPIC_MODE_DISABLED:
6251 break;
6252 case LAPIC_MODE_XAPIC:
6253 if (flexpriority_enabled) {
6254 sec_exec_control |=
6255 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006256 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6257
6258 /*
6259 * Flush the TLB, reloading the APIC access page will
6260 * only do so if its physical address has changed, but
6261 * the guest may have inserted a non-APIC mapping into
6262 * the TLB while the APIC access page was disabled.
6263 */
6264 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006265 }
6266 break;
6267 case LAPIC_MODE_X2APIC:
6268 if (cpu_has_vmx_virtualize_x2apic_mode())
6269 sec_exec_control |=
6270 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6271 break;
Yang Zhang8d146952013-01-25 10:18:50 +08006272 }
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006273 secondary_exec_controls_set(vmx, sec_exec_control);
Yang Zhang8d146952013-01-25 10:18:50 +08006274
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006275 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006276}
6277
Sean Christophersona4148b72020-03-20 14:28:24 -07006278static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
Tang Chen38b99172014-09-24 15:57:54 +08006279{
Sean Christophersona4148b72020-03-20 14:28:24 -07006280 struct page *page;
6281
Sean Christopherson1196cb92020-03-20 14:28:23 -07006282 /* Defer reload until vmcs01 is the current VMCS. */
6283 if (is_guest_mode(vcpu)) {
6284 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true;
6285 return;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07006286 }
Sean Christopherson1196cb92020-03-20 14:28:23 -07006287
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006288 if (!(secondary_exec_controls_get(to_vmx(vcpu)) &
6289 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
6290 return;
6291
Sean Christophersona4148b72020-03-20 14:28:24 -07006292 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
6293 if (is_error_page(page))
6294 return;
6295
6296 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
Sean Christopherson1196cb92020-03-20 14:28:23 -07006297 vmx_flush_tlb_current(vcpu);
Sean Christophersona4148b72020-03-20 14:28:24 -07006298
6299 /*
6300 * Do not pin apic access page in memory, the MMU notifier
6301 * will call us again if it is migrated or swapped out.
6302 */
6303 put_page(page);
Tang Chen38b99172014-09-24 15:57:54 +08006304}
6305
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006306static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006307{
6308 u16 status;
6309 u8 old;
6310
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006311 if (max_isr == -1)
6312 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006313
6314 status = vmcs_read16(GUEST_INTR_STATUS);
6315 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006316 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006317 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006318 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006319 vmcs_write16(GUEST_INTR_STATUS, status);
6320 }
6321}
6322
6323static void vmx_set_rvi(int vector)
6324{
6325 u16 status;
6326 u8 old;
6327
Wei Wang4114c272014-11-05 10:53:43 +08006328 if (vector == -1)
6329 vector = 0;
6330
Yang Zhangc7c9c562013-01-25 10:18:51 +08006331 status = vmcs_read16(GUEST_INTR_STATUS);
6332 old = (u8)status & 0xff;
6333 if ((u8)vector != old) {
6334 status &= ~0xff;
6335 status |= (u8)vector;
6336 vmcs_write16(GUEST_INTR_STATUS, status);
6337 }
6338}
6339
6340static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6341{
Liran Alon851c1a182017-12-24 18:12:56 +02006342 /*
6343 * When running L2, updating RVI is only relevant when
6344 * vmcs12 virtual-interrupt-delivery enabled.
6345 * However, it can be enabled only when L1 also
6346 * intercepts external-interrupts and in that case
6347 * we should not update vmcs02 RVI but instead intercept
6348 * interrupt. Therefore, do nothing when running L2.
6349 */
6350 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08006351 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006352}
6353
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006354static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006355{
6356 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006357 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02006358 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006359
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006360 WARN_ON(!vcpu->arch.apicv_active);
6361 if (pi_test_on(&vmx->pi_desc)) {
6362 pi_clear_on(&vmx->pi_desc);
6363 /*
Liran Alond9ff2742019-11-11 14:25:25 +02006364 * IOMMU can write to PID.ON, so the barrier matters even on UP.
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006365 * But on x86 this is just a compiler barrier anyway.
6366 */
6367 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02006368 max_irr_updated =
6369 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6370
6371 /*
6372 * If we are running L2 and L1 has a new pending interrupt
6373 * which can be injected, we should re-evaluate
6374 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02006375 * If L1 intercepts external-interrupts, we should
6376 * exit from L2 to L1. Otherwise, interrupt should be
6377 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02006378 */
Liran Alon851c1a182017-12-24 18:12:56 +02006379 if (is_guest_mode(vcpu) && max_irr_updated) {
6380 if (nested_exit_on_intr(vcpu))
6381 kvm_vcpu_exiting_guest_mode(vcpu);
6382 else
6383 kvm_make_request(KVM_REQ_EVENT, vcpu);
6384 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006385 } else {
6386 max_irr = kvm_lapic_find_highest_irr(vcpu);
6387 }
6388 vmx_hwapic_irr_update(vcpu, max_irr);
6389 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006390}
6391
Wanpeng Li17e433b2019-08-05 10:03:19 +08006392static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
6393{
Joao Martins9482ae42019-11-11 17:20:10 +00006394 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6395
6396 return pi_test_on(pi_desc) ||
Joao Martins29881b62019-11-11 17:20:12 +00006397 (pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc));
Wanpeng Li17e433b2019-08-05 10:03:19 +08006398}
6399
Andrey Smetanin63086302015-11-10 15:36:32 +03006400static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006401{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006402 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08006403 return;
6404
Yang Zhangc7c9c562013-01-25 10:18:51 +08006405 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6406 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6407 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6408 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6409}
6410
Paolo Bonzini967235d2016-12-19 14:03:45 +01006411static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
6412{
6413 struct vcpu_vmx *vmx = to_vmx(vcpu);
6414
6415 pi_clear_on(&vmx->pi_desc);
6416 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
6417}
6418
Sean Christopherson95b5a482019-04-19 22:50:59 -07006419static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006420{
Sean Christopherson87915852020-04-15 13:34:54 -07006421 u32 intr_info = vmx_get_intr_info(&vmx->vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006422
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006423 /* if exit due to PF check for async PF */
Sean Christopherson87915852020-04-15 13:34:54 -07006424 if (is_page_fault(intr_info)) {
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02006425 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags();
Andi Kleena0861c02009-06-08 17:37:09 +08006426 /* Handle machine checks before interrupts are enabled */
Sean Christopherson87915852020-04-15 13:34:54 -07006427 } else if (is_machine_check(intr_info)) {
Andi Kleena0861c02009-06-08 17:37:09 +08006428 kvm_machine_check();
Gleb Natapov20f65982009-05-11 13:35:55 +03006429 /* We need to handle NMIs before interrupts are enabled */
Sean Christopherson87915852020-04-15 13:34:54 -07006430 } else if (is_nmi(intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07006431 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03006432 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07006433 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006434 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03006435}
Gleb Natapov20f65982009-05-11 13:35:55 +03006436
Sean Christopherson95b5a482019-04-19 22:50:59 -07006437static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006438{
Sean Christopherson49def502019-04-19 22:50:56 -07006439 unsigned int vector;
6440 unsigned long entry;
6441#ifdef CONFIG_X86_64
6442 unsigned long tmp;
6443#endif
6444 gate_desc *desc;
Sean Christopherson87915852020-04-15 13:34:54 -07006445 u32 intr_info = vmx_get_intr_info(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006446
Sean Christopherson49def502019-04-19 22:50:56 -07006447 if (WARN_ONCE(!is_external_intr(intr_info),
6448 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
6449 return;
6450
6451 vector = intr_info & INTR_INFO_VECTOR_MASK;
Sean Christopherson23420802019-04-19 22:50:57 -07006452 desc = (gate_desc *)host_idt_base + vector;
Sean Christopherson49def502019-04-19 22:50:56 -07006453 entry = gate_offset(desc);
6454
Sean Christopherson165072b2019-04-19 22:50:58 -07006455 kvm_before_interrupt(vcpu);
6456
Sean Christopherson49def502019-04-19 22:50:56 -07006457 asm volatile(
Yang Zhanga547c6d2013-04-11 19:25:10 +08006458#ifdef CONFIG_X86_64
Uros Bizjak551896e2020-05-04 17:57:06 +02006459 "mov %%rsp, %[sp]\n\t"
6460 "and $-16, %%rsp\n\t"
6461 "push %[ss]\n\t"
Sean Christopherson49def502019-04-19 22:50:56 -07006462 "push %[sp]\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08006463#endif
Sean Christopherson49def502019-04-19 22:50:56 -07006464 "pushf\n\t"
Uros Bizjak551896e2020-05-04 17:57:06 +02006465 "push %[cs]\n\t"
Sean Christopherson49def502019-04-19 22:50:56 -07006466 CALL_NOSPEC
6467 :
Yang Zhanga547c6d2013-04-11 19:25:10 +08006468#ifdef CONFIG_X86_64
Sean Christopherson49def502019-04-19 22:50:56 -07006469 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08006470#endif
Sean Christopherson49def502019-04-19 22:50:56 -07006471 ASM_CALL_CONSTRAINT
6472 :
Nick Desaulniers428b8f12020-03-23 12:12:43 -07006473 [thunk_target]"r"(entry),
Uros Bizjak551896e2020-05-04 17:57:06 +02006474#ifdef CONFIG_X86_64
Sean Christopherson49def502019-04-19 22:50:56 -07006475 [ss]"i"(__KERNEL_DS),
Uros Bizjak551896e2020-05-04 17:57:06 +02006476#endif
Sean Christopherson49def502019-04-19 22:50:56 -07006477 [cs]"i"(__KERNEL_CS)
6478 );
Sean Christopherson165072b2019-04-19 22:50:58 -07006479
6480 kvm_after_interrupt(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006481}
Sean Christopherson95b5a482019-04-19 22:50:59 -07006482STACK_FRAME_NON_STANDARD(handle_external_interrupt_irqoff);
6483
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006484static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006485{
6486 struct vcpu_vmx *vmx = to_vmx(vcpu);
6487
6488 if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6489 handle_external_interrupt_irqoff(vcpu);
6490 else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)
6491 handle_exception_nmi_irqoff(vmx);
6492}
Yang Zhanga547c6d2013-04-11 19:25:10 +08006493
Sean Christophersoncb97c2d2020-02-18 15:40:11 -08006494static bool vmx_has_emulated_msr(u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006495{
Tom Lendackybc226f02018-05-10 22:06:39 +02006496 switch (index) {
6497 case MSR_IA32_SMBASE:
6498 /*
6499 * We cannot do SMM unless we can run the guest in big
6500 * real mode.
6501 */
6502 return enable_unrestricted_guest || emulate_invalid_guest_state;
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02006503 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
6504 return nested;
Tom Lendackybc226f02018-05-10 22:06:39 +02006505 case MSR_AMD64_VIRT_SPEC_CTRL:
6506 /* This is AMD only. */
6507 return false;
6508 default:
6509 return true;
6510 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006511}
6512
Avi Kivity51aa01d2010-07-20 14:31:20 +03006513static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6514{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006515 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006516 bool unblock_nmi;
6517 u8 vector;
6518 bool idtv_info_valid;
6519
6520 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006521
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006522 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006523 if (vmx->loaded_vmcs->nmi_known_unmasked)
6524 return;
Sean Christopherson87915852020-04-15 13:34:54 -07006525
6526 exit_intr_info = vmx_get_intr_info(&vmx->vcpu);
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006527 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6528 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6529 /*
6530 * SDM 3: 27.7.1.2 (September 2008)
6531 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6532 * a guest IRET fault.
6533 * SDM 3: 23.2.2 (September 2008)
6534 * Bit 12 is undefined in any of the following cases:
6535 * If the VM exit sets the valid bit in the IDT-vectoring
6536 * information field.
6537 * If the VM exit is due to a double fault.
6538 */
6539 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6540 vector != DF_VECTOR && !idtv_info_valid)
6541 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6542 GUEST_INTR_STATE_NMI);
6543 else
6544 vmx->loaded_vmcs->nmi_known_unmasked =
6545 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6546 & GUEST_INTR_STATE_NMI);
6547 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
6548 vmx->loaded_vmcs->vnmi_blocked_time +=
6549 ktime_to_ns(ktime_sub(ktime_get(),
6550 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006551}
6552
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006553static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006554 u32 idt_vectoring_info,
6555 int instr_len_field,
6556 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006557{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006558 u8 vector;
6559 int type;
6560 bool idtv_info_valid;
6561
6562 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006563
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006564 vcpu->arch.nmi_injected = false;
6565 kvm_clear_exception_queue(vcpu);
6566 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006567
6568 if (!idtv_info_valid)
6569 return;
6570
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006571 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006572
Avi Kivity668f6122008-07-02 09:28:55 +03006573 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6574 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006575
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006576 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006577 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006578 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006579 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006580 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006581 * Clear bit "block by NMI" before VM entry if a NMI
6582 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006583 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006584 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006585 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006586 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006587 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006588 /* fall through */
6589 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006590 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006591 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03006592 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006593 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03006594 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006595 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006596 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006597 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006598 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03006599 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006600 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006601 break;
6602 default:
6603 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006604 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006605}
6606
Avi Kivity83422e12010-07-20 14:43:23 +03006607static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6608{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006609 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006610 VM_EXIT_INSTRUCTION_LEN,
6611 IDT_VECTORING_ERROR_CODE);
6612}
6613
Avi Kivityb463a6f2010-07-20 15:06:17 +03006614static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6615{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006616 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006617 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6618 VM_ENTRY_INSTRUCTION_LEN,
6619 VM_ENTRY_EXCEPTION_ERROR_CODE);
6620
6621 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6622}
6623
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006624static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6625{
6626 int i, nr_msrs;
6627 struct perf_guest_switch_msr *msrs;
6628
6629 msrs = perf_guest_get_msrs(&nr_msrs);
6630
6631 if (!msrs)
6632 return;
6633
6634 for (i = 0; i < nr_msrs; i++)
6635 if (msrs[i].host == msrs[i].guest)
6636 clear_atomic_switch_msr(vmx, msrs[i].msr);
6637 else
6638 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006639 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006640}
6641
Sean Christophersonf459a702018-08-27 15:21:11 -07006642static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006643{
6644 struct vcpu_vmx *vmx = to_vmx(vcpu);
6645 u64 tscl;
6646 u32 delta_tsc;
6647
Sean Christophersond264ee02018-08-27 15:21:12 -07006648 if (vmx->req_immediate_exit) {
Sean Christopherson804939e2019-05-07 12:18:05 -07006649 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
6650 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6651 } else if (vmx->hv_deadline_tsc != -1) {
Sean Christophersonf459a702018-08-27 15:21:11 -07006652 tscl = rdtsc();
6653 if (vmx->hv_deadline_tsc > tscl)
6654 /* set_hv_timer ensures the delta fits in 32-bits */
6655 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6656 cpu_preemption_timer_multi);
6657 else
6658 delta_tsc = 0;
6659
Sean Christopherson804939e2019-05-07 12:18:05 -07006660 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
6661 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6662 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
6663 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
6664 vmx->loaded_vmcs->hv_timer_soft_disabled = true;
Sean Christophersonf459a702018-08-27 15:21:11 -07006665 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006666}
6667
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006668void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006669{
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006670 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
6671 vmx->loaded_vmcs->host_state.rsp = host_rsp;
6672 vmcs_writel(HOST_RSP, host_rsp);
6673 }
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006674}
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006675
Wanpeng Li404d5d72020-04-28 14:23:25 +08006676static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006677{
6678 switch (to_vmx(vcpu)->exit_reason) {
6679 case EXIT_REASON_MSR_WRITE:
6680 return handle_fastpath_set_msr_irqoff(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04006681 case EXIT_REASON_PREEMPTION_TIMER:
6682 return handle_fastpath_preemption_timer(vcpu);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006683 default:
6684 return EXIT_FASTPATH_NONE;
6685 }
6686}
6687
Sean Christophersonfc2ba5a2019-01-25 07:41:19 -08006688bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006689
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006690static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
6691 struct vcpu_vmx *vmx)
6692{
6693 /*
6694 * VMENTER enables interrupts (host state), but the kernel state is
6695 * interrupts disabled when this is invoked. Also tell RCU about
6696 * it. This is the same logic as for exit_to_user_mode().
6697 *
6698 * This ensures that e.g. latency analysis on the host observes
6699 * guest mode as interrupt enabled.
6700 *
6701 * guest_enter_irqoff() informs context tracking about the
6702 * transition to guest mode and if enabled adjusts RCU state
6703 * accordingly.
6704 */
6705 instrumentation_begin();
6706 trace_hardirqs_on_prepare();
6707 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
6708 instrumentation_end();
6709
6710 guest_enter_irqoff();
6711 lockdep_hardirqs_on(CALLER_ADDR0);
6712
6713 /* L1D Flush includes CPU buffer clear to mitigate MDS */
6714 if (static_branch_unlikely(&vmx_l1d_should_flush))
6715 vmx_l1d_flush(vcpu);
6716 else if (static_branch_unlikely(&mds_user_clear))
6717 mds_clear_cpu_buffers();
6718
Thomas Gleixner2245d392020-07-08 21:52:00 +02006719 if (vcpu->arch.cr2 != native_read_cr2())
6720 native_write_cr2(vcpu->arch.cr2);
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006721
6722 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
6723 vmx->loaded_vmcs->launched);
6724
Thomas Gleixner2245d392020-07-08 21:52:00 +02006725 vcpu->arch.cr2 = native_read_cr2();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006726
6727 /*
6728 * VMEXIT disables interrupts (host state), but tracing and lockdep
6729 * have them in state 'on' as recorded before entering guest mode.
6730 * Same as enter_from_user_mode().
6731 *
6732 * guest_exit_irqoff() restores host context and reinstates RCU if
6733 * enabled and required.
6734 *
6735 * This needs to be done before the below as native_read_msr()
6736 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
6737 * into world and some more.
6738 */
6739 lockdep_hardirqs_off(CALLER_ADDR0);
6740 guest_exit_irqoff();
6741
6742 instrumentation_begin();
6743 trace_hardirqs_off_finish();
6744 instrumentation_end();
6745}
6746
Wanpeng Li404d5d72020-04-28 14:23:25 +08006747static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006748{
Wanpeng Li404d5d72020-04-28 14:23:25 +08006749 fastpath_t exit_fastpath;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006750 struct vcpu_vmx *vmx = to_vmx(vcpu);
6751 unsigned long cr3, cr4;
6752
Wanpeng Li404d5d72020-04-28 14:23:25 +08006753reenter_guest:
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006754 /* Record the guest's net vcpu time for enforced NMI injections. */
6755 if (unlikely(!enable_vnmi &&
6756 vmx->loaded_vmcs->soft_vnmi_blocked))
6757 vmx->loaded_vmcs->entry_time = ktime_get();
6758
6759 /* Don't enter VMX if guest state is invalid, let the exit handler
6760 start emulation until we arrive back to a valid state */
6761 if (vmx->emulation_required)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006762 return EXIT_FASTPATH_NONE;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006763
6764 if (vmx->ple_window_dirty) {
6765 vmx->ple_window_dirty = false;
6766 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6767 }
6768
wanpeng lic9dfd3f2020-02-17 18:37:43 +08006769 /*
6770 * We did this in prepare_switch_to_guest, because it needs to
6771 * be within srcu_read_lock.
6772 */
6773 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006774
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006775 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006776 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006777 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006778 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6779
6780 cr3 = __get_current_cr3_fast();
6781 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6782 vmcs_writel(HOST_CR3, cr3);
6783 vmx->loaded_vmcs->host_state.cr3 = cr3;
6784 }
6785
6786 cr4 = cr4_read_shadow();
6787 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
6788 vmcs_writel(HOST_CR4, cr4);
6789 vmx->loaded_vmcs->host_state.cr4 = cr4;
6790 }
6791
6792 /* When single-stepping over STI and MOV SS, we must clear the
6793 * corresponding interruptibility bits in the guest state. Otherwise
6794 * vmentry fails as it then expects bit 14 (BS) in pending debug
6795 * exceptions being set, but that's not correct for the guest debugging
6796 * case. */
6797 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6798 vmx_set_interrupt_shadow(vcpu, 0);
6799
Aaron Lewis139a12c2019-10-21 16:30:25 -07006800 kvm_load_guest_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006801
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006802 pt_guest_enter(vmx);
6803
Vitaly Kuznetsov49097762020-06-19 11:40:46 +02006804 atomic_switch_perf_msrs(vmx);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006805
Sean Christopherson804939e2019-05-07 12:18:05 -07006806 if (enable_preemption_timer)
6807 vmx_update_hv_timer(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006808
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08006809 if (lapic_in_kernel(vcpu) &&
6810 vcpu->arch.apic->lapic_timer.timer_advance_ns)
6811 kvm_wait_lapic_expire(vcpu);
6812
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006813 /*
6814 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6815 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6816 * is no need to worry about the conditional branch over the wrmsr
6817 * being speculatively taken.
6818 */
6819 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
6820
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006821 /* The actual VMENTER/EXIT is in the .noinstr.text section. */
6822 vmx_vcpu_enter_exit(vcpu, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006823
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006824 /*
6825 * We do not use IBRS in the kernel. If this vCPU has used the
6826 * SPEC_CTRL MSR it may have left it on; save the value and
6827 * turn it off. This is much more efficient than blindly adding
6828 * it to the atomic save/restore list. Especially as the former
6829 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6830 *
6831 * For non-nested case:
6832 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6833 * save it.
6834 *
6835 * For nested case:
6836 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6837 * save it.
6838 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006839 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006840 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006841
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006842 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006843
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006844 /* All fields are clean at this point */
6845 if (static_branch_unlikely(&enable_evmcs))
6846 current_evmcs->hv_clean_fields |=
6847 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6848
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08006849 if (static_branch_unlikely(&enable_evmcs))
6850 current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index;
6851
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006852 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006853 if (vmx->host_debugctlmsr)
6854 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006855
Avi Kivityaa67f602012-08-01 16:48:03 +03006856#ifndef CONFIG_X86_64
6857 /*
6858 * The sysexit path does not restore ds/es, so we must set them to
6859 * a reasonable value ourselves.
6860 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006861 * We can't defer this to vmx_prepare_switch_to_host() since that
6862 * function may be executed in interrupt context, which saves and
6863 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006864 */
6865 loadsegment(ds, __USER_DS);
6866 loadsegment(es, __USER_DS);
6867#endif
6868
Sean Christophersone5d03de2020-04-15 13:34:51 -07006869 vmx_register_cache_reset(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006870
Chao Peng2ef444f2018-10-24 16:05:12 +08006871 pt_guest_exit(vmx);
6872
Aaron Lewis139a12c2019-10-21 16:30:25 -07006873 kvm_load_host_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006874
Gleb Natapove0b890d2013-09-25 12:51:33 +03006875 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006876 vmx->idt_vectoring_info = 0;
6877
Sean Christopherson873e1da2020-04-10 10:47:02 -07006878 if (unlikely(vmx->fail)) {
6879 vmx->exit_reason = 0xdead;
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006880 return EXIT_FASTPATH_NONE;
Sean Christopherson873e1da2020-04-10 10:47:02 -07006881 }
6882
6883 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
6884 if (unlikely((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY))
Sean Christophersonbeb8d932019-04-19 22:50:55 -07006885 kvm_machine_check();
6886
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006887 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
6888
Sean Christopherson873e1da2020-04-10 10:47:02 -07006889 if (unlikely(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006890 return EXIT_FASTPATH_NONE;
6891
Jim Mattsonb060ca32017-09-14 16:31:42 -07006892 vmx->loaded_vmcs->launched = 1;
6893 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03006894
Avi Kivity51aa01d2010-07-20 14:31:20 +03006895 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006896 vmx_complete_interrupts(vmx);
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006897
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006898 if (is_guest_mode(vcpu))
6899 return EXIT_FASTPATH_NONE;
6900
6901 exit_fastpath = vmx_exit_handlers_fastpath(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006902 if (exit_fastpath == EXIT_FASTPATH_REENTER_GUEST) {
6903 if (!kvm_vcpu_exit_request(vcpu)) {
6904 /*
6905 * FIXME: this goto should be a loop in vcpu_enter_guest,
6906 * but it would incur the cost of a retpoline for now.
6907 * Revisit once static calls are available.
6908 */
Wanpeng Li379a3c82020-04-28 14:23:27 +08006909 if (vcpu->arch.apicv_active)
6910 vmx_sync_pir_to_irr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006911 goto reenter_guest;
6912 }
6913 exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
6914 }
6915
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006916 return exit_fastpath;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006917}
6918
Avi Kivity6aa8b732006-12-10 02:21:36 -08006919static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6920{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006921 struct vcpu_vmx *vmx = to_vmx(vcpu);
6922
Kai Huang843e4332015-01-28 10:54:28 +08006923 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006924 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006925 free_vpid(vmx->vpid);
Sean Christopherson55d23752018-12-03 13:53:18 -08006926 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006927 free_loaded_vmcs(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006928}
6929
Sean Christopherson987b2592019-12-18 13:54:55 -08006930static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006931{
Ben Gardon41836832019-02-11 11:02:52 -08006932 struct vcpu_vmx *vmx;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006933 unsigned long *msr_bitmap;
Sean Christopherson34109c02019-12-18 13:54:50 -08006934 int i, cpu, err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006935
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006936 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0);
6937 vmx = to_vmx(vcpu);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006938
Peter Feiner4e595162016-07-07 14:49:58 -07006939 err = -ENOMEM;
6940
Sean Christopherson034d8e22019-12-18 13:54:49 -08006941 vmx->vpid = allocate_vpid();
6942
Peter Feiner4e595162016-07-07 14:49:58 -07006943 /*
6944 * If PML is turned on, failure on enabling PML just results in failure
6945 * of creating the vcpu, therefore we can simplify PML logic (by
6946 * avoiding dealing with cases, such as enabling PML partially on vcpus
Miaohe Lin67b0ae42019-12-11 14:26:22 +08006947 * for the guest), etc.
Peter Feiner4e595162016-07-07 14:49:58 -07006948 */
6949 if (enable_pml) {
Ben Gardon41836832019-02-11 11:02:52 -08006950 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Peter Feiner4e595162016-07-07 14:49:58 -07006951 if (!vmx->pml_pg)
Sean Christopherson987b2592019-12-18 13:54:55 -08006952 goto free_vpid;
Peter Feiner4e595162016-07-07 14:49:58 -07006953 }
6954
Jim Mattson7d737102019-12-03 16:24:42 -08006955 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) != NR_SHARED_MSRS);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006956
Xiaoyao Li4be53412019-10-20 17:11:00 +08006957 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6958 u32 index = vmx_msr_index[i];
6959 u32 data_low, data_high;
6960 int j = vmx->nmsrs;
6961
6962 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6963 continue;
6964 if (wrmsr_safe(index, data_low, data_high) < 0)
6965 continue;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006966
Xiaoyao Li4be53412019-10-20 17:11:00 +08006967 vmx->guest_msrs[j].index = i;
6968 vmx->guest_msrs[j].data = 0;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006969 switch (index) {
6970 case MSR_IA32_TSX_CTRL:
6971 /*
6972 * No need to pass TSX_CTRL_CPUID_CLEAR through, so
6973 * let's avoid changing CPUID bits under the host
6974 * kernel's feet.
6975 */
6976 vmx->guest_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
6977 break;
6978 default:
6979 vmx->guest_msrs[j].mask = -1ull;
6980 break;
6981 }
Xiaoyao Li4be53412019-10-20 17:11:00 +08006982 ++vmx->nmsrs;
6983 }
6984
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006985 err = alloc_loaded_vmcs(&vmx->vmcs01);
6986 if (err < 0)
Jim Mattson7d737102019-12-03 16:24:42 -08006987 goto free_pml;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006988
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006989 msr_bitmap = vmx->vmcs01.msr_bitmap;
Jim Mattson788fc1e2018-11-09 09:35:11 -08006990 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006991 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
6992 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
6993 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
6994 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6995 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6996 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
Sean Christopherson987b2592019-12-18 13:54:55 -08006997 if (kvm_cstate_in_guest(vcpu->kvm)) {
Wanpeng Lib5170062019-05-21 14:06:53 +08006998 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
6999 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
7000 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
7001 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
7002 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007003 vmx->msr_bitmap_mode = 0;
7004
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007005 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03007006 cpu = get_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08007007 vmx_vcpu_load(vcpu, cpu);
7008 vcpu->cpu = cpu;
Xiaoyao Li1b842922019-10-20 17:11:01 +08007009 init_vmcs(vmx);
Sean Christopherson34109c02019-12-18 13:54:50 -08007010 vmx_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03007011 put_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08007012 if (cpu_need_virtualize_apic_accesses(vcpu)) {
Sean Christopherson987b2592019-12-18 13:54:55 -08007013 err = alloc_apic_access_page(vcpu->kvm);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007014 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02007015 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007016 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007017
Sean Christophersone90008d2018-03-05 12:04:37 -08007018 if (enable_ept && !enable_unrestricted_guest) {
Sean Christopherson987b2592019-12-18 13:54:55 -08007019 err = init_rmode_identity_map(vcpu->kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08007020 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02007021 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08007022 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007023
Roman Kagan63aff652018-07-19 21:59:07 +03007024 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007025 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01007026 vmx_capability.ept);
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007027 else
7028 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08007029
Wincy Van705699a2015-02-03 23:58:17 +08007030 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007031 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007032
Paolo Bonzinibab0c312020-02-11 18:40:58 +01007033 vcpu->arch.microcode_version = 0x100000000ULL;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007034 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007035
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02007036 /*
7037 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
7038 * or POSTED_INTR_WAKEUP_VECTOR.
7039 */
7040 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
7041 vmx->pi_desc.sn = 1;
7042
Lan Tianyu53963a72018-12-06 15:34:36 +08007043 vmx->ept_pointer = INVALID_PAGE;
7044
Sean Christophersona9dd6f02019-12-18 13:54:52 -08007045 return 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007046
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007047free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08007048 free_loaded_vmcs(vmx->loaded_vmcs);
Peter Feiner4e595162016-07-07 14:49:58 -07007049free_pml:
7050 vmx_destroy_pml_buffer(vmx);
Sean Christopherson987b2592019-12-18 13:54:55 -08007051free_vpid:
Wanpeng Li991e7a02015-09-16 17:30:05 +08007052 free_vpid(vmx->vpid);
Sean Christophersona9dd6f02019-12-18 13:54:52 -08007053 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007054}
7055
Thomas Gleixner65fd4cb2019-02-19 11:10:49 +01007056#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"
7057#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 -04007058
Wanpeng Lib31c1142018-03-12 04:53:04 -07007059static int vmx_vm_init(struct kvm *kvm)
7060{
Tianyu Lan877ad952018-07-19 08:40:23 +00007061 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
7062
Wanpeng Lib31c1142018-03-12 04:53:04 -07007063 if (!ple_gap)
7064 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007065
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007066 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
7067 switch (l1tf_mitigation) {
7068 case L1TF_MITIGATION_OFF:
7069 case L1TF_MITIGATION_FLUSH_NOWARN:
7070 /* 'I explicitly don't care' is set */
7071 break;
7072 case L1TF_MITIGATION_FLUSH:
7073 case L1TF_MITIGATION_FLUSH_NOSMT:
7074 case L1TF_MITIGATION_FULL:
7075 /*
7076 * Warn upon starting the first VM in a potentially
7077 * insecure environment.
7078 */
Josh Poimboeufb2849092019-01-30 07:13:58 -06007079 if (sched_smt_active())
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007080 pr_warn_once(L1TF_MSG_SMT);
7081 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
7082 pr_warn_once(L1TF_MSG_L1D);
7083 break;
7084 case L1TF_MITIGATION_FULL_FORCE:
7085 /* Flush is enforced */
7086 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007087 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007088 }
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007089 kvm_apicv_init(kvm, enable_apicv);
Wanpeng Lib31c1142018-03-12 04:53:04 -07007090 return 0;
7091}
7092
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007093static int __init vmx_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03007094{
7095 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08007096 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007097
Sean Christophersonff10e222019-12-20 20:45:10 -08007098 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
7099 !this_cpu_has(X86_FEATURE_VMX)) {
7100 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id());
7101 return -EIO;
7102 }
7103
Sean Christopherson7caaa712018-12-03 13:53:01 -08007104 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007105 return -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007106 if (nested)
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01007107 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept);
Yang, Sheng002c7f72007-07-31 14:23:01 +03007108 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7109 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7110 smp_processor_id());
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007111 return -EIO;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007112 }
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007113 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007114}
7115
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007116static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007117{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007118 u8 cache;
7119 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007120
Chia-I Wu222f06e2020-02-13 13:30:34 -08007121 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in
7122 * memory aliases with conflicting memory types and sometimes MCEs.
7123 * We have to be careful as to what are honored and when.
7124 *
7125 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to
7126 * UC. The effective memory type is UC or WC depending on guest PAT.
7127 * This was historically the source of MCEs and we want to be
7128 * conservative.
7129 *
7130 * When there is no need to deal with noncoherent DMA (e.g., no VT-d
7131 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The
7132 * EPT memory type is set to WB. The effective memory type is forced
7133 * WB.
7134 *
7135 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The
7136 * EPT memory type is used to emulate guest CD/MTRR.
Sheng Yang522c68c2009-04-27 20:35:43 +08007137 */
Chia-I Wu222f06e2020-02-13 13:30:34 -08007138
Paolo Bonzini606decd2015-10-01 13:12:47 +02007139 if (is_mmio) {
7140 cache = MTRR_TYPE_UNCACHABLE;
7141 goto exit;
7142 }
7143
7144 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007145 ipat = VMX_EPT_IPAT_BIT;
7146 cache = MTRR_TYPE_WRBACK;
7147 goto exit;
7148 }
7149
7150 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
7151 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02007152 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08007153 cache = MTRR_TYPE_WRBACK;
7154 else
7155 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007156 goto exit;
7157 }
7158
Xiao Guangrongff536042015-06-15 16:55:22 +08007159 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007160
7161exit:
7162 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08007163}
7164
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007165static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007166{
7167 /*
7168 * These bits in the secondary execution controls field
7169 * are dynamic, the others are mostly based on the hypervisor
7170 * architecture and the guest's CPUID. Do not touch the
7171 * dynamic bits.
7172 */
7173 u32 mask =
7174 SECONDARY_EXEC_SHADOW_VMCS |
7175 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02007176 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7177 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007178
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007179 u32 new_ctl = vmx->secondary_exec_control;
7180 u32 cur_ctl = secondary_exec_controls_get(vmx);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007181
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007182 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007183}
7184
David Matlack8322ebb2016-11-29 18:14:09 -08007185/*
7186 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
7187 * (indicating "allowed-1") if they are supported in the guest's CPUID.
7188 */
7189static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
7190{
7191 struct vcpu_vmx *vmx = to_vmx(vcpu);
7192 struct kvm_cpuid_entry2 *entry;
7193
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007194 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
7195 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08007196
7197#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
7198 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007199 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08007200} while (0)
7201
7202 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007203 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME));
7204 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME));
7205 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC));
7206 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE));
7207 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE));
7208 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE));
7209 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE));
7210 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE));
7211 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR));
7212 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM));
7213 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX));
7214 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX));
7215 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID));
7216 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE));
David Matlack8322ebb2016-11-29 18:14:09 -08007217
7218 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007219 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE));
7220 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP));
7221 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP));
7222 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU));
7223 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP));
7224 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57));
David Matlack8322ebb2016-11-29 18:14:09 -08007225
7226#undef cr4_fixed1_update
7227}
7228
Liran Alon5f76f6f2018-09-14 03:25:52 +03007229static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
7230{
7231 struct vcpu_vmx *vmx = to_vmx(vcpu);
7232
7233 if (kvm_mpx_supported()) {
7234 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
7235
7236 if (mpx_enabled) {
7237 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7238 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7239 } else {
7240 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7241 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7242 }
7243 }
7244}
7245
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007246static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7247{
7248 struct vcpu_vmx *vmx = to_vmx(vcpu);
7249 struct kvm_cpuid_entry2 *best = NULL;
7250 int i;
7251
7252 for (i = 0; i < PT_CPUID_LEAVES; i++) {
7253 best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7254 if (!best)
7255 return;
7256 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7257 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7258 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7259 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7260 }
7261
7262 /* Get the number of configurable Address Ranges for filtering */
7263 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7264 PT_CAP_num_address_ranges);
7265
7266 /* Initialize and clear the no dependency bits */
7267 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7268 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7269
7270 /*
7271 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7272 * will inject an #GP
7273 */
7274 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7275 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7276
7277 /*
7278 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7279 * PSBFreq can be set
7280 */
7281 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7282 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7283 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7284
7285 /*
7286 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7287 * MTCFreq can be set
7288 */
7289 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7290 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7291 RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7292
7293 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7294 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7295 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7296 RTIT_CTL_PTW_EN);
7297
7298 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7299 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7300 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7301
7302 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7303 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7304 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7305
7306 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */
7307 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7308 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7309
7310 /* unmask address range configure area */
7311 for (i = 0; i < vmx->pt_desc.addr_range; i++)
Gustavo A. R. Silvad14eff12018-12-26 14:40:59 -06007312 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007313}
7314
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007315static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08007316{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007317 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007318
Aaron Lewis72041602019-10-21 16:30:20 -07007319 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7320 vcpu->arch.xsaves_enabled = false;
7321
Paolo Bonzini80154d72017-08-24 13:55:35 +02007322 if (cpu_has_secondary_exec_ctrls()) {
7323 vmx_compute_secondary_exec_control(vmx);
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007324 vmcs_set_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007325 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007326
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007327 if (nested_vmx_allowed(vcpu))
7328 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007329 FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7330 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007331 else
7332 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007333 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7334 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX);
David Matlack8322ebb2016-11-29 18:14:09 -08007335
Liran Alon5f76f6f2018-09-14 03:25:52 +03007336 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08007337 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03007338 nested_vmx_entry_exit_ctls_update(vcpu);
7339 }
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007340
7341 if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7342 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7343 update_intel_pt_cfg(vcpu);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007344
7345 if (boot_cpu_has(X86_FEATURE_RTM)) {
7346 struct shared_msr_entry *msr;
7347 msr = find_msr_entry(vmx, MSR_IA32_TSX_CTRL);
7348 if (msr) {
7349 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
7350 vmx_set_guest_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
7351 }
7352 }
Sheng Yang0e851882009-12-18 16:48:46 +08007353}
7354
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007355static __init void vmx_set_cpu_caps(void)
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007356{
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007357 kvm_set_cpu_caps();
7358
7359 /* CPUID 0x1 */
7360 if (nested)
7361 kvm_cpu_cap_set(X86_FEATURE_VMX);
7362
7363 /* CPUID 0x7 */
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007364 if (kvm_mpx_supported())
7365 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX);
7366 if (cpu_has_vmx_invpcid())
7367 kvm_cpu_cap_check_and_set(X86_FEATURE_INVPCID);
7368 if (vmx_pt_mode_is_host_guest())
7369 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT);
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007370
Sean Christopherson90d2f602020-03-02 15:56:47 -08007371 if (vmx_umip_emulated())
7372 kvm_cpu_cap_set(X86_FEATURE_UMIP);
7373
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007374 /* CPUID 0xD.1 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01007375 supported_xss = 0;
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007376 if (!vmx_xsaves_supported())
7377 kvm_cpu_cap_clear(X86_FEATURE_XSAVES);
7378
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007379 /* CPUID 0x80000001 */
7380 if (!cpu_has_vmx_rdtscp())
7381 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007382
7383 if (vmx_waitpkg_supported())
7384 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007385}
7386
Sean Christophersond264ee02018-08-27 15:21:12 -07007387static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7388{
7389 to_vmx(vcpu)->req_immediate_exit = true;
7390}
7391
Oliver Upton35a57132020-02-04 15:26:31 -08007392static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
7393 struct x86_instruction_info *info)
7394{
7395 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7396 unsigned short port;
7397 bool intercept;
7398 int size;
7399
7400 if (info->intercept == x86_intercept_in ||
7401 info->intercept == x86_intercept_ins) {
7402 port = info->src_val;
7403 size = info->dst_bytes;
7404 } else {
7405 port = info->dst_val;
7406 size = info->src_bytes;
7407 }
7408
7409 /*
7410 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
7411 * VM-exits depend on the 'unconditional IO exiting' VM-execution
7412 * control.
7413 *
7414 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
7415 */
7416 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7417 intercept = nested_cpu_has(vmcs12,
7418 CPU_BASED_UNCOND_IO_EXITING);
7419 else
7420 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
7421
Oliver Upton86f7e902020-02-29 11:30:14 -08007422 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
Oliver Upton35a57132020-02-04 15:26:31 -08007423 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
7424}
7425
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007426static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7427 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007428 enum x86_intercept_stage stage,
7429 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007430{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007431 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007432
Oliver Upton35a57132020-02-04 15:26:31 -08007433 switch (info->intercept) {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007434 /*
7435 * RDPID causes #UD if disabled through secondary execution controls.
7436 * Because it is marked as EmulateOnUD, we need to intercept it here.
7437 */
Oliver Upton35a57132020-02-04 15:26:31 -08007438 case x86_intercept_rdtscp:
7439 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007440 exception->vector = UD_VECTOR;
7441 exception->error_code_valid = false;
Oliver Upton35a57132020-02-04 15:26:31 -08007442 return X86EMUL_PROPAGATE_FAULT;
7443 }
7444 break;
7445
7446 case x86_intercept_in:
7447 case x86_intercept_ins:
7448 case x86_intercept_out:
7449 case x86_intercept_outs:
7450 return vmx_check_intercept_io(vcpu, info);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007451
Oliver Upton86f7e902020-02-29 11:30:14 -08007452 case x86_intercept_lgdt:
7453 case x86_intercept_lidt:
7454 case x86_intercept_lldt:
7455 case x86_intercept_ltr:
7456 case x86_intercept_sgdt:
7457 case x86_intercept_sidt:
7458 case x86_intercept_sldt:
7459 case x86_intercept_str:
7460 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
7461 return X86EMUL_CONTINUE;
7462
7463 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
7464 break;
7465
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007466 /* TODO: check more intercepts... */
Oliver Upton35a57132020-02-04 15:26:31 -08007467 default:
7468 break;
7469 }
7470
Paolo Bonzini07721fe2020-02-04 15:26:29 -08007471 return X86EMUL_UNHANDLEABLE;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007472}
7473
Yunhong Jiang64672c92016-06-13 14:19:59 -07007474#ifdef CONFIG_X86_64
7475/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
7476static inline int u64_shl_div_u64(u64 a, unsigned int shift,
7477 u64 divisor, u64 *result)
7478{
7479 u64 low = a << shift, high = a >> (64 - shift);
7480
7481 /* To avoid the overflow on divq */
7482 if (high >= divisor)
7483 return 1;
7484
7485 /* Low hold the result, high hold rem which is discarded */
7486 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
7487 "rm" (divisor), "0" (low), "1" (high));
7488 *result = low;
7489
7490 return 0;
7491}
7492
Sean Christophersonf9927982019-04-16 13:32:46 -07007493static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
7494 bool *expired)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007495{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007496 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007497 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
Sean Christopherson39497d72019-04-17 10:15:32 -07007498 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007499
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007500 vmx = to_vmx(vcpu);
7501 tscl = rdtsc();
7502 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
7503 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Sean Christopherson39497d72019-04-17 10:15:32 -07007504 lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
7505 ktimer->timer_advance_ns);
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007506
7507 if (delta_tsc > lapic_timer_advance_cycles)
7508 delta_tsc -= lapic_timer_advance_cycles;
7509 else
7510 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007511
7512 /* Convert to host delta tsc if tsc scaling is enabled */
7513 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
Sean Christopherson0967fa12019-04-16 13:32:48 -07007514 delta_tsc && u64_shl_div_u64(delta_tsc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007515 kvm_tsc_scaling_ratio_frac_bits,
Sean Christopherson0967fa12019-04-16 13:32:48 -07007516 vcpu->arch.tsc_scaling_ratio, &delta_tsc))
Yunhong Jiang64672c92016-06-13 14:19:59 -07007517 return -ERANGE;
7518
7519 /*
7520 * If the delta tsc can't fit in the 32 bit after the multi shift,
7521 * we can't use the preemption timer.
7522 * It's possible that it fits on later vmentries, but checking
7523 * on every vmentry is costly so we just use an hrtimer.
7524 */
7525 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
7526 return -ERANGE;
7527
7528 vmx->hv_deadline_tsc = tscl + delta_tsc;
Sean Christophersonf9927982019-04-16 13:32:46 -07007529 *expired = !delta_tsc;
7530 return 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007531}
7532
7533static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
7534{
Sean Christophersonf459a702018-08-27 15:21:11 -07007535 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007536}
7537#endif
7538
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007539static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007540{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007541 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007542 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007543}
7544
Kai Huang843e4332015-01-28 10:54:28 +08007545static void vmx_slot_enable_log_dirty(struct kvm *kvm,
7546 struct kvm_memory_slot *slot)
7547{
Jay Zhou3c9bd402020-02-27 09:32:27 +08007548 if (!kvm_dirty_log_manual_protect_and_init_set(kvm))
7549 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
Kai Huang843e4332015-01-28 10:54:28 +08007550 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
7551}
7552
7553static void vmx_slot_disable_log_dirty(struct kvm *kvm,
7554 struct kvm_memory_slot *slot)
7555{
7556 kvm_mmu_slot_set_dirty(kvm, slot);
7557}
7558
7559static void vmx_flush_log_dirty(struct kvm *kvm)
7560{
7561 kvm_flush_pml_buffers(kvm);
7562}
7563
7564static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
7565 struct kvm_memory_slot *memslot,
7566 gfn_t offset, unsigned long mask)
7567{
7568 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
7569}
7570
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007571static void __pi_post_block(struct kvm_vcpu *vcpu)
7572{
7573 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7574 struct pi_desc old, new;
7575 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007576
7577 do {
7578 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007579 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
7580 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007581
7582 dest = cpu_physical_id(vcpu->cpu);
7583
7584 if (x2apic_enabled())
7585 new.ndst = dest;
7586 else
7587 new.ndst = (dest << 8) & 0xFF00;
7588
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007589 /* set 'NV' to 'notification vector' */
7590 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02007591 } while (cmpxchg64(&pi_desc->control, old.control,
7592 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007593
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007594 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
7595 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007596 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007597 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007598 vcpu->pre_pcpu = -1;
7599 }
7600}
7601
Feng Wuefc64402015-09-18 22:29:51 +08007602/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007603 * This routine does the following things for vCPU which is going
7604 * to be blocked if VT-d PI is enabled.
7605 * - Store the vCPU to the wakeup list, so when interrupts happen
7606 * we can find the right vCPU to wake up.
7607 * - Change the Posted-interrupt descriptor as below:
7608 * 'NDST' <-- vcpu->pre_pcpu
7609 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
7610 * - If 'ON' is set during this process, which means at least one
7611 * interrupt is posted for this vCPU, we cannot block it, in
7612 * this case, return 1, otherwise, return 0.
7613 *
7614 */
Yunhong Jiangbc225122016-06-13 14:19:58 -07007615static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08007616{
Feng Wubf9f6ac2015-09-18 22:29:55 +08007617 unsigned int dest;
7618 struct pi_desc old, new;
7619 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7620
7621 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08007622 !irq_remapping_cap(IRQ_POSTING_CAP) ||
7623 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +08007624 return 0;
7625
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007626 WARN_ON(irqs_disabled());
7627 local_irq_disable();
7628 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
7629 vcpu->pre_pcpu = vcpu->cpu;
7630 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
7631 list_add_tail(&vcpu->blocked_vcpu_list,
7632 &per_cpu(blocked_vcpu_on_cpu,
7633 vcpu->pre_pcpu));
7634 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
7635 }
Feng Wubf9f6ac2015-09-18 22:29:55 +08007636
7637 do {
7638 old.control = new.control = pi_desc->control;
7639
Feng Wubf9f6ac2015-09-18 22:29:55 +08007640 WARN((pi_desc->sn == 1),
7641 "Warning: SN field of posted-interrupts "
7642 "is set before blocking\n");
7643
7644 /*
7645 * Since vCPU can be preempted during this process,
7646 * vcpu->cpu could be different with pre_pcpu, we
7647 * need to set pre_pcpu as the destination of wakeup
7648 * notification event, then we can find the right vCPU
7649 * to wakeup in wakeup handler if interrupts happen
7650 * when the vCPU is in blocked state.
7651 */
7652 dest = cpu_physical_id(vcpu->pre_pcpu);
7653
7654 if (x2apic_enabled())
7655 new.ndst = dest;
7656 else
7657 new.ndst = (dest << 8) & 0xFF00;
7658
7659 /* set 'NV' to 'wakeup vector' */
7660 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02007661 } while (cmpxchg64(&pi_desc->control, old.control,
7662 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007663
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007664 /* We should not block the vCPU if an interrupt is posted for it. */
7665 if (pi_test_on(pi_desc) == 1)
7666 __pi_post_block(vcpu);
7667
7668 local_irq_enable();
7669 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007670}
7671
Yunhong Jiangbc225122016-06-13 14:19:58 -07007672static int vmx_pre_block(struct kvm_vcpu *vcpu)
7673{
7674 if (pi_pre_block(vcpu))
7675 return 1;
7676
Yunhong Jiang64672c92016-06-13 14:19:59 -07007677 if (kvm_lapic_hv_timer_in_use(vcpu))
7678 kvm_lapic_switch_to_sw_timer(vcpu);
7679
Yunhong Jiangbc225122016-06-13 14:19:58 -07007680 return 0;
7681}
7682
7683static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08007684{
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007685 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +08007686 return;
7687
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007688 WARN_ON(irqs_disabled());
7689 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007690 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007691 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +08007692}
7693
Yunhong Jiangbc225122016-06-13 14:19:58 -07007694static void vmx_post_block(struct kvm_vcpu *vcpu)
7695{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007696 if (kvm_x86_ops.set_hv_timer)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007697 kvm_lapic_switch_to_hv_timer(vcpu);
7698
Yunhong Jiangbc225122016-06-13 14:19:58 -07007699 pi_post_block(vcpu);
7700}
7701
Feng Wubf9f6ac2015-09-18 22:29:55 +08007702/*
Feng Wuefc64402015-09-18 22:29:51 +08007703 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
7704 *
7705 * @kvm: kvm
7706 * @host_irq: host irq of the interrupt
7707 * @guest_irq: gsi of the interrupt
7708 * @set: set or unset PI
7709 * returns 0 on success, < 0 on failure
7710 */
7711static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
7712 uint32_t guest_irq, bool set)
7713{
7714 struct kvm_kernel_irq_routing_entry *e;
7715 struct kvm_irq_routing_table *irq_rt;
7716 struct kvm_lapic_irq irq;
7717 struct kvm_vcpu *vcpu;
7718 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01007719 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +08007720
7721 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08007722 !irq_remapping_cap(IRQ_POSTING_CAP) ||
7723 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +08007724 return 0;
7725
7726 idx = srcu_read_lock(&kvm->irq_srcu);
7727 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01007728 if (guest_irq >= irq_rt->nr_rt_entries ||
7729 hlist_empty(&irq_rt->map[guest_irq])) {
7730 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
7731 guest_irq, irq_rt->nr_rt_entries);
7732 goto out;
7733 }
Feng Wuefc64402015-09-18 22:29:51 +08007734
7735 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
7736 if (e->type != KVM_IRQ_ROUTING_MSI)
7737 continue;
7738 /*
7739 * VT-d PI cannot support posting multicast/broadcast
7740 * interrupts to a vCPU, we still use interrupt remapping
7741 * for these kind of interrupts.
7742 *
7743 * For lowest-priority interrupts, we only support
7744 * those with single CPU as the destination, e.g. user
7745 * configures the interrupts via /proc/irq or uses
7746 * irqbalance to make the interrupts single-CPU.
7747 *
7748 * We will support full lowest-priority interrupt later.
Alexander Graffdcf7562019-09-05 14:58:18 +02007749 *
7750 * In addition, we can only inject generic interrupts using
7751 * the PI mechanism, refuse to route others through it.
Feng Wuefc64402015-09-18 22:29:51 +08007752 */
7753
Radim Krčmář371313132016-07-12 22:09:27 +02007754 kvm_set_msi_irq(kvm, e, &irq);
Alexander Graffdcf7562019-09-05 14:58:18 +02007755 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
7756 !kvm_irq_is_postable(&irq)) {
Feng Wu23a1c252016-01-25 16:53:32 +08007757 /*
7758 * Make sure the IRTE is in remapped mode if
7759 * we don't handle it in posted mode.
7760 */
7761 ret = irq_set_vcpu_affinity(host_irq, NULL);
7762 if (ret < 0) {
7763 printk(KERN_INFO
7764 "failed to back to remapped mode, irq: %u\n",
7765 host_irq);
7766 goto out;
7767 }
7768
Feng Wuefc64402015-09-18 22:29:51 +08007769 continue;
Feng Wu23a1c252016-01-25 16:53:32 +08007770 }
Feng Wuefc64402015-09-18 22:29:51 +08007771
7772 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
7773 vcpu_info.vector = irq.vector;
7774
hu huajun2698d822018-04-11 15:16:40 +08007775 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +08007776 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
7777
7778 if (set)
7779 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +08007780 else
Feng Wuefc64402015-09-18 22:29:51 +08007781 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +08007782
7783 if (ret < 0) {
7784 printk(KERN_INFO "%s: failed to update PI IRTE\n",
7785 __func__);
7786 goto out;
7787 }
7788 }
7789
7790 ret = 0;
7791out:
7792 srcu_read_unlock(&kvm->irq_srcu, idx);
7793 return ret;
7794}
7795
Ashok Rajc45dcc72016-06-22 14:59:56 +08007796static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7797{
7798 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7799 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007800 FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007801 else
7802 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007803 ~FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007804}
7805
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007806static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Ladi Prosek72d7b372017-10-11 16:54:41 +02007807{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007808 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7809 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007810 return -EBUSY;
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -04007811 return !is_smm(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02007812}
7813
Ladi Prosek0234bf82017-10-11 16:54:40 +02007814static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7815{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007816 struct vcpu_vmx *vmx = to_vmx(vcpu);
7817
7818 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7819 if (vmx->nested.smm.guest_mode)
7820 nested_vmx_vmexit(vcpu, -1, 0, 0);
7821
7822 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7823 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007824 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007825 return 0;
7826}
7827
Sean Christophersoned193212019-04-02 08:03:09 -07007828static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007829{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007830 struct vcpu_vmx *vmx = to_vmx(vcpu);
7831 int ret;
7832
7833 if (vmx->nested.smm.vmxon) {
7834 vmx->nested.vmxon = true;
7835 vmx->nested.smm.vmxon = false;
7836 }
7837
7838 if (vmx->nested.smm.guest_mode) {
Sean Christophersona633e412018-09-26 09:23:47 -07007839 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007840 if (ret)
7841 return ret;
7842
7843 vmx->nested.smm.guest_mode = false;
7844 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007845 return 0;
7846}
7847
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007848static void enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007849{
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007850 /* RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007851}
7852
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007853static bool vmx_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
7854{
Yi Wang9481b7f2019-07-15 12:35:17 +08007855 return false;
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007856}
7857
Liran Alon4b9852f2019-08-26 13:24:49 +03007858static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7859{
7860 return to_vmx(vcpu)->nested.vmxon;
7861}
7862
Jim Mattson93dff2f2020-05-08 13:36:43 -07007863static void vmx_migrate_timers(struct kvm_vcpu *vcpu)
7864{
7865 if (is_guest_mode(vcpu)) {
7866 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer;
7867
7868 if (hrtimer_try_to_cancel(timer) == 1)
7869 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
7870 }
7871}
7872
Sean Christopherson6e4fd062020-03-21 13:26:01 -07007873static void hardware_unsetup(void)
Sean Christophersona3203382018-12-03 13:53:11 -08007874{
7875 if (nested)
7876 nested_vmx_hardware_unsetup();
7877
7878 free_kvm_area();
7879}
7880
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007881static bool vmx_check_apicv_inhibit_reasons(ulong bit)
7882{
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -06007883 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
7884 BIT(APICV_INHIBIT_REASON_HYPERV);
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007885
7886 return supported & BIT(bit);
7887}
7888
Sean Christophersone286ac02020-03-21 13:26:02 -07007889static struct kvm_x86_ops vmx_x86_ops __initdata = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007890 .hardware_unsetup = hardware_unsetup,
Sean Christopherson484014f2020-03-21 13:25:57 -07007891
Avi Kivity6aa8b732006-12-10 02:21:36 -08007892 .hardware_enable = hardware_enable,
7893 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007894 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007895 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007896
Sean Christopherson484014f2020-03-21 13:25:57 -07007897 .vm_size = sizeof(struct kvm_vmx),
Wanpeng Lib31c1142018-03-12 04:53:04 -07007898 .vm_init = vmx_vm_init,
7899
Avi Kivity6aa8b732006-12-10 02:21:36 -08007900 .vcpu_create = vmx_create_vcpu,
7901 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007902 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007903
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007904 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007905 .vcpu_load = vmx_vcpu_load,
7906 .vcpu_put = vmx_vcpu_put,
7907
Paolo Bonzini69869822020-07-10 17:48:06 +02007908 .update_exception_bitmap = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007909 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007910 .get_msr = vmx_get_msr,
7911 .set_msr = vmx_set_msr,
7912 .get_segment_base = vmx_get_segment_base,
7913 .get_segment = vmx_get_segment,
7914 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007915 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007916 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
7917 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007918 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007919 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007920 .get_idt = vmx_get_idt,
7921 .set_idt = vmx_set_idt,
7922 .get_gdt = vmx_get_gdt,
7923 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007924 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007925 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007926 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007927 .get_rflags = vmx_get_rflags,
7928 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007929
Sean Christopherson77809382020-03-20 14:28:18 -07007930 .tlb_flush_all = vmx_flush_tlb_all,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07007931 .tlb_flush_current = vmx_flush_tlb_current,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007932 .tlb_flush_gva = vmx_flush_tlb_gva,
Sean Christophersone64419d2020-03-20 14:28:10 -07007933 .tlb_flush_guest = vmx_flush_tlb_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007934
Avi Kivity6aa8b732006-12-10 02:21:36 -08007935 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007936 .handle_exit = vmx_handle_exit,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08007937 .skip_emulated_instruction = vmx_skip_emulated_instruction,
7938 .update_emulated_instruction = vmx_update_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007939 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7940 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007941 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007942 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007943 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007944 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007945 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007946 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007947 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007948 .get_nmi_mask = vmx_get_nmi_mask,
7949 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007950 .enable_nmi_window = enable_nmi_window,
7951 .enable_irq_window = enable_irq_window,
7952 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007953 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007954 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007955 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007956 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007957 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007958 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007959 .hwapic_irr_update = vmx_hwapic_irr_update,
7960 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007961 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007962 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7963 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Wanpeng Li17e433b2019-08-05 10:03:19 +08007964 .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007965
Izik Eiduscbc94022007-10-25 00:29:55 +02007966 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007967 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007968 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007969
Avi Kivity586f9602010-11-18 13:09:54 +02007970 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007971
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007972 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007973
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007974 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007975
Leonid Shatz326e7422018-11-06 12:14:25 +02007976 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007977
Sean Christopherson484014f2020-03-21 13:25:57 -07007978 .load_mmu_pgd = vmx_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007979
7980 .check_intercept = vmx_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007981 .handle_exit_irqoff = vmx_handle_exit_irqoff,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007982
Sean Christophersond264ee02018-08-27 15:21:12 -07007983 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007984
7985 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007986
7987 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
7988 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
7989 .flush_log_dirty = vmx_flush_log_dirty,
7990 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +02007991
Feng Wubf9f6ac2015-09-18 22:29:55 +08007992 .pre_block = vmx_pre_block,
7993 .post_block = vmx_post_block,
7994
Wei Huang25462f72015-06-19 15:45:05 +02007995 .pmu_ops = &intel_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04007996 .nested_ops = &vmx_nested_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007997
7998 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007999
8000#ifdef CONFIG_X86_64
8001 .set_hv_timer = vmx_set_hv_timer,
8002 .cancel_hv_timer = vmx_cancel_hv_timer,
8003#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08008004
8005 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02008006
Ladi Prosek72d7b372017-10-11 16:54:41 +02008007 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02008008 .pre_enter_smm = vmx_pre_enter_smm,
8009 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008010 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02008011
Singh, Brijesh05d5a482019-02-15 17:24:12 +00008012 .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
Liran Alon4b9852f2019-08-26 13:24:49 +03008013 .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
Jim Mattson93dff2f2020-05-08 13:36:43 -07008014 .migrate_timers = vmx_migrate_timers,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008015};
8016
Avi Kivity6aa8b732006-12-10 02:21:36 -08008017static __init int hardware_setup(void)
8018{
8019 unsigned long host_bndcfgs;
8020 struct desc_ptr dt;
Sean Christopherson703c3352020-03-02 15:57:03 -08008021 int r, i, ept_lpage_level;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008022
Avi Kivity6aa8b732006-12-10 02:21:36 -08008023 store_idt(&dt);
8024 host_idt_base = dt.address;
8025
8026 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
8027 kvm_define_shared_msr(i, vmx_msr_index[i]);
8028
8029 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
8030 return -EIO;
8031
8032 if (boot_cpu_has(X86_FEATURE_NX))
8033 kvm_enable_efer_bits(EFER_NX);
8034
8035 if (boot_cpu_has(X86_FEATURE_MPX)) {
8036 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
8037 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
8038 }
8039
Sean Christopherson7f5581f2020-03-02 15:56:24 -08008040 if (!cpu_has_vmx_mpx())
Sean Christophersoncfc48182020-03-02 15:56:23 -08008041 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
8042 XFEATURE_MASK_BNDCSR);
8043
Avi Kivity6aa8b732006-12-10 02:21:36 -08008044 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
8045 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
8046 enable_vpid = 0;
8047
8048 if (!cpu_has_vmx_ept() ||
8049 !cpu_has_vmx_ept_4levels() ||
8050 !cpu_has_vmx_ept_mt_wb() ||
8051 !cpu_has_vmx_invept_global())
8052 enable_ept = 0;
8053
8054 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
8055 enable_ept_ad_bits = 0;
8056
8057 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Avi Kivity873a7c42006-12-13 00:34:14 -08008058 enable_unrestricted_guest = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008059
8060 if (!cpu_has_vmx_flexpriority())
8061 flexpriority_enabled = 0;
8062
8063 if (!cpu_has_virtual_nmis())
8064 enable_vnmi = 0;
8065
8066 /*
8067 * set_apic_access_page_addr() is used to reload apic access
8068 * page upon invalidation. No need to do anything if not
8069 * using the APIC_ACCESS_ADDR VMCS field.
8070 */
8071 if (!flexpriority_enabled)
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008072 vmx_x86_ops.set_apic_access_page_addr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008073
8074 if (!cpu_has_vmx_tpr_shadow())
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008075 vmx_x86_ops.update_cr8_intercept = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008076
Avi Kivity6aa8b732006-12-10 02:21:36 -08008077#if IS_ENABLED(CONFIG_HYPERV)
8078 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
8079 && enable_ept) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008080 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
8081 vmx_x86_ops.tlb_remote_flush_with_range =
Avi Kivity6aa8b732006-12-10 02:21:36 -08008082 hv_remote_flush_tlb_with_range;
8083 }
8084#endif
8085
8086 if (!cpu_has_vmx_ple()) {
8087 ple_gap = 0;
8088 ple_window = 0;
8089 ple_window_grow = 0;
8090 ple_window_max = 0;
8091 ple_window_shrink = 0;
8092 }
8093
8094 if (!cpu_has_vmx_apicv()) {
8095 enable_apicv = 0;
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008096 vmx_x86_ops.sync_pir_to_irr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008097 }
8098
8099 if (cpu_has_vmx_tsc_scaling()) {
8100 kvm_has_tsc_control = true;
8101 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
8102 kvm_tsc_scaling_ratio_frac_bits = 48;
8103 }
8104
8105 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8106
8107 if (enable_ept)
8108 vmx_enable_tdp();
Sean Christopherson703c3352020-03-02 15:57:03 -08008109
8110 if (!enable_ept)
8111 ept_lpage_level = 0;
8112 else if (cpu_has_vmx_ept_1g_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07008113 ept_lpage_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08008114 else if (cpu_has_vmx_ept_2m_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07008115 ept_lpage_level = PG_LEVEL_2M;
Sean Christopherson703c3352020-03-02 15:57:03 -08008116 else
Sean Christopherson3bae0452020-04-27 17:54:22 -07008117 ept_lpage_level = PG_LEVEL_4K;
Sean Christopherson83013052020-07-15 20:41:22 -07008118 kvm_configure_mmu(enable_ept, vmx_get_max_tdp_level(), ept_lpage_level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008119
8120 /*
8121 * Only enable PML when hardware supports PML feature, and both EPT
8122 * and EPT A/D bit features are enabled -- PML depends on them to work.
8123 */
8124 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
8125 enable_pml = 0;
8126
8127 if (!enable_pml) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008128 vmx_x86_ops.slot_enable_log_dirty = NULL;
8129 vmx_x86_ops.slot_disable_log_dirty = NULL;
8130 vmx_x86_ops.flush_log_dirty = NULL;
8131 vmx_x86_ops.enable_log_dirty_pt_masked = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008132 }
8133
8134 if (!cpu_has_vmx_preemption_timer())
8135 enable_preemption_timer = false;
8136
8137 if (enable_preemption_timer) {
8138 u64 use_timer_freq = 5000ULL * 1000 * 1000;
8139 u64 vmx_msr;
8140
8141 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8142 cpu_preemption_timer_multi =
8143 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8144
8145 if (tsc_khz)
8146 use_timer_freq = (u64)tsc_khz * 1000;
8147 use_timer_freq >>= cpu_preemption_timer_multi;
8148
8149 /*
8150 * KVM "disables" the preemption timer by setting it to its max
8151 * value. Don't use the timer if it might cause spurious exits
8152 * at a rate faster than 0.1 Hz (of uninterrupted guest time).
8153 */
8154 if (use_timer_freq > 0xffffffffu / 10)
8155 enable_preemption_timer = false;
8156 }
8157
8158 if (!enable_preemption_timer) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008159 vmx_x86_ops.set_hv_timer = NULL;
8160 vmx_x86_ops.cancel_hv_timer = NULL;
8161 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008162 }
8163
8164 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
8165
8166 kvm_mce_cap_supported |= MCG_LMCE_P;
8167
8168 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
8169 return -EINVAL;
8170 if (!enable_ept || !cpu_has_vmx_intel_pt())
8171 pt_mode = PT_MODE_SYSTEM;
8172
8173 if (nested) {
8174 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
8175 vmx_capability.ept);
8176
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07008177 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008178 if (r)
8179 return r;
8180 }
8181
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08008182 vmx_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08008183
Avi Kivity6aa8b732006-12-10 02:21:36 -08008184 r = alloc_kvm_area();
8185 if (r)
8186 nested_vmx_hardware_unsetup();
8187 return r;
8188}
8189
Sean Christophersond008dfd2020-03-21 13:25:56 -07008190static struct kvm_x86_init_ops vmx_init_ops __initdata = {
8191 .cpu_has_kvm_support = cpu_has_kvm_support,
8192 .disabled_by_bios = vmx_disabled_by_bios,
8193 .check_processor_compatibility = vmx_check_processor_compat,
8194 .hardware_setup = hardware_setup,
8195
8196 .runtime_ops = &vmx_x86_ops,
8197};
8198
Avi Kivity6aa8b732006-12-10 02:21:36 -08008199static void vmx_cleanup_l1d_flush(void)
8200{
8201 if (vmx_l1d_flush_pages) {
8202 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
8203 vmx_l1d_flush_pages = NULL;
8204 }
8205 /* Restore state so sysfs ignores VMX */
8206 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
8207}
8208
8209static void vmx_exit(void)
8210{
8211#ifdef CONFIG_KEXEC_CORE
8212 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
8213 synchronize_rcu();
8214#endif
8215
8216 kvm_exit();
8217
8218#if IS_ENABLED(CONFIG_HYPERV)
8219 if (static_branch_unlikely(&enable_evmcs)) {
8220 int cpu;
8221 struct hv_vp_assist_page *vp_ap;
8222 /*
8223 * Reset everything to support using non-enlightened VMCS
8224 * access later (e.g. when we reload the module with
8225 * enlightened_vmcs=0)
8226 */
8227 for_each_online_cpu(cpu) {
8228 vp_ap = hv_get_vp_assist_page(cpu);
8229
8230 if (!vp_ap)
8231 continue;
8232
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008233 vp_ap->nested_control.features.directhypercall = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008234 vp_ap->current_nested_vmcs = 0;
8235 vp_ap->enlighten_vmentry = 0;
8236 }
8237
8238 static_branch_disable(&enable_evmcs);
8239 }
8240#endif
8241 vmx_cleanup_l1d_flush();
8242}
8243module_exit(vmx_exit);
8244
8245static int __init vmx_init(void)
8246{
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008247 int r, cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008248
8249#if IS_ENABLED(CONFIG_HYPERV)
8250 /*
8251 * Enlightened VMCS usage should be recommended and the host needs
8252 * to support eVMCS v1 or above. We can also disable eVMCS support
8253 * with module parameter.
8254 */
8255 if (enlightened_vmcs &&
8256 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
8257 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
8258 KVM_EVMCS_VERSION) {
8259 int cpu;
8260
8261 /* Check that we have assist pages on all online CPUs */
8262 for_each_online_cpu(cpu) {
8263 if (!hv_get_vp_assist_page(cpu)) {
8264 enlightened_vmcs = false;
8265 break;
8266 }
8267 }
8268
8269 if (enlightened_vmcs) {
8270 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
8271 static_branch_enable(&enable_evmcs);
8272 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008273
8274 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
8275 vmx_x86_ops.enable_direct_tlbflush
8276 = hv_enable_direct_tlbflush;
8277
Avi Kivity6aa8b732006-12-10 02:21:36 -08008278 } else {
8279 enlightened_vmcs = false;
8280 }
8281#endif
8282
Sean Christophersond008dfd2020-03-21 13:25:56 -07008283 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008284 __alignof__(struct vcpu_vmx), THIS_MODULE);
8285 if (r)
8286 return r;
8287
8288 /*
8289 * Must be called after kvm_init() so enable_ept is properly set
8290 * up. Hand the parameter mitigation value in which was stored in
8291 * the pre module init parser. If no parameter was given, it will
8292 * contain 'auto' which will be turned into the default 'cond'
8293 * mitigation mode.
8294 */
Waiman Long19a36d32019-08-26 15:30:23 -04008295 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
8296 if (r) {
8297 vmx_exit();
8298 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008299 }
8300
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008301 for_each_possible_cpu(cpu) {
8302 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
8303 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
8304 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
8305 }
8306
Avi Kivity6aa8b732006-12-10 02:21:36 -08008307#ifdef CONFIG_KEXEC_CORE
8308 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8309 crash_vmclear_local_loaded_vmcss);
8310#endif
8311 vmx_check_vmcs12_offsets();
8312
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008313 /*
Mohammed Gamalb96e6502020-09-03 16:11:22 +02008314 * Shadow paging doesn't have a (further) performance penalty
8315 * from GUEST_MAXPHYADDR < HOST_MAXPHYADDR so enable it
8316 * by default
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008317 */
Mohammed Gamalb96e6502020-09-03 16:11:22 +02008318 if (!enable_ept)
8319 allow_smaller_maxphyaddr = true;
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008320
Avi Kivity6aa8b732006-12-10 02:21:36 -08008321 return 0;
8322}
8323module_init(vmx_init);