blob: 1ad9faca44efbc946cafeb46c52aa03c7541e5ad [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity6aa8b732006-12-10 02:21:36 -08002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * This module enables machines with Intel VT-x extensions to run virtual
6 * machines without emulation or binary translation.
7 *
8 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Avi Kivity6aa8b732006-12-10 02:21:36 -080014 */
15
Sean Christopherson199b1182018-12-03 13:52:53 -080016#include <linux/frame.h>
17#include <linux/highmem.h>
18#include <linux/hrtimer.h>
19#include <linux/kernel.h>
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/module.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020022#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070023#include <linux/mod_devicetable.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080024#include <linux/mm.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080025#include <linux/sched.h>
Josh Poimboeufb2849092019-01-30 07:13:58 -060026#include <linux/sched/smt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040028#include <linux/tboot.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080029#include <linux/trace_events.h>
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +020030#include <linux/entry-kvm.h>
Avi Kivitye4956062007-06-28 14:15:57 -040031
Sean Christopherson199b1182018-12-03 13:52:53 -080032#include <asm/apic.h>
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020033#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080034#include <asm/cpu.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010035#include <asm/cpu_device_id.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010036#include <asm/debugreg.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080037#include <asm/desc.h>
38#include <asm/fpu/internal.h>
39#include <asm/io.h>
Feng Wuefc64402015-09-18 22:29:51 +080040#include <asm/irq_remapping.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080041#include <asm/kexec.h>
42#include <asm/perf_event.h>
43#include <asm/mce.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070044#include <asm/mmu_context.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010045#include <asm/mshyperv.h>
Benjamin Thielb10c3072020-01-23 18:29:45 +010046#include <asm/mwait.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080047#include <asm/spec-ctrl.h>
48#include <asm/virtext.h>
49#include <asm/vmx.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Sean Christopherson3077c192018-12-03 13:53:02 -080051#include "capabilities.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080052#include "cpuid.h"
Sean Christopherson4cebd742018-12-03 13:52:58 -080053#include "evmcs.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080054#include "irq.h"
55#include "kvm_cache_regs.h"
56#include "lapic.h"
57#include "mmu.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080058#include "nested.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080060#include "trace.h"
Sean Christophersoncb1d4742018-12-03 13:53:04 -080061#include "vmcs.h"
Sean Christopherson609363c2018-12-03 13:53:05 -080062#include "vmcs12.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080063#include "vmx.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080064#include "x86.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Valdis Klētnieks575b2552020-02-27 21:49:52 -050069#ifdef MODULE
Josh Triplette9bda3b2012-03-20 23:33:51 -070070static const struct x86_cpu_id vmx_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010071 X86_MATCH_FEATURE(X86_FEATURE_VMX, NULL),
Josh Triplette9bda3b2012-03-20 23:33:51 -070072 {}
73};
74MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050075#endif
Josh Triplette9bda3b2012-03-20 23:33:51 -070076
Sean Christopherson2c4fd912018-12-03 13:53:03 -080077bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020078module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080079
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010080static bool __read_mostly enable_vnmi = 1;
81module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
82
Sean Christopherson2c4fd912018-12-03 13:53:03 -080083bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020085
Sean Christopherson2c4fd912018-12-03 13:53:03 -080086bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020087module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080088
Sean Christopherson2c4fd912018-12-03 13:53:03 -080089bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070090module_param_named(unrestricted_guest,
91 enable_unrestricted_guest, bool, S_IRUGO);
92
Sean Christopherson2c4fd912018-12-03 13:53:03 -080093bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080094module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
95
Avi Kivitya27685c2012-06-12 20:30:18 +030096static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020097module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030098
Rusty Russell476bc002012-01-13 09:32:18 +103099static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +0300100module_param(fasteoi, bool, S_IRUGO);
101
Vitaly Kuznetsova4443262020-02-20 18:22:04 +0100102bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800103module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800104
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200110static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Sean Christopherson2c4fd912018-12-03 13:53:03 -0800113bool __read_mostly enable_pml = 1;
Kai Huang843e4332015-01-28 10:54:28 +0800114module_param_named(pml, enable_pml, bool, S_IRUGO);
115
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200116static bool __read_mostly dump_invalid_vmcs = 0;
117module_param(dump_invalid_vmcs, bool, 0644);
118
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100119#define MSR_BITMAP_MODE_X2APIC 1
120#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100121
Haozhong Zhang64903d62015-10-20 15:39:09 +0800122#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
123
Yunhong Jiang64672c92016-06-13 14:19:59 -0700124/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
125static int __read_mostly cpu_preemption_timer_multi;
126static bool __read_mostly enable_preemption_timer = 1;
127#ifdef CONFIG_X86_64
128module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
129#endif
130
Sean Christopherson3de63472018-07-13 08:42:30 -0700131#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800132#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
133#define KVM_VM_CR0_ALWAYS_ON \
134 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
135 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200136
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800137#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200138#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
139#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
140
Avi Kivity78ac8b42010-04-08 18:19:35 +0300141#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
142
Chao Pengbf8c55d2018-10-24 16:05:14 +0800143#define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \
144 RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \
145 RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \
146 RTIT_STATUS_BYTECNT))
147
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800148/*
149 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
150 * ple_gap: upper bound on the amount of time between two successive
151 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500152 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153 * ple_window: upper bound on the amount of time a guest is allowed to execute
154 * in a PAUSE loop. Tests indicate that most spinlocks are held for
155 * less than 2^12 cycles
156 * Time is measured based on a counter that runs at the same rate as the TSC,
157 * refer SDM volume 3b section 21.6.13 & 22.1.3.
158 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400159static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500160module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200161
Babu Moger7fbc85a2018-03-16 16:37:22 -0400162static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
163module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200165/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400166static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400167module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200168
169/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400170static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400171module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200172
173/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
175module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200176
Chao Pengf99e3da2018-10-24 16:05:10 +0800177/* Default is SYSTEM mode, 1 for host-guest mode */
178int __read_mostly pt_mode = PT_MODE_SYSTEM;
179module_param(pt_mode, int, S_IRUGO);
180
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200181static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200182static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200183static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200184
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200185/* Storage for pre module init parameter parsing */
186static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200187
188static const struct {
189 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200190 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200191} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200192 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
193 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
194 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
195 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
196 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
197 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200198};
199
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200200#define L1D_CACHE_ORDER 4
201static void *vmx_l1d_flush_pages;
202
203static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
204{
205 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200206 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200207
Waiman Long19a36d32019-08-26 15:30:23 -0400208 if (!boot_cpu_has_bug(X86_BUG_L1TF)) {
209 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
210 return 0;
211 }
212
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200213 if (!enable_ept) {
214 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
215 return 0;
216 }
217
Yi Wangd806afa2018-08-16 13:42:39 +0800218 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
219 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200220
Yi Wangd806afa2018-08-16 13:42:39 +0800221 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
222 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
223 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
224 return 0;
225 }
226 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200227
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200228 /* If set to auto use the default l1tf mitigation method */
229 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
230 switch (l1tf_mitigation) {
231 case L1TF_MITIGATION_OFF:
232 l1tf = VMENTER_L1D_FLUSH_NEVER;
233 break;
234 case L1TF_MITIGATION_FLUSH_NOWARN:
235 case L1TF_MITIGATION_FLUSH:
236 case L1TF_MITIGATION_FLUSH_NOSMT:
237 l1tf = VMENTER_L1D_FLUSH_COND;
238 break;
239 case L1TF_MITIGATION_FULL:
240 case L1TF_MITIGATION_FULL_FORCE:
241 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
242 break;
243 }
244 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
245 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
246 }
247
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200248 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
249 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Ben Gardon41836832019-02-11 11:02:52 -0800250 /*
251 * This allocation for vmx_l1d_flush_pages is not tied to a VM
252 * lifetime and so should not be charged to a memcg.
253 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200254 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
255 if (!page)
256 return -ENOMEM;
257 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200258
259 /*
260 * Initialize each page with a different pattern in
261 * order to protect against KSM in the nested
262 * virtualization case.
263 */
264 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
265 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
266 PAGE_SIZE);
267 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200268 }
269
270 l1tf_vmx_mitigation = l1tf;
271
Thomas Gleixner895ae472018-07-13 16:23:22 +0200272 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
273 static_branch_enable(&vmx_l1d_should_flush);
274 else
275 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200276
Nicolai Stange427362a2018-07-21 22:25:00 +0200277 if (l1tf == VMENTER_L1D_FLUSH_COND)
278 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200279 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200280 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200281 return 0;
282}
283
284static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200285{
286 unsigned int i;
287
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200288 if (s) {
289 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200290 if (vmentry_l1d_param[i].for_parse &&
291 sysfs_streq(s, vmentry_l1d_param[i].option))
292 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200293 }
294 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200295 return -EINVAL;
296}
297
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200298static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
299{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200300 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200301
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200302 l1tf = vmentry_l1d_flush_parse(s);
303 if (l1tf < 0)
304 return l1tf;
305
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200306 if (!boot_cpu_has(X86_BUG_L1TF))
307 return 0;
308
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200309 /*
310 * Has vmx_init() run already? If not then this is the pre init
311 * parameter parsing. In that case just store the value and let
312 * vmx_init() do the proper setup after enable_ept has been
313 * established.
314 */
315 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
316 vmentry_l1d_flush_param = l1tf;
317 return 0;
318 }
319
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200320 mutex_lock(&vmx_l1d_flush_mutex);
321 ret = vmx_setup_l1d_flush(l1tf);
322 mutex_unlock(&vmx_l1d_flush_mutex);
323 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200324}
325
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200326static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
327{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200328 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
329 return sprintf(s, "???\n");
330
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200331 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200332}
333
334static const struct kernel_param_ops vmentry_l1d_flush_ops = {
335 .set = vmentry_l1d_flush_set,
336 .get = vmentry_l1d_flush_get,
337};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200338module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200339
Gleb Natapovd99e4152012-12-20 16:57:45 +0200340static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yi Wang1e4329ee2018-11-08 11:22:21 +0800341static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj15d45072018-02-01 22:59:43 +0100342 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300343
Sean Christopherson453eafb2018-12-20 12:25:17 -0800344void vmx_vmexit(void);
345
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700346#define vmx_insn_failed(fmt...) \
347do { \
348 WARN_ONCE(1, fmt); \
349 pr_warn_ratelimited(fmt); \
350} while (0)
351
Sean Christopherson6e202092019-07-19 13:41:08 -0700352asmlinkage void vmread_error(unsigned long field, bool fault)
353{
354 if (fault)
355 kvm_spurious_fault();
356 else
357 vmx_insn_failed("kvm: vmread failed: field=%lx\n", field);
358}
359
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700360noinline void vmwrite_error(unsigned long field, unsigned long value)
361{
362 vmx_insn_failed("kvm: vmwrite failed: field=%lx val=%lx err=%d\n",
363 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
364}
365
366noinline void vmclear_error(struct vmcs *vmcs, u64 phys_addr)
367{
368 vmx_insn_failed("kvm: vmclear failed: %p/%llx\n", vmcs, phys_addr);
369}
370
371noinline void vmptrld_error(struct vmcs *vmcs, u64 phys_addr)
372{
373 vmx_insn_failed("kvm: vmptrld failed: %p/%llx\n", vmcs, phys_addr);
374}
375
376noinline void invvpid_error(unsigned long ext, u16 vpid, gva_t gva)
377{
378 vmx_insn_failed("kvm: invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n",
379 ext, vpid, gva);
380}
381
382noinline void invept_error(unsigned long ext, u64 eptp, gpa_t gpa)
383{
384 vmx_insn_failed("kvm: invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n",
385 ext, eptp, gpa);
386}
387
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800389DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300390/*
391 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
392 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
393 */
394static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395
Sheng Yang2384d2b2008-01-17 15:14:33 +0800396static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
397static DEFINE_SPINLOCK(vmx_vpid_lock);
398
Sean Christopherson3077c192018-12-03 13:53:02 -0800399struct vmcs_config vmcs_config;
400struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800401
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402#define VMX_SEGMENT_FIELD(seg) \
403 [VCPU_SREG_##seg] = { \
404 .selector = GUEST_##seg##_SELECTOR, \
405 .base = GUEST_##seg##_BASE, \
406 .limit = GUEST_##seg##_LIMIT, \
407 .ar_bytes = GUEST_##seg##_AR_BYTES, \
408 }
409
Mathias Krause772e0312012-08-30 01:30:19 +0200410static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 unsigned selector;
412 unsigned base;
413 unsigned limit;
414 unsigned ar_bytes;
415} kvm_vmx_segment_fields[] = {
416 VMX_SEGMENT_FIELD(CS),
417 VMX_SEGMENT_FIELD(DS),
418 VMX_SEGMENT_FIELD(ES),
419 VMX_SEGMENT_FIELD(FS),
420 VMX_SEGMENT_FIELD(GS),
421 VMX_SEGMENT_FIELD(SS),
422 VMX_SEGMENT_FIELD(TR),
423 VMX_SEGMENT_FIELD(LDTR),
424};
425
Sean Christophersonec0241f2020-04-15 13:34:52 -0700426static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
427{
428 vmx->segment_cache.bitmask = 0;
429}
430
Sean Christopherson23420802019-04-19 22:50:57 -0700431static unsigned long host_idt_base;
Avi Kivity26bb0982009-09-07 11:14:12 +0300432
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300433/*
Jim Mattson898a8112018-12-05 15:28:59 -0800434 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
435 * will emulate SYSCALL in legacy mode if the vendor string in guest
436 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
437 * support this emulation, IA32_STAR must always be included in
438 * vmx_msr_index[], even in i386 builds.
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300439 */
Sean Christophersoncf3646e2018-12-03 13:53:15 -0800440const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800441#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300442 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400444 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Paolo Bonzinic11f83e2019-11-18 12:23:00 -0500445 MSR_IA32_TSX_CTRL,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100448#if IS_ENABLED(CONFIG_HYPERV)
449static bool __read_mostly enlightened_vmcs = true;
450module_param(enlightened_vmcs, bool, 0444);
451
Tianyu Lan877ad952018-07-19 08:40:23 +0000452/* check_ept_pointer() should be under protection of ept_pointer_lock. */
453static void check_ept_pointer_match(struct kvm *kvm)
454{
455 struct kvm_vcpu *vcpu;
456 u64 tmp_eptp = INVALID_PAGE;
457 int i;
458
459 kvm_for_each_vcpu(i, vcpu, kvm) {
460 if (!VALID_PAGE(tmp_eptp)) {
461 tmp_eptp = to_vmx(vcpu)->ept_pointer;
462 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
463 to_kvm_vmx(kvm)->ept_pointers_match
464 = EPT_POINTERS_MISMATCH;
465 return;
466 }
467 }
468
469 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
470}
471
Yi Wang8997f652019-01-21 15:27:05 +0800472static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800473 void *data)
474{
475 struct kvm_tlb_range *range = data;
476
477 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
478 range->pages);
479}
480
481static inline int __hv_remote_flush_tlb_with_range(struct kvm *kvm,
482 struct kvm_vcpu *vcpu, struct kvm_tlb_range *range)
483{
484 u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
485
486 /*
487 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs address
488 * of the base of EPT PML4 table, strip off EPT configuration
489 * information.
490 */
491 if (range)
492 return hyperv_flush_guest_mapping_range(ept_pointer & PAGE_MASK,
493 kvm_fill_hv_flush_list_func, (void *)range);
494 else
495 return hyperv_flush_guest_mapping(ept_pointer & PAGE_MASK);
496}
497
498static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
499 struct kvm_tlb_range *range)
Tianyu Lan877ad952018-07-19 08:40:23 +0000500{
Lan Tianyua5c214d2018-10-13 22:54:05 +0800501 struct kvm_vcpu *vcpu;
Lan Tianyub7c1c222019-01-04 15:20:44 +0800502 int ret = 0, i;
Tianyu Lan877ad952018-07-19 08:40:23 +0000503
504 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
505
506 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
507 check_ept_pointer_match(kvm);
508
509 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyu53963a72018-12-06 15:34:36 +0800510 kvm_for_each_vcpu(i, vcpu, kvm) {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800511 /* If ept_pointer is invalid pointer, bypass flush request. */
512 if (VALID_PAGE(to_vmx(vcpu)->ept_pointer))
513 ret |= __hv_remote_flush_tlb_with_range(
514 kvm, vcpu, range);
Lan Tianyu53963a72018-12-06 15:34:36 +0800515 }
Lan Tianyua5c214d2018-10-13 22:54:05 +0800516 } else {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800517 ret = __hv_remote_flush_tlb_with_range(kvm,
518 kvm_get_vcpu(kvm, 0), range);
Tianyu Lan877ad952018-07-19 08:40:23 +0000519 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000520
Tianyu Lan877ad952018-07-19 08:40:23 +0000521 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
522 return ret;
523}
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800524static int hv_remote_flush_tlb(struct kvm *kvm)
525{
526 return hv_remote_flush_tlb_with_range(kvm, NULL);
527}
528
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800529static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)
530{
531 struct hv_enlightened_vmcs *evmcs;
532 struct hv_partition_assist_pg **p_hv_pa_pg =
533 &vcpu->kvm->arch.hyperv.hv_pa_pg;
534 /*
535 * Synthetic VM-Exit is not enabled in current code and so All
536 * evmcs in singe VM shares same assist page.
537 */
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200538 if (!*p_hv_pa_pg)
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800539 *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200540
541 if (!*p_hv_pa_pg)
542 return -ENOMEM;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800543
544 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs;
545
546 evmcs->partition_assist_page =
547 __pa(*p_hv_pa_pg);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200548 evmcs->hv_vm_id = (unsigned long)vcpu->kvm;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800549 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;
550
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800551 return 0;
552}
553
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100554#endif /* IS_ENABLED(CONFIG_HYPERV) */
555
Yunhong Jiang64672c92016-06-13 14:19:59 -0700556/*
557 * Comment's format: document - errata name - stepping - processor name.
558 * Refer from
559 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
560 */
561static u32 vmx_preemption_cpu_tfms[] = {
562/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
5630x000206E6,
564/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
565/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
566/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
5670x00020652,
568/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
5690x00020655,
570/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
571/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
572/*
573 * 320767.pdf - AAP86 - B1 -
574 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
575 */
5760x000106E5,
577/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
5780x000106A0,
579/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
5800x000106A1,
581/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
5820x000106A4,
583 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
584 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
585 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
5860x000106A5,
Wei Huang3d82c562018-12-03 14:13:32 -0600587 /* Xeon E3-1220 V2 */
5880x000306A8,
Yunhong Jiang64672c92016-06-13 14:19:59 -0700589};
590
591static inline bool cpu_has_broken_vmx_preemption_timer(void)
592{
593 u32 eax = cpuid_eax(0x00000001), i;
594
595 /* Clear the reserved bits */
596 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000597 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700598 if (eax == vmx_preemption_cpu_tfms[i])
599 return true;
600
601 return false;
602}
603
Paolo Bonzini35754c92015-07-29 12:05:37 +0200604static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800605{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200606 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800607}
608
Sheng Yang04547152009-04-01 15:52:31 +0800609static inline bool report_flexpriority(void)
610{
611 return flexpriority_enabled;
612}
613
Sean Christopherson1e7a4832020-09-23 11:04:02 -0700614static inline int __vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800615{
616 int i;
617
Sean Christophersonfbc18002020-09-23 11:03:59 -0700618 for (i = 0; i < vmx->nr_uret_msrs; ++i)
Sean Christophersoneb3db1b2020-09-23 11:03:58 -0700619 if (vmx_msr_index[vmx->guest_uret_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300620 return i;
621 return -1;
622}
623
Sean Christophersoneb3db1b2020-09-23 11:03:58 -0700624struct vmx_uret_msr *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300625{
626 int i;
627
Sean Christopherson1e7a4832020-09-23 11:04:02 -0700628 i = __vmx_find_uret_msr(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300629 if (i >= 0)
Sean Christophersoneb3db1b2020-09-23 11:03:58 -0700630 return &vmx->guest_uret_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000631 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800632}
633
Sean Christophersoneb3db1b2020-09-23 11:03:58 -0700634static int vmx_set_guest_msr(struct vcpu_vmx *vmx, struct vmx_uret_msr *msr, u64 data)
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500635{
636 int ret = 0;
637
638 u64 old_msr_data = msr->data;
639 msr->data = data;
Sean Christophersone9bb1ae2020-09-23 11:04:00 -0700640 if (msr - vmx->guest_uret_msrs < vmx->nr_active_uret_msrs) {
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500641 preempt_disable();
Sean Christopherson7e34fbd2020-09-23 11:03:55 -0700642 ret = kvm_set_user_return_msr(msr->index, msr->data, msr->mask);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500643 preempt_enable();
644 if (ret)
645 msr->data = old_msr_data;
646 }
647 return ret;
648}
649
Dave Young2965faa2015-09-09 15:38:55 -0700650#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800651static void crash_vmclear_local_loaded_vmcss(void)
652{
653 int cpu = raw_smp_processor_id();
654 struct loaded_vmcs *v;
655
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800656 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
657 loaded_vmcss_on_cpu_link)
658 vmcs_clear(v->vmcs);
659}
Dave Young2965faa2015-09-09 15:38:55 -0700660#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800661
Nadav Har'Eld462b812011-05-24 15:26:10 +0300662static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300664 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800665 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666
Nadav Har'Eld462b812011-05-24 15:26:10 +0300667 if (loaded_vmcs->cpu != cpu)
668 return; /* vcpu migration can race with cpu offline */
669 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670 per_cpu(current_vmcs, cpu) = NULL;
Sean Christopherson31603d42020-03-21 12:37:49 -0700671
672 vmcs_clear(loaded_vmcs->vmcs);
673 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
674 vmcs_clear(loaded_vmcs->shadow_vmcs);
675
Nadav Har'Eld462b812011-05-24 15:26:10 +0300676 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800677
678 /*
Sean Christopherson31603d42020-03-21 12:37:49 -0700679 * Ensure all writes to loaded_vmcs, including deleting it from its
680 * current percpu list, complete before setting loaded_vmcs->vcpu to
681 * -1, otherwise a different cpu can see vcpu == -1 first and add
682 * loaded_vmcs to its percpu list before it's deleted from this cpu's
683 * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800684 */
685 smp_wmb();
686
Sean Christopherson31603d42020-03-21 12:37:49 -0700687 loaded_vmcs->cpu = -1;
688 loaded_vmcs->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689}
690
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800691void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800692{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800693 int cpu = loaded_vmcs->cpu;
694
695 if (cpu != -1)
696 smp_call_function_single(cpu,
697 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800698}
699
Avi Kivity2fb92db2011-04-27 19:42:18 +0300700static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
701 unsigned field)
702{
703 bool ret;
704 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
705
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700706 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) {
707 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS);
Avi Kivity2fb92db2011-04-27 19:42:18 +0300708 vmx->segment_cache.bitmask = 0;
709 }
710 ret = vmx->segment_cache.bitmask & mask;
711 vmx->segment_cache.bitmask |= mask;
712 return ret;
713}
714
715static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
716{
717 u16 *p = &vmx->segment_cache.seg[seg].selector;
718
719 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
720 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
721 return *p;
722}
723
724static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
725{
726 ulong *p = &vmx->segment_cache.seg[seg].base;
727
728 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
729 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
730 return *p;
731}
732
733static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
734{
735 u32 *p = &vmx->segment_cache.seg[seg].limit;
736
737 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
738 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
739 return *p;
740}
741
742static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
743{
744 u32 *p = &vmx->segment_cache.seg[seg].ar;
745
746 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
747 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
748 return *p;
749}
750
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800751void update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300752{
753 u32 eb;
754
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100755 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800756 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200757 /*
758 * Guest access to VMware backdoor ports could legitimately
759 * trigger #GP because of TSS I/O permission bitmap.
760 * We intercept those #GP and allow access to them anyway
761 * as VMware does.
762 */
763 if (enable_vmware_backdoor)
764 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100765 if ((vcpu->guest_debug &
766 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
767 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
768 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300769 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300770 eb = ~0;
Paolo Bonzinia0c13432020-07-10 17:48:08 +0200771 if (!vmx_need_pf_intercept(vcpu))
Miaohe Lin49f933d2020-02-27 11:20:54 +0800772 eb &= ~(1u << PF_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300773
774 /* When we are running a nested L2 guest and L1 specified for it a
775 * certain exception bitmap, we must trap the same exceptions and pass
776 * them to L1. When running L2, we will only handle the exceptions
777 * specified above if L1 did not want them.
778 */
779 if (is_guest_mode(vcpu))
780 eb |= get_vmcs12(vcpu)->exception_bitmap;
781
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300782 vmcs_write32(EXCEPTION_BITMAP, eb);
783}
784
Ashok Raj15d45072018-02-01 22:59:43 +0100785/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100786 * Check if MSR is intercepted for currently loaded MSR bitmap.
787 */
788static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
789{
790 unsigned long *msr_bitmap;
791 int f = sizeof(unsigned long);
792
793 if (!cpu_has_vmx_msr_bitmap())
794 return true;
795
796 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
797
798 if (msr <= 0x1fff) {
799 return !!test_bit(msr, msr_bitmap + 0x800 / f);
800 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
801 msr &= 0x1fff;
802 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
803 }
804
805 return true;
806}
807
Gleb Natapov2961e8762013-11-25 15:37:13 +0200808static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
809 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200810{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200811 vm_entry_controls_clearbit(vmx, entry);
812 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200813}
814
Sean Christophersona128a932020-09-23 11:03:57 -0700815int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr)
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400816{
817 unsigned int i;
818
819 for (i = 0; i < m->nr; ++i) {
820 if (m->val[i].index == msr)
821 return i;
822 }
823 return -ENOENT;
824}
825
Avi Kivity61d2ef22010-04-28 16:40:38 +0300826static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
827{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400828 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300829 struct msr_autoload *m = &vmx->msr_autoload;
830
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200831 switch (msr) {
832 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800833 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200834 clear_atomic_switch_msr_special(vmx,
835 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200836 VM_EXIT_LOAD_IA32_EFER);
837 return;
838 }
839 break;
840 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800841 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200842 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200843 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
844 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
845 return;
846 }
847 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200848 }
Sean Christophersona128a932020-09-23 11:03:57 -0700849 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400850 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400851 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400852 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400853 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400854 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200855
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400856skip_guest:
Sean Christophersona128a932020-09-23 11:03:57 -0700857 i = vmx_find_loadstore_msr_slot(&m->host, msr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400858 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300859 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400860
861 --m->host.nr;
862 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400863 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300864}
865
Gleb Natapov2961e8762013-11-25 15:37:13 +0200866static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
867 unsigned long entry, unsigned long exit,
868 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
869 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200870{
871 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700872 if (host_val_vmcs != HOST_IA32_EFER)
873 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200874 vm_entry_controls_setbit(vmx, entry);
875 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200876}
877
Avi Kivity61d2ef22010-04-28 16:40:38 +0300878static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400879 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300880{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400881 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300882 struct msr_autoload *m = &vmx->msr_autoload;
883
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200884 switch (msr) {
885 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800886 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200887 add_atomic_switch_msr_special(vmx,
888 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200889 VM_EXIT_LOAD_IA32_EFER,
890 GUEST_IA32_EFER,
891 HOST_IA32_EFER,
892 guest_val, host_val);
893 return;
894 }
895 break;
896 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800897 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200898 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200899 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
900 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
901 GUEST_IA32_PERF_GLOBAL_CTRL,
902 HOST_IA32_PERF_GLOBAL_CTRL,
903 guest_val, host_val);
904 return;
905 }
906 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +0100907 case MSR_IA32_PEBS_ENABLE:
908 /* PEBS needs a quiescent period after being disabled (to write
909 * a record). Disabling PEBS through VMX MSR swapping doesn't
910 * provide that period, so a CPU could write host's record into
911 * guest's memory.
912 */
913 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +0200914 }
915
Sean Christophersona128a932020-09-23 11:03:57 -0700916 i = vmx_find_loadstore_msr_slot(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400917 if (!entry_only)
Sean Christophersona128a932020-09-23 11:03:57 -0700918 j = vmx_find_loadstore_msr_slot(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300919
Sean Christophersonce833b22020-09-23 11:03:56 -0700920 if ((i < 0 && m->guest.nr == MAX_NR_LOADSTORE_MSRS) ||
921 (j < 0 && m->host.nr == MAX_NR_LOADSTORE_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +0200922 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +0200923 "Can't add msr %x\n", msr);
924 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300925 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400926 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400927 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400928 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400929 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400930 m->guest.val[i].index = msr;
931 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300932
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400933 if (entry_only)
934 return;
935
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400936 if (j < 0) {
937 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400938 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300939 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400940 m->host.val[j].index = msr;
941 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300942}
943
Sean Christopherson86e3e492020-09-23 11:04:04 -0700944static bool update_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300945{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100946 u64 guest_efer = vmx->vcpu.arch.efer;
947 u64 ignore_bits = 0;
Sean Christopherson86e3e492020-09-23 11:04:04 -0700948 int i;
Eddie Dong2cc51562007-05-21 07:28:09 +0300949
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100950 /* Shadow paging assumes NX to be available. */
951 if (!enable_ept)
952 guest_efer |= EFER_NX;
Roel Kluin3a34a882009-08-04 02:08:45 -0700953
Avi Kivity51c6cf62007-08-29 03:48:05 +0300954 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100955 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +0300956 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100957 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300958#ifdef CONFIG_X86_64
959 ignore_bits |= EFER_LMA | EFER_LME;
960 /* SCE is meaningful only in long mode on Intel */
961 if (guest_efer & EFER_LMA)
962 ignore_bits &= ~(u64)EFER_SCE;
963#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +0300964
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800965 /*
966 * On EPT, we can't emulate NX, so we must switch EFER atomically.
967 * On CPUs that support "load IA32_EFER", always switch EFER
968 * atomically, since it's faster than switching it manually.
969 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800970 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800971 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +0300972 if (!(guest_efer & EFER_LMA))
973 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -0800974 if (guest_efer != host_efer)
975 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400976 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -0700977 else
978 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +0300979 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100980 }
Sean Christopherson86e3e492020-09-23 11:04:04 -0700981
982 i = __vmx_find_uret_msr(vmx, MSR_EFER);
983 if (i < 0)
984 return false;
985
986 clear_atomic_switch_msr(vmx, MSR_EFER);
987
988 guest_efer &= ~ignore_bits;
989 guest_efer |= host_efer & ignore_bits;
990
991 vmx->guest_uret_msrs[i].data = guest_efer;
992 vmx->guest_uret_msrs[i].mask = ~ignore_bits;
993
994 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300995}
996
Andy Lutomirskie28baea2017-02-20 08:56:11 -0800997#ifdef CONFIG_X86_32
998/*
999 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1000 * VMCS rather than the segment table. KVM uses this helper to figure
1001 * out the current bases to poke them into the VMCS before entry.
1002 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001003static unsigned long segment_base(u16 selector)
1004{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001005 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001006 unsigned long v;
1007
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001008 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001009 return 0;
1010
Thomas Garnier45fc8752017-03-14 10:05:08 -07001011 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001012
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001013 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001014 u16 ldt_selector = kvm_read_ldt();
1015
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001016 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001017 return 0;
1018
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001019 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001020 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001021 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001022 return v;
1023}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001024#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001025
Sean Christophersone348ac72019-12-10 15:24:33 -08001026static inline bool pt_can_write_msr(struct vcpu_vmx *vmx)
1027{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001028 return vmx_pt_mode_is_host_guest() &&
Sean Christophersone348ac72019-12-10 15:24:33 -08001029 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
1030}
1031
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001032static inline bool pt_output_base_valid(struct kvm_vcpu *vcpu, u64 base)
1033{
1034 /* The base must be 128-byte aligned and a legal physical address. */
Sean Christopherson7096cbf2020-09-24 12:42:50 -07001035 return !kvm_vcpu_is_illegal_gpa(vcpu, base) && !(base & 0x7f);
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07001036}
1037
Chao Peng2ef444f2018-10-24 16:05:12 +08001038static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range)
1039{
1040 u32 i;
1041
1042 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1043 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1044 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1045 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1046 for (i = 0; i < addr_range; i++) {
1047 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1048 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1049 }
1050}
1051
1052static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range)
1053{
1054 u32 i;
1055
1056 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1057 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1058 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1059 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1060 for (i = 0; i < addr_range; i++) {
1061 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1062 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1063 }
1064}
1065
1066static void pt_guest_enter(struct vcpu_vmx *vmx)
1067{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001068 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001069 return;
1070
Chao Peng2ef444f2018-10-24 16:05:12 +08001071 /*
Chao Pengb08c2892018-10-24 16:05:15 +08001072 * GUEST_IA32_RTIT_CTL is already set in the VMCS.
1073 * Save host state before VM entry.
Chao Peng2ef444f2018-10-24 16:05:12 +08001074 */
Chao Pengb08c2892018-10-24 16:05:15 +08001075 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
Chao Peng2ef444f2018-10-24 16:05:12 +08001076 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1077 wrmsrl(MSR_IA32_RTIT_CTL, 0);
1078 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1079 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1080 }
1081}
1082
1083static void pt_guest_exit(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
1088 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1089 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1090 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1091 }
1092
1093 /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */
1094 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
1095}
1096
Sean Christopherson13b964a2019-05-07 09:06:31 -07001097void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1098 unsigned long fs_base, unsigned long gs_base)
1099{
1100 if (unlikely(fs_sel != host->fs_sel)) {
1101 if (!(fs_sel & 7))
1102 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1103 else
1104 vmcs_write16(HOST_FS_SELECTOR, 0);
1105 host->fs_sel = fs_sel;
1106 }
1107 if (unlikely(gs_sel != host->gs_sel)) {
1108 if (!(gs_sel & 7))
1109 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1110 else
1111 vmcs_write16(HOST_GS_SELECTOR, 0);
1112 host->gs_sel = gs_sel;
1113 }
1114 if (unlikely(fs_base != host->fs_base)) {
1115 vmcs_writel(HOST_FS_BASE, fs_base);
1116 host->fs_base = fs_base;
1117 }
1118 if (unlikely(gs_base != host->gs_base)) {
1119 vmcs_writel(HOST_GS_BASE, gs_base);
1120 host->gs_base = gs_base;
1121 }
1122}
1123
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001124void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001125{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001126 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001127 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001128#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01001129 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001130#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001131 unsigned long fs_base, gs_base;
1132 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03001133 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001134
Sean Christophersond264ee02018-08-27 15:21:12 -07001135 vmx->req_immediate_exit = false;
1136
Liran Alonf48b4712018-11-20 18:03:25 +02001137 /*
1138 * Note that guest MSRs to be saved/restored can also be changed
1139 * when guest state is loaded. This happens when guest transitions
1140 * to/from long-mode by setting MSR_EFER.LMA.
1141 */
Sean Christopherson658ece82020-09-23 11:04:01 -07001142 if (!vmx->guest_uret_msrs_loaded) {
1143 vmx->guest_uret_msrs_loaded = true;
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001144 for (i = 0; i < vmx->nr_active_uret_msrs; ++i)
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001145 kvm_set_user_return_msr(vmx->guest_uret_msrs[i].index,
1146 vmx->guest_uret_msrs[i].data,
1147 vmx->guest_uret_msrs[i].mask);
Liran Alonf48b4712018-11-20 18:03:25 +02001148
1149 }
wanpeng lic9dfd3f2020-02-17 18:37:43 +08001150
1151 if (vmx->nested.need_vmcs12_to_shadow_sync)
1152 nested_sync_vmcs12_to_shadow(vcpu);
1153
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001154 if (vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001155 return;
1156
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001157 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001158
Avi Kivity33ed6322007-05-02 16:54:03 +03001159 /*
1160 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1161 * allow segment selectors with cpl > 0 or ti == 1.
1162 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07001163 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01001164
1165#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001166 savesegment(ds, host_state->ds_sel);
1167 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07001168
1169 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001170 if (likely(is_64bit_mm(current->mm))) {
Thomas Gleixner67580342020-05-28 16:13:52 -04001171 current_save_fsgs();
Sean Christophersone368b872018-07-23 12:32:41 -07001172 fs_sel = current->thread.fsindex;
1173 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001174 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07001175 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001176 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07001177 savesegment(fs, fs_sel);
1178 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001179 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07001180 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03001181 }
1182
Paolo Bonzini4679b612018-09-24 17:23:01 +02001183 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03001184#else
Sean Christophersone368b872018-07-23 12:32:41 -07001185 savesegment(fs, fs_sel);
1186 savesegment(gs, gs_sel);
1187 fs_base = segment_base(fs_sel);
1188 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001189#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001190
Sean Christopherson13b964a2019-05-07 09:06:31 -07001191 vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001192 vmx->guest_state_loaded = true;
Avi Kivity33ed6322007-05-02 16:54:03 +03001193}
1194
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001195static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001196{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001197 struct vmcs_host_state *host_state;
1198
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001199 if (!vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001200 return;
1201
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001202 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001203
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001204 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001205
Avi Kivityc8770e72010-11-11 12:37:26 +02001206#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001207 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001208#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001209 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1210 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001211#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001212 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001213#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001214 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001215#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001216 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001217 if (host_state->fs_sel & 7)
1218 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001219#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001220 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1221 loadsegment(ds, host_state->ds_sel);
1222 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001223 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001224#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001225 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001226#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001227 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001228#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001229 load_fixmap_gdt(raw_smp_processor_id());
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001230 vmx->guest_state_loaded = false;
Sean Christopherson658ece82020-09-23 11:04:01 -07001231 vmx->guest_uret_msrs_loaded = false;
Avi Kivity33ed6322007-05-02 16:54:03 +03001232}
1233
Sean Christopherson678e3152018-07-23 12:32:43 -07001234#ifdef CONFIG_X86_64
1235static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001236{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001237 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001238 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001239 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1240 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001241 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001242}
1243
Sean Christopherson678e3152018-07-23 12:32:43 -07001244static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1245{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001246 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001247 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001248 wrmsrl(MSR_KERNEL_GS_BASE, data);
1249 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001250 vmx->msr_guest_kernel_gs_base = data;
1251}
1252#endif
1253
Sean Christopherson5c911be2020-05-01 09:31:17 -07001254void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
1255 struct loaded_vmcs *buddy)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001258 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Sean Christopherson5c911be2020-05-01 09:31:17 -07001259 struct vmcs *prev;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001261 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001262 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001263 local_irq_disable();
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001264
1265 /*
Sean Christopherson31603d42020-03-21 12:37:49 -07001266 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
1267 * this cpu's percpu list, otherwise it may not yet be deleted
1268 * from its previous cpu's percpu list. Pairs with the
1269 * smb_wmb() in __loaded_vmcs_clear().
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001270 */
1271 smp_rmb();
1272
Nadav Har'Eld462b812011-05-24 15:26:10 +03001273 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1274 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001275 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001276 }
1277
Sean Christopherson5c911be2020-05-01 09:31:17 -07001278 prev = per_cpu(current_vmcs, cpu);
1279 if (prev != vmx->loaded_vmcs->vmcs) {
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001280 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1281 vmcs_load(vmx->loaded_vmcs->vmcs);
Sean Christopherson5c911be2020-05-01 09:31:17 -07001282
1283 /*
1284 * No indirect branch prediction barrier needed when switching
1285 * the active VMCS within a guest, e.g. on nested VM-Enter.
1286 * The L1 VMM can protect itself with retpolines, IBPB or IBRS.
1287 */
1288 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev))
1289 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001290 }
1291
1292 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001293 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001294 unsigned long sysenter_esp;
1295
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001296 /*
1297 * Flush all EPTP/VPID contexts, the new pCPU may have stale
1298 * TLB entries from its previous association with the vCPU.
1299 */
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001300 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001301
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 /*
1303 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001304 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001306 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001307 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001308 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309
1310 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1311 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001312
Nadav Har'Eld462b812011-05-24 15:26:10 +03001313 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 }
Feng Wu28b835d2015-09-18 22:29:54 +08001315
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001316 /* Setup TSC multiplier */
1317 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001318 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1319 decache_tsc_multiplier(vmx);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001320}
1321
1322/*
1323 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1324 * vcpu mutex is already taken.
1325 */
Sean Christopherson1af1bb02020-05-06 16:58:50 -07001326static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001327{
1328 struct vcpu_vmx *vmx = to_vmx(vcpu);
1329
Sean Christopherson5c911be2020-05-01 09:31:17 -07001330 vmx_vcpu_load_vmcs(vcpu, cpu, NULL);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001331
Feng Wu28b835d2015-09-18 22:29:54 +08001332 vmx_vcpu_pi_load(vcpu, cpu);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001333
Wanpeng Li74c55932017-11-29 01:31:20 -08001334 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001335}
1336
Sean Christopherson13b964a2019-05-07 09:06:31 -07001337static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338{
Feng Wu28b835d2015-09-18 22:29:54 +08001339 vmx_vcpu_pi_put(vcpu);
1340
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001341 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342}
1343
Wanpeng Lif244dee2017-07-20 01:11:54 -07001344static bool emulation_required(struct kvm_vcpu *vcpu)
1345{
Sean Christopherson2ba44932020-09-23 11:44:48 -07001346 return emulate_invalid_guest_state && !vmx_guest_state_valid(vcpu);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001347}
1348
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001349unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001351 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001352 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001353
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001354 if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) {
1355 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Avi Kivity6de12732011-03-07 12:51:22 +02001356 rflags = vmcs_readl(GUEST_RFLAGS);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001357 if (vmx->rmode.vm86_active) {
Avi Kivity6de12732011-03-07 12:51:22 +02001358 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
Sean Christophersone7bddc52019-09-27 14:45:18 -07001359 save_rflags = vmx->rmode.save_rflags;
Avi Kivity6de12732011-03-07 12:51:22 +02001360 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1361 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001362 vmx->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001363 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001364 return vmx->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365}
1366
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001367void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001369 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001370 unsigned long old_rflags;
Wanpeng Lif244dee2017-07-20 01:11:54 -07001371
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00001372 if (is_unrestricted_guest(vcpu)) {
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001373 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001374 vmx->rflags = rflags;
1375 vmcs_writel(GUEST_RFLAGS, rflags);
1376 return;
1377 }
1378
1379 old_rflags = vmx_get_rflags(vcpu);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001380 vmx->rflags = rflags;
1381 if (vmx->rmode.vm86_active) {
1382 vmx->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001383 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001384 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001386
Sean Christophersone7bddc52019-09-27 14:45:18 -07001387 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM)
1388 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389}
1390
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001391u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001392{
1393 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1394 int ret = 0;
1395
1396 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001397 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001398 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001399 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001400
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001401 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001402}
1403
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001404void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001405{
1406 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1407 u32 interruptibility = interruptibility_old;
1408
1409 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1410
Jan Kiszka48005f62010-02-19 19:38:07 +01001411 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001412 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001413 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001414 interruptibility |= GUEST_INTR_STATE_STI;
1415
1416 if ((interruptibility != interruptibility_old))
1417 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1418}
1419
Chao Pengbf8c55d2018-10-24 16:05:14 +08001420static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data)
1421{
1422 struct vcpu_vmx *vmx = to_vmx(vcpu);
1423 unsigned long value;
1424
1425 /*
1426 * Any MSR write that attempts to change bits marked reserved will
1427 * case a #GP fault.
1428 */
1429 if (data & vmx->pt_desc.ctl_bitmask)
1430 return 1;
1431
1432 /*
1433 * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will
1434 * result in a #GP unless the same write also clears TraceEn.
1435 */
1436 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) &&
1437 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN))
1438 return 1;
1439
1440 /*
1441 * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit
1442 * and FabricEn would cause #GP, if
1443 * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0
1444 */
1445 if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) &&
1446 !(data & RTIT_CTL_FABRIC_EN) &&
1447 !intel_pt_validate_cap(vmx->pt_desc.caps,
1448 PT_CAP_single_range_output))
1449 return 1;
1450
1451 /*
1452 * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that
1453 * utilize encodings marked reserved will casue a #GP fault.
1454 */
1455 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods);
1456 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) &&
1457 !test_bit((data & RTIT_CTL_MTC_RANGE) >>
1458 RTIT_CTL_MTC_RANGE_OFFSET, &value))
1459 return 1;
1460 value = intel_pt_validate_cap(vmx->pt_desc.caps,
1461 PT_CAP_cycle_thresholds);
1462 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1463 !test_bit((data & RTIT_CTL_CYC_THRESH) >>
1464 RTIT_CTL_CYC_THRESH_OFFSET, &value))
1465 return 1;
1466 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods);
1467 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1468 !test_bit((data & RTIT_CTL_PSB_FREQ) >>
1469 RTIT_CTL_PSB_FREQ_OFFSET, &value))
1470 return 1;
1471
1472 /*
1473 * If ADDRx_CFG is reserved or the encodings is >2 will
1474 * cause a #GP fault.
1475 */
1476 value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET;
1477 if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2))
1478 return 1;
1479 value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET;
1480 if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2))
1481 return 1;
1482 value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET;
1483 if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2))
1484 return 1;
1485 value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET;
1486 if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2))
1487 return 1;
1488
1489 return 0;
1490}
1491
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07001492static bool vmx_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
1493{
1494 return true;
1495}
1496
Sean Christopherson1957aa62019-08-27 14:40:39 -07001497static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498{
Paolo Bonzinifede8072020-04-27 11:55:59 -04001499 unsigned long rip, orig_rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500
Sean Christopherson1957aa62019-08-27 14:40:39 -07001501 /*
1502 * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on
1503 * undefined behavior: Intel's SDM doesn't mandate the VMCS field be
1504 * set when EPT misconfig occurs. In practice, real hardware updates
1505 * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors
1506 * (namely Hyper-V) don't set it due to it being undefined behavior,
1507 * i.e. we end up advancing IP with some random value.
1508 */
1509 if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
1510 to_vmx(vcpu)->exit_reason != EXIT_REASON_EPT_MISCONFIG) {
Paolo Bonzinifede8072020-04-27 11:55:59 -04001511 orig_rip = kvm_rip_read(vcpu);
1512 rip = orig_rip + vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
1513#ifdef CONFIG_X86_64
1514 /*
1515 * We need to mask out the high 32 bits of RIP if not in 64-bit
1516 * mode, but just finding out that we are in 64-bit mode is
1517 * quite expensive. Only do it if there was a carry.
1518 */
1519 if (unlikely(((rip ^ orig_rip) >> 31) == 3) && !is_64_bit_mode(vcpu))
1520 rip = (u32)rip;
1521#endif
Sean Christopherson1957aa62019-08-27 14:40:39 -07001522 kvm_rip_write(vcpu, rip);
1523 } else {
1524 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
1525 return 0;
1526 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527
Glauber Costa2809f5d2009-05-12 16:21:05 -04001528 /* skipping an emulated instruction also counts */
1529 vmx_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001530
Sean Christopherson60fc3d02019-08-27 14:40:38 -07001531 return 1;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001532}
1533
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02001534/*
Oliver Upton5ef8acb2020-02-07 02:36:07 -08001535 * Recognizes a pending MTF VM-exit and records the nested state for later
1536 * delivery.
1537 */
1538static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)
1539{
1540 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1541 struct vcpu_vmx *vmx = to_vmx(vcpu);
1542
1543 if (!is_guest_mode(vcpu))
1544 return;
1545
1546 /*
1547 * Per the SDM, MTF takes priority over debug-trap exceptions besides
1548 * T-bit traps. As instruction emulation is completed (i.e. at the
1549 * instruction boundary), any #DB exception pending delivery must be a
1550 * debug-trap. Record the pending MTF state to be delivered in
1551 * vmx_check_nested_events().
1552 */
1553 if (nested_cpu_has_mtf(vmcs12) &&
1554 (!vcpu->arch.exception.pending ||
1555 vcpu->arch.exception.nr == DB_VECTOR))
1556 vmx->nested.mtf_pending = true;
1557 else
1558 vmx->nested.mtf_pending = false;
1559}
1560
1561static int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu)
1562{
1563 vmx_update_emulated_instruction(vcpu);
1564 return skip_emulated_instruction(vcpu);
1565}
1566
Wanpeng Licaa057a2018-03-12 04:53:03 -07001567static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1568{
1569 /*
1570 * Ensure that we clear the HLT state in the VMCS. We don't need to
1571 * explicitly skip the instruction because if the HLT state is set,
1572 * then the instruction is already executing and RIP has already been
1573 * advanced.
1574 */
1575 if (kvm_hlt_in_guest(vcpu->kvm) &&
1576 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1577 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1578}
1579
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001580static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001581{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001582 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001583 unsigned nr = vcpu->arch.exception.nr;
1584 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001585 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001586 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001587
Jim Mattsonda998b42018-10-16 14:29:22 -07001588 kvm_deliver_exception_payload(vcpu);
1589
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001590 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001591 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001592 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1593 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001594
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001595 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001596 int inc_eip = 0;
1597 if (kvm_exception_is_soft(nr))
1598 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07001599 kvm_inject_realmode_interrupt(vcpu, nr, inc_eip);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001600 return;
1601 }
1602
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001603 WARN_ON_ONCE(vmx->emulation_required);
1604
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001605 if (kvm_exception_is_soft(nr)) {
1606 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1607 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001608 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1609 } else
1610 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1611
1612 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001613
1614 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001615}
1616
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617/*
Eddie Donga75beee2007-05-17 18:55:15 +03001618 * Swap MSR entry in host/guest MSR entry array.
1619 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001620static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001621{
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001622 struct vmx_uret_msr tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001623
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001624 tmp = vmx->guest_uret_msrs[to];
1625 vmx->guest_uret_msrs[to] = vmx->guest_uret_msrs[from];
1626 vmx->guest_uret_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001627}
1628
1629/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001630 * Set up the vmcs to automatically save and restore system
1631 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1632 * mode, as fiddling with msrs is very expensive.
1633 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001634static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001635{
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001636 int nr_active_uret_msrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001637
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001638 nr_active_uret_msrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001639#ifdef CONFIG_X86_64
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001640 /*
1641 * The SYSCALL MSRs are only needed on long mode guests, and only
1642 * when EFER.SCE is set.
1643 */
1644 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
Sean Christopherson1e7a4832020-09-23 11:04:02 -07001645 index = __vmx_find_uret_msr(vmx, MSR_STAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001646 if (index >= 0)
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001647 move_msr_up(vmx, index, nr_active_uret_msrs++);
Sean Christopherson1e7a4832020-09-23 11:04:02 -07001648 index = __vmx_find_uret_msr(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001649 if (index >= 0)
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001650 move_msr_up(vmx, index, nr_active_uret_msrs++);
Sean Christopherson1e7a4832020-09-23 11:04:02 -07001651 index = __vmx_find_uret_msr(vmx, MSR_SYSCALL_MASK);
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001652 if (index >= 0)
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001653 move_msr_up(vmx, index, nr_active_uret_msrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001654 }
Eddie Donga75beee2007-05-17 18:55:15 +03001655#endif
Sean Christopherson86e3e492020-09-23 11:04:04 -07001656 if (update_transition_efer(vmx)) {
1657 index = __vmx_find_uret_msr(vmx, MSR_EFER);
1658 if (index >= 0)
1659 move_msr_up(vmx, index, nr_active_uret_msrs++);
1660 }
Sean Christophersonef1d2ee2020-09-23 11:04:03 -07001661 if (guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) {
1662 index = __vmx_find_uret_msr(vmx, MSR_TSC_AUX);
1663 if (index >= 0)
1664 move_msr_up(vmx, index, nr_active_uret_msrs++);
1665 }
Sean Christopherson1e7a4832020-09-23 11:04:02 -07001666 index = __vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001667 if (index >= 0)
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001668 move_msr_up(vmx, index, nr_active_uret_msrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001669
Sean Christophersone9bb1ae2020-09-23 11:04:00 -07001670 vmx->nr_active_uret_msrs = nr_active_uret_msrs;
Sean Christopherson658ece82020-09-23 11:04:01 -07001671 vmx->guest_uret_msrs_loaded = false;
Avi Kivity58972972009-02-24 22:26:47 +02001672
Yang Zhang8d146952013-01-25 10:18:50 +08001673 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001674 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001675}
1676
Leonid Shatz326e7422018-11-06 12:14:25 +02001677static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001679 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1680 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001681
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001682 /*
1683 * We're here if L1 chose not to trap WRMSR to TSC. According
1684 * to the spec, this should set L1's TSC; The offset that L1
1685 * set for L2 remains unchanged, and still needs to be added
1686 * to the newly set TSC to get L2's TSC.
1687 */
1688 if (is_guest_mode(vcpu) &&
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08001689 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001690 g_tsc_offset = vmcs12->tsc_offset;
1691
1692 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1693 vcpu->arch.tsc_offset - g_tsc_offset,
1694 offset);
1695 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1696 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697}
1698
Nadav Har'El801d3422011-05-25 23:02:23 +03001699/*
1700 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1701 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1702 * all guests if the "nested" module option is off, and can also be disabled
1703 * for a single guest by disabling its VMX cpuid bit.
1704 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001705bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001706{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001707 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001708}
1709
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001710static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1711 uint64_t val)
1712{
1713 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1714
1715 return !(val & ~valid_bits);
1716}
1717
Tom Lendacky801e4592018-02-21 13:39:51 -06001718static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1719{
Paolo Bonzini13893092018-02-26 13:40:09 +01001720 switch (msr->index) {
1721 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1722 if (!nested)
1723 return 1;
1724 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
Like Xu27461da32020-05-29 15:43:45 +08001725 case MSR_IA32_PERF_CAPABILITIES:
1726 msr->data = vmx_get_perf_capabilities();
1727 return 0;
Paolo Bonzini13893092018-02-26 13:40:09 +01001728 default:
Peter Xu12bc2132020-06-22 18:04:42 -04001729 return KVM_MSR_RET_INVALID;
Paolo Bonzini13893092018-02-26 13:40:09 +01001730 }
Tom Lendacky801e4592018-02-21 13:39:51 -06001731}
1732
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001733/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734 * Reads an msr value (of 'msr_index') into 'pdata'.
1735 * Returns 0 on success, non-0 otherwise.
1736 * Assumes vcpu_load() was already called.
1737 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001738static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001740 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001741 struct vmx_uret_msr *msr;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001742 u32 index;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001744 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001745#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001747 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748 break;
1749 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001750 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001752 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001753 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001754 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001755#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001757 return kvm_get_msr_common(vcpu, msr_info);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001758 case MSR_IA32_TSX_CTRL:
1759 if (!msr_info->host_initiated &&
1760 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
1761 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001762 goto find_uret_msr;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001763 case MSR_IA32_UMWAIT_CONTROL:
1764 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
1765 return 1;
1766
1767 msr_info->data = vmx->msr_ia32_umwait_control;
1768 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001769 case MSR_IA32_SPEC_CTRL:
1770 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001771 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1772 return 1;
1773
1774 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1775 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001776 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001777 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 break;
1779 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001780 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781 break;
1782 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001783 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001785 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001786 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001787 (!msr_info->host_initiated &&
1788 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001789 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001790 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001791 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001792 case MSR_IA32_MCG_EXT_CTL:
1793 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001794 !(vmx->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001795 FEAT_CTL_LMCE_ENABLED))
Jan Kiszkacae50132014-01-04 18:47:22 +01001796 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001797 msr_info->data = vcpu->arch.mcg_ext_ctl;
1798 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001799 case MSR_IA32_FEAT_CTL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001800 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001801 break;
1802 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1803 if (!nested_vmx_allowed(vcpu))
1804 return 1;
Vitaly Kuznetsov31de3d22020-02-05 13:30:33 +01001805 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1806 &msr_info->data))
1807 return 1;
1808 /*
1809 * Enlightened VMCS v1 doesn't have certain fields, but buggy
1810 * Hyper-V versions are still trying to use corresponding
1811 * features when they are exposed. Filter out the essential
1812 * minimum.
1813 */
1814 if (!msr_info->host_initiated &&
1815 vmx->nested.enlightened_vmcs_enabled)
1816 nested_evmcs_filter_control_msr(msr_info->index,
1817 &msr_info->data);
1818 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001819 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001820 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001821 return 1;
1822 msr_info->data = vmx->pt_desc.guest.ctl;
1823 break;
1824 case MSR_IA32_RTIT_STATUS:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001825 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001826 return 1;
1827 msr_info->data = vmx->pt_desc.guest.status;
1828 break;
1829 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001830 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001831 !intel_pt_validate_cap(vmx->pt_desc.caps,
1832 PT_CAP_cr3_filtering))
1833 return 1;
1834 msr_info->data = vmx->pt_desc.guest.cr3_match;
1835 break;
1836 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001837 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001838 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1839 PT_CAP_topa_output) &&
1840 !intel_pt_validate_cap(vmx->pt_desc.caps,
1841 PT_CAP_single_range_output)))
1842 return 1;
1843 msr_info->data = vmx->pt_desc.guest.output_base;
1844 break;
1845 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001846 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001847 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1848 PT_CAP_topa_output) &&
1849 !intel_pt_validate_cap(vmx->pt_desc.caps,
1850 PT_CAP_single_range_output)))
1851 return 1;
1852 msr_info->data = vmx->pt_desc.guest.output_mask;
1853 break;
1854 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
1855 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christopherson2ef76192020-03-02 15:56:22 -08001856 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001857 (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
1858 PT_CAP_num_address_ranges)))
1859 return 1;
1860 if (index % 2)
1861 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
1862 else
1863 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
1864 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001865 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001866 if (!msr_info->host_initiated &&
1867 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001868 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001869 goto find_uret_msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001871 find_uret_msr:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001872 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001873 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001874 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001875 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001877 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878 }
1879
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 return 0;
1881}
1882
Sean Christopherson24085002020-04-28 16:10:24 -07001883static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu,
1884 u64 data)
1885{
1886#ifdef CONFIG_X86_64
1887 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
1888 return (u32)data;
1889#endif
1890 return (unsigned long)data;
1891}
1892
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893/*
Miaohe Lin311497e2019-12-11 14:26:25 +08001894 * Writes msr value into the appropriate "register".
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895 * Returns 0 on success, non-0 otherwise.
1896 * Assumes vcpu_load() was already called.
1897 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001898static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001900 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07001901 struct vmx_uret_msr *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001902 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08001903 u32 msr_index = msr_info->index;
1904 u64 data = msr_info->data;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001905 u32 index;
Eddie Dong2cc51562007-05-21 07:28:09 +03001906
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001908 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08001909 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03001910 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001911#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001913 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001914 vmcs_writel(GUEST_FS_BASE, data);
1915 break;
1916 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001917 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918 vmcs_writel(GUEST_GS_BASE, data);
1919 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001920 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001921 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001922 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923#endif
1924 case MSR_IA32_SYSENTER_CS:
Sean Christophersonde70d272019-05-07 09:06:36 -07001925 if (is_guest_mode(vcpu))
1926 get_vmcs12(vcpu)->guest_sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927 vmcs_write32(GUEST_SYSENTER_CS, data);
1928 break;
1929 case MSR_IA32_SYSENTER_EIP:
Sean Christopherson24085002020-04-28 16:10:24 -07001930 if (is_guest_mode(vcpu)) {
1931 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07001932 get_vmcs12(vcpu)->guest_sysenter_eip = data;
Sean Christopherson24085002020-04-28 16:10:24 -07001933 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02001934 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 break;
1936 case MSR_IA32_SYSENTER_ESP:
Sean Christopherson24085002020-04-28 16:10:24 -07001937 if (is_guest_mode(vcpu)) {
1938 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07001939 get_vmcs12(vcpu)->guest_sysenter_esp = data;
Sean Christopherson24085002020-04-28 16:10:24 -07001940 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02001941 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 break;
Sean Christopherson699a1ac2019-05-07 09:06:37 -07001943 case MSR_IA32_DEBUGCTLMSR:
1944 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
1945 VM_EXIT_SAVE_DEBUG_CONTROLS)
1946 get_vmcs12(vcpu)->guest_ia32_debugctl = data;
1947
1948 ret = kvm_set_msr_common(vcpu, msr_info);
1949 break;
1950
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001951 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001952 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001953 (!msr_info->host_initiated &&
1954 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001955 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001956 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07001957 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08001959 vmcs_write64(GUEST_BNDCFGS, data);
1960 break;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001961 case MSR_IA32_UMWAIT_CONTROL:
1962 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
1963 return 1;
1964
1965 /* The reserved bit 1 and non-32 bit [63:32] should be zero */
1966 if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32)))
1967 return 1;
1968
1969 vmx->msr_ia32_umwait_control = data;
1970 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001971 case MSR_IA32_SPEC_CTRL:
1972 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001973 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1974 return 1;
1975
Maxim Levitsky841c2be2020-07-08 14:57:31 +03001976 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001977 return 1;
1978
1979 vmx->spec_ctrl = data;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001980 if (!data)
1981 break;
1982
1983 /*
1984 * For non-nested:
1985 * When it's written (to non-zero) for the first time, pass
1986 * it through.
1987 *
1988 * For nested:
1989 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08001990 * nested_vmx_prepare_msr_bitmap. We should not touch the
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001991 * vmcs02.msr_bitmap here since it gets completely overwritten
1992 * in the merging. We update the vmcs01 here for L1 as well
1993 * since it will end up touching the MSR anyway now.
1994 */
1995 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
1996 MSR_IA32_SPEC_CTRL,
1997 MSR_TYPE_RW);
1998 break;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001999 case MSR_IA32_TSX_CTRL:
2000 if (!msr_info->host_initiated &&
2001 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
2002 return 1;
2003 if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR))
2004 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002005 goto find_uret_msr;
Ashok Raj15d45072018-02-01 22:59:43 +01002006 case MSR_IA32_PRED_CMD:
2007 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01002008 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2009 return 1;
2010
2011 if (data & ~PRED_CMD_IBPB)
2012 return 1;
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002013 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL))
2014 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002015 if (!data)
2016 break;
2017
2018 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2019
2020 /*
2021 * For non-nested:
2022 * When it's written (to non-zero) for the first time, pass
2023 * it through.
2024 *
2025 * For nested:
2026 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002027 * nested_vmx_prepare_msr_bitmap. We should not touch the
Ashok Raj15d45072018-02-01 22:59:43 +01002028 * vmcs02.msr_bitmap here since it gets completely overwritten
2029 * in the merging.
2030 */
2031 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
2032 MSR_TYPE_W);
2033 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002034 case MSR_IA32_CR_PAT:
Sean Christophersond28f4292019-05-07 09:06:27 -07002035 if (!kvm_pat_valid(data))
2036 return 1;
2037
Sean Christopherson142e4be2019-05-07 09:06:35 -07002038 if (is_guest_mode(vcpu) &&
2039 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
2040 get_vmcs12(vcpu)->guest_ia32_pat = data;
2041
Sheng Yang468d4722008-10-09 16:01:55 +08002042 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2043 vmcs_write64(GUEST_IA32_PAT, data);
2044 vcpu->arch.pat = data;
2045 break;
2046 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002047 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002048 break;
Will Auldba904632012-11-29 12:42:50 -08002049 case MSR_IA32_TSC_ADJUST:
2050 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002051 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002052 case MSR_IA32_MCG_EXT_CTL:
2053 if ((!msr_info->host_initiated &&
2054 !(to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002055 FEAT_CTL_LMCE_ENABLED)) ||
Ashok Rajc45dcc72016-06-22 14:59:56 +08002056 (data & ~MCG_EXT_CTL_LMCE_EN))
2057 return 1;
2058 vcpu->arch.mcg_ext_ctl = data;
2059 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002060 case MSR_IA32_FEAT_CTL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002061 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08002062 (to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002063 FEAT_CTL_LOCKED && !msr_info->host_initiated))
Jan Kiszkacae50132014-01-04 18:47:22 +01002064 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08002065 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002066 if (msr_info->host_initiated && data == 0)
2067 vmx_leave_nested(vcpu);
2068 break;
2069 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08002070 if (!msr_info->host_initiated)
2071 return 1; /* they are read-only */
2072 if (!nested_vmx_allowed(vcpu))
2073 return 1;
2074 return vmx_set_vmx_msr(vcpu, msr_index, data);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002075 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08002076 if (!vmx_pt_mode_is_host_guest() ||
Luwei Kangee85dec2018-10-24 16:05:16 +08002077 vmx_rtit_ctl_check(vcpu, data) ||
2078 vmx->nested.vmxon)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002079 return 1;
2080 vmcs_write64(GUEST_IA32_RTIT_CTL, data);
2081 vmx->pt_desc.guest.ctl = data;
Chao Pengb08c2892018-10-24 16:05:15 +08002082 pt_update_intercept_for_msr(vmx);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002083 break;
2084 case MSR_IA32_RTIT_STATUS:
Sean Christophersone348ac72019-12-10 15:24:33 -08002085 if (!pt_can_write_msr(vmx))
2086 return 1;
2087 if (data & MSR_IA32_RTIT_STATUS_MASK)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002088 return 1;
2089 vmx->pt_desc.guest.status = data;
2090 break;
2091 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christophersone348ac72019-12-10 15:24:33 -08002092 if (!pt_can_write_msr(vmx))
2093 return 1;
2094 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2095 PT_CAP_cr3_filtering))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002096 return 1;
2097 vmx->pt_desc.guest.cr3_match = data;
2098 break;
2099 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christophersone348ac72019-12-10 15:24:33 -08002100 if (!pt_can_write_msr(vmx))
2101 return 1;
2102 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2103 PT_CAP_topa_output) &&
2104 !intel_pt_validate_cap(vmx->pt_desc.caps,
2105 PT_CAP_single_range_output))
2106 return 1;
Sean Christopherson1cc6cbc2020-09-24 12:42:48 -07002107 if (!pt_output_base_valid(vcpu, data))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002108 return 1;
2109 vmx->pt_desc.guest.output_base = data;
2110 break;
2111 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christophersone348ac72019-12-10 15:24:33 -08002112 if (!pt_can_write_msr(vmx))
2113 return 1;
2114 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2115 PT_CAP_topa_output) &&
2116 !intel_pt_validate_cap(vmx->pt_desc.caps,
2117 PT_CAP_single_range_output))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002118 return 1;
2119 vmx->pt_desc.guest.output_mask = data;
2120 break;
2121 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christophersone348ac72019-12-10 15:24:33 -08002122 if (!pt_can_write_msr(vmx))
2123 return 1;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002124 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christophersone348ac72019-12-10 15:24:33 -08002125 if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
2126 PT_CAP_num_address_ranges))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002127 return 1;
Sean Christophersonfe6ed362019-12-10 15:24:32 -08002128 if (is_noncanonical_address(data, vcpu))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002129 return 1;
2130 if (index % 2)
2131 vmx->pt_desc.guest.addr_b[index / 2] = data;
2132 else
2133 vmx->pt_desc.guest.addr_a[index / 2] = data;
2134 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002135 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002136 if (!msr_info->host_initiated &&
2137 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002138 return 1;
2139 /* Check reserved bit, higher 32 bits should be zero */
2140 if ((data >> 32) != 0)
2141 return 1;
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002142 goto find_uret_msr;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002143
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002145 find_uret_msr:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002146 msr = find_msr_entry(vmx, msr_index);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05002147 if (msr)
2148 ret = vmx_set_guest_msr(vmx, msr, data);
2149 else
2150 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151 }
2152
Eddie Dong2cc51562007-05-21 07:28:09 +03002153 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154}
2155
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002156static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157{
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002158 unsigned long guest_owned_bits;
2159
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002160 kvm_register_mark_available(vcpu, reg);
2161
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002162 switch (reg) {
2163 case VCPU_REGS_RSP:
2164 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2165 break;
2166 case VCPU_REGS_RIP:
2167 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2168 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002169 case VCPU_EXREG_PDPTR:
2170 if (enable_ept)
2171 ept_save_pdptrs(vcpu);
2172 break;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07002173 case VCPU_EXREG_CR0:
2174 guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2175
2176 vcpu->arch.cr0 &= ~guest_owned_bits;
2177 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits;
2178 break;
Sean Christopherson34059c22019-09-27 14:45:23 -07002179 case VCPU_EXREG_CR3:
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002180 if (is_unrestricted_guest(vcpu) ||
2181 (enable_ept && is_paging(vcpu)))
Sean Christopherson34059c22019-09-27 14:45:23 -07002182 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2183 break;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002184 case VCPU_EXREG_CR4:
2185 guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2186
2187 vcpu->arch.cr4 &= ~guest_owned_bits;
2188 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits;
2189 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002190 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07002191 WARN_ON_ONCE(1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002192 break;
2193 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194}
2195
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196static __init int cpu_has_kvm_support(void)
2197{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002198 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199}
2200
2201static __init int vmx_disabled_by_bios(void)
2202{
Sean Christophersona4d0b2f2019-12-20 20:45:09 -08002203 return !boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2204 !boot_cpu_has(X86_FEATURE_VMX);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205}
2206
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002207static int kvm_cpu_vmxon(u64 vmxon_pointer)
Dongxiao Xu7725b892010-05-11 18:29:38 +08002208{
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002209 u64 msr;
2210
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002211 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002212 intel_pt_handle_vmx(1);
2213
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002214 asm_volatile_goto("1: vmxon %[vmxon_pointer]\n\t"
2215 _ASM_EXTABLE(1b, %l[fault])
2216 : : [vmxon_pointer] "m"(vmxon_pointer)
2217 : : fault);
2218 return 0;
2219
2220fault:
2221 WARN_ONCE(1, "VMXON faulted, MSR_IA32_FEAT_CTL (0x3a) = 0x%llx\n",
2222 rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr) ? 0xdeadbeef : msr);
2223 intel_pt_handle_vmx(0);
2224 cr4_clear_bits(X86_CR4_VMXE);
2225
2226 return -EFAULT;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002227}
2228
Radim Krčmář13a34e02014-08-28 15:13:03 +02002229static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230{
2231 int cpu = raw_smp_processor_id();
2232 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002233 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002235 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02002236 return -EBUSY;
2237
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002238 /*
2239 * This can happen if we hot-added a CPU but failed to allocate
2240 * VP assist page for it.
2241 */
2242 if (static_branch_unlikely(&enable_evmcs) &&
2243 !hv_get_vp_assist_page(cpu))
2244 return -EFAULT;
2245
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002246 r = kvm_cpu_vmxon(phys_addr);
2247 if (r)
2248 return r;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002249
David Hildenbrandfdf288b2017-08-24 20:51:29 +02002250 if (enable_ept)
2251 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02002252
2253 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254}
2255
Nadav Har'Eld462b812011-05-24 15:26:10 +03002256static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002257{
2258 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002259 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002260
Nadav Har'Eld462b812011-05-24 15:26:10 +03002261 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2262 loaded_vmcss_on_cpu_link)
2263 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002264}
2265
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002266
2267/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2268 * tricks.
2269 */
2270static void kvm_cpu_vmxoff(void)
2271{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002272 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002273
2274 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002275 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002276}
2277
Radim Krčmář13a34e02014-08-28 15:13:03 +02002278static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002280 vmclear_local_loaded_vmcss();
2281 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282}
2283
Sean Christopherson7a57c092020-03-12 11:04:16 -07002284/*
2285 * There is no X86_FEATURE for SGX yet, but anyway we need to query CPUID
2286 * directly instead of going through cpu_has(), to ensure KVM is trapping
2287 * ENCLS whenever it's supported in hardware. It does not matter whether
2288 * the host OS supports or has enabled SGX.
2289 */
2290static bool cpu_has_sgx(void)
2291{
2292 return cpuid_eax(0) >= 0x12 && (cpuid_eax(0x12) & BIT(0));
2293}
2294
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002295static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002296 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297{
2298 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002299 u32 ctl = ctl_min | ctl_opt;
2300
2301 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2302
2303 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2304 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2305
2306 /* Ensure minimum (required) set of control bits are supported. */
2307 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002308 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002309
2310 *result = ctl;
2311 return 0;
2312}
2313
Sean Christopherson7caaa712018-12-03 13:53:01 -08002314static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
2315 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002316{
2317 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002318 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002319 u32 _pin_based_exec_control = 0;
2320 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002321 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002322 u32 _vmexit_control = 0;
2323 u32 _vmentry_control = 0;
2324
Paolo Bonzini13893092018-02-26 13:40:09 +01002325 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05302326 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002327#ifdef CONFIG_X86_64
2328 CPU_BASED_CR8_LOAD_EXITING |
2329 CPU_BASED_CR8_STORE_EXITING |
2330#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002331 CPU_BASED_CR3_LOAD_EXITING |
2332 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08002333 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002334 CPU_BASED_MOV_DR_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08002335 CPU_BASED_USE_TSC_OFFSETTING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002336 CPU_BASED_MWAIT_EXITING |
2337 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002338 CPU_BASED_INVLPG_EXITING |
2339 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002340
Sheng Yangf78e0e22007-10-29 09:40:42 +08002341 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002342 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002343 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002344 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2345 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002346 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002347#ifdef CONFIG_X86_64
2348 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2349 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2350 ~CPU_BASED_CR8_STORE_EXITING;
2351#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002352 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002353 min2 = 0;
2354 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002355 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002356 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002357 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002358 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002359 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002360 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02002361 SECONDARY_EXEC_DESC |
Sean Christopherson7f3603b2020-09-23 09:50:47 -07002362 SECONDARY_EXEC_ENABLE_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002363 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002364 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002365 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08002366 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08002367 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02002368 SECONDARY_EXEC_RDSEED_EXITING |
2369 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08002370 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04002371 SECONDARY_EXEC_TSC_SCALING |
Tao Xue69e72fa2019-07-16 14:55:49 +08002372 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Chao Pengf99e3da2018-10-24 16:05:10 +08002373 SECONDARY_EXEC_PT_USE_GPA |
2374 SECONDARY_EXEC_PT_CONCEAL_VMX |
Sean Christopherson7a57c092020-03-12 11:04:16 -07002375 SECONDARY_EXEC_ENABLE_VMFUNC;
2376 if (cpu_has_sgx())
2377 opt2 |= SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08002378 if (adjust_vmx_controls(min2, opt2,
2379 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002380 &_cpu_based_2nd_exec_control) < 0)
2381 return -EIO;
2382 }
2383#ifndef CONFIG_X86_64
2384 if (!(_cpu_based_2nd_exec_control &
2385 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2386 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2387#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002388
2389 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2390 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002391 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002392 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2393 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002394
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002395 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08002396 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002397
Sheng Yangd56f5462008-04-25 10:13:16 +08002398 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002399 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2400 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002401 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2402 CPU_BASED_CR3_STORE_EXITING |
2403 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08002404 } else if (vmx_cap->ept) {
2405 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002406 pr_warn_once("EPT CAP should not exist if not support "
2407 "1-setting enable EPT VM-execution control\n");
2408 }
2409 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002410 vmx_cap->vpid) {
2411 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002412 pr_warn_once("VPID CAP should not exist if not support "
2413 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002414 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002415
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002416 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002417#ifdef CONFIG_X86_64
2418 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2419#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002420 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002421 VM_EXIT_LOAD_IA32_PAT |
2422 VM_EXIT_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002423 VM_EXIT_CLEAR_BNDCFGS |
2424 VM_EXIT_PT_CONCEAL_PIP |
2425 VM_EXIT_CLEAR_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002426 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2427 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002428 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002429
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002430 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2431 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2432 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002433 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2434 &_pin_based_exec_control) < 0)
2435 return -EIO;
2436
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002437 if (cpu_has_broken_vmx_preemption_timer())
2438 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002439 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002440 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002441 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2442
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002443 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002444 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2445 VM_ENTRY_LOAD_IA32_PAT |
2446 VM_ENTRY_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002447 VM_ENTRY_LOAD_BNDCFGS |
2448 VM_ENTRY_PT_CONCEAL_PIP |
2449 VM_ENTRY_LOAD_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002450 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2451 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002452 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002454 /*
2455 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2456 * can't be used due to an errata where VM Exit may incorrectly clear
2457 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2458 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2459 */
2460 if (boot_cpu_data.x86 == 0x6) {
2461 switch (boot_cpu_data.x86_model) {
2462 case 26: /* AAK155 */
2463 case 30: /* AAP115 */
2464 case 37: /* AAT100 */
2465 case 44: /* BC86,AAY89,BD102 */
2466 case 46: /* BA97 */
Sean Christopherson85ba2b12019-01-14 12:12:02 -08002467 _vmentry_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002468 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2469 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2470 "does not work properly. Using workaround\n");
2471 break;
2472 default:
2473 break;
2474 }
2475 }
2476
2477
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002478 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002479
2480 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2481 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002482 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002483
2484#ifdef CONFIG_X86_64
2485 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2486 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002487 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002488#endif
2489
2490 /* Require Write-Back (WB) memory type for VMCS accesses. */
2491 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002492 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002493
Yang, Sheng002c7f72007-07-31 14:23:01 +03002494 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002495 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002496 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002497
Liran Alon2307af12018-06-29 22:59:04 +03002498 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002499
Yang, Sheng002c7f72007-07-31 14:23:01 +03002500 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2501 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002502 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002503 vmcs_conf->vmexit_ctrl = _vmexit_control;
2504 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002505
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002506 if (static_branch_unlikely(&enable_evmcs))
2507 evmcs_sanitize_exec_ctrls(vmcs_conf);
2508
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002509 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002510}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511
Ben Gardon41836832019-02-11 11:02:52 -08002512struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513{
2514 int node = cpu_to_node(cpu);
2515 struct page *pages;
2516 struct vmcs *vmcs;
2517
Ben Gardon41836832019-02-11 11:02:52 -08002518 pages = __alloc_pages_node(node, flags, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 if (!pages)
2520 return NULL;
2521 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002522 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002523
2524 /* KVM supports Enlightened VMCS v1 only */
2525 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002526 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002527 else
Liran Alon392b2f22018-06-23 02:35:01 +03002528 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002529
Liran Alon491a6032018-06-23 02:35:12 +03002530 if (shadow)
2531 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 return vmcs;
2533}
2534
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002535void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002537 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538}
2539
Nadav Har'Eld462b812011-05-24 15:26:10 +03002540/*
2541 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2542 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002543void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002544{
2545 if (!loaded_vmcs->vmcs)
2546 return;
2547 loaded_vmcs_clear(loaded_vmcs);
2548 free_vmcs(loaded_vmcs->vmcs);
2549 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002550 if (loaded_vmcs->msr_bitmap)
2551 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002552 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002553}
2554
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002555int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002556{
Liran Alon491a6032018-06-23 02:35:12 +03002557 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002558 if (!loaded_vmcs->vmcs)
2559 return -ENOMEM;
2560
Sean Christophersond260f9e2020-03-21 12:37:50 -07002561 vmcs_clear(loaded_vmcs->vmcs);
2562
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002563 loaded_vmcs->shadow_vmcs = NULL;
Sean Christopherson804939e2019-05-07 12:18:05 -07002564 loaded_vmcs->hv_timer_soft_disabled = false;
Sean Christophersond260f9e2020-03-21 12:37:50 -07002565 loaded_vmcs->cpu = -1;
2566 loaded_vmcs->launched = 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002567
2568 if (cpu_has_vmx_msr_bitmap()) {
Ben Gardon41836832019-02-11 11:02:52 -08002569 loaded_vmcs->msr_bitmap = (unsigned long *)
2570 __get_free_page(GFP_KERNEL_ACCOUNT);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002571 if (!loaded_vmcs->msr_bitmap)
2572 goto out_vmcs;
2573 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002574
Arnd Bergmann1f008e12018-05-25 17:36:17 +02002575 if (IS_ENABLED(CONFIG_HYPERV) &&
2576 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002577 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2578 struct hv_enlightened_vmcs *evmcs =
2579 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2580
2581 evmcs->hv_enlightenments_control.msr_bitmap = 1;
2582 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002583 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002584
2585 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002586 memset(&loaded_vmcs->controls_shadow, 0,
2587 sizeof(struct vmcs_controls_shadow));
Sean Christophersond7ee0392018-07-23 12:32:47 -07002588
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002589 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002590
2591out_vmcs:
2592 free_loaded_vmcs(loaded_vmcs);
2593 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002594}
2595
Sam Ravnborg39959582007-06-01 00:47:13 -07002596static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597{
2598 int cpu;
2599
Zachary Amsden3230bb42009-09-29 11:38:37 -10002600 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002602 per_cpu(vmxarea, cpu) = NULL;
2603 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604}
2605
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606static __init int alloc_kvm_area(void)
2607{
2608 int cpu;
2609
Zachary Amsden3230bb42009-09-29 11:38:37 -10002610 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002611 struct vmcs *vmcs;
2612
Ben Gardon41836832019-02-11 11:02:52 -08002613 vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614 if (!vmcs) {
2615 free_kvm_area();
2616 return -ENOMEM;
2617 }
2618
Liran Alon2307af12018-06-29 22:59:04 +03002619 /*
2620 * When eVMCS is enabled, alloc_vmcs_cpu() sets
2621 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
2622 * revision_id reported by MSR_IA32_VMX_BASIC.
2623 *
Linus Torvalds312a4662018-12-26 17:03:51 -08002624 * However, even though not explicitly documented by
Liran Alon2307af12018-06-29 22:59:04 +03002625 * TLFS, VMXArea passed as VMXON argument should
2626 * still be marked with revision_id reported by
2627 * physical CPU.
2628 */
2629 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002630 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002631
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 per_cpu(vmxarea, cpu) = vmcs;
2633 }
2634 return 0;
2635}
2636
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002637static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002638 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002640 if (!emulate_invalid_guest_state) {
2641 /*
2642 * CS and SS RPL should be equal during guest entry according
2643 * to VMX spec, but in reality it is not always so. Since vcpu
2644 * is in the middle of the transition from real mode to
2645 * protected mode it is safe to assume that RPL 0 is a good
2646 * default value.
2647 */
2648 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03002649 save->selector &= ~SEGMENT_RPL_MASK;
2650 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02002651 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002653 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654}
2655
2656static void enter_pmode(struct kvm_vcpu *vcpu)
2657{
2658 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002659 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660
Gleb Natapovd99e4152012-12-20 16:57:45 +02002661 /*
2662 * Update real mode segment cache. It may be not up-to-date if sement
2663 * register was written while vcpu was in a guest mode.
2664 */
2665 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2666 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2667 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2668 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2669 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2670 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2671
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002672 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002674 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675
2676 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002677 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2678 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679 vmcs_writel(GUEST_RFLAGS, flags);
2680
Rusty Russell66aee912007-07-17 23:34:16 +10002681 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2682 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683
2684 update_exception_bitmap(vcpu);
2685
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002686 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2687 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2688 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2689 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2690 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2691 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692}
2693
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002694static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695{
Mathias Krause772e0312012-08-30 01:30:19 +02002696 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002697 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698
Gleb Natapovd99e4152012-12-20 16:57:45 +02002699 var.dpl = 0x3;
2700 if (seg == VCPU_SREG_CS)
2701 var.type = 0x3;
2702
2703 if (!emulate_invalid_guest_state) {
2704 var.selector = var.base >> 4;
2705 var.base = var.base & 0xffff0;
2706 var.limit = 0xffff;
2707 var.g = 0;
2708 var.db = 0;
2709 var.present = 1;
2710 var.s = 1;
2711 var.l = 0;
2712 var.unusable = 0;
2713 var.type = 0x3;
2714 var.avl = 0;
2715 if (save->base & 0xf)
2716 printk_once(KERN_WARNING "kvm: segment base is not "
2717 "paragraph aligned when entering "
2718 "protected mode (seg=%d)", seg);
2719 }
2720
2721 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05002722 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002723 vmcs_write32(sf->limit, var.limit);
2724 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725}
2726
2727static void enter_rmode(struct kvm_vcpu *vcpu)
2728{
2729 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002730 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002731 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002733 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2734 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2735 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2736 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2737 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002738 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2739 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002740
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002741 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742
Gleb Natapov776e58e2011-03-13 12:34:27 +02002743 /*
2744 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002745 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002746 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002747 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002748 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2749 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002750
Avi Kivity2fb92db2011-04-27 19:42:18 +03002751 vmx_segment_cache_clear(vmx);
2752
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002753 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2756
2757 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002758 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002760 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761
2762 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002763 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 update_exception_bitmap(vcpu);
2765
Gleb Natapovd99e4152012-12-20 16:57:45 +02002766 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2767 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2768 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2769 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2770 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2771 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002772
Eddie Dong8668a3c2007-10-10 14:26:45 +08002773 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774}
2775
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002776void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Amit Shah401d10d2009-02-20 22:53:37 +05302777{
2778 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07002779 struct vmx_uret_msr *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity26bb0982009-09-07 11:14:12 +03002780
2781 if (!msr)
2782 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302783
Avi Kivityf6801df2010-01-21 15:31:50 +02002784 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302785 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002786 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302787 msr->data = efer;
2788 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002789 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302790
2791 msr->data = efer & ~EFER_LME;
2792 }
2793 setup_msrs(vmx);
2794}
2795
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002796#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797
2798static void enter_lmode(struct kvm_vcpu *vcpu)
2799{
2800 u32 guest_tr_ar;
2801
Avi Kivity2fb92db2011-04-27 19:42:18 +03002802 vmx_segment_cache_clear(to_vmx(vcpu));
2803
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002805 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002806 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2807 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002809 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
2810 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 }
Avi Kivityda38f432010-07-06 11:30:49 +03002812 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813}
2814
2815static void exit_lmode(struct kvm_vcpu *vcpu)
2816{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002817 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002818 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819}
2820
2821#endif
2822
Sean Christopherson77809382020-03-20 14:28:18 -07002823static void vmx_flush_tlb_all(struct kvm_vcpu *vcpu)
Sean Christopherson5058b692020-03-20 14:28:14 -07002824{
2825 struct vcpu_vmx *vmx = to_vmx(vcpu);
2826
2827 /*
Sean Christopherson77809382020-03-20 14:28:18 -07002828 * INVEPT must be issued when EPT is enabled, irrespective of VPID, as
2829 * the CPU is not required to invalidate guest-physical mappings on
2830 * VM-Entry, even if VPID is disabled. Guest-physical mappings are
2831 * associated with the root EPT structure and not any particular VPID
2832 * (INVVPID also isn't required to invalidate guest-physical mappings).
Sean Christopherson5058b692020-03-20 14:28:14 -07002833 */
2834 if (enable_ept) {
2835 ept_sync_global();
2836 } else if (enable_vpid) {
2837 if (cpu_has_vmx_invvpid_global()) {
2838 vpid_sync_vcpu_global();
2839 } else {
2840 vpid_sync_vcpu_single(vmx->vpid);
2841 vpid_sync_vcpu_single(vmx->nested.vpid02);
2842 }
2843 }
2844}
2845
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002846static void vmx_flush_tlb_current(struct kvm_vcpu *vcpu)
2847{
Sean Christopherson2a40b902020-07-15 20:41:18 -07002848 struct kvm_mmu *mmu = vcpu->arch.mmu;
2849 u64 root_hpa = mmu->root_hpa;
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002850
2851 /* No flush required if the current context is invalid. */
2852 if (!VALID_PAGE(root_hpa))
2853 return;
2854
2855 if (enable_ept)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002856 ept_sync_context(construct_eptp(vcpu, root_hpa,
2857 mmu->shadow_root_level));
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002858 else if (!is_guest_mode(vcpu))
2859 vpid_sync_context(to_vmx(vcpu)->vpid);
2860 else
2861 vpid_sync_context(nested_get_vpid02(vcpu));
2862}
2863
Junaid Shahidfaff8752018-06-29 13:10:05 -07002864static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
2865{
Junaid Shahidfaff8752018-06-29 13:10:05 -07002866 /*
Sean Christophersonad104b52020-03-20 14:28:11 -07002867 * vpid_sync_vcpu_addr() is a nop if vmx->vpid==0, see the comment in
2868 * vmx_flush_tlb_guest() for an explanation of why this is ok.
Junaid Shahidfaff8752018-06-29 13:10:05 -07002869 */
Sean Christophersonad104b52020-03-20 14:28:11 -07002870 vpid_sync_vcpu_addr(to_vmx(vcpu)->vpid, addr);
Junaid Shahidfaff8752018-06-29 13:10:05 -07002871}
2872
Sean Christophersone64419d2020-03-20 14:28:10 -07002873static void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu)
2874{
2875 /*
2876 * vpid_sync_context() is a nop if vmx->vpid==0, e.g. if enable_vpid==0
2877 * or a vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit
2878 * are required to flush GVA->{G,H}PA mappings from the TLB if vpid is
2879 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed),
2880 * i.e. no explicit INVVPID is necessary.
2881 */
2882 vpid_sync_context(to_vmx(vcpu)->vpid);
2883}
2884
Peter Shier43fea4e2020-08-20 16:05:45 -07002885void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08002886{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002887 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2888
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002889 if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002890 return;
2891
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02002892 if (is_pae_paging(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002893 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
2894 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
2895 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
2896 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002897 }
2898}
2899
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002900void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03002901{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002902 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2903
Sean Christopherson9932b492020-04-15 13:34:50 -07002904 if (WARN_ON_ONCE(!is_pae_paging(vcpu)))
2905 return;
2906
2907 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2908 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2909 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2910 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002911
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002912 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002913}
2914
Sheng Yang14394422008-04-28 12:24:45 +08002915static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2916 unsigned long cr0,
2917 struct kvm_vcpu *vcpu)
2918{
Sean Christopherson2183f562019-05-07 12:17:56 -07002919 struct vcpu_vmx *vmx = to_vmx(vcpu);
2920
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002921 if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
Sean Christopherson34059c22019-09-27 14:45:23 -07002922 vmx_cache_reg(vcpu, VCPU_EXREG_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08002923 if (!(cr0 & X86_CR0_PG)) {
2924 /* From paging/starting to nonpaging */
Sean Christopherson2183f562019-05-07 12:17:56 -07002925 exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
2926 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08002927 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002928 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002929 } else if (!is_paging(vcpu)) {
2930 /* From nonpaging to paging */
Sean Christopherson2183f562019-05-07 12:17:56 -07002931 exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
2932 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08002933 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002934 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002935 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002936
2937 if (!(cr0 & X86_CR0_WP))
2938 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002939}
2940
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002941void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002943 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002944 unsigned long hw_cr0;
2945
Sean Christopherson3de63472018-07-13 08:42:30 -07002946 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002947 if (is_unrestricted_guest(vcpu))
Gleb Natapov50378782013-02-04 16:00:28 +02002948 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02002949 else {
Gleb Natapov50378782013-02-04 16:00:28 +02002950 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002951
Gleb Natapov218e7632013-01-21 15:36:45 +02002952 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
2953 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954
Gleb Natapov218e7632013-01-21 15:36:45 +02002955 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
2956 enter_rmode(vcpu);
2957 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002959#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002960 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002961 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10002963 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 exit_lmode(vcpu);
2965 }
2966#endif
2967
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00002968 if (enable_ept && !is_unrestricted_guest(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08002969 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2970
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002972 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002973 vcpu->arch.cr0 = cr0;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07002974 kvm_register_mark_available(vcpu, VCPU_EXREG_CR0);
Gleb Natapov14168782013-01-21 15:36:49 +02002975
2976 /* depends on vcpu->arch.cr0 to be set to a new value */
2977 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978}
2979
Sean Christophersond468d942020-07-15 20:41:20 -07002980static int vmx_get_max_tdp_level(void)
Sean Christopherson0047fca2020-05-01 21:32:33 -07002981{
Sean Christophersond468d942020-07-15 20:41:20 -07002982 if (cpu_has_vmx_ept_5levels())
Sean Christopherson0047fca2020-05-01 21:32:33 -07002983 return 5;
2984 return 4;
2985}
2986
Sean Christopherson2a40b902020-07-15 20:41:18 -07002987u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,
2988 int root_level)
Sheng Yang14394422008-04-28 12:24:45 +08002989{
Yu Zhang855feb62017-08-24 20:27:55 +08002990 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08002991
Sean Christopherson2a40b902020-07-15 20:41:18 -07002992 eptp |= (root_level == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08002993
Peter Feiner995f00a2017-06-30 17:26:32 -07002994 if (enable_ept_ad_bits &&
2995 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02002996 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08002997 eptp |= (root_hpa & PAGE_MASK);
2998
2999 return eptp;
3000}
3001
Sean Christopherson2a40b902020-07-15 20:41:18 -07003002static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,
3003 int pgd_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004{
Tianyu Lan877ad952018-07-19 08:40:23 +00003005 struct kvm *kvm = vcpu->kvm;
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003006 bool update_guest_cr3 = true;
Sheng Yang14394422008-04-28 12:24:45 +08003007 unsigned long guest_cr3;
3008 u64 eptp;
3009
Avi Kivity089d0342009-03-23 18:26:32 +02003010 if (enable_ept) {
Sean Christopherson2a40b902020-07-15 20:41:18 -07003011 eptp = construct_eptp(vcpu, pgd, pgd_level);
Sheng Yang14394422008-04-28 12:24:45 +08003012 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00003013
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003014 if (kvm_x86_ops.tlb_remote_flush) {
Tianyu Lan877ad952018-07-19 08:40:23 +00003015 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3016 to_vmx(vcpu)->ept_pointer = eptp;
3017 to_kvm_vmx(kvm)->ept_pointers_match
3018 = EPT_POINTERS_CHECK;
3019 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3020 }
3021
Paolo Bonzinidf7e0682020-05-20 08:37:37 -04003022 if (!enable_unrestricted_guest && !is_paging(vcpu))
Tianyu Lan877ad952018-07-19 08:40:23 +00003023 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Sean Christophersonb17b7432019-09-27 14:45:17 -07003024 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3025 guest_cr3 = vcpu->arch.cr3;
3026 else /* vmcs01.GUEST_CR3 is already up-to-date. */
3027 update_guest_cr3 = false;
Peter Shier43fea4e2020-08-20 16:05:45 -07003028 vmx_ept_load_pdptrs(vcpu);
Sean Christophersonbe100ef2020-03-20 14:28:33 -07003029 } else {
3030 guest_cr3 = pgd;
Sheng Yang14394422008-04-28 12:24:45 +08003031 }
3032
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003033 if (update_guest_cr3)
3034 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035}
3036
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003037int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003039 struct vcpu_vmx *vmx = to_vmx(vcpu);
Ben Serebrin085e68e2015-04-16 11:58:05 -07003040 /*
3041 * Pass through host's Machine Check Enable value to hw_cr4, which
3042 * is in force while we are in guest mode. Do not let guests control
3043 * this bit, even if host CR4.MCE == 0.
3044 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003045 unsigned long hw_cr4;
3046
3047 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003048 if (is_unrestricted_guest(vcpu))
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003049 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003050 else if (vmx->rmode.vm86_active)
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003051 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3052 else
3053 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003054
Sean Christopherson64f7a112018-04-30 10:01:06 -07003055 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3056 if (cr4 & X86_CR4_UMIP) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003057 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07003058 hw_cr4 &= ~X86_CR4_UMIP;
3059 } else if (!is_guest_mode(vcpu) ||
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003060 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
3061 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
3062 }
Sean Christopherson64f7a112018-04-30 10:01:06 -07003063 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02003064
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003065 if (cr4 & X86_CR4_VMXE) {
3066 /*
3067 * To use VMXON (and later other VMX instructions), a guest
3068 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3069 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003070 * is here. We operate under the default treatment of SMM,
3071 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003072 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003073 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003074 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003075 }
David Matlack38991522016-11-29 18:14:08 -08003076
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003077 if (vmx->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003078 return 1;
3079
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003080 vcpu->arch.cr4 = cr4;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07003081 kvm_register_mark_available(vcpu, VCPU_EXREG_CR4);
Sheng Yang14394422008-04-28 12:24:45 +08003082
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003083 if (!is_unrestricted_guest(vcpu)) {
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003084 if (enable_ept) {
3085 if (!is_paging(vcpu)) {
3086 hw_cr4 &= ~X86_CR4_PAE;
3087 hw_cr4 |= X86_CR4_PSE;
3088 } else if (!(cr4 & X86_CR4_PAE)) {
3089 hw_cr4 &= ~X86_CR4_PAE;
3090 }
3091 }
3092
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003093 /*
Huaitong Handdba2622016-03-22 16:51:15 +08003094 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3095 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3096 * to be manually disabled when guest switches to non-paging
3097 * mode.
3098 *
3099 * If !enable_unrestricted_guest, the CPU is always running
3100 * with CR0.PG=1 and CR4 needs to be modified.
3101 * If enable_unrestricted_guest, the CPU automatically
3102 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003103 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003104 if (!is_paging(vcpu))
3105 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3106 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003107
Sheng Yang14394422008-04-28 12:24:45 +08003108 vmcs_writel(CR4_READ_SHADOW, cr4);
3109 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003110 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111}
3112
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003113void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114{
Avi Kivitya9179492011-01-03 14:28:52 +02003115 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116 u32 ar;
3117
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003118 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003119 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003120 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003121 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003122 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003123 var->base = vmx_read_guest_seg_base(vmx, seg);
3124 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3125 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003126 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003127 var->base = vmx_read_guest_seg_base(vmx, seg);
3128 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3129 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3130 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003131 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132 var->type = ar & 15;
3133 var->s = (ar >> 4) & 1;
3134 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003135 /*
3136 * Some userspaces do not preserve unusable property. Since usable
3137 * segment has to be present according to VMX spec we can use present
3138 * property to amend userspace bug by making unusable segment always
3139 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3140 * segment as unusable.
3141 */
3142 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 var->avl = (ar >> 12) & 1;
3144 var->l = (ar >> 13) & 1;
3145 var->db = (ar >> 14) & 1;
3146 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147}
3148
Avi Kivitya9179492011-01-03 14:28:52 +02003149static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3150{
Avi Kivitya9179492011-01-03 14:28:52 +02003151 struct kvm_segment s;
3152
3153 if (to_vmx(vcpu)->rmode.vm86_active) {
3154 vmx_get_segment(vcpu, &s, seg);
3155 return s.base;
3156 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003157 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003158}
3159
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003160int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003161{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003162 struct vcpu_vmx *vmx = to_vmx(vcpu);
3163
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003164 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003165 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003166 else {
3167 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003168 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003169 }
Avi Kivity69c73022011-03-07 15:26:44 +02003170}
3171
Avi Kivity653e3102007-05-07 10:55:37 +03003172static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 u32 ar;
3175
Avi Kivityf0495f92012-06-07 17:06:10 +03003176 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177 ar = 1 << 16;
3178 else {
3179 ar = var->type & 15;
3180 ar |= (var->s & 1) << 4;
3181 ar |= (var->dpl & 3) << 5;
3182 ar |= (var->present & 1) << 7;
3183 ar |= (var->avl & 1) << 12;
3184 ar |= (var->l & 1) << 13;
3185 ar |= (var->db & 1) << 14;
3186 ar |= (var->g & 1) << 15;
3187 }
Avi Kivity653e3102007-05-07 10:55:37 +03003188
3189 return ar;
3190}
3191
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003192void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03003193{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003194 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003195 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003196
Avi Kivity2fb92db2011-04-27 19:42:18 +03003197 vmx_segment_cache_clear(vmx);
3198
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003199 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3200 vmx->rmode.segs[seg] = *var;
3201 if (seg == VCPU_SREG_TR)
3202 vmcs_write16(sf->selector, var->selector);
3203 else if (var->s)
3204 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003205 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003206 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003207
Avi Kivity653e3102007-05-07 10:55:37 +03003208 vmcs_writel(sf->base, var->base);
3209 vmcs_write32(sf->limit, var->limit);
3210 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003211
3212 /*
3213 * Fix the "Accessed" bit in AR field of segment registers for older
3214 * qemu binaries.
3215 * IA32 arch specifies that at the time of processor reset the
3216 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003217 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003218 * state vmexit when "unrestricted guest" mode is turned on.
3219 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3220 * tree. Newer qemu binaries with that qemu fix would not need this
3221 * kvm hack.
3222 */
Krish Sadhukhanbddd82d2020-09-21 08:10:25 +00003223 if (is_unrestricted_guest(vcpu) && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003224 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003225
Gleb Natapovf924d662012-12-12 19:10:55 +02003226 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003227
3228out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003229 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230}
3231
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3233{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003234 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235
3236 *db = (ar >> 14) & 1;
3237 *l = (ar >> 13) & 1;
3238}
3239
Gleb Natapov89a27f42010-02-16 10:51:48 +02003240static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003242 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3243 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244}
3245
Gleb Natapov89a27f42010-02-16 10:51:48 +02003246static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003248 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3249 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250}
3251
Gleb Natapov89a27f42010-02-16 10:51:48 +02003252static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003254 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3255 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256}
3257
Gleb Natapov89a27f42010-02-16 10:51:48 +02003258static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003260 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3261 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262}
3263
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003264static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3265{
3266 struct kvm_segment var;
3267 u32 ar;
3268
3269 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003270 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003271 if (seg == VCPU_SREG_CS)
3272 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003273 ar = vmx_segment_access_rights(&var);
3274
3275 if (var.base != (var.selector << 4))
3276 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003277 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003278 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003279 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003280 return false;
3281
3282 return true;
3283}
3284
3285static bool code_segment_valid(struct kvm_vcpu *vcpu)
3286{
3287 struct kvm_segment cs;
3288 unsigned int cs_rpl;
3289
3290 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003291 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003292
Avi Kivity1872a3f2009-01-04 23:26:52 +02003293 if (cs.unusable)
3294 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003295 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003296 return false;
3297 if (!cs.s)
3298 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003299 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003300 if (cs.dpl > cs_rpl)
3301 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003302 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003303 if (cs.dpl != cs_rpl)
3304 return false;
3305 }
3306 if (!cs.present)
3307 return false;
3308
3309 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3310 return true;
3311}
3312
3313static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3314{
3315 struct kvm_segment ss;
3316 unsigned int ss_rpl;
3317
3318 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003319 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003320
Avi Kivity1872a3f2009-01-04 23:26:52 +02003321 if (ss.unusable)
3322 return true;
3323 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003324 return false;
3325 if (!ss.s)
3326 return false;
3327 if (ss.dpl != ss_rpl) /* DPL != RPL */
3328 return false;
3329 if (!ss.present)
3330 return false;
3331
3332 return true;
3333}
3334
3335static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3336{
3337 struct kvm_segment var;
3338 unsigned int rpl;
3339
3340 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03003341 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003342
Avi Kivity1872a3f2009-01-04 23:26:52 +02003343 if (var.unusable)
3344 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003345 if (!var.s)
3346 return false;
3347 if (!var.present)
3348 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003349 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003350 if (var.dpl < rpl) /* DPL < RPL */
3351 return false;
3352 }
3353
3354 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3355 * rights flags
3356 */
3357 return true;
3358}
3359
3360static bool tr_valid(struct kvm_vcpu *vcpu)
3361{
3362 struct kvm_segment tr;
3363
3364 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3365
Avi Kivity1872a3f2009-01-04 23:26:52 +02003366 if (tr.unusable)
3367 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03003368 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003369 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003370 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003371 return false;
3372 if (!tr.present)
3373 return false;
3374
3375 return true;
3376}
3377
3378static bool ldtr_valid(struct kvm_vcpu *vcpu)
3379{
3380 struct kvm_segment ldtr;
3381
3382 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3383
Avi Kivity1872a3f2009-01-04 23:26:52 +02003384 if (ldtr.unusable)
3385 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03003386 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003387 return false;
3388 if (ldtr.type != 2)
3389 return false;
3390 if (!ldtr.present)
3391 return false;
3392
3393 return true;
3394}
3395
3396static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3397{
3398 struct kvm_segment cs, ss;
3399
3400 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3401 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3402
Nadav Amitb32a9912015-03-29 16:33:04 +03003403 return ((cs.selector & SEGMENT_RPL_MASK) ==
3404 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003405}
3406
3407/*
3408 * Check if guest state is valid. Returns true if valid, false if
3409 * not.
3410 * We assume that registers are always usable
3411 */
Sean Christopherson2ba44932020-09-23 11:44:48 -07003412bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003413{
3414 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003415 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003416 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3417 return false;
3418 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3419 return false;
3420 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3421 return false;
3422 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3423 return false;
3424 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3425 return false;
3426 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3427 return false;
3428 } else {
3429 /* protected mode guest state checks */
3430 if (!cs_ss_rpl_check(vcpu))
3431 return false;
3432 if (!code_segment_valid(vcpu))
3433 return false;
3434 if (!stack_segment_valid(vcpu))
3435 return false;
3436 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3437 return false;
3438 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3439 return false;
3440 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3441 return false;
3442 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3443 return false;
3444 if (!tr_valid(vcpu))
3445 return false;
3446 if (!ldtr_valid(vcpu))
3447 return false;
3448 }
3449 /* TODO:
3450 * - Add checks on RIP
3451 * - Add checks on RFLAGS
3452 */
3453
3454 return true;
3455}
3456
Mike Dayd77c26f2007-10-08 09:02:08 -04003457static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003459 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003460 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003461 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003463 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003464 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003465 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3466 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003467 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003468 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003469 r = kvm_write_guest_page(kvm, fn++, &data,
3470 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003471 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003472 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003473 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3474 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003475 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003476 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3477 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003478 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003479 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003480 r = kvm_write_guest_page(kvm, fn, &data,
3481 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3482 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003483out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003484 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003485 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003486}
3487
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003488static int init_rmode_identity_map(struct kvm *kvm)
3489{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003490 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Peter Xu2a5755b2020-01-09 09:57:14 -05003491 int i, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08003492 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003493 u32 tmp;
3494
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003495 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003496 mutex_lock(&kvm->slots_lock);
3497
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003498 if (likely(kvm_vmx->ept_identity_pagetable_done))
Peter Xu2a5755b2020-01-09 09:57:14 -05003499 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003500
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003501 if (!kvm_vmx->ept_identity_map_addr)
3502 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
3503 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08003504
David Hildenbrandd8a6e362017-08-24 20:51:34 +02003505 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003506 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08003507 if (r < 0)
Peter Xu2a5755b2020-01-09 09:57:14 -05003508 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003509
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003510 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3511 if (r < 0)
3512 goto out;
3513 /* Set up identity-mapping pagetable for EPT in real mode */
3514 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3515 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3516 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3517 r = kvm_write_guest_page(kvm, identity_map_pfn,
3518 &tmp, i * sizeof(tmp), sizeof(tmp));
3519 if (r < 0)
3520 goto out;
3521 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003522 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003523
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003524out:
Tang Chena255d472014-09-16 18:41:58 +08003525 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003526 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003527}
3528
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529static void seg_setup(int seg)
3530{
Mathias Krause772e0312012-08-30 01:30:19 +02003531 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003532 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533
3534 vmcs_write16(sf->selector, 0);
3535 vmcs_writel(sf->base, 0);
3536 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003537 ar = 0x93;
3538 if (seg == VCPU_SREG_CS)
3539 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003540
3541 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542}
3543
Sheng Yangf78e0e22007-10-29 09:40:42 +08003544static int alloc_apic_access_page(struct kvm *kvm)
3545{
Xiao Guangrong44841412012-09-07 14:14:20 +08003546 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003547 int r = 0;
3548
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003549 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003550 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003551 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02003552 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3553 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003554 if (r)
3555 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003556
Tang Chen73a6d942014-09-11 13:38:00 +08003557 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003558 if (is_error_page(page)) {
3559 r = -EFAULT;
3560 goto out;
3561 }
3562
Tang Chenc24ae0d2014-09-24 15:57:58 +08003563 /*
3564 * Do not pin the page in memory, so that memory hot-unplug
3565 * is able to migrate it.
3566 */
3567 put_page(page);
3568 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003569out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003570 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003571 return r;
3572}
3573
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003574int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003575{
3576 int vpid;
3577
Avi Kivity919818a2009-03-23 18:01:29 +02003578 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003579 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003580 spin_lock(&vmx_vpid_lock);
3581 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003582 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003583 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003584 else
3585 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003586 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003587 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003588}
3589
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003590void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003591{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003592 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003593 return;
3594 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003595 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003596 spin_unlock(&vmx_vpid_lock);
3597}
3598
Yi Wang1e4329ee2018-11-08 11:22:21 +08003599static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003600 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003601{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003602 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003603
3604 if (!cpu_has_vmx_msr_bitmap())
3605 return;
3606
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003607 if (static_branch_unlikely(&enable_evmcs))
3608 evmcs_touch_msr_bitmap();
3609
Sheng Yang25c5f222008-03-28 13:18:56 +08003610 /*
3611 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3612 * have the write-low and read-high bitmap offsets the wrong way round.
3613 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3614 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003615 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003616 if (type & MSR_TYPE_R)
3617 /* read-low */
3618 __clear_bit(msr, msr_bitmap + 0x000 / f);
3619
3620 if (type & MSR_TYPE_W)
3621 /* write-low */
3622 __clear_bit(msr, msr_bitmap + 0x800 / f);
3623
Sheng Yang25c5f222008-03-28 13:18:56 +08003624 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3625 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003626 if (type & MSR_TYPE_R)
3627 /* read-high */
3628 __clear_bit(msr, msr_bitmap + 0x400 / f);
3629
3630 if (type & MSR_TYPE_W)
3631 /* write-high */
3632 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3633
3634 }
3635}
3636
Yi Wang1e4329ee2018-11-08 11:22:21 +08003637static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003638 u32 msr, int type)
3639{
3640 int f = sizeof(unsigned long);
3641
3642 if (!cpu_has_vmx_msr_bitmap())
3643 return;
3644
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003645 if (static_branch_unlikely(&enable_evmcs))
3646 evmcs_touch_msr_bitmap();
3647
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003648 /*
3649 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3650 * have the write-low and read-high bitmap offsets the wrong way round.
3651 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3652 */
3653 if (msr <= 0x1fff) {
3654 if (type & MSR_TYPE_R)
3655 /* read-low */
3656 __set_bit(msr, msr_bitmap + 0x000 / f);
3657
3658 if (type & MSR_TYPE_W)
3659 /* write-low */
3660 __set_bit(msr, msr_bitmap + 0x800 / f);
3661
3662 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3663 msr &= 0x1fff;
3664 if (type & MSR_TYPE_R)
3665 /* read-high */
3666 __set_bit(msr, msr_bitmap + 0x400 / f);
3667
3668 if (type & MSR_TYPE_W)
3669 /* write-high */
3670 __set_bit(msr, msr_bitmap + 0xc00 / f);
3671
3672 }
3673}
3674
Yi Wang1e4329ee2018-11-08 11:22:21 +08003675static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003676 u32 msr, int type, bool value)
3677{
3678 if (value)
3679 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
3680 else
3681 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
3682}
3683
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003684static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003685{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003686 u8 mode = 0;
3687
3688 if (cpu_has_secondary_exec_ctrls() &&
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003689 (secondary_exec_controls_get(to_vmx(vcpu)) &
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003690 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3691 mode |= MSR_BITMAP_MODE_X2APIC;
3692 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3693 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3694 }
3695
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003696 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003697}
3698
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003699static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
3700 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003701{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003702 int msr;
3703
3704 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3705 unsigned word = msr / BITS_PER_LONG;
3706 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
3707 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003708 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003709
3710 if (mode & MSR_BITMAP_MODE_X2APIC) {
3711 /*
3712 * TPR reads and writes can be virtualized even if virtual interrupt
3713 * delivery is not in use.
3714 */
3715 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
3716 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
3717 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
3718 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3719 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
3720 }
3721 }
3722}
3723
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003724void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003725{
3726 struct vcpu_vmx *vmx = to_vmx(vcpu);
3727 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3728 u8 mode = vmx_msr_bitmap_mode(vcpu);
3729 u8 changed = mode ^ vmx->msr_bitmap_mode;
3730
3731 if (!changed)
3732 return;
3733
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003734 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
3735 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
3736
3737 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003738}
3739
Chao Pengb08c2892018-10-24 16:05:15 +08003740void pt_update_intercept_for_msr(struct vcpu_vmx *vmx)
3741{
3742 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3743 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
3744 u32 i;
3745
3746 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_STATUS,
3747 MSR_TYPE_RW, flag);
3748 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_BASE,
3749 MSR_TYPE_RW, flag);
3750 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_MASK,
3751 MSR_TYPE_RW, flag);
3752 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_CR3_MATCH,
3753 MSR_TYPE_RW, flag);
3754 for (i = 0; i < vmx->pt_desc.addr_range; i++) {
3755 vmx_set_intercept_for_msr(msr_bitmap,
3756 MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
3757 vmx_set_intercept_for_msr(msr_bitmap,
3758 MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
3759 }
3760}
3761
Liran Alone6c67d82018-09-04 10:56:52 +03003762static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3763{
3764 struct vcpu_vmx *vmx = to_vmx(vcpu);
3765 void *vapic_page;
3766 u32 vppr;
3767 int rvi;
3768
3769 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3770 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003771 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
Liran Alone6c67d82018-09-04 10:56:52 +03003772 return false;
3773
Paolo Bonzini7e712682018-10-03 13:44:26 +02003774 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003775
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003776 vapic_page = vmx->nested.virtual_apic_map.hva;
Liran Alone6c67d82018-09-04 10:56:52 +03003777 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
Liran Alone6c67d82018-09-04 10:56:52 +03003778
3779 return ((rvi & 0xf0) > (vppr & 0xf0));
3780}
3781
Wincy Van06a55242017-04-28 13:13:59 +08003782static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
3783 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003784{
3785#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08003786 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
3787
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003788 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08003789 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003790 * The vector of interrupt to be delivered to vcpu had
3791 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08003792 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003793 * Following cases will be reached in this block, and
3794 * we always send a notification event in all cases as
3795 * explained below.
3796 *
3797 * Case 1: vcpu keeps in non-root mode. Sending a
3798 * notification event posts the interrupt to vcpu.
3799 *
3800 * Case 2: vcpu exits to root mode and is still
3801 * runnable. PIR will be synced to vIRR before the
3802 * next vcpu entry. Sending a notification event in
3803 * this case has no effect, as vcpu is not in root
3804 * mode.
3805 *
3806 * Case 3: vcpu exits to root mode and is blocked.
3807 * vcpu_block() has already synced PIR to vIRR and
3808 * never blocks vcpu if vIRR is not cleared. Therefore,
3809 * a blocked vcpu here does not wait for any requested
3810 * interrupts in PIR, and sending a notification event
3811 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08003812 */
Feng Wu28b835d2015-09-18 22:29:54 +08003813
Wincy Van06a55242017-04-28 13:13:59 +08003814 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003815 return true;
3816 }
3817#endif
3818 return false;
3819}
3820
Wincy Van705699a2015-02-03 23:58:17 +08003821static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
3822 int vector)
3823{
3824 struct vcpu_vmx *vmx = to_vmx(vcpu);
3825
3826 if (is_guest_mode(vcpu) &&
3827 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08003828 /*
3829 * If a posted intr is not recognized by hardware,
3830 * we will accomplish it in the next vmentry.
3831 */
3832 vmx->nested.pi_pending = true;
3833 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02003834 /* the PIR and ON have been set by L1. */
3835 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
3836 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08003837 return 0;
3838 }
3839 return -1;
3840}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841/*
Yang Zhanga20ed542013-04-11 19:25:15 +08003842 * Send interrupt to vcpu via posted interrupt way.
3843 * 1. If target vcpu is running(non-root mode), send posted interrupt
3844 * notification to vcpu and hardware will sync PIR to vIRR atomically.
3845 * 2. If target vcpu isn't running(root mode), kick it to pick up the
3846 * interrupt from PIR in next vmentry.
3847 */
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003848static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
Yang Zhanga20ed542013-04-11 19:25:15 +08003849{
3850 struct vcpu_vmx *vmx = to_vmx(vcpu);
3851 int r;
3852
Wincy Van705699a2015-02-03 23:58:17 +08003853 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
3854 if (!r)
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003855 return 0;
3856
3857 if (!vcpu->arch.apicv_active)
3858 return -1;
Wincy Van705699a2015-02-03 23:58:17 +08003859
Yang Zhanga20ed542013-04-11 19:25:15 +08003860 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003861 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003862
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003863 /* If a previous notification has sent the IPI, nothing to do. */
3864 if (pi_test_and_set_on(&vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003865 return 0;
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003866
Wanpeng Li379a3c82020-04-28 14:23:27 +08003867 if (vcpu != kvm_get_running_vcpu() &&
3868 !kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08003869 kvm_vcpu_kick(vcpu);
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003870
3871 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003872}
3873
Avi Kivity6aa8b732006-12-10 02:21:36 -08003874/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003875 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3876 * will not change in the lifetime of the guest.
3877 * Note that host-state that does change is set elsewhere. E.g., host-state
3878 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3879 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003880void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003881{
3882 u32 low32, high32;
3883 unsigned long tmpl;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003884 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003885
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07003886 cr0 = read_cr0();
3887 WARN_ON(cr0 & X86_CR0_TS);
3888 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003889
3890 /*
3891 * Save the most likely value for this task's CR3 in the VMCS.
3892 * We can't use __get_current_cr3_fast() because we're not atomic.
3893 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07003894 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003895 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003896 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003897
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003898 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003899 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003900 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003901 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003902
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003903 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003904#ifdef CONFIG_X86_64
3905 /*
3906 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003907 * vmx_prepare_switch_to_host(), in case userspace uses
3908 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03003909 */
3910 vmcs_write16(HOST_DS_SELECTOR, 0);
3911 vmcs_write16(HOST_ES_SELECTOR, 0);
3912#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003913 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3914 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003915#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003916 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3917 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3918
Sean Christopherson23420802019-04-19 22:50:57 -07003919 vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003920
Sean Christopherson453eafb2018-12-20 12:25:17 -08003921 vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003922
3923 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
3924 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
3925 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
3926 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
3927
3928 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3929 rdmsr(MSR_IA32_CR_PAT, low32, high32);
3930 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
3931 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003932
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003933 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003934 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003935}
3936
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003937void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003938{
Sean Christophersonfa71e952020-07-02 21:04:22 -07003939 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS;
3940 if (!enable_ept)
3941 vmx->vcpu.arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003942 if (is_guest_mode(&vmx->vcpu))
3943 vmx->vcpu.arch.cr4_guest_owned_bits &=
3944 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003945 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
3946}
3947
Sean Christophersonc075c3e2019-05-07 12:17:53 -07003948u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
Yang Zhang01e439b2013-04-11 19:25:12 +08003949{
3950 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
3951
Andrey Smetanind62caab2015-11-10 15:36:33 +03003952 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08003953 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003954
3955 if (!enable_vnmi)
3956 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
3957
Sean Christopherson804939e2019-05-07 12:18:05 -07003958 if (!enable_preemption_timer)
3959 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
3960
Yang Zhang01e439b2013-04-11 19:25:12 +08003961 return pin_based_exec_ctrl;
3962}
3963
Andrey Smetanind62caab2015-11-10 15:36:33 +03003964static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
3965{
3966 struct vcpu_vmx *vmx = to_vmx(vcpu);
3967
Sean Christophersonc5f2c762019-05-07 12:17:55 -07003968 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03003969 if (cpu_has_secondary_exec_ctrls()) {
3970 if (kvm_vcpu_apicv_active(vcpu))
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003971 secondary_exec_controls_setbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03003972 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3973 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3974 else
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003975 secondary_exec_controls_clearbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03003976 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3977 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3978 }
3979
3980 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003981 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003982}
3983
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08003984u32 vmx_exec_control(struct vcpu_vmx *vmx)
3985{
3986 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
3987
3988 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
3989 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
3990
3991 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
3992 exec_control &= ~CPU_BASED_TPR_SHADOW;
3993#ifdef CONFIG_X86_64
3994 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3995 CPU_BASED_CR8_LOAD_EXITING;
3996#endif
3997 }
3998 if (!enable_ept)
3999 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4000 CPU_BASED_CR3_LOAD_EXITING |
4001 CPU_BASED_INVLPG_EXITING;
4002 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4003 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4004 CPU_BASED_MONITOR_EXITING);
4005 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4006 exec_control &= ~CPU_BASED_HLT_EXITING;
4007 return exec_control;
4008}
4009
Sean Christopherson8b50b922020-09-24 17:30:11 -07004010/*
4011 * Adjust a single secondary execution control bit to intercept/allow an
4012 * instruction in the guest. This is usually done based on whether or not a
4013 * feature has been exposed to the guest in order to correctly emulate faults.
4014 */
4015static inline void
4016vmx_adjust_secondary_exec_control(struct vcpu_vmx *vmx, u32 *exec_control,
4017 u32 control, bool enabled, bool exiting)
4018{
4019 /*
4020 * If the control is for an opt-in feature, clear the control if the
4021 * feature is not exposed to the guest, i.e. not enabled. If the
4022 * control is opt-out, i.e. an exiting control, clear the control if
4023 * the feature _is_ exposed to the guest, i.e. exiting/interception is
4024 * disabled for the associated instruction. Note, the caller is
4025 * responsible presetting exec_control to set all supported bits.
4026 */
4027 if (enabled == exiting)
4028 *exec_control &= ~control;
4029
4030 /*
4031 * Update the nested MSR settings so that a nested VMM can/can't set
4032 * controls for features that are/aren't exposed to the guest.
4033 */
4034 if (nested) {
4035 if (enabled)
4036 vmx->nested.msrs.secondary_ctls_high |= control;
4037 else
4038 vmx->nested.msrs.secondary_ctls_high &= ~control;
4039 }
4040}
4041
4042/*
4043 * Wrapper macro for the common case of adjusting a secondary execution control
4044 * based on a single guest CPUID bit, with a dedicated feature bit. This also
4045 * verifies that the control is actually supported by KVM and hardware.
4046 */
4047#define vmx_adjust_sec_exec_control(vmx, exec_control, name, feat_name, ctrl_name, exiting) \
4048({ \
4049 bool __enabled; \
4050 \
4051 if (cpu_has_vmx_##name()) { \
4052 __enabled = guest_cpuid_has(&(vmx)->vcpu, \
4053 X86_FEATURE_##feat_name); \
4054 vmx_adjust_secondary_exec_control(vmx, exec_control, \
4055 SECONDARY_EXEC_##ctrl_name, __enabled, exiting); \
4056 } \
4057})
4058
4059/* More macro magic for ENABLE_/opt-in versus _EXITING/opt-out controls. */
4060#define vmx_adjust_sec_exec_feature(vmx, exec_control, lname, uname) \
4061 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, ENABLE_##uname, false)
4062
4063#define vmx_adjust_sec_exec_exiting(vmx, exec_control, lname, uname) \
4064 vmx_adjust_sec_exec_control(vmx, exec_control, lname, uname, uname##_EXITING, true)
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004065
Paolo Bonzini80154d72017-08-24 13:55:35 +02004066static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004067{
Paolo Bonzini80154d72017-08-24 13:55:35 +02004068 struct kvm_vcpu *vcpu = &vmx->vcpu;
4069
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004070 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004071
Sean Christopherson2ef76192020-03-02 15:56:22 -08004072 if (vmx_pt_mode_is_system())
Chao Pengf99e3da2018-10-24 16:05:10 +08004073 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004074 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004075 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4076 if (vmx->vpid == 0)
4077 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4078 if (!enable_ept) {
4079 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4080 enable_unrestricted_guest = 0;
4081 }
4082 if (!enable_unrestricted_guest)
4083 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004084 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004085 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02004086 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004087 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4088 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004089 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004090
4091 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4092 * in vmx_set_cr4. */
4093 exec_control &= ~SECONDARY_EXEC_DESC;
4094
Abel Gordonabc4fc52013-04-18 14:35:25 +03004095 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4096 (handle_vmptrld).
4097 We can NOT enable shadow_vmcs here because we don't have yet
4098 a current VMCS12
4099 */
4100 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004101
4102 if (!enable_pml)
4103 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004104
Sean Christophersonbecdad82020-09-23 09:50:45 -07004105 if (cpu_has_vmx_xsaves()) {
Paolo Bonzini3db13482017-08-24 14:48:03 +02004106 /* Exposing XSAVES only when XSAVE is exposed */
4107 bool xsaves_enabled =
Sean Christopherson96be4e02019-12-10 14:44:15 -08004108 boot_cpu_has(X86_FEATURE_XSAVE) &&
Paolo Bonzini3db13482017-08-24 14:48:03 +02004109 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4110 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4111
Aaron Lewis72041602019-10-21 16:30:20 -07004112 vcpu->arch.xsaves_enabled = xsaves_enabled;
4113
Sean Christopherson8b50b922020-09-24 17:30:11 -07004114 vmx_adjust_secondary_exec_control(vmx, &exec_control,
4115 SECONDARY_EXEC_XSAVES,
4116 xsaves_enabled, false);
Paolo Bonzini3db13482017-08-24 14:48:03 +02004117 }
4118
Sean Christopherson8b50b922020-09-24 17:30:11 -07004119 vmx_adjust_sec_exec_feature(vmx, &exec_control, rdtscp, RDTSCP);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004120
Sean Christophersonb936d3e2020-09-23 09:50:46 -07004121 /*
4122 * Expose INVPCID if and only if PCID is also exposed to the guest.
4123 * INVPCID takes a #UD when it's disabled in the VMCS, but a #GP or #PF
4124 * if CR4.PCIDE=0. Enumerating CPUID.INVPCID=1 would lead to incorrect
4125 * behavior from the guest perspective (it would expect #GP or #PF).
4126 */
4127 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
4128 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
Sean Christopherson8b50b922020-09-24 17:30:11 -07004129 vmx_adjust_sec_exec_feature(vmx, &exec_control, invpcid, INVPCID);
Sean Christophersonb936d3e2020-09-23 09:50:46 -07004130
Paolo Bonzini80154d72017-08-24 13:55:35 +02004131
Sean Christopherson8b50b922020-09-24 17:30:11 -07004132 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdrand, RDRAND);
4133 vmx_adjust_sec_exec_exiting(vmx, &exec_control, rdseed, RDSEED);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004134
Sean Christopherson8b50b922020-09-24 17:30:11 -07004135 vmx_adjust_sec_exec_control(vmx, &exec_control, waitpkg, WAITPKG,
4136 ENABLE_USR_WAIT_PAUSE, false);
Tao Xue69e72fa2019-07-16 14:55:49 +08004137
Paolo Bonzini80154d72017-08-24 13:55:35 +02004138 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004139}
4140
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004141static void ept_set_mmio_spte_mask(void)
4142{
4143 /*
4144 * EPT Misconfigurations can be generated if the value of bits 2:0
4145 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004146 */
Paolo Bonzinie7581ca2020-05-19 05:04:49 -04004147 kvm_mmu_set_mmio_spte_mask(VMX_EPT_MISCONFIG_WX_VALUE, 0);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004148}
4149
Wanpeng Lif53cd632014-12-02 19:14:58 +08004150#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151
Sean Christopherson944c3462018-12-03 13:53:09 -08004152/*
Xiaoyao Li1b842922019-10-20 17:11:01 +08004153 * Noting that the initialization of Guest-state Area of VMCS is in
4154 * vmx_vcpu_reset().
Sean Christopherson944c3462018-12-03 13:53:09 -08004155 */
Xiaoyao Li1b842922019-10-20 17:11:01 +08004156static void init_vmcs(struct vcpu_vmx *vmx)
Sean Christopherson944c3462018-12-03 13:53:09 -08004157{
Sean Christopherson944c3462018-12-03 13:53:09 -08004158 if (nested)
Xiaoyao Li1b842922019-10-20 17:11:01 +08004159 nested_vmx_set_vmcs_shadowing_bitmap();
Sean Christopherson944c3462018-12-03 13:53:09 -08004160
Sheng Yang25c5f222008-03-28 13:18:56 +08004161 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004162 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08004163
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4165
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 /* Control */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004167 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004168
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004169 exec_controls_set(vmx, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004170
Dan Williamsdfa169b2016-06-02 11:17:24 -07004171 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004172 vmx_compute_secondary_exec_control(vmx);
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004173 secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07004174 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004175
Andrey Smetanind62caab2015-11-10 15:36:33 +03004176 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004177 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4178 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4179 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4180 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4181
4182 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004183
Li RongQing0bcf2612015-12-03 13:29:34 +08004184 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004185 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004186 }
4187
Wanpeng Lib31c1142018-03-12 04:53:04 -07004188 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004189 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004190 vmx->ple_window = ple_window;
4191 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004192 }
4193
Xiao Guangrongc3707952011-07-12 03:28:04 +08004194 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4195 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004196 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4197
Avi Kivity9581d442010-10-19 16:46:55 +02004198 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4199 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004200 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4202 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004203
Bandan Das2a499e42017-08-03 15:54:41 -04004204 if (cpu_has_vmx_vmfunc())
4205 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4206
Eddie Dong2cc51562007-05-21 07:28:09 +03004207 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4208 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004209 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03004210 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004211 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004212
Radim Krčmář74545702015-04-27 15:11:25 +02004213 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4214 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004215
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004216 vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217
4218 /* 22.2.1, 20.8.1 */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004219 vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004220
Sean Christophersonfa71e952020-07-02 21:04:22 -07004221 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4222 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004223
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004224 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004225
Xiaoyao Li35fbe0d2019-10-20 17:10:58 +08004226 if (vmx->vpid != 0)
4227 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4228
Sean Christophersonbecdad82020-09-23 09:50:45 -07004229 if (cpu_has_vmx_xsaves())
Wanpeng Lif53cd632014-12-02 19:14:58 +08004230 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4231
Peter Feiner4e595162016-07-07 14:49:58 -07004232 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07004233 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4234 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4235 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07004236
4237 if (cpu_has_vmx_encls_vmexit())
4238 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Chao Peng2ef444f2018-10-24 16:05:12 +08004239
Sean Christopherson2ef76192020-03-02 15:56:22 -08004240 if (vmx_pt_mode_is_host_guest()) {
Chao Peng2ef444f2018-10-24 16:05:12 +08004241 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
4242 /* Bit[6~0] are forced to 1, writes are ignored. */
4243 vmx->pt_desc.guest.output_mask = 0x7F;
4244 vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
4245 }
Paolo Bonzini8c4182b2020-07-10 17:48:10 +02004246
4247 /*
4248 * If EPT is enabled, #PF is only trapped if MAXPHYADDR is mismatched
4249 * between guest and host. In that case we only care about present
4250 * faults.
4251 */
4252 if (enable_ept) {
4253 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, PFERR_PRESENT_MASK);
4254 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, PFERR_PRESENT_MASK);
4255 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004256}
4257
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004258static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004259{
4260 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004261 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004262 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004263
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004264 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004265 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004266
Tao Xu6e3ba4a2019-07-16 14:55:50 +08004267 vmx->msr_ia32_umwait_control = 0;
4268
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004269 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Wanpeng Li95c06542019-09-05 14:26:28 +08004270 vmx->hv_deadline_tsc = -1;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004271 kvm_set_cr8(vcpu, 0);
4272
4273 if (!init_event) {
4274 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4275 MSR_IA32_APICBASE_ENABLE;
4276 if (kvm_vcpu_is_reset_bsp(vcpu))
4277 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4278 apic_base_msr.host_initiated = true;
4279 kvm_set_apic_base(vcpu, &apic_base_msr);
4280 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004281
Avi Kivity2fb92db2011-04-27 19:42:18 +03004282 vmx_segment_cache_clear(vmx);
4283
Avi Kivity5706be02008-08-20 15:07:31 +03004284 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004285 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004286 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004287
4288 seg_setup(VCPU_SREG_DS);
4289 seg_setup(VCPU_SREG_ES);
4290 seg_setup(VCPU_SREG_FS);
4291 seg_setup(VCPU_SREG_GS);
4292 seg_setup(VCPU_SREG_SS);
4293
4294 vmcs_write16(GUEST_TR_SELECTOR, 0);
4295 vmcs_writel(GUEST_TR_BASE, 0);
4296 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4297 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4298
4299 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4300 vmcs_writel(GUEST_LDTR_BASE, 0);
4301 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4302 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4303
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004304 if (!init_event) {
4305 vmcs_write32(GUEST_SYSENTER_CS, 0);
4306 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4307 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4308 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4309 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004310
Wanpeng Lic37c2872017-11-20 14:52:21 -08004311 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01004312 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004313
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004314 vmcs_writel(GUEST_GDTR_BASE, 0);
4315 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4316
4317 vmcs_writel(GUEST_IDTR_BASE, 0);
4318 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4319
Anthony Liguori443381a2010-12-06 10:53:38 -06004320 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004321 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004322 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07004323 if (kvm_mpx_supported())
4324 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004325
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004326 setup_msrs(vmx);
4327
Avi Kivity6aa8b732006-12-10 02:21:36 -08004328 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4329
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004330 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004331 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004332 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004333 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004334 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004335 vmcs_write32(TPR_THRESHOLD, 0);
4336 }
4337
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004338 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004339
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004340 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004341 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06004342 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004343 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004344 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004345
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004346 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004347
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004348 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004349 if (init_event)
4350 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351}
4352
Jan Kiszkac9a79532014-03-07 20:03:15 +01004353static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004354{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08004355 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004356}
4357
Jan Kiszkac9a79532014-03-07 20:03:15 +01004358static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004359{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004360 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004361 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01004362 enable_irq_window(vcpu);
4363 return;
4364 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004365
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08004366 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004367}
4368
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004369static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004370{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004371 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004372 uint32_t intr;
4373 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004374
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004375 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004376
Avi Kivityfa89a812008-09-01 15:57:51 +03004377 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004378 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004379 int inc_eip = 0;
4380 if (vcpu->arch.interrupt.soft)
4381 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004382 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
Eddie Dong85f455f2007-07-06 12:20:49 +03004383 return;
4384 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004385 intr = irq | INTR_INFO_VALID_MASK;
4386 if (vcpu->arch.interrupt.soft) {
4387 intr |= INTR_TYPE_SOFT_INTR;
4388 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4389 vmx->vcpu.arch.event_exit_inst_len);
4390 } else
4391 intr |= INTR_TYPE_EXT_INTR;
4392 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004393
4394 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004395}
4396
Sheng Yangf08864b2008-05-15 18:23:25 +08004397static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4398{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004399 struct vcpu_vmx *vmx = to_vmx(vcpu);
4400
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004401 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004402 /*
4403 * Tracking the NMI-blocked state in software is built upon
4404 * finding the next open IRQ window. This, in turn, depends on
4405 * well-behaving guests: They have to keep IRQs disabled at
4406 * least as long as the NMI handler runs. Otherwise we may
4407 * cause NMI nesting, maybe breaking the guest. But as this is
4408 * highly unlikely, we can live with the residual risk.
4409 */
4410 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
4411 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4412 }
4413
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004414 ++vcpu->stat.nmi_injections;
4415 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004416
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004417 if (vmx->rmode.vm86_active) {
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004418 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004419 return;
4420 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08004421
Sheng Yangf08864b2008-05-15 18:23:25 +08004422 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4423 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004424
4425 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004426}
4427
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004428bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004429{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004430 struct vcpu_vmx *vmx = to_vmx(vcpu);
4431 bool masked;
4432
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004433 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004434 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004435 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02004436 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004437 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4438 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4439 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004440}
4441
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004442void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004443{
4444 struct vcpu_vmx *vmx = to_vmx(vcpu);
4445
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004446 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004447 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4448 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4449 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4450 }
4451 } else {
4452 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4453 if (masked)
4454 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4455 GUEST_INTR_STATE_NMI);
4456 else
4457 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4458 GUEST_INTR_STATE_NMI);
4459 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004460}
4461
Sean Christopherson1b660b62020-04-22 19:25:44 -07004462bool vmx_nmi_blocked(struct kvm_vcpu *vcpu)
4463{
4464 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
4465 return false;
4466
4467 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4468 return true;
4469
4470 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4471 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
4472 GUEST_INTR_STATE_NMI));
4473}
4474
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004475static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Jan Kiszka2505dc92013-04-14 12:12:47 +02004476{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004477 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004478 return -EBUSY;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004479
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004480 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
4481 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004482 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004483
Sean Christopherson1b660b62020-04-22 19:25:44 -07004484 return !vmx_nmi_blocked(vcpu);
4485}
Sean Christopherson429ab572020-04-22 19:25:42 -07004486
Sean Christopherson1b660b62020-04-22 19:25:44 -07004487bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)
4488{
4489 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Sean Christopherson88c604b2020-04-22 19:25:41 -07004490 return false;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004491
Sean Christopherson7ab0abd2020-04-22 19:25:50 -07004492 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) ||
Sean Christopherson1b660b62020-04-22 19:25:44 -07004493 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4494 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Jan Kiszka2505dc92013-04-14 12:12:47 +02004495}
4496
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004497static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Gleb Natapov78646122009-03-23 12:12:11 +02004498{
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004499 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004500 return -EBUSY;
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004501
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004502 /*
4503 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
4504 * e.g. if the IRQ arrived asynchronously after checking nested events.
4505 */
4506 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004507 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004508
Sean Christopherson1b660b62020-04-22 19:25:44 -07004509 return !vmx_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02004510}
4511
Izik Eiduscbc94022007-10-25 00:29:55 +02004512static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4513{
4514 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004515
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004516 if (enable_unrestricted_guest)
4517 return 0;
4518
Peter Xu6a3c6232020-01-09 09:57:16 -05004519 mutex_lock(&kvm->slots_lock);
4520 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4521 PAGE_SIZE * 3);
4522 mutex_unlock(&kvm->slots_lock);
4523
Izik Eiduscbc94022007-10-25 00:29:55 +02004524 if (ret)
4525 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004526 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004527 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004528}
4529
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004530static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4531{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004532 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004533 return 0;
4534}
4535
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004536static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004537{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004538 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004539 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004540 /*
4541 * Update instruction length as we may reinject the exception
4542 * from user space while in guest debugging mode.
4543 */
4544 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4545 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004546 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004547 return false;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004548 fallthrough;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004549 case DB_VECTOR:
Miaohe Lina8cfbae2020-02-19 10:45:48 +08004550 return !(vcpu->guest_debug &
4551 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP));
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004552 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004553 case OF_VECTOR:
4554 case BR_VECTOR:
4555 case UD_VECTOR:
4556 case DF_VECTOR:
4557 case SS_VECTOR:
4558 case GP_VECTOR:
4559 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004560 return true;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004561 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004562 return false;
4563}
4564
4565static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4566 int vec, u32 err_code)
4567{
4568 /*
4569 * Instruction with address size override prefix opcode 0x67
4570 * Cause the #SS fault with 0 error code in VM86 mode.
4571 */
4572 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004573 if (kvm_emulate_instruction(vcpu, 0)) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004574 if (vcpu->arch.halt_request) {
4575 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004576 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004577 }
4578 return 1;
4579 }
4580 return 0;
4581 }
4582
4583 /*
4584 * Forward all other exceptions that are valid in real mode.
4585 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4586 * the required debugging infrastructure rework.
4587 */
4588 kvm_queue_exception(vcpu, vec);
4589 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004590}
4591
Andi Kleena0861c02009-06-08 17:37:09 +08004592/*
4593 * Trigger machine check on the host. We assume all the MSRs are already set up
4594 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4595 * We pass a fake environment to the machine check handler because we want
4596 * the guest to be always treated like user space, no matter what context
4597 * it used internally.
4598 */
4599static void kvm_machine_check(void)
4600{
Uros Bizjakfb56baa2020-04-14 09:14:14 +02004601#if defined(CONFIG_X86_MCE)
Andi Kleena0861c02009-06-08 17:37:09 +08004602 struct pt_regs regs = {
4603 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4604 .flags = X86_EFLAGS_IF,
4605 };
4606
Thomas Gleixner8cd501c2020-02-25 23:33:23 +01004607 do_machine_check(&regs);
Andi Kleena0861c02009-06-08 17:37:09 +08004608#endif
4609}
4610
Avi Kivity851ba692009-08-24 11:10:17 +03004611static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004612{
Sean Christopherson95b5a482019-04-19 22:50:59 -07004613 /* handled by vmx_vcpu_run() */
Andi Kleena0861c02009-06-08 17:37:09 +08004614 return 1;
4615}
4616
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004617/*
4618 * If the host has split lock detection disabled, then #AC is
4619 * unconditionally injected into the guest, which is the pre split lock
4620 * detection behaviour.
4621 *
4622 * If the host has split lock detection enabled then #AC is
4623 * only injected into the guest when:
4624 * - Guest CPL == 3 (user mode)
4625 * - Guest has #AC detection enabled in CR0
4626 * - Guest EFLAGS has AC bit set
4627 */
4628static inline bool guest_inject_ac(struct kvm_vcpu *vcpu)
4629{
4630 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
4631 return true;
4632
4633 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) &&
4634 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
4635}
4636
Sean Christopherson95b5a482019-04-19 22:50:59 -07004637static int handle_exception_nmi(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004638{
Avi Kivity1155f762007-11-22 11:30:47 +02004639 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004640 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004641 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004642 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004643 u32 vect_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004644
Avi Kivity1155f762007-11-22 11:30:47 +02004645 vect_info = vmx->idt_vectoring_info;
Sean Christophersonf27ad732020-04-27 10:18:37 -07004646 intr_info = vmx_get_intr_info(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004647
Paolo Bonzini2ea72032019-06-06 14:57:25 +02004648 if (is_machine_check(intr_info) || is_nmi(intr_info))
Sean Christopherson95b5a482019-04-19 22:50:59 -07004649 return 1; /* handled by handle_exception_nmi_irqoff() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004650
Wanpeng Li082d06e2018-04-03 16:28:48 -07004651 if (is_invalid_opcode(intr_info))
4652 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004653
Avi Kivity6aa8b732006-12-10 02:21:36 -08004654 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004655 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004657
Liran Alon9e869482018-03-12 13:12:51 +02004658 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4659 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christophersona6c6ed12019-08-27 14:40:30 -07004660
4661 /*
4662 * VMware backdoor emulation on #GP interception only handles
4663 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
4664 * error code on #GP.
4665 */
4666 if (error_code) {
4667 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4668 return 1;
4669 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004670 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon9e869482018-03-12 13:12:51 +02004671 }
4672
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004673 /*
4674 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4675 * MMIO, it is better to report an internal error.
4676 * See the comments in vmx_handle_exit.
4677 */
4678 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4679 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4680 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4681 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Jim Mattson1aa561b2020-06-03 16:56:21 -07004682 vcpu->run->internal.ndata = 4;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004683 vcpu->run->internal.data[0] = vect_info;
4684 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004685 vcpu->run->internal.data[2] = error_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07004686 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004687 return 0;
4688 }
4689
Avi Kivity6aa8b732006-12-10 02:21:36 -08004690 if (is_page_fault(intr_info)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07004691 cr2 = vmx_get_exit_qual(vcpu);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004692 if (enable_ept && !vcpu->arch.apf.host_apf_flags) {
4693 /*
4694 * EPT will cause page fault only if we need to
4695 * detect illegal GPAs.
4696 */
4697 kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
4698 return 1;
4699 } else
4700 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701 }
4702
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004703 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004704
4705 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4706 return handle_rmode_exception(vcpu, ex_no, error_code);
4707
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004708 switch (ex_no) {
4709 case DB_VECTOR:
Sean Christopherson5addc232020-04-15 13:34:53 -07004710 dr6 = vmx_get_exit_qual(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004711 if (!(vcpu->guest_debug &
4712 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004713 if (is_icebp(intr_info))
Sean Christopherson1957aa62019-08-27 14:40:39 -07004714 WARN_ON(!skip_emulated_instruction(vcpu));
Huw Daviesfd2a4452014-04-16 10:02:51 +01004715
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004716 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004717 return 1;
4718 }
Peter Xu13196632020-05-05 16:49:58 -04004719 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004720 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004721 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004722 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004723 /*
4724 * Update instruction length as we may reinject #BP from
4725 * user space while in guest debugging mode. Reading it for
4726 * #DB as well causes no harm, it is not used in that case.
4727 */
4728 vmx->vcpu.arch.event_exit_inst_len =
4729 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004730 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004731 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004732 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4733 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004734 break;
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004735 case AC_VECTOR:
4736 if (guest_inject_ac(vcpu)) {
4737 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4738 return 1;
4739 }
4740
4741 /*
4742 * Handle split lock. Depending on detection mode this will
4743 * either warn and disable split lock detection for this
4744 * task or force SIGBUS on it.
4745 */
4746 if (handle_guest_split_lock(kvm_rip_read(vcpu)))
4747 return 1;
4748 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004749 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004750 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4751 kvm_run->ex.exception = ex_no;
4752 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004753 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004755 return 0;
4756}
4757
Andrea Arcangelif399e602019-11-04 17:59:58 -05004758static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004759{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004760 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761 return 1;
4762}
4763
Avi Kivity851ba692009-08-24 11:10:17 +03004764static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004765{
Avi Kivity851ba692009-08-24 11:10:17 +03004766 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004767 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004768 return 0;
4769}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770
Avi Kivity851ba692009-08-24 11:10:17 +03004771static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772{
He, Qingbfdaab02007-09-12 14:18:28 +08004773 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004774 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004775 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004776
Sean Christopherson5addc232020-04-15 13:34:53 -07004777 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02004778 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004779
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004780 ++vcpu->stat.io_exits;
4781
Sean Christopherson432baf62018-03-08 08:57:26 -08004782 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004783 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004784
4785 port = exit_qualification >> 16;
4786 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004787 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004788
Sean Christophersondca7f122018-03-08 08:57:27 -08004789 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790}
4791
Ingo Molnar102d8322007-02-19 14:37:47 +02004792static void
4793vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4794{
4795 /*
4796 * Patch in the VMCALL instruction:
4797 */
4798 hypercall[0] = 0x0f;
4799 hypercall[1] = 0x01;
4800 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004801}
4802
Guo Chao0fa06072012-06-28 15:16:19 +08004803/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004804static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4805{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004806 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004807 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4808 unsigned long orig_val = val;
4809
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004810 /*
4811 * We get here when L2 changed cr0 in a way that did not change
4812 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004813 * but did change L0 shadowed bits. So we first calculate the
4814 * effective cr0 value that L1 would like to write into the
4815 * hardware. It consists of the L2-owned bits from the new
4816 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004817 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004818 val = (val & ~vmcs12->cr0_guest_host_mask) |
4819 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4820
David Matlack38991522016-11-29 18:14:08 -08004821 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004822 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004823
4824 if (kvm_set_cr0(vcpu, val))
4825 return 1;
4826 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004827 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004828 } else {
4829 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08004830 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004831 return 1;
David Matlack38991522016-11-29 18:14:08 -08004832
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004833 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004834 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004835}
4836
4837static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4838{
4839 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004840 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4841 unsigned long orig_val = val;
4842
4843 /* analogously to handle_set_cr0 */
4844 val = (val & ~vmcs12->cr4_guest_host_mask) |
4845 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4846 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004847 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004848 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004849 return 0;
4850 } else
4851 return kvm_set_cr4(vcpu, val);
4852}
4853
Paolo Bonzini0367f202016-07-12 10:44:55 +02004854static int handle_desc(struct kvm_vcpu *vcpu)
4855{
4856 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004857 return kvm_emulate_instruction(vcpu, 0);
Paolo Bonzini0367f202016-07-12 10:44:55 +02004858}
4859
Avi Kivity851ba692009-08-24 11:10:17 +03004860static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004861{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004862 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863 int cr;
4864 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004865 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004866 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867
Sean Christopherson5addc232020-04-15 13:34:53 -07004868 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 cr = exit_qualification & 15;
4870 reg = (exit_qualification >> 8) & 15;
4871 switch ((exit_qualification >> 4) & 3) {
4872 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03004873 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004874 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004875 switch (cr) {
4876 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004877 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004878 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004880 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03004881 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004882 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004884 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004885 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004886 case 8: {
4887 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03004888 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004889 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004890 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004891 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08004892 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004893 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08004894 return ret;
4895 /*
4896 * TODO: we might be squashing a
4897 * KVM_GUESTDBG_SINGLESTEP-triggered
4898 * KVM_EXIT_DEBUG here.
4899 */
Avi Kivity851ba692009-08-24 11:10:17 +03004900 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004901 return 0;
4902 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02004903 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004904 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03004905 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004906 WARN_ONCE(1, "Guest should always own CR0.TS");
4907 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02004908 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08004909 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004910 case 1: /*mov from cr*/
4911 switch (cr) {
4912 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004913 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02004914 val = kvm_read_cr3(vcpu);
4915 kvm_register_write(vcpu, reg, val);
4916 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004917 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004919 val = kvm_get_cr8(vcpu);
4920 kvm_register_write(vcpu, reg, val);
4921 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004922 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004923 }
4924 break;
4925 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02004926 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004927 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02004928 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004929
Kyle Huey6affcbe2016-11-29 12:40:40 -08004930 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004931 default:
4932 break;
4933 }
Avi Kivity851ba692009-08-24 11:10:17 +03004934 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03004935 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08004936 (int)(exit_qualification >> 4) & 3, cr);
4937 return 0;
4938}
4939
Avi Kivity851ba692009-08-24 11:10:17 +03004940static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004941{
He, Qingbfdaab02007-09-12 14:18:28 +08004942 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004943 int dr, dr7, reg;
4944
Sean Christopherson5addc232020-04-15 13:34:53 -07004945 exit_qualification = vmx_get_exit_qual(vcpu);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004946 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4947
4948 /* First, if DR does not exist, trigger UD */
4949 if (!kvm_require_dr(vcpu, dr))
4950 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951
Jan Kiszkaf2483412010-01-20 18:20:20 +01004952 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03004953 if (!kvm_require_cpl(vcpu, 0))
4954 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004955 dr7 = vmcs_readl(GUEST_DR7);
4956 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004957 /*
4958 * As the vm-exit takes precedence over the debug trap, we
4959 * need to emulate the latter, either for the host or the
4960 * guest debugging itself.
4961 */
4962 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Paolo Bonzini45981de2020-05-06 05:59:39 -04004963 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_RTM | DR6_FIXED_1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004964 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02004965 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004966 vcpu->run->debug.arch.exception = DB_VECTOR;
4967 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004968 return 0;
4969 } else {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004970 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004971 return 1;
4972 }
4973 }
4974
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004975 if (vcpu->guest_debug == 0) {
Sean Christopherson2183f562019-05-07 12:17:56 -07004976 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004977
4978 /*
4979 * No more DR vmexits; force a reload of the debug registers
4980 * and reenter on this instruction. The next vmexit will
4981 * retrieve the full state of the debug registers.
4982 */
4983 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
4984 return 1;
4985 }
4986
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004987 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4988 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03004989 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004990
4991 if (kvm_get_dr(vcpu, dr, &val))
4992 return 1;
4993 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03004994 } else
Nadav Amit57773922014-06-18 17:19:23 +03004995 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004996 return 1;
4997
Kyle Huey6affcbe2016-11-29 12:40:40 -08004998 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004999}
5000
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005001static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5002{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005003 get_debugreg(vcpu->arch.db[0], 0);
5004 get_debugreg(vcpu->arch.db[1], 1);
5005 get_debugreg(vcpu->arch.db[2], 2);
5006 get_debugreg(vcpu->arch.db[3], 3);
5007 get_debugreg(vcpu->arch.dr6, 6);
5008 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5009
5010 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Sean Christopherson2183f562019-05-07 12:17:56 -07005011 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005012}
5013
Gleb Natapov020df072010-04-13 10:05:23 +03005014static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5015{
5016 vmcs_writel(GUEST_DR7, val);
5017}
5018
Avi Kivity851ba692009-08-24 11:10:17 +03005019static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005020{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01005021 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005022 return 1;
5023}
5024
Avi Kivity851ba692009-08-24 11:10:17 +03005025static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005026{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005027 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005028
Avi Kivity3842d132010-07-27 12:30:24 +03005029 kvm_make_request(KVM_REQ_EVENT, vcpu);
5030
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005031 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005032 return 1;
5033}
5034
Avi Kivity851ba692009-08-24 11:10:17 +03005035static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005036{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005037 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005038}
5039
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005040static int handle_invd(struct kvm_vcpu *vcpu)
5041{
Tom Lendacky28e2b2f2020-09-24 13:41:58 -05005042 /* Treat an INVD instruction as a NOP and just skip it. */
5043 return kvm_skip_emulated_instruction(vcpu);
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005044}
5045
Avi Kivity851ba692009-08-24 11:10:17 +03005046static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005047{
Sean Christopherson5addc232020-04-15 13:34:53 -07005048 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005049
5050 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005051 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005052}
5053
Avi Kivityfee84b02011-11-10 14:57:25 +02005054static int handle_rdpmc(struct kvm_vcpu *vcpu)
5055{
5056 int err;
5057
5058 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005059 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02005060}
5061
Avi Kivity851ba692009-08-24 11:10:17 +03005062static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005063{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005064 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005065}
5066
Dexuan Cui2acf9232010-06-10 11:27:12 +08005067static int handle_xsetbv(struct kvm_vcpu *vcpu)
5068{
5069 u64 new_bv = kvm_read_edx_eax(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07005070 u32 index = kvm_rcx_read(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005071
5072 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005073 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005074 return 1;
5075}
5076
Avi Kivity851ba692009-08-24 11:10:17 +03005077static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005078{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005079 if (likely(fasteoi)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07005080 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005081 int access_type, offset;
5082
5083 access_type = exit_qualification & APIC_ACCESS_TYPE;
5084 offset = exit_qualification & APIC_ACCESS_OFFSET;
5085 /*
5086 * Sane guest uses MOV to write EOI, with written value
5087 * not cared. So make a short-circuit here by avoiding
5088 * heavy instruction emulation.
5089 */
5090 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5091 (offset == APIC_EOI)) {
5092 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005093 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005094 }
5095 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005096 return kvm_emulate_instruction(vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005097}
5098
Yang Zhangc7c9c562013-01-25 10:18:51 +08005099static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5100{
Sean Christopherson5addc232020-04-15 13:34:53 -07005101 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005102 int vector = exit_qualification & 0xff;
5103
5104 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5105 kvm_apic_set_eoi_accelerated(vcpu, vector);
5106 return 1;
5107}
5108
Yang Zhang83d4c282013-01-25 10:18:49 +08005109static int handle_apic_write(struct kvm_vcpu *vcpu)
5110{
Sean Christopherson5addc232020-04-15 13:34:53 -07005111 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhang83d4c282013-01-25 10:18:49 +08005112 u32 offset = exit_qualification & 0xfff;
5113
5114 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5115 kvm_apic_write_nodecode(vcpu, offset);
5116 return 1;
5117}
5118
Avi Kivity851ba692009-08-24 11:10:17 +03005119static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005120{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005121 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005122 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005123 bool has_error_code = false;
5124 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005125 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005126 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005127
5128 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005129 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005130 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005131
Sean Christopherson5addc232020-04-15 13:34:53 -07005132 exit_qualification = vmx_get_exit_qual(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005133
5134 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005135 if (reason == TASK_SWITCH_GATE && idt_v) {
5136 switch (type) {
5137 case INTR_TYPE_NMI_INTR:
5138 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005139 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005140 break;
5141 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005142 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005143 kvm_clear_interrupt_queue(vcpu);
5144 break;
5145 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005146 if (vmx->idt_vectoring_info &
5147 VECTORING_INFO_DELIVER_CODE_MASK) {
5148 has_error_code = true;
5149 error_code =
5150 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5151 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005152 fallthrough;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005153 case INTR_TYPE_SOFT_EXCEPTION:
5154 kvm_clear_exception_queue(vcpu);
5155 break;
5156 default:
5157 break;
5158 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005159 }
Izik Eidus37817f22008-03-24 23:14:53 +02005160 tss_selector = exit_qualification;
5161
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005162 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5163 type != INTR_TYPE_EXT_INTR &&
5164 type != INTR_TYPE_NMI_INTR))
Sean Christopherson1957aa62019-08-27 14:40:39 -07005165 WARN_ON(!skip_emulated_instruction(vcpu));
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005166
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005167 /*
5168 * TODO: What about debug traps on tss switch?
5169 * Are we supposed to inject them and update dr6?
5170 */
Sean Christopherson10517782019-08-27 14:40:35 -07005171 return kvm_task_switch(vcpu, tss_selector,
5172 type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005173 reason, has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02005174}
5175
Avi Kivity851ba692009-08-24 11:10:17 +03005176static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005177{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005178 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005179 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01005180 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005181
Sean Christopherson5addc232020-04-15 13:34:53 -07005182 exit_qualification = vmx_get_exit_qual(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005183
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005184 /*
5185 * EPT violation happened while executing iret from NMI,
5186 * "blocked by NMI" bit has to be set before next VM entry.
5187 * There are errata that may cause this bit to not be set:
5188 * AAK134, BY25.
5189 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005190 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005191 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005192 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005193 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5194
Sheng Yang14394422008-04-28 12:24:45 +08005195 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005196 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005197
Junaid Shahid27959a42016-12-06 16:46:10 -08005198 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005199 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08005200 ? PFERR_USER_MASK : 0;
5201 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005202 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08005203 ? PFERR_WRITE_MASK : 0;
5204 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005205 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08005206 ? PFERR_FETCH_MASK : 0;
5207 /* ept page table entry is present? */
5208 error_code |= (exit_qualification &
5209 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5210 EPT_VIOLATION_EXECUTABLE))
5211 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005212
Paolo Bonzinieebed242016-11-28 14:39:58 +01005213 error_code |= (exit_qualification & 0x100) != 0 ?
5214 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005215
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005216 vcpu->arch.exit_qualification = exit_qualification;
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005217
5218 /*
5219 * Check that the GPA doesn't exceed physical memory limits, as that is
5220 * a guest page fault. We have to emulate the instruction here, because
5221 * if the illegal address is that of a paging structure, then
5222 * EPT_VIOLATION_ACC_WRITE bit is set. Alternatively, if supported we
5223 * would also use advanced VM-exit information for EPT violations to
5224 * reconstruct the page fault error code.
5225 */
Sean Christophersondc465152020-09-24 12:42:49 -07005226 if (unlikely(kvm_vcpu_is_illegal_gpa(vcpu, gpa)))
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005227 return kvm_emulate_instruction(vcpu, 0);
5228
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005229 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005230}
5231
Avi Kivity851ba692009-08-24 11:10:17 +03005232static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005233{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005234 gpa_t gpa;
5235
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005236 /*
5237 * A nested guest cannot optimize MMIO vmexits, because we have an
5238 * nGPA here instead of the required GPA.
5239 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005240 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005241 if (!is_guest_mode(vcpu) &&
5242 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08005243 trace_kvm_fast_mmio(gpa);
Sean Christopherson1957aa62019-08-27 14:40:39 -07005244 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005245 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005246
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07005247 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005248}
5249
Avi Kivity851ba692009-08-24 11:10:17 +03005250static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005251{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005252 WARN_ON_ONCE(!enable_vnmi);
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005253 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Sheng Yangf08864b2008-05-15 18:23:25 +08005254 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005255 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005256
5257 return 1;
5258}
5259
Mohammed Gamal80ced182009-09-01 12:48:18 +02005260static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005261{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005262 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity49e9d552010-09-19 14:34:08 +02005263 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005264 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005265
Sean Christopherson2183f562019-05-07 12:17:56 -07005266 intr_window_requested = exec_controls_get(vmx) &
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005267 CPU_BASED_INTR_WINDOW_EXITING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005268
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005269 while (vmx->emulation_required && count-- != 0) {
Sean Christophersondb438592020-04-22 19:25:48 -07005270 if (intr_window_requested && !vmx_interrupt_blocked(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005271 return handle_interrupt_window(&vmx->vcpu);
5272
Radim Krčmář72875d82017-04-26 22:32:19 +02005273 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005274 return 1;
5275
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005276 if (!kvm_emulate_instruction(vcpu, 0))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005277 return 0;
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005278
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005279 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
Sean Christopherson8fff2712019-08-27 14:40:37 -07005280 vcpu->arch.exception.pending) {
5281 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5282 vcpu->run->internal.suberror =
5283 KVM_INTERNAL_ERROR_EMULATION;
5284 vcpu->run->internal.ndata = 0;
5285 return 0;
5286 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005287
Gleb Natapov8d76c492013-05-08 18:38:44 +03005288 if (vcpu->arch.halt_request) {
5289 vcpu->arch.halt_request = 0;
Sean Christopherson8fff2712019-08-27 14:40:37 -07005290 return kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005291 }
5292
Sean Christopherson8fff2712019-08-27 14:40:37 -07005293 /*
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005294 * Note, return 1 and not 0, vcpu_run() will invoke
5295 * xfer_to_guest_mode() which will create a proper return
5296 * code.
Sean Christopherson8fff2712019-08-27 14:40:37 -07005297 */
Thomas Gleixner72c3c0f2020-07-23 00:00:09 +02005298 if (__xfer_to_guest_mode_work_pending())
Sean Christopherson8fff2712019-08-27 14:40:37 -07005299 return 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005300 }
5301
Sean Christopherson8fff2712019-08-27 14:40:37 -07005302 return 1;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005303}
5304
5305static void grow_ple_window(struct kvm_vcpu *vcpu)
5306{
5307 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005308 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005309
Babu Mogerc8e88712018-03-16 16:37:24 -04005310 vmx->ple_window = __grow_ple_window(old, ple_window,
5311 ple_window_grow,
5312 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005313
Peter Xu4f75bcc2019-09-06 10:17:22 +08005314 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005315 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005316 trace_kvm_ple_window_update(vcpu->vcpu_id,
5317 vmx->ple_window, old);
5318 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005319}
5320
5321static void shrink_ple_window(struct kvm_vcpu *vcpu)
5322{
5323 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005324 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005325
Babu Mogerc8e88712018-03-16 16:37:24 -04005326 vmx->ple_window = __shrink_ple_window(old, ple_window,
5327 ple_window_shrink,
5328 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005329
Peter Xu4f75bcc2019-09-06 10:17:22 +08005330 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005331 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005332 trace_kvm_ple_window_update(vcpu->vcpu_id,
5333 vmx->ple_window, old);
5334 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005335}
5336
Peng Haoe01bca22018-04-07 05:47:32 +08005337static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005338{
5339 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
5340 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
5341 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
5342 0ull, VMX_EPT_EXECUTABLE_MASK,
5343 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005344 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005345
5346 ept_set_mmio_spte_mask();
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005347}
5348
Avi Kivity6aa8b732006-12-10 02:21:36 -08005349/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005350 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5351 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5352 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005353static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005354{
Wanpeng Lib31c1142018-03-12 04:53:04 -07005355 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005356 grow_ple_window(vcpu);
5357
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08005358 /*
5359 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
5360 * VM-execution control is ignored if CPL > 0. OTOH, KVM
5361 * never set PAUSE_EXITING and just set PLE if supported,
5362 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
5363 */
5364 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005365 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005366}
5367
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005368static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08005369{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005370 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08005371}
5372
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005373static int handle_mwait(struct kvm_vcpu *vcpu)
5374{
5375 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
5376 return handle_nop(vcpu);
5377}
5378
Jim Mattson45ec3682017-08-23 16:32:04 -07005379static int handle_invalid_op(struct kvm_vcpu *vcpu)
5380{
5381 kvm_queue_exception(vcpu, UD_VECTOR);
5382 return 1;
5383}
5384
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005385static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5386{
5387 return 1;
5388}
5389
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005390static int handle_monitor(struct kvm_vcpu *vcpu)
5391{
5392 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
5393 return handle_nop(vcpu);
5394}
5395
Junaid Shahideb4b2482018-06-27 14:59:14 -07005396static int handle_invpcid(struct kvm_vcpu *vcpu)
5397{
5398 u32 vmx_instruction_info;
5399 unsigned long type;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005400 gva_t gva;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005401 struct {
5402 u64 pcid;
5403 u64 gla;
5404 } operand;
5405
5406 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5407 kvm_queue_exception(vcpu, UD_VECTOR);
5408 return 1;
5409 }
5410
5411 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5412 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5413
5414 if (type > 3) {
5415 kvm_inject_gp(vcpu, 0);
5416 return 1;
5417 }
5418
5419 /* According to the Intel instruction reference, the memory operand
5420 * is read even if it isn't needed (e.g., for type==all)
5421 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005422 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005423 vmx_instruction_info, false,
5424 sizeof(operand), &gva))
Junaid Shahideb4b2482018-06-27 14:59:14 -07005425 return 1;
5426
Babu Moger97150922020-09-11 14:29:12 -05005427 return kvm_handle_invpcid(vcpu, type, gva);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005428}
5429
Kai Huang843e4332015-01-28 10:54:28 +08005430static int handle_pml_full(struct kvm_vcpu *vcpu)
5431{
5432 unsigned long exit_qualification;
5433
5434 trace_kvm_pml_full(vcpu->vcpu_id);
5435
Sean Christopherson5addc232020-04-15 13:34:53 -07005436 exit_qualification = vmx_get_exit_qual(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005437
5438 /*
5439 * PML buffer FULL happened while executing iret from NMI,
5440 * "blocked by NMI" bit has to be set before next VM entry.
5441 */
5442 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005443 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005444 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5445 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5446 GUEST_INTR_STATE_NMI);
5447
5448 /*
5449 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5450 * here.., and there's no userspace involvement needed for PML.
5451 */
5452 return 1;
5453}
5454
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005455static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07005456{
Sean Christopherson804939e2019-05-07 12:18:05 -07005457 struct vcpu_vmx *vmx = to_vmx(vcpu);
5458
5459 if (!vmx->req_immediate_exit &&
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005460 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) {
Sean Christophersond264ee02018-08-27 15:21:12 -07005461 kvm_lapic_expired_hv_timer(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005462 return EXIT_FASTPATH_REENTER_GUEST;
5463 }
Sean Christopherson804939e2019-05-07 12:18:05 -07005464
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005465 return EXIT_FASTPATH_NONE;
5466}
5467
5468static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5469{
5470 handle_fastpath_preemption_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005471 return 1;
5472}
5473
Sean Christophersone4027cf2018-12-03 13:53:12 -08005474/*
5475 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5476 * are overwritten by nested_vmx_setup() when nested=1.
5477 */
5478static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5479{
5480 kvm_queue_exception(vcpu, UD_VECTOR);
5481 return 1;
5482}
5483
Sean Christopherson0b665d32018-08-14 09:33:34 -07005484static int handle_encls(struct kvm_vcpu *vcpu)
5485{
5486 /*
5487 * SGX virtualization is not yet supported. There is no software
5488 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
5489 * to prevent the guest from executing ENCLS.
5490 */
5491 kvm_queue_exception(vcpu, UD_VECTOR);
5492 return 1;
5493}
5494
Nadav Har'El0140cae2011-05-25 23:06:28 +03005495/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005496 * The exit handlers return 1 if the exit was handled fully and guest execution
5497 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5498 * to be done to userspace and return 0.
5499 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005500static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Sean Christopherson95b5a482019-04-19 22:50:59 -07005501 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005502 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005503 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005504 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005505 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005506 [EXIT_REASON_CR_ACCESS] = handle_cr,
5507 [EXIT_REASON_DR_ACCESS] = handle_dr,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005508 [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
5509 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
5510 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005511 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005512 [EXIT_REASON_HLT] = kvm_emulate_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005513 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005514 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005515 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005516 [EXIT_REASON_VMCALL] = handle_vmcall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005517 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5518 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5519 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5520 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5521 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5522 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5523 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5524 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5525 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005526 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5527 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005528 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005529 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005530 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005531 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005532 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005533 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005534 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5535 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005536 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5537 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005538 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005539 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005540 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005541 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005542 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5543 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Jim Mattson45ec3682017-08-23 16:32:04 -07005544 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07005545 [EXIT_REASON_RDSEED] = handle_invalid_op,
Kai Huang843e4332015-01-28 10:54:28 +08005546 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005547 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005548 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005549 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005550 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005551};
5552
5553static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005554 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555
Sean Christopherson235ba742020-09-23 13:13:46 -07005556static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
5557 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02005558{
Sean Christopherson235ba742020-09-23 13:13:46 -07005559 struct vcpu_vmx *vmx = to_vmx(vcpu);
5560
Sean Christopherson5addc232020-04-15 13:34:53 -07005561 *info1 = vmx_get_exit_qual(vcpu);
Sean Christopherson235ba742020-09-23 13:13:46 -07005562 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
5563 *info2 = vmx->idt_vectoring_info;
5564 *intr_info = vmx_get_intr_info(vcpu);
5565 if (is_exception_with_error_code(*intr_info))
5566 *error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5567 else
5568 *error_code = 0;
5569 } else {
5570 *info2 = 0;
5571 *intr_info = 0;
5572 *error_code = 0;
5573 }
Avi Kivity586f9602010-11-18 13:09:54 +02005574}
5575
Kai Huanga3eaa862015-11-04 13:46:05 +08005576static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005577{
Kai Huanga3eaa862015-11-04 13:46:05 +08005578 if (vmx->pml_pg) {
5579 __free_page(vmx->pml_pg);
5580 vmx->pml_pg = NULL;
5581 }
Kai Huang843e4332015-01-28 10:54:28 +08005582}
5583
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005584static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005585{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005586 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005587 u64 *pml_buf;
5588 u16 pml_idx;
5589
5590 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5591
5592 /* Do nothing if PML buffer is empty */
5593 if (pml_idx == (PML_ENTITY_NUM - 1))
5594 return;
5595
5596 /* PML index always points to next available PML buffer entity */
5597 if (pml_idx >= PML_ENTITY_NUM)
5598 pml_idx = 0;
5599 else
5600 pml_idx++;
5601
5602 pml_buf = page_address(vmx->pml_pg);
5603 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5604 u64 gpa;
5605
5606 gpa = pml_buf[pml_idx];
5607 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005608 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005609 }
5610
5611 /* reset PML index */
5612 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5613}
5614
5615/*
5616 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
5617 * Called before reporting dirty_bitmap to userspace.
5618 */
5619static void kvm_flush_pml_buffers(struct kvm *kvm)
5620{
5621 int i;
5622 struct kvm_vcpu *vcpu;
5623 /*
5624 * We only need to kick vcpu out of guest mode here, as PML buffer
5625 * is flushed at beginning of all VMEXITs, and it's obvious that only
5626 * vcpus running in guest are possible to have unflushed GPAs in PML
5627 * buffer.
5628 */
5629 kvm_for_each_vcpu(i, vcpu, kvm)
5630 kvm_vcpu_kick(vcpu);
5631}
5632
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005633static void vmx_dump_sel(char *name, uint32_t sel)
5634{
5635 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005636 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005637 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5638 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5639 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5640}
5641
5642static void vmx_dump_dtsel(char *name, uint32_t limit)
5643{
5644 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5645 name, vmcs_read32(limit),
5646 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5647}
5648
Paolo Bonzini69090812019-04-15 15:16:17 +02005649void dump_vmcs(void)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005650{
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005651 u32 vmentry_ctl, vmexit_ctl;
5652 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
5653 unsigned long cr4;
5654 u64 efer;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005655
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005656 if (!dump_invalid_vmcs) {
5657 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
5658 return;
5659 }
5660
5661 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5662 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5663 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5664 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5665 cr4 = vmcs_readl(GUEST_CR4);
5666 efer = vmcs_read64(GUEST_IA32_EFER);
5667 secondary_exec_control = 0;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005668 if (cpu_has_secondary_exec_ctrls())
5669 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5670
5671 pr_err("*** Guest State ***\n");
5672 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5673 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5674 vmcs_readl(CR0_GUEST_HOST_MASK));
5675 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5676 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5677 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
5678 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
5679 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
5680 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005681 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5682 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5683 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5684 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005685 }
5686 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5687 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5688 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5689 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5690 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5691 vmcs_readl(GUEST_SYSENTER_ESP),
5692 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5693 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5694 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5695 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5696 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5697 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5698 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5699 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5700 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5701 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5702 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
5703 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
5704 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005705 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5706 efer, vmcs_read64(GUEST_IA32_PAT));
5707 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5708 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005709 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005710 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005711 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005712 pr_err("PerfGlobCtl = 0x%016llx\n",
5713 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005714 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005715 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005716 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5717 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5718 vmcs_read32(GUEST_ACTIVITY_STATE));
5719 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5720 pr_err("InterruptStatus = %04x\n",
5721 vmcs_read16(GUEST_INTR_STATUS));
5722
5723 pr_err("*** Host State ***\n");
5724 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5725 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5726 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5727 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5728 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5729 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5730 vmcs_read16(HOST_TR_SELECTOR));
5731 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5732 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5733 vmcs_readl(HOST_TR_BASE));
5734 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5735 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5736 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5737 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5738 vmcs_readl(HOST_CR4));
5739 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5740 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5741 vmcs_read32(HOST_IA32_SYSENTER_CS),
5742 vmcs_readl(HOST_IA32_SYSENTER_EIP));
5743 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005744 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5745 vmcs_read64(HOST_IA32_EFER),
5746 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005747 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005748 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005749 pr_err("PerfGlobCtl = 0x%016llx\n",
5750 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005751
5752 pr_err("*** Control State ***\n");
5753 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5754 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5755 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5756 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5757 vmcs_read32(EXCEPTION_BITMAP),
5758 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5759 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5760 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5761 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5762 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5763 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5764 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5765 vmcs_read32(VM_EXIT_INTR_INFO),
5766 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5767 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5768 pr_err(" reason=%08x qualification=%016lx\n",
5769 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5770 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5771 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5772 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005773 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005774 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005775 pr_err("TSC Multiplier = 0x%016llx\n",
5776 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005777 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
5778 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
5779 u16 status = vmcs_read16(GUEST_INTR_STATUS);
5780 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
5781 }
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005782 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005783 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
5784 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005785 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005786 }
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005787 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5788 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5789 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005790 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005791 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5792 pr_err("PLE Gap=%08x Window=%08x\n",
5793 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5794 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5795 pr_err("Virtual processor ID = 0x%04x\n",
5796 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5797}
5798
Avi Kivity6aa8b732006-12-10 02:21:36 -08005799/*
5800 * The guest has exited. See if we can fix it or if we need userspace
5801 * assistance.
5802 */
Wanpeng Li404d5d72020-04-28 14:23:25 +08005803static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005804{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005805 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005806 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005807 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005808
Kai Huang843e4332015-01-28 10:54:28 +08005809 /*
5810 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5811 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5812 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5813 * mode as if vcpus is in root mode, the PML buffer must has been
5814 * flushed already.
5815 */
5816 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005817 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005818
Sean Christophersondb438592020-04-22 19:25:48 -07005819 /*
5820 * We should never reach this point with a pending nested VM-Enter, and
5821 * more specifically emulation of L2 due to invalid guest state (see
5822 * below) should never happen as that means we incorrectly allowed a
5823 * nested VM-Enter with an invalid vmcs12.
5824 */
5825 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5826
Mohammed Gamal80ced182009-09-01 12:48:18 +02005827 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02005828 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02005829 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005830
Paolo Bonzini96b100c2020-03-17 18:32:50 +01005831 if (is_guest_mode(vcpu)) {
5832 /*
5833 * The host physical addresses of some pages of guest memory
5834 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
5835 * Page). The CPU may write to these pages via their host
5836 * physical address while L2 is running, bypassing any
5837 * address-translation-based dirty tracking (e.g. EPT write
5838 * protection).
5839 *
5840 * Mark them dirty on every exit from L2 to prevent them from
5841 * getting out of sync with dirty tracking.
5842 */
5843 nested_mark_vmcs12_pages_dirty(vcpu);
5844
Sean Christophersonf47baae2020-04-15 10:55:16 -07005845 if (nested_vmx_reflect_vmexit(vcpu))
Sean Christopherson789afc52020-04-15 10:55:10 -07005846 return 1;
Paolo Bonzini96b100c2020-03-17 18:32:50 +01005847 }
Nadav Har'El644d7112011-05-25 23:12:35 +03005848
Mohammed Gamal51207022010-05-31 22:40:54 +03005849 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005850 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03005851 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5852 vcpu->run->fail_entry.hardware_entry_failure_reason
5853 = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07005854 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Mohammed Gamal51207022010-05-31 22:40:54 +03005855 return 0;
5856 }
5857
Avi Kivity29bd8a72007-09-10 17:27:03 +03005858 if (unlikely(vmx->fail)) {
Paolo Bonzini3b20e032019-07-19 18:15:08 +02005859 dump_vmcs();
Avi Kivity851ba692009-08-24 11:10:17 +03005860 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5861 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03005862 = vmcs_read32(VM_INSTRUCTION_ERROR);
Jim Mattson8a14fe42020-06-03 16:56:22 -07005863 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005864 return 0;
5865 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005866
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005867 /*
5868 * Note:
5869 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
5870 * delivery event since it indicates guest is accessing MMIO.
5871 * The vm-exit can be triggered again after return to guest that
5872 * will cause infinite loop.
5873 */
Mike Dayd77c26f2007-10-08 09:02:08 -04005874 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08005875 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02005876 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00005877 exit_reason != EXIT_REASON_PML_FULL &&
Wanpeng Li99b82a12020-08-19 16:55:27 +08005878 exit_reason != EXIT_REASON_APIC_ACCESS &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005879 exit_reason != EXIT_REASON_TASK_SWITCH)) {
5880 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5881 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005882 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005883 vcpu->run->internal.data[0] = vectoring_info;
5884 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005885 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
5886 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
5887 vcpu->run->internal.ndata++;
5888 vcpu->run->internal.data[3] =
5889 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5890 }
Jim Mattson1aa561b2020-06-03 16:56:21 -07005891 vcpu->run->internal.data[vcpu->run->internal.ndata++] =
Jim Mattson8a14fe42020-06-03 16:56:22 -07005892 vcpu->arch.last_vmentry_cpu;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005893 return 0;
5894 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005895
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005896 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005897 vmx->loaded_vmcs->soft_vnmi_blocked)) {
Sean Christophersondb438592020-04-22 19:25:48 -07005898 if (!vmx_interrupt_blocked(vcpu)) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005899 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5900 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
5901 vcpu->arch.nmi_pending) {
5902 /*
5903 * This CPU don't support us in finding the end of an
5904 * NMI-blocked window if the guest runs with IRQs
5905 * disabled. So we pull the trigger after 1 s of
5906 * futile waiting, but inform the user about this.
5907 */
5908 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
5909 "state on VCPU %d after 1 s timeout\n",
5910 __func__, vcpu->vcpu_id);
5911 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5912 }
5913 }
5914
Wanpeng Li404d5d72020-04-28 14:23:25 +08005915 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08005916 return 1;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08005917
5918 if (exit_reason >= kvm_vmx_max_exit_handlers)
5919 goto unexpected_vmexit;
5920#ifdef CONFIG_RETPOLINE
5921 if (exit_reason == EXIT_REASON_MSR_WRITE)
5922 return kvm_emulate_wrmsr(vcpu);
5923 else if (exit_reason == EXIT_REASON_PREEMPTION_TIMER)
5924 return handle_preemption_timer(vcpu);
5925 else if (exit_reason == EXIT_REASON_INTERRUPT_WINDOW)
5926 return handle_interrupt_window(vcpu);
5927 else if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
5928 return handle_external_interrupt(vcpu);
5929 else if (exit_reason == EXIT_REASON_HLT)
5930 return kvm_emulate_halt(vcpu);
5931 else if (exit_reason == EXIT_REASON_EPT_MISCONFIG)
5932 return handle_ept_misconfig(vcpu);
5933#endif
5934
5935 exit_reason = array_index_nospec(exit_reason,
5936 kvm_vmx_max_exit_handlers);
5937 if (!kvm_vmx_exit_handlers[exit_reason])
5938 goto unexpected_vmexit;
5939
5940 return kvm_vmx_exit_handlers[exit_reason](vcpu);
5941
5942unexpected_vmexit:
5943 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", exit_reason);
5944 dump_vmcs();
5945 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5946 vcpu->run->internal.suberror =
5947 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
Jim Mattson1aa561b2020-06-03 16:56:21 -07005948 vcpu->run->internal.ndata = 2;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08005949 vcpu->run->internal.data[0] = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07005950 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08005951 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005952}
5953
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005954/*
5955 * Software based L1D cache flush which is used when microcode providing
5956 * the cache control MSR is not loaded.
5957 *
5958 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
5959 * flush it is required to read in 64 KiB because the replacement algorithm
5960 * is not exactly LRU. This could be sized at runtime via topology
5961 * information but as all relevant affected CPUs have 32KiB L1D cache size
5962 * there is no point in doing so.
5963 */
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02005964static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005965{
5966 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005967
5968 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02005969 * This code is only executed when the the flush mode is 'cond' or
5970 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005971 */
Nicolai Stange427362a2018-07-21 22:25:00 +02005972 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02005973 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005974
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005975 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02005976 * Clear the per-vcpu flush bit, it gets set again
5977 * either from vcpu_run() or from one of the unsafe
5978 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005979 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02005980 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02005981 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02005982
5983 /*
5984 * Clear the per-cpu flush bit, it gets set again from
5985 * the interrupt handlers.
5986 */
5987 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
5988 kvm_clear_cpu_l1tf_flush_l1d();
5989
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005990 if (!flush_l1d)
5991 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005992 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005993
5994 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005995
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02005996 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02005997 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02005998 return;
5999 }
6000
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006001 asm volatile(
6002 /* First ensure the pages are in the TLB */
6003 "xorl %%eax, %%eax\n"
6004 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02006005 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006006 "addl $4096, %%eax\n\t"
6007 "cmpl %%eax, %[size]\n\t"
6008 "jne .Lpopulate_tlb\n\t"
6009 "xorl %%eax, %%eax\n\t"
6010 "cpuid\n\t"
6011 /* Now fill the cache */
6012 "xorl %%eax, %%eax\n"
6013 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006014 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006015 "addl $64, %%eax\n\t"
6016 "cmpl %%eax, %[size]\n\t"
6017 "jne .Lfill_cache\n\t"
6018 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006019 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006020 [size] "r" (size)
6021 : "eax", "ebx", "ecx", "edx");
6022}
6023
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006024static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006025{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006026 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Liran Alon132f4f72019-11-11 14:30:54 +02006027 int tpr_threshold;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006028
6029 if (is_guest_mode(vcpu) &&
6030 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6031 return;
6032
Liran Alon132f4f72019-11-11 14:30:54 +02006033 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
Liran Alon02d496cf2019-11-11 14:30:55 +02006034 if (is_guest_mode(vcpu))
6035 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6036 else
6037 vmcs_write32(TPR_THRESHOLD, tpr_threshold);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006038}
6039
Sean Christopherson97b7ead2018-12-03 13:53:16 -08006040void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08006041{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006042 struct vcpu_vmx *vmx = to_vmx(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006043 u32 sec_exec_control;
6044
Jim Mattson8d860bb2018-05-09 16:56:05 -04006045 if (!lapic_in_kernel(vcpu))
6046 return;
6047
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07006048 if (!flexpriority_enabled &&
6049 !cpu_has_vmx_virtualize_x2apic_mode())
6050 return;
6051
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006052 /* Postpone execution until vmcs01 is the current VMCS. */
6053 if (is_guest_mode(vcpu)) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006054 vmx->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006055 return;
6056 }
6057
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006058 sec_exec_control = secondary_exec_controls_get(vmx);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006059 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6060 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08006061
Jim Mattson8d860bb2018-05-09 16:56:05 -04006062 switch (kvm_get_apic_mode(vcpu)) {
6063 case LAPIC_MODE_INVALID:
6064 WARN_ONCE(true, "Invalid local APIC state");
6065 case LAPIC_MODE_DISABLED:
6066 break;
6067 case LAPIC_MODE_XAPIC:
6068 if (flexpriority_enabled) {
6069 sec_exec_control |=
6070 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006071 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6072
6073 /*
6074 * Flush the TLB, reloading the APIC access page will
6075 * only do so if its physical address has changed, but
6076 * the guest may have inserted a non-APIC mapping into
6077 * the TLB while the APIC access page was disabled.
6078 */
6079 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006080 }
6081 break;
6082 case LAPIC_MODE_X2APIC:
6083 if (cpu_has_vmx_virtualize_x2apic_mode())
6084 sec_exec_control |=
6085 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6086 break;
Yang Zhang8d146952013-01-25 10:18:50 +08006087 }
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006088 secondary_exec_controls_set(vmx, sec_exec_control);
Yang Zhang8d146952013-01-25 10:18:50 +08006089
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006090 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006091}
6092
Sean Christophersona4148b72020-03-20 14:28:24 -07006093static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
Tang Chen38b99172014-09-24 15:57:54 +08006094{
Sean Christophersona4148b72020-03-20 14:28:24 -07006095 struct page *page;
6096
Sean Christopherson1196cb92020-03-20 14:28:23 -07006097 /* Defer reload until vmcs01 is the current VMCS. */
6098 if (is_guest_mode(vcpu)) {
6099 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true;
6100 return;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07006101 }
Sean Christopherson1196cb92020-03-20 14:28:23 -07006102
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006103 if (!(secondary_exec_controls_get(to_vmx(vcpu)) &
6104 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
6105 return;
6106
Sean Christophersona4148b72020-03-20 14:28:24 -07006107 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
6108 if (is_error_page(page))
6109 return;
6110
6111 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
Sean Christopherson1196cb92020-03-20 14:28:23 -07006112 vmx_flush_tlb_current(vcpu);
Sean Christophersona4148b72020-03-20 14:28:24 -07006113
6114 /*
6115 * Do not pin apic access page in memory, the MMU notifier
6116 * will call us again if it is migrated or swapped out.
6117 */
6118 put_page(page);
Tang Chen38b99172014-09-24 15:57:54 +08006119}
6120
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006121static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006122{
6123 u16 status;
6124 u8 old;
6125
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006126 if (max_isr == -1)
6127 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006128
6129 status = vmcs_read16(GUEST_INTR_STATUS);
6130 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006131 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006132 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006133 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006134 vmcs_write16(GUEST_INTR_STATUS, status);
6135 }
6136}
6137
6138static void vmx_set_rvi(int vector)
6139{
6140 u16 status;
6141 u8 old;
6142
Wei Wang4114c272014-11-05 10:53:43 +08006143 if (vector == -1)
6144 vector = 0;
6145
Yang Zhangc7c9c562013-01-25 10:18:51 +08006146 status = vmcs_read16(GUEST_INTR_STATUS);
6147 old = (u8)status & 0xff;
6148 if ((u8)vector != old) {
6149 status &= ~0xff;
6150 status |= (u8)vector;
6151 vmcs_write16(GUEST_INTR_STATUS, status);
6152 }
6153}
6154
6155static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6156{
Liran Alon851c1a182017-12-24 18:12:56 +02006157 /*
6158 * When running L2, updating RVI is only relevant when
6159 * vmcs12 virtual-interrupt-delivery enabled.
6160 * However, it can be enabled only when L1 also
6161 * intercepts external-interrupts and in that case
6162 * we should not update vmcs02 RVI but instead intercept
6163 * interrupt. Therefore, do nothing when running L2.
6164 */
6165 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08006166 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006167}
6168
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006169static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006170{
6171 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006172 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02006173 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006174
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006175 WARN_ON(!vcpu->arch.apicv_active);
6176 if (pi_test_on(&vmx->pi_desc)) {
6177 pi_clear_on(&vmx->pi_desc);
6178 /*
Liran Alond9ff2742019-11-11 14:25:25 +02006179 * IOMMU can write to PID.ON, so the barrier matters even on UP.
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006180 * But on x86 this is just a compiler barrier anyway.
6181 */
6182 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02006183 max_irr_updated =
6184 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6185
6186 /*
6187 * If we are running L2 and L1 has a new pending interrupt
6188 * which can be injected, we should re-evaluate
6189 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02006190 * If L1 intercepts external-interrupts, we should
6191 * exit from L2 to L1. Otherwise, interrupt should be
6192 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02006193 */
Liran Alon851c1a182017-12-24 18:12:56 +02006194 if (is_guest_mode(vcpu) && max_irr_updated) {
6195 if (nested_exit_on_intr(vcpu))
6196 kvm_vcpu_exiting_guest_mode(vcpu);
6197 else
6198 kvm_make_request(KVM_REQ_EVENT, vcpu);
6199 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006200 } else {
6201 max_irr = kvm_lapic_find_highest_irr(vcpu);
6202 }
6203 vmx_hwapic_irr_update(vcpu, max_irr);
6204 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006205}
6206
Andrey Smetanin63086302015-11-10 15:36:32 +03006207static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006208{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006209 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08006210 return;
6211
Yang Zhangc7c9c562013-01-25 10:18:51 +08006212 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6213 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6214 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6215 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6216}
6217
Paolo Bonzini967235d2016-12-19 14:03:45 +01006218static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
6219{
6220 struct vcpu_vmx *vmx = to_vmx(vcpu);
6221
6222 pi_clear_on(&vmx->pi_desc);
6223 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
6224}
6225
Sean Christopherson535f7ef2020-09-15 12:15:04 -07006226void vmx_do_interrupt_nmi_irqoff(unsigned long entry);
6227
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006228static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, u32 intr_info)
6229{
6230 unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK;
6231 gate_desc *desc = (gate_desc *)host_idt_base + vector;
6232
6233 kvm_before_interrupt(vcpu);
6234 vmx_do_interrupt_nmi_irqoff(gate_offset(desc));
6235 kvm_after_interrupt(vcpu);
6236}
6237
Sean Christopherson95b5a482019-04-19 22:50:59 -07006238static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006239{
Sean Christopherson87915852020-04-15 13:34:54 -07006240 u32 intr_info = vmx_get_intr_info(&vmx->vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006241
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006242 /* if exit due to PF check for async PF */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006243 if (is_page_fault(intr_info))
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02006244 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags();
Andi Kleena0861c02009-06-08 17:37:09 +08006245 /* Handle machine checks before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006246 else if (is_machine_check(intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08006247 kvm_machine_check();
Gleb Natapov20f65982009-05-11 13:35:55 +03006248 /* We need to handle NMIs before interrupts are enabled */
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006249 else if (is_nmi(intr_info))
6250 handle_interrupt_nmi_irqoff(&vmx->vcpu, intr_info);
Avi Kivity51aa01d2010-07-20 14:31:20 +03006251}
Gleb Natapov20f65982009-05-11 13:35:55 +03006252
Sean Christopherson95b5a482019-04-19 22:50:59 -07006253static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006254{
Sean Christopherson87915852020-04-15 13:34:54 -07006255 u32 intr_info = vmx_get_intr_info(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006256
Sean Christopherson49def502019-04-19 22:50:56 -07006257 if (WARN_ONCE(!is_external_intr(intr_info),
6258 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
6259 return;
6260
Sean Christopherson1a5488e2020-09-15 12:15:05 -07006261 handle_interrupt_nmi_irqoff(vcpu, intr_info);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006262}
Sean Christopherson95b5a482019-04-19 22:50:59 -07006263
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006264static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006265{
6266 struct vcpu_vmx *vmx = to_vmx(vcpu);
6267
6268 if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6269 handle_external_interrupt_irqoff(vcpu);
6270 else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)
6271 handle_exception_nmi_irqoff(vmx);
6272}
Yang Zhanga547c6d2013-04-11 19:25:10 +08006273
Sean Christophersoncb97c2d2020-02-18 15:40:11 -08006274static bool vmx_has_emulated_msr(u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006275{
Tom Lendackybc226f02018-05-10 22:06:39 +02006276 switch (index) {
6277 case MSR_IA32_SMBASE:
6278 /*
6279 * We cannot do SMM unless we can run the guest in big
6280 * real mode.
6281 */
6282 return enable_unrestricted_guest || emulate_invalid_guest_state;
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02006283 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
6284 return nested;
Tom Lendackybc226f02018-05-10 22:06:39 +02006285 case MSR_AMD64_VIRT_SPEC_CTRL:
6286 /* This is AMD only. */
6287 return false;
6288 default:
6289 return true;
6290 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006291}
6292
Avi Kivity51aa01d2010-07-20 14:31:20 +03006293static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6294{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006295 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006296 bool unblock_nmi;
6297 u8 vector;
6298 bool idtv_info_valid;
6299
6300 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006301
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006302 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006303 if (vmx->loaded_vmcs->nmi_known_unmasked)
6304 return;
Sean Christopherson87915852020-04-15 13:34:54 -07006305
6306 exit_intr_info = vmx_get_intr_info(&vmx->vcpu);
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006307 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6308 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6309 /*
6310 * SDM 3: 27.7.1.2 (September 2008)
6311 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6312 * a guest IRET fault.
6313 * SDM 3: 23.2.2 (September 2008)
6314 * Bit 12 is undefined in any of the following cases:
6315 * If the VM exit sets the valid bit in the IDT-vectoring
6316 * information field.
6317 * If the VM exit is due to a double fault.
6318 */
6319 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6320 vector != DF_VECTOR && !idtv_info_valid)
6321 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6322 GUEST_INTR_STATE_NMI);
6323 else
6324 vmx->loaded_vmcs->nmi_known_unmasked =
6325 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6326 & GUEST_INTR_STATE_NMI);
6327 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
6328 vmx->loaded_vmcs->vnmi_blocked_time +=
6329 ktime_to_ns(ktime_sub(ktime_get(),
6330 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006331}
6332
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006333static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006334 u32 idt_vectoring_info,
6335 int instr_len_field,
6336 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006337{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006338 u8 vector;
6339 int type;
6340 bool idtv_info_valid;
6341
6342 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006343
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006344 vcpu->arch.nmi_injected = false;
6345 kvm_clear_exception_queue(vcpu);
6346 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006347
6348 if (!idtv_info_valid)
6349 return;
6350
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006351 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006352
Avi Kivity668f6122008-07-02 09:28:55 +03006353 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6354 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006355
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006356 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006357 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006358 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006359 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006360 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006361 * Clear bit "block by NMI" before VM entry if a NMI
6362 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006363 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006364 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006365 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006366 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006367 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006368 fallthrough;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006369 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006370 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006371 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03006372 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006373 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03006374 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006375 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006376 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006377 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006378 fallthrough;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006379 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006380 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006381 break;
6382 default:
6383 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006384 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006385}
6386
Avi Kivity83422e12010-07-20 14:43:23 +03006387static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6388{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006389 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006390 VM_EXIT_INSTRUCTION_LEN,
6391 IDT_VECTORING_ERROR_CODE);
6392}
6393
Avi Kivityb463a6f2010-07-20 15:06:17 +03006394static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6395{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006396 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006397 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6398 VM_ENTRY_INSTRUCTION_LEN,
6399 VM_ENTRY_EXCEPTION_ERROR_CODE);
6400
6401 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6402}
6403
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006404static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6405{
6406 int i, nr_msrs;
6407 struct perf_guest_switch_msr *msrs;
6408
6409 msrs = perf_guest_get_msrs(&nr_msrs);
6410
6411 if (!msrs)
6412 return;
6413
6414 for (i = 0; i < nr_msrs; i++)
6415 if (msrs[i].host == msrs[i].guest)
6416 clear_atomic_switch_msr(vmx, msrs[i].msr);
6417 else
6418 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006419 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006420}
6421
Sean Christophersonf459a702018-08-27 15:21:11 -07006422static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006423{
6424 struct vcpu_vmx *vmx = to_vmx(vcpu);
6425 u64 tscl;
6426 u32 delta_tsc;
6427
Sean Christophersond264ee02018-08-27 15:21:12 -07006428 if (vmx->req_immediate_exit) {
Sean Christopherson804939e2019-05-07 12:18:05 -07006429 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
6430 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6431 } else if (vmx->hv_deadline_tsc != -1) {
Sean Christophersonf459a702018-08-27 15:21:11 -07006432 tscl = rdtsc();
6433 if (vmx->hv_deadline_tsc > tscl)
6434 /* set_hv_timer ensures the delta fits in 32-bits */
6435 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6436 cpu_preemption_timer_multi);
6437 else
6438 delta_tsc = 0;
6439
Sean Christopherson804939e2019-05-07 12:18:05 -07006440 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
6441 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6442 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
6443 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
6444 vmx->loaded_vmcs->hv_timer_soft_disabled = true;
Sean Christophersonf459a702018-08-27 15:21:11 -07006445 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006446}
6447
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006448void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006449{
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006450 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
6451 vmx->loaded_vmcs->host_state.rsp = host_rsp;
6452 vmcs_writel(HOST_RSP, host_rsp);
6453 }
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006454}
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006455
Wanpeng Li404d5d72020-04-28 14:23:25 +08006456static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006457{
6458 switch (to_vmx(vcpu)->exit_reason) {
6459 case EXIT_REASON_MSR_WRITE:
6460 return handle_fastpath_set_msr_irqoff(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04006461 case EXIT_REASON_PREEMPTION_TIMER:
6462 return handle_fastpath_preemption_timer(vcpu);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006463 default:
6464 return EXIT_FASTPATH_NONE;
6465 }
6466}
6467
Sean Christophersonfc2ba5a2019-01-25 07:41:19 -08006468bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006469
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006470static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
6471 struct vcpu_vmx *vmx)
6472{
6473 /*
6474 * VMENTER enables interrupts (host state), but the kernel state is
6475 * interrupts disabled when this is invoked. Also tell RCU about
6476 * it. This is the same logic as for exit_to_user_mode().
6477 *
6478 * This ensures that e.g. latency analysis on the host observes
6479 * guest mode as interrupt enabled.
6480 *
6481 * guest_enter_irqoff() informs context tracking about the
6482 * transition to guest mode and if enabled adjusts RCU state
6483 * accordingly.
6484 */
6485 instrumentation_begin();
6486 trace_hardirqs_on_prepare();
6487 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
6488 instrumentation_end();
6489
6490 guest_enter_irqoff();
6491 lockdep_hardirqs_on(CALLER_ADDR0);
6492
6493 /* L1D Flush includes CPU buffer clear to mitigate MDS */
6494 if (static_branch_unlikely(&vmx_l1d_should_flush))
6495 vmx_l1d_flush(vcpu);
6496 else if (static_branch_unlikely(&mds_user_clear))
6497 mds_clear_cpu_buffers();
6498
Thomas Gleixner2245d392020-07-08 21:52:00 +02006499 if (vcpu->arch.cr2 != native_read_cr2())
6500 native_write_cr2(vcpu->arch.cr2);
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006501
6502 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
6503 vmx->loaded_vmcs->launched);
6504
Thomas Gleixner2245d392020-07-08 21:52:00 +02006505 vcpu->arch.cr2 = native_read_cr2();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006506
6507 /*
6508 * VMEXIT disables interrupts (host state), but tracing and lockdep
6509 * have them in state 'on' as recorded before entering guest mode.
6510 * Same as enter_from_user_mode().
6511 *
6512 * guest_exit_irqoff() restores host context and reinstates RCU if
6513 * enabled and required.
6514 *
6515 * This needs to be done before the below as native_read_msr()
6516 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
6517 * into world and some more.
6518 */
6519 lockdep_hardirqs_off(CALLER_ADDR0);
6520 guest_exit_irqoff();
6521
6522 instrumentation_begin();
6523 trace_hardirqs_off_finish();
6524 instrumentation_end();
6525}
6526
Wanpeng Li404d5d72020-04-28 14:23:25 +08006527static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006528{
Wanpeng Li404d5d72020-04-28 14:23:25 +08006529 fastpath_t exit_fastpath;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006530 struct vcpu_vmx *vmx = to_vmx(vcpu);
6531 unsigned long cr3, cr4;
6532
Wanpeng Li404d5d72020-04-28 14:23:25 +08006533reenter_guest:
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006534 /* Record the guest's net vcpu time for enforced NMI injections. */
6535 if (unlikely(!enable_vnmi &&
6536 vmx->loaded_vmcs->soft_vnmi_blocked))
6537 vmx->loaded_vmcs->entry_time = ktime_get();
6538
6539 /* Don't enter VMX if guest state is invalid, let the exit handler
6540 start emulation until we arrive back to a valid state */
6541 if (vmx->emulation_required)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006542 return EXIT_FASTPATH_NONE;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006543
6544 if (vmx->ple_window_dirty) {
6545 vmx->ple_window_dirty = false;
6546 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6547 }
6548
wanpeng lic9dfd3f2020-02-17 18:37:43 +08006549 /*
6550 * We did this in prepare_switch_to_guest, because it needs to
6551 * be within srcu_read_lock.
6552 */
6553 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006554
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006555 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006556 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006557 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006558 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6559
6560 cr3 = __get_current_cr3_fast();
6561 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6562 vmcs_writel(HOST_CR3, cr3);
6563 vmx->loaded_vmcs->host_state.cr3 = cr3;
6564 }
6565
6566 cr4 = cr4_read_shadow();
6567 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
6568 vmcs_writel(HOST_CR4, cr4);
6569 vmx->loaded_vmcs->host_state.cr4 = cr4;
6570 }
6571
6572 /* When single-stepping over STI and MOV SS, we must clear the
6573 * corresponding interruptibility bits in the guest state. Otherwise
6574 * vmentry fails as it then expects bit 14 (BS) in pending debug
6575 * exceptions being set, but that's not correct for the guest debugging
6576 * case. */
6577 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6578 vmx_set_interrupt_shadow(vcpu, 0);
6579
Aaron Lewis139a12c2019-10-21 16:30:25 -07006580 kvm_load_guest_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006581
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006582 pt_guest_enter(vmx);
6583
Vitaly Kuznetsov49097762020-06-19 11:40:46 +02006584 atomic_switch_perf_msrs(vmx);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006585
Sean Christopherson804939e2019-05-07 12:18:05 -07006586 if (enable_preemption_timer)
6587 vmx_update_hv_timer(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006588
Wanpeng Li010fd372020-09-10 17:50:41 +08006589 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08006590
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006591 /*
6592 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6593 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6594 * is no need to worry about the conditional branch over the wrmsr
6595 * being speculatively taken.
6596 */
6597 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
6598
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006599 /* The actual VMENTER/EXIT is in the .noinstr.text section. */
6600 vmx_vcpu_enter_exit(vcpu, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006601
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006602 /*
6603 * We do not use IBRS in the kernel. If this vCPU has used the
6604 * SPEC_CTRL MSR it may have left it on; save the value and
6605 * turn it off. This is much more efficient than blindly adding
6606 * it to the atomic save/restore list. Especially as the former
6607 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6608 *
6609 * For non-nested case:
6610 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6611 * save it.
6612 *
6613 * For nested case:
6614 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6615 * save it.
6616 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006617 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006618 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006619
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006620 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006621
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006622 /* All fields are clean at this point */
6623 if (static_branch_unlikely(&enable_evmcs))
6624 current_evmcs->hv_clean_fields |=
6625 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6626
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08006627 if (static_branch_unlikely(&enable_evmcs))
6628 current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index;
6629
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006630 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006631 if (vmx->host_debugctlmsr)
6632 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006633
Avi Kivityaa67f602012-08-01 16:48:03 +03006634#ifndef CONFIG_X86_64
6635 /*
6636 * The sysexit path does not restore ds/es, so we must set them to
6637 * a reasonable value ourselves.
6638 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006639 * We can't defer this to vmx_prepare_switch_to_host() since that
6640 * function may be executed in interrupt context, which saves and
6641 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006642 */
6643 loadsegment(ds, __USER_DS);
6644 loadsegment(es, __USER_DS);
6645#endif
6646
Sean Christophersone5d03de2020-04-15 13:34:51 -07006647 vmx_register_cache_reset(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006648
Chao Peng2ef444f2018-10-24 16:05:12 +08006649 pt_guest_exit(vmx);
6650
Aaron Lewis139a12c2019-10-21 16:30:25 -07006651 kvm_load_host_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006652
Gleb Natapove0b890d2013-09-25 12:51:33 +03006653 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006654 vmx->idt_vectoring_info = 0;
6655
Sean Christopherson873e1da2020-04-10 10:47:02 -07006656 if (unlikely(vmx->fail)) {
6657 vmx->exit_reason = 0xdead;
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006658 return EXIT_FASTPATH_NONE;
Sean Christopherson873e1da2020-04-10 10:47:02 -07006659 }
6660
6661 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
6662 if (unlikely((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY))
Sean Christophersonbeb8d932019-04-19 22:50:55 -07006663 kvm_machine_check();
6664
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006665 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
6666
Sean Christopherson873e1da2020-04-10 10:47:02 -07006667 if (unlikely(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006668 return EXIT_FASTPATH_NONE;
6669
Jim Mattsonb060ca32017-09-14 16:31:42 -07006670 vmx->loaded_vmcs->launched = 1;
6671 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03006672
Avi Kivity51aa01d2010-07-20 14:31:20 +03006673 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006674 vmx_complete_interrupts(vmx);
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006675
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006676 if (is_guest_mode(vcpu))
6677 return EXIT_FASTPATH_NONE;
6678
6679 exit_fastpath = vmx_exit_handlers_fastpath(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006680 if (exit_fastpath == EXIT_FASTPATH_REENTER_GUEST) {
6681 if (!kvm_vcpu_exit_request(vcpu)) {
6682 /*
6683 * FIXME: this goto should be a loop in vcpu_enter_guest,
6684 * but it would incur the cost of a retpoline for now.
6685 * Revisit once static calls are available.
6686 */
Wanpeng Li379a3c82020-04-28 14:23:27 +08006687 if (vcpu->arch.apicv_active)
6688 vmx_sync_pir_to_irr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006689 goto reenter_guest;
6690 }
6691 exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
6692 }
6693
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006694 return exit_fastpath;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006695}
6696
Avi Kivity6aa8b732006-12-10 02:21:36 -08006697static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6698{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006699 struct vcpu_vmx *vmx = to_vmx(vcpu);
6700
Kai Huang843e4332015-01-28 10:54:28 +08006701 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006702 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006703 free_vpid(vmx->vpid);
Sean Christopherson55d23752018-12-03 13:53:18 -08006704 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006705 free_loaded_vmcs(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006706}
6707
Sean Christopherson987b2592019-12-18 13:54:55 -08006708static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006709{
Ben Gardon41836832019-02-11 11:02:52 -08006710 struct vcpu_vmx *vmx;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006711 unsigned long *msr_bitmap;
Sean Christopherson34109c02019-12-18 13:54:50 -08006712 int i, cpu, err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006713
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006714 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0);
6715 vmx = to_vmx(vcpu);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006716
Peter Feiner4e595162016-07-07 14:49:58 -07006717 err = -ENOMEM;
6718
Sean Christopherson034d8e22019-12-18 13:54:49 -08006719 vmx->vpid = allocate_vpid();
6720
Peter Feiner4e595162016-07-07 14:49:58 -07006721 /*
6722 * If PML is turned on, failure on enabling PML just results in failure
6723 * of creating the vcpu, therefore we can simplify PML logic (by
6724 * avoiding dealing with cases, such as enabling PML partially on vcpus
Miaohe Lin67b0ae42019-12-11 14:26:22 +08006725 * for the guest), etc.
Peter Feiner4e595162016-07-07 14:49:58 -07006726 */
6727 if (enable_pml) {
Ben Gardon41836832019-02-11 11:02:52 -08006728 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Peter Feiner4e595162016-07-07 14:49:58 -07006729 if (!vmx->pml_pg)
Sean Christopherson987b2592019-12-18 13:54:55 -08006730 goto free_vpid;
Peter Feiner4e595162016-07-07 14:49:58 -07006731 }
6732
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006733 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) != MAX_NR_USER_RETURN_MSRS);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006734
Xiaoyao Li4be53412019-10-20 17:11:00 +08006735 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6736 u32 index = vmx_msr_index[i];
6737 u32 data_low, data_high;
Sean Christophersonfbc18002020-09-23 11:03:59 -07006738 int j = vmx->nr_uret_msrs;
Xiaoyao Li4be53412019-10-20 17:11:00 +08006739
6740 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6741 continue;
6742 if (wrmsr_safe(index, data_low, data_high) < 0)
6743 continue;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006744
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006745 vmx->guest_uret_msrs[j].index = i;
6746 vmx->guest_uret_msrs[j].data = 0;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006747 switch (index) {
6748 case MSR_IA32_TSX_CTRL:
6749 /*
6750 * No need to pass TSX_CTRL_CPUID_CLEAR through, so
6751 * let's avoid changing CPUID bits under the host
6752 * kernel's feet.
6753 */
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006754 vmx->guest_uret_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006755 break;
6756 default:
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07006757 vmx->guest_uret_msrs[j].mask = -1ull;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006758 break;
6759 }
Sean Christophersonfbc18002020-09-23 11:03:59 -07006760 ++vmx->nr_uret_msrs;
Xiaoyao Li4be53412019-10-20 17:11:00 +08006761 }
6762
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006763 err = alloc_loaded_vmcs(&vmx->vmcs01);
6764 if (err < 0)
Jim Mattson7d737102019-12-03 16:24:42 -08006765 goto free_pml;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006766
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006767 msr_bitmap = vmx->vmcs01.msr_bitmap;
Jim Mattson788fc1e2018-11-09 09:35:11 -08006768 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006769 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
6770 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
6771 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
6772 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6773 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6774 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
Sean Christopherson987b2592019-12-18 13:54:55 -08006775 if (kvm_cstate_in_guest(vcpu->kvm)) {
Wanpeng Lib5170062019-05-21 14:06:53 +08006776 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
6777 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
6778 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
6779 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
6780 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006781 vmx->msr_bitmap_mode = 0;
6782
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006783 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03006784 cpu = get_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006785 vmx_vcpu_load(vcpu, cpu);
6786 vcpu->cpu = cpu;
Xiaoyao Li1b842922019-10-20 17:11:01 +08006787 init_vmcs(vmx);
Sean Christopherson34109c02019-12-18 13:54:50 -08006788 vmx_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006789 put_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006790 if (cpu_need_virtualize_apic_accesses(vcpu)) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006791 err = alloc_apic_access_page(vcpu->kvm);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006792 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006793 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02006794 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006795
Sean Christophersone90008d2018-03-05 12:04:37 -08006796 if (enable_ept && !enable_unrestricted_guest) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006797 err = init_rmode_identity_map(vcpu->kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08006798 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02006799 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006800 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006801
Roman Kagan63aff652018-07-19 21:59:07 +03006802 if (nested)
Chenyi Qiangb9757a42020-08-28 16:56:22 +08006803 memcpy(&vmx->nested.msrs, &vmcs_config.nested, sizeof(vmx->nested.msrs));
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006804 else
6805 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08006806
Wincy Van705699a2015-02-03 23:58:17 +08006807 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006808 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006809
Paolo Bonzinibab0c312020-02-11 18:40:58 +01006810 vcpu->arch.microcode_version = 0x100000000ULL;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08006811 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006812
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02006813 /*
6814 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
6815 * or POSTED_INTR_WAKEUP_VECTOR.
6816 */
6817 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
6818 vmx->pi_desc.sn = 1;
6819
Lan Tianyu53963a72018-12-06 15:34:36 +08006820 vmx->ept_pointer = INVALID_PAGE;
6821
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006822 return 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006823
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006824free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006825 free_loaded_vmcs(vmx->loaded_vmcs);
Peter Feiner4e595162016-07-07 14:49:58 -07006826free_pml:
6827 vmx_destroy_pml_buffer(vmx);
Sean Christopherson987b2592019-12-18 13:54:55 -08006828free_vpid:
Wanpeng Li991e7a02015-09-16 17:30:05 +08006829 free_vpid(vmx->vpid);
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006830 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006831}
6832
Thomas Gleixner65fd4cb2019-02-19 11:10:49 +01006833#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"
6834#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 -04006835
Wanpeng Lib31c1142018-03-12 04:53:04 -07006836static int vmx_vm_init(struct kvm *kvm)
6837{
Tianyu Lan877ad952018-07-19 08:40:23 +00006838 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
6839
Wanpeng Lib31c1142018-03-12 04:53:04 -07006840 if (!ple_gap)
6841 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006842
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006843 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
6844 switch (l1tf_mitigation) {
6845 case L1TF_MITIGATION_OFF:
6846 case L1TF_MITIGATION_FLUSH_NOWARN:
6847 /* 'I explicitly don't care' is set */
6848 break;
6849 case L1TF_MITIGATION_FLUSH:
6850 case L1TF_MITIGATION_FLUSH_NOSMT:
6851 case L1TF_MITIGATION_FULL:
6852 /*
6853 * Warn upon starting the first VM in a potentially
6854 * insecure environment.
6855 */
Josh Poimboeufb2849092019-01-30 07:13:58 -06006856 if (sched_smt_active())
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006857 pr_warn_once(L1TF_MSG_SMT);
6858 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
6859 pr_warn_once(L1TF_MSG_L1D);
6860 break;
6861 case L1TF_MITIGATION_FULL_FORCE:
6862 /* Flush is enforced */
6863 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006864 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006865 }
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06006866 kvm_apicv_init(kvm, enable_apicv);
Wanpeng Lib31c1142018-03-12 04:53:04 -07006867 return 0;
6868}
6869
Sean Christophersonf257d6d2019-04-19 22:18:17 -07006870static int __init vmx_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03006871{
6872 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08006873 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03006874
Sean Christophersonff10e222019-12-20 20:45:10 -08006875 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
6876 !this_cpu_has(X86_FEATURE_VMX)) {
6877 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id());
6878 return -EIO;
6879 }
6880
Sean Christopherson7caaa712018-12-03 13:53:01 -08006881 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07006882 return -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006883 if (nested)
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006884 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept);
Yang, Sheng002c7f72007-07-31 14:23:01 +03006885 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6886 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6887 smp_processor_id());
Sean Christophersonf257d6d2019-04-19 22:18:17 -07006888 return -EIO;
Yang, Sheng002c7f72007-07-31 14:23:01 +03006889 }
Sean Christophersonf257d6d2019-04-19 22:18:17 -07006890 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03006891}
6892
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006893static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08006894{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006895 u8 cache;
6896 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006897
Chia-I Wu222f06e2020-02-13 13:30:34 -08006898 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in
6899 * memory aliases with conflicting memory types and sometimes MCEs.
6900 * We have to be careful as to what are honored and when.
6901 *
6902 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to
6903 * UC. The effective memory type is UC or WC depending on guest PAT.
6904 * This was historically the source of MCEs and we want to be
6905 * conservative.
6906 *
6907 * When there is no need to deal with noncoherent DMA (e.g., no VT-d
6908 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The
6909 * EPT memory type is set to WB. The effective memory type is forced
6910 * WB.
6911 *
6912 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The
6913 * EPT memory type is used to emulate guest CD/MTRR.
Sheng Yang522c68c2009-04-27 20:35:43 +08006914 */
Chia-I Wu222f06e2020-02-13 13:30:34 -08006915
Paolo Bonzini606decd2015-10-01 13:12:47 +02006916 if (is_mmio) {
6917 cache = MTRR_TYPE_UNCACHABLE;
6918 goto exit;
6919 }
6920
6921 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006922 ipat = VMX_EPT_IPAT_BIT;
6923 cache = MTRR_TYPE_WRBACK;
6924 goto exit;
6925 }
6926
6927 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
6928 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02006929 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08006930 cache = MTRR_TYPE_WRBACK;
6931 else
6932 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006933 goto exit;
6934 }
6935
Xiao Guangrongff536042015-06-15 16:55:22 +08006936 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006937
6938exit:
6939 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08006940}
6941
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006942static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006943{
6944 /*
6945 * These bits in the secondary execution controls field
6946 * are dynamic, the others are mostly based on the hypervisor
6947 * architecture and the guest's CPUID. Do not touch the
6948 * dynamic bits.
6949 */
6950 u32 mask =
6951 SECONDARY_EXEC_SHADOW_VMCS |
6952 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02006953 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6954 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006955
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006956 u32 new_ctl = vmx->secondary_exec_control;
6957 u32 cur_ctl = secondary_exec_controls_get(vmx);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006958
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006959 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006960}
6961
David Matlack8322ebb2016-11-29 18:14:09 -08006962/*
6963 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
6964 * (indicating "allowed-1") if they are supported in the guest's CPUID.
6965 */
6966static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
6967{
6968 struct vcpu_vmx *vmx = to_vmx(vcpu);
6969 struct kvm_cpuid_entry2 *entry;
6970
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006971 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
6972 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08006973
6974#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
6975 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006976 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08006977} while (0)
6978
6979 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08006980 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME));
6981 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME));
6982 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC));
6983 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE));
6984 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE));
6985 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE));
6986 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE));
6987 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE));
6988 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR));
6989 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM));
6990 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX));
6991 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX));
6992 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID));
6993 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE));
David Matlack8322ebb2016-11-29 18:14:09 -08006994
6995 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08006996 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE));
6997 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP));
6998 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP));
6999 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU));
7000 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP));
7001 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57));
David Matlack8322ebb2016-11-29 18:14:09 -08007002
7003#undef cr4_fixed1_update
7004}
7005
Liran Alon5f76f6f2018-09-14 03:25:52 +03007006static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
7007{
7008 struct vcpu_vmx *vmx = to_vmx(vcpu);
7009
7010 if (kvm_mpx_supported()) {
7011 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
7012
7013 if (mpx_enabled) {
7014 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7015 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7016 } else {
7017 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7018 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7019 }
7020 }
7021}
7022
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007023static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7024{
7025 struct vcpu_vmx *vmx = to_vmx(vcpu);
7026 struct kvm_cpuid_entry2 *best = NULL;
7027 int i;
7028
7029 for (i = 0; i < PT_CPUID_LEAVES; i++) {
7030 best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7031 if (!best)
7032 return;
7033 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7034 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7035 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7036 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7037 }
7038
7039 /* Get the number of configurable Address Ranges for filtering */
7040 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7041 PT_CAP_num_address_ranges);
7042
7043 /* Initialize and clear the no dependency bits */
7044 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7045 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7046
7047 /*
7048 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7049 * will inject an #GP
7050 */
7051 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7052 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7053
7054 /*
7055 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7056 * PSBFreq can be set
7057 */
7058 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7059 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7060 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7061
7062 /*
7063 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7064 * MTCFreq can be set
7065 */
7066 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7067 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7068 RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7069
7070 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7071 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7072 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7073 RTIT_CTL_PTW_EN);
7074
7075 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7076 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7077 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7078
7079 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7080 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7081 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7082
7083 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */
7084 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7085 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7086
7087 /* unmask address range configure area */
7088 for (i = 0; i < vmx->pt_desc.addr_range; i++)
Gustavo A. R. Silvad14eff12018-12-26 14:40:59 -06007089 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007090}
7091
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007092static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08007093{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007094 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007095
Aaron Lewis72041602019-10-21 16:30:20 -07007096 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7097 vcpu->arch.xsaves_enabled = false;
7098
Paolo Bonzini80154d72017-08-24 13:55:35 +02007099 if (cpu_has_secondary_exec_ctrls()) {
7100 vmx_compute_secondary_exec_control(vmx);
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007101 vmcs_set_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007102 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007103
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007104 if (nested_vmx_allowed(vcpu))
7105 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007106 FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7107 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007108 else
7109 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007110 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7111 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX);
David Matlack8322ebb2016-11-29 18:14:09 -08007112
Liran Alon5f76f6f2018-09-14 03:25:52 +03007113 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08007114 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03007115 nested_vmx_entry_exit_ctls_update(vcpu);
7116 }
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007117
7118 if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7119 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7120 update_intel_pt_cfg(vcpu);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007121
7122 if (boot_cpu_has(X86_FEATURE_RTM)) {
Sean Christophersoneb3db1b2020-09-23 11:03:58 -07007123 struct vmx_uret_msr *msr;
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007124 msr = find_msr_entry(vmx, MSR_IA32_TSX_CTRL);
7125 if (msr) {
7126 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
7127 vmx_set_guest_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
7128 }
7129 }
Sheng Yang0e851882009-12-18 16:48:46 +08007130}
7131
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007132static __init void vmx_set_cpu_caps(void)
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007133{
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007134 kvm_set_cpu_caps();
7135
7136 /* CPUID 0x1 */
7137 if (nested)
7138 kvm_cpu_cap_set(X86_FEATURE_VMX);
7139
7140 /* CPUID 0x7 */
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007141 if (kvm_mpx_supported())
7142 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX);
7143 if (cpu_has_vmx_invpcid())
7144 kvm_cpu_cap_check_and_set(X86_FEATURE_INVPCID);
7145 if (vmx_pt_mode_is_host_guest())
7146 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT);
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007147
Sean Christopherson90d2f602020-03-02 15:56:47 -08007148 if (vmx_umip_emulated())
7149 kvm_cpu_cap_set(X86_FEATURE_UMIP);
7150
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007151 /* CPUID 0xD.1 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01007152 supported_xss = 0;
Sean Christophersonbecdad82020-09-23 09:50:45 -07007153 if (!cpu_has_vmx_xsaves())
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007154 kvm_cpu_cap_clear(X86_FEATURE_XSAVES);
7155
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007156 /* CPUID 0x80000001 */
7157 if (!cpu_has_vmx_rdtscp())
7158 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007159
Sean Christophersonbecdad82020-09-23 09:50:45 -07007160 if (cpu_has_vmx_waitpkg())
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007161 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007162}
7163
Sean Christophersond264ee02018-08-27 15:21:12 -07007164static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7165{
7166 to_vmx(vcpu)->req_immediate_exit = true;
7167}
7168
Oliver Upton35a57132020-02-04 15:26:31 -08007169static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
7170 struct x86_instruction_info *info)
7171{
7172 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7173 unsigned short port;
7174 bool intercept;
7175 int size;
7176
7177 if (info->intercept == x86_intercept_in ||
7178 info->intercept == x86_intercept_ins) {
7179 port = info->src_val;
7180 size = info->dst_bytes;
7181 } else {
7182 port = info->dst_val;
7183 size = info->src_bytes;
7184 }
7185
7186 /*
7187 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
7188 * VM-exits depend on the 'unconditional IO exiting' VM-execution
7189 * control.
7190 *
7191 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
7192 */
7193 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7194 intercept = nested_cpu_has(vmcs12,
7195 CPU_BASED_UNCOND_IO_EXITING);
7196 else
7197 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
7198
Oliver Upton86f7e902020-02-29 11:30:14 -08007199 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
Oliver Upton35a57132020-02-04 15:26:31 -08007200 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
7201}
7202
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007203static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7204 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007205 enum x86_intercept_stage stage,
7206 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007207{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007208 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007209
Oliver Upton35a57132020-02-04 15:26:31 -08007210 switch (info->intercept) {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007211 /*
7212 * RDPID causes #UD if disabled through secondary execution controls.
7213 * Because it is marked as EmulateOnUD, we need to intercept it here.
7214 */
Oliver Upton35a57132020-02-04 15:26:31 -08007215 case x86_intercept_rdtscp:
Sean Christopherson7f3603b2020-09-23 09:50:47 -07007216 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_RDTSCP)) {
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007217 exception->vector = UD_VECTOR;
7218 exception->error_code_valid = false;
Oliver Upton35a57132020-02-04 15:26:31 -08007219 return X86EMUL_PROPAGATE_FAULT;
7220 }
7221 break;
7222
7223 case x86_intercept_in:
7224 case x86_intercept_ins:
7225 case x86_intercept_out:
7226 case x86_intercept_outs:
7227 return vmx_check_intercept_io(vcpu, info);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007228
Oliver Upton86f7e902020-02-29 11:30:14 -08007229 case x86_intercept_lgdt:
7230 case x86_intercept_lidt:
7231 case x86_intercept_lldt:
7232 case x86_intercept_ltr:
7233 case x86_intercept_sgdt:
7234 case x86_intercept_sidt:
7235 case x86_intercept_sldt:
7236 case x86_intercept_str:
7237 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
7238 return X86EMUL_CONTINUE;
7239
7240 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
7241 break;
7242
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007243 /* TODO: check more intercepts... */
Oliver Upton35a57132020-02-04 15:26:31 -08007244 default:
7245 break;
7246 }
7247
Paolo Bonzini07721fe2020-02-04 15:26:29 -08007248 return X86EMUL_UNHANDLEABLE;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007249}
7250
Yunhong Jiang64672c92016-06-13 14:19:59 -07007251#ifdef CONFIG_X86_64
7252/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
7253static inline int u64_shl_div_u64(u64 a, unsigned int shift,
7254 u64 divisor, u64 *result)
7255{
7256 u64 low = a << shift, high = a >> (64 - shift);
7257
7258 /* To avoid the overflow on divq */
7259 if (high >= divisor)
7260 return 1;
7261
7262 /* Low hold the result, high hold rem which is discarded */
7263 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
7264 "rm" (divisor), "0" (low), "1" (high));
7265 *result = low;
7266
7267 return 0;
7268}
7269
Sean Christophersonf9927982019-04-16 13:32:46 -07007270static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
7271 bool *expired)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007272{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007273 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007274 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
Sean Christopherson39497d72019-04-17 10:15:32 -07007275 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007276
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007277 vmx = to_vmx(vcpu);
7278 tscl = rdtsc();
7279 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
7280 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Sean Christopherson39497d72019-04-17 10:15:32 -07007281 lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
7282 ktimer->timer_advance_ns);
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007283
7284 if (delta_tsc > lapic_timer_advance_cycles)
7285 delta_tsc -= lapic_timer_advance_cycles;
7286 else
7287 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007288
7289 /* Convert to host delta tsc if tsc scaling is enabled */
7290 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
Sean Christopherson0967fa12019-04-16 13:32:48 -07007291 delta_tsc && u64_shl_div_u64(delta_tsc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007292 kvm_tsc_scaling_ratio_frac_bits,
Sean Christopherson0967fa12019-04-16 13:32:48 -07007293 vcpu->arch.tsc_scaling_ratio, &delta_tsc))
Yunhong Jiang64672c92016-06-13 14:19:59 -07007294 return -ERANGE;
7295
7296 /*
7297 * If the delta tsc can't fit in the 32 bit after the multi shift,
7298 * we can't use the preemption timer.
7299 * It's possible that it fits on later vmentries, but checking
7300 * on every vmentry is costly so we just use an hrtimer.
7301 */
7302 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
7303 return -ERANGE;
7304
7305 vmx->hv_deadline_tsc = tscl + delta_tsc;
Sean Christophersonf9927982019-04-16 13:32:46 -07007306 *expired = !delta_tsc;
7307 return 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007308}
7309
7310static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
7311{
Sean Christophersonf459a702018-08-27 15:21:11 -07007312 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007313}
7314#endif
7315
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007316static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007317{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007318 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007319 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007320}
7321
Kai Huang843e4332015-01-28 10:54:28 +08007322static void vmx_slot_enable_log_dirty(struct kvm *kvm,
7323 struct kvm_memory_slot *slot)
7324{
Jay Zhou3c9bd402020-02-27 09:32:27 +08007325 if (!kvm_dirty_log_manual_protect_and_init_set(kvm))
7326 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
Kai Huang843e4332015-01-28 10:54:28 +08007327 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
7328}
7329
7330static void vmx_slot_disable_log_dirty(struct kvm *kvm,
7331 struct kvm_memory_slot *slot)
7332{
7333 kvm_mmu_slot_set_dirty(kvm, slot);
7334}
7335
7336static void vmx_flush_log_dirty(struct kvm *kvm)
7337{
7338 kvm_flush_pml_buffers(kvm);
7339}
7340
7341static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
7342 struct kvm_memory_slot *memslot,
7343 gfn_t offset, unsigned long mask)
7344{
7345 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
7346}
7347
Yunhong Jiangbc225122016-06-13 14:19:58 -07007348static int vmx_pre_block(struct kvm_vcpu *vcpu)
7349{
7350 if (pi_pre_block(vcpu))
7351 return 1;
7352
Yunhong Jiang64672c92016-06-13 14:19:59 -07007353 if (kvm_lapic_hv_timer_in_use(vcpu))
7354 kvm_lapic_switch_to_sw_timer(vcpu);
7355
Yunhong Jiangbc225122016-06-13 14:19:58 -07007356 return 0;
7357}
7358
Yunhong Jiangbc225122016-06-13 14:19:58 -07007359static void vmx_post_block(struct kvm_vcpu *vcpu)
7360{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007361 if (kvm_x86_ops.set_hv_timer)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007362 kvm_lapic_switch_to_hv_timer(vcpu);
7363
Yunhong Jiangbc225122016-06-13 14:19:58 -07007364 pi_post_block(vcpu);
7365}
7366
Ashok Rajc45dcc72016-06-22 14:59:56 +08007367static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7368{
7369 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7370 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007371 FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007372 else
7373 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007374 ~FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007375}
7376
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007377static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Ladi Prosek72d7b372017-10-11 16:54:41 +02007378{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007379 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7380 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007381 return -EBUSY;
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -04007382 return !is_smm(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02007383}
7384
Ladi Prosek0234bf82017-10-11 16:54:40 +02007385static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7386{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007387 struct vcpu_vmx *vmx = to_vmx(vcpu);
7388
7389 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7390 if (vmx->nested.smm.guest_mode)
7391 nested_vmx_vmexit(vcpu, -1, 0, 0);
7392
7393 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7394 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007395 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007396 return 0;
7397}
7398
Sean Christophersoned193212019-04-02 08:03:09 -07007399static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007400{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007401 struct vcpu_vmx *vmx = to_vmx(vcpu);
7402 int ret;
7403
7404 if (vmx->nested.smm.vmxon) {
7405 vmx->nested.vmxon = true;
7406 vmx->nested.smm.vmxon = false;
7407 }
7408
7409 if (vmx->nested.smm.guest_mode) {
Sean Christophersona633e412018-09-26 09:23:47 -07007410 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007411 if (ret)
7412 return ret;
7413
7414 vmx->nested.smm.guest_mode = false;
7415 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007416 return 0;
7417}
7418
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007419static void enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007420{
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007421 /* RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007422}
7423
Liran Alon4b9852f2019-08-26 13:24:49 +03007424static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7425{
7426 return to_vmx(vcpu)->nested.vmxon;
7427}
7428
Jim Mattson93dff2f2020-05-08 13:36:43 -07007429static void vmx_migrate_timers(struct kvm_vcpu *vcpu)
7430{
7431 if (is_guest_mode(vcpu)) {
7432 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer;
7433
7434 if (hrtimer_try_to_cancel(timer) == 1)
7435 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
7436 }
7437}
7438
Sean Christopherson6e4fd062020-03-21 13:26:01 -07007439static void hardware_unsetup(void)
Sean Christophersona3203382018-12-03 13:53:11 -08007440{
7441 if (nested)
7442 nested_vmx_hardware_unsetup();
7443
7444 free_kvm_area();
7445}
7446
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007447static bool vmx_check_apicv_inhibit_reasons(ulong bit)
7448{
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -06007449 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
7450 BIT(APICV_INHIBIT_REASON_HYPERV);
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007451
7452 return supported & BIT(bit);
7453}
7454
Sean Christophersone286ac02020-03-21 13:26:02 -07007455static struct kvm_x86_ops vmx_x86_ops __initdata = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007456 .hardware_unsetup = hardware_unsetup,
Sean Christopherson484014f2020-03-21 13:25:57 -07007457
Avi Kivity6aa8b732006-12-10 02:21:36 -08007458 .hardware_enable = hardware_enable,
7459 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007460 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007461 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007462
Sean Christopherson484014f2020-03-21 13:25:57 -07007463 .vm_size = sizeof(struct kvm_vmx),
Wanpeng Lib31c1142018-03-12 04:53:04 -07007464 .vm_init = vmx_vm_init,
7465
Avi Kivity6aa8b732006-12-10 02:21:36 -08007466 .vcpu_create = vmx_create_vcpu,
7467 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007468 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007469
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007470 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007471 .vcpu_load = vmx_vcpu_load,
7472 .vcpu_put = vmx_vcpu_put,
7473
Paolo Bonzini69869822020-07-10 17:48:06 +02007474 .update_exception_bitmap = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007475 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007476 .get_msr = vmx_get_msr,
7477 .set_msr = vmx_set_msr,
7478 .get_segment_base = vmx_get_segment_base,
7479 .get_segment = vmx_get_segment,
7480 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007481 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007482 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
7483 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007484 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007485 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007486 .get_idt = vmx_get_idt,
7487 .set_idt = vmx_set_idt,
7488 .get_gdt = vmx_get_gdt,
7489 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007490 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007491 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007492 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007493 .get_rflags = vmx_get_rflags,
7494 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007495
Sean Christopherson77809382020-03-20 14:28:18 -07007496 .tlb_flush_all = vmx_flush_tlb_all,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07007497 .tlb_flush_current = vmx_flush_tlb_current,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007498 .tlb_flush_gva = vmx_flush_tlb_gva,
Sean Christophersone64419d2020-03-20 14:28:10 -07007499 .tlb_flush_guest = vmx_flush_tlb_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007500
Avi Kivity6aa8b732006-12-10 02:21:36 -08007501 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007502 .handle_exit = vmx_handle_exit,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08007503 .skip_emulated_instruction = vmx_skip_emulated_instruction,
7504 .update_emulated_instruction = vmx_update_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007505 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7506 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007507 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007508 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007509 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007510 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007511 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007512 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007513 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007514 .get_nmi_mask = vmx_get_nmi_mask,
7515 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007516 .enable_nmi_window = enable_nmi_window,
7517 .enable_irq_window = enable_irq_window,
7518 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007519 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007520 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007521 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007522 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007523 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007524 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007525 .hwapic_irr_update = vmx_hwapic_irr_update,
7526 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007527 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007528 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7529 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007530 .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007531
Izik Eiduscbc94022007-10-25 00:29:55 +02007532 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007533 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007534 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007535
Avi Kivity586f9602010-11-18 13:09:54 +02007536 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007537
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007538 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007539
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007540 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007541
Leonid Shatz326e7422018-11-06 12:14:25 +02007542 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007543
Sean Christopherson484014f2020-03-21 13:25:57 -07007544 .load_mmu_pgd = vmx_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007545
7546 .check_intercept = vmx_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007547 .handle_exit_irqoff = vmx_handle_exit_irqoff,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007548
Sean Christophersond264ee02018-08-27 15:21:12 -07007549 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007550
7551 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007552
7553 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
7554 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
7555 .flush_log_dirty = vmx_flush_log_dirty,
7556 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +02007557
Feng Wubf9f6ac2015-09-18 22:29:55 +08007558 .pre_block = vmx_pre_block,
7559 .post_block = vmx_post_block,
7560
Wei Huang25462f72015-06-19 15:45:05 +02007561 .pmu_ops = &intel_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04007562 .nested_ops = &vmx_nested_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007563
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007564 .update_pi_irte = pi_update_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007565
7566#ifdef CONFIG_X86_64
7567 .set_hv_timer = vmx_set_hv_timer,
7568 .cancel_hv_timer = vmx_cancel_hv_timer,
7569#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007570
7571 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007572
Ladi Prosek72d7b372017-10-11 16:54:41 +02007573 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007574 .pre_enter_smm = vmx_pre_enter_smm,
7575 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007576 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007577
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07007578 .can_emulate_instruction = vmx_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03007579 .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
Jim Mattson93dff2f2020-05-08 13:36:43 -07007580 .migrate_timers = vmx_migrate_timers,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007581};
7582
Avi Kivity6aa8b732006-12-10 02:21:36 -08007583static __init int hardware_setup(void)
7584{
7585 unsigned long host_bndcfgs;
7586 struct desc_ptr dt;
Sean Christopherson703c3352020-03-02 15:57:03 -08007587 int r, i, ept_lpage_level;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007588
Avi Kivity6aa8b732006-12-10 02:21:36 -08007589 store_idt(&dt);
7590 host_idt_base = dt.address;
7591
7592 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
Sean Christopherson7e34fbd2020-09-23 11:03:55 -07007593 kvm_define_user_return_msr(i, vmx_msr_index[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007594
7595 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
7596 return -EIO;
7597
7598 if (boot_cpu_has(X86_FEATURE_NX))
7599 kvm_enable_efer_bits(EFER_NX);
7600
7601 if (boot_cpu_has(X86_FEATURE_MPX)) {
7602 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7603 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7604 }
7605
Sean Christopherson7f5581f2020-03-02 15:56:24 -08007606 if (!cpu_has_vmx_mpx())
Sean Christophersoncfc48182020-03-02 15:56:23 -08007607 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
7608 XFEATURE_MASK_BNDCSR);
7609
Avi Kivity6aa8b732006-12-10 02:21:36 -08007610 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7611 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7612 enable_vpid = 0;
7613
7614 if (!cpu_has_vmx_ept() ||
7615 !cpu_has_vmx_ept_4levels() ||
7616 !cpu_has_vmx_ept_mt_wb() ||
7617 !cpu_has_vmx_invept_global())
7618 enable_ept = 0;
7619
7620 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7621 enable_ept_ad_bits = 0;
7622
7623 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Avi Kivity873a7c42006-12-13 00:34:14 -08007624 enable_unrestricted_guest = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007625
7626 if (!cpu_has_vmx_flexpriority())
7627 flexpriority_enabled = 0;
7628
7629 if (!cpu_has_virtual_nmis())
7630 enable_vnmi = 0;
7631
7632 /*
7633 * set_apic_access_page_addr() is used to reload apic access
7634 * page upon invalidation. No need to do anything if not
7635 * using the APIC_ACCESS_ADDR VMCS field.
7636 */
7637 if (!flexpriority_enabled)
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007638 vmx_x86_ops.set_apic_access_page_addr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007639
7640 if (!cpu_has_vmx_tpr_shadow())
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007641 vmx_x86_ops.update_cr8_intercept = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007642
Avi Kivity6aa8b732006-12-10 02:21:36 -08007643#if IS_ENABLED(CONFIG_HYPERV)
7644 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7645 && enable_ept) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007646 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
7647 vmx_x86_ops.tlb_remote_flush_with_range =
Avi Kivity6aa8b732006-12-10 02:21:36 -08007648 hv_remote_flush_tlb_with_range;
7649 }
7650#endif
7651
7652 if (!cpu_has_vmx_ple()) {
7653 ple_gap = 0;
7654 ple_window = 0;
7655 ple_window_grow = 0;
7656 ple_window_max = 0;
7657 ple_window_shrink = 0;
7658 }
7659
7660 if (!cpu_has_vmx_apicv()) {
7661 enable_apicv = 0;
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007662 vmx_x86_ops.sync_pir_to_irr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007663 }
7664
7665 if (cpu_has_vmx_tsc_scaling()) {
7666 kvm_has_tsc_control = true;
7667 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7668 kvm_tsc_scaling_ratio_frac_bits = 48;
7669 }
7670
7671 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7672
7673 if (enable_ept)
7674 vmx_enable_tdp();
Sean Christopherson703c3352020-03-02 15:57:03 -08007675
7676 if (!enable_ept)
7677 ept_lpage_level = 0;
7678 else if (cpu_has_vmx_ept_1g_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007679 ept_lpage_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08007680 else if (cpu_has_vmx_ept_2m_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07007681 ept_lpage_level = PG_LEVEL_2M;
Sean Christopherson703c3352020-03-02 15:57:03 -08007682 else
Sean Christopherson3bae0452020-04-27 17:54:22 -07007683 ept_lpage_level = PG_LEVEL_4K;
Sean Christopherson83013052020-07-15 20:41:22 -07007684 kvm_configure_mmu(enable_ept, vmx_get_max_tdp_level(), ept_lpage_level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007685
7686 /*
7687 * Only enable PML when hardware supports PML feature, and both EPT
7688 * and EPT A/D bit features are enabled -- PML depends on them to work.
7689 */
7690 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7691 enable_pml = 0;
7692
7693 if (!enable_pml) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007694 vmx_x86_ops.slot_enable_log_dirty = NULL;
7695 vmx_x86_ops.slot_disable_log_dirty = NULL;
7696 vmx_x86_ops.flush_log_dirty = NULL;
7697 vmx_x86_ops.enable_log_dirty_pt_masked = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007698 }
7699
7700 if (!cpu_has_vmx_preemption_timer())
7701 enable_preemption_timer = false;
7702
7703 if (enable_preemption_timer) {
7704 u64 use_timer_freq = 5000ULL * 1000 * 1000;
7705 u64 vmx_msr;
7706
7707 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7708 cpu_preemption_timer_multi =
7709 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7710
7711 if (tsc_khz)
7712 use_timer_freq = (u64)tsc_khz * 1000;
7713 use_timer_freq >>= cpu_preemption_timer_multi;
7714
7715 /*
7716 * KVM "disables" the preemption timer by setting it to its max
7717 * value. Don't use the timer if it might cause spurious exits
7718 * at a rate faster than 0.1 Hz (of uninterrupted guest time).
7719 */
7720 if (use_timer_freq > 0xffffffffu / 10)
7721 enable_preemption_timer = false;
7722 }
7723
7724 if (!enable_preemption_timer) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07007725 vmx_x86_ops.set_hv_timer = NULL;
7726 vmx_x86_ops.cancel_hv_timer = NULL;
7727 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007728 }
7729
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007730 kvm_set_posted_intr_wakeup_handler(pi_wakeup_handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007731
7732 kvm_mce_cap_supported |= MCG_LMCE_P;
7733
7734 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
7735 return -EINVAL;
7736 if (!enable_ept || !cpu_has_vmx_intel_pt())
7737 pt_mode = PT_MODE_SYSTEM;
7738
7739 if (nested) {
7740 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
7741 vmx_capability.ept);
7742
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07007743 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007744 if (r)
7745 return r;
7746 }
7747
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007748 vmx_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08007749
Avi Kivity6aa8b732006-12-10 02:21:36 -08007750 r = alloc_kvm_area();
7751 if (r)
7752 nested_vmx_hardware_unsetup();
7753 return r;
7754}
7755
Sean Christophersond008dfd2020-03-21 13:25:56 -07007756static struct kvm_x86_init_ops vmx_init_ops __initdata = {
7757 .cpu_has_kvm_support = cpu_has_kvm_support,
7758 .disabled_by_bios = vmx_disabled_by_bios,
7759 .check_processor_compatibility = vmx_check_processor_compat,
7760 .hardware_setup = hardware_setup,
7761
7762 .runtime_ops = &vmx_x86_ops,
7763};
7764
Avi Kivity6aa8b732006-12-10 02:21:36 -08007765static void vmx_cleanup_l1d_flush(void)
7766{
7767 if (vmx_l1d_flush_pages) {
7768 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
7769 vmx_l1d_flush_pages = NULL;
7770 }
7771 /* Restore state so sysfs ignores VMX */
7772 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
7773}
7774
7775static void vmx_exit(void)
7776{
7777#ifdef CONFIG_KEXEC_CORE
7778 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
7779 synchronize_rcu();
7780#endif
7781
7782 kvm_exit();
7783
7784#if IS_ENABLED(CONFIG_HYPERV)
7785 if (static_branch_unlikely(&enable_evmcs)) {
7786 int cpu;
7787 struct hv_vp_assist_page *vp_ap;
7788 /*
7789 * Reset everything to support using non-enlightened VMCS
7790 * access later (e.g. when we reload the module with
7791 * enlightened_vmcs=0)
7792 */
7793 for_each_online_cpu(cpu) {
7794 vp_ap = hv_get_vp_assist_page(cpu);
7795
7796 if (!vp_ap)
7797 continue;
7798
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08007799 vp_ap->nested_control.features.directhypercall = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007800 vp_ap->current_nested_vmcs = 0;
7801 vp_ap->enlighten_vmentry = 0;
7802 }
7803
7804 static_branch_disable(&enable_evmcs);
7805 }
7806#endif
7807 vmx_cleanup_l1d_flush();
7808}
7809module_exit(vmx_exit);
7810
7811static int __init vmx_init(void)
7812{
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02007813 int r, cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007814
7815#if IS_ENABLED(CONFIG_HYPERV)
7816 /*
7817 * Enlightened VMCS usage should be recommended and the host needs
7818 * to support eVMCS v1 or above. We can also disable eVMCS support
7819 * with module parameter.
7820 */
7821 if (enlightened_vmcs &&
7822 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
7823 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
7824 KVM_EVMCS_VERSION) {
7825 int cpu;
7826
7827 /* Check that we have assist pages on all online CPUs */
7828 for_each_online_cpu(cpu) {
7829 if (!hv_get_vp_assist_page(cpu)) {
7830 enlightened_vmcs = false;
7831 break;
7832 }
7833 }
7834
7835 if (enlightened_vmcs) {
7836 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
7837 static_branch_enable(&enable_evmcs);
7838 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08007839
7840 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
7841 vmx_x86_ops.enable_direct_tlbflush
7842 = hv_enable_direct_tlbflush;
7843
Avi Kivity6aa8b732006-12-10 02:21:36 -08007844 } else {
7845 enlightened_vmcs = false;
7846 }
7847#endif
7848
Sean Christophersond008dfd2020-03-21 13:25:56 -07007849 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
Avi Kivity6aa8b732006-12-10 02:21:36 -08007850 __alignof__(struct vcpu_vmx), THIS_MODULE);
7851 if (r)
7852 return r;
7853
7854 /*
7855 * Must be called after kvm_init() so enable_ept is properly set
7856 * up. Hand the parameter mitigation value in which was stored in
7857 * the pre module init parser. If no parameter was given, it will
7858 * contain 'auto' which will be turned into the default 'cond'
7859 * mitigation mode.
7860 */
Waiman Long19a36d32019-08-26 15:30:23 -04007861 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
7862 if (r) {
7863 vmx_exit();
7864 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007865 }
7866
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02007867 for_each_possible_cpu(cpu) {
7868 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Xiaoyao Li8888cdd2020-09-23 11:31:11 -07007869
7870 pi_init(cpu);
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02007871 }
7872
Avi Kivity6aa8b732006-12-10 02:21:36 -08007873#ifdef CONFIG_KEXEC_CORE
7874 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
7875 crash_vmclear_local_loaded_vmcss);
7876#endif
7877 vmx_check_vmcs12_offsets();
7878
Mohammed Gamal3edd6832020-07-10 17:48:11 +02007879 /*
7880 * Intel processors don't have problems with
7881 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR so enable
7882 * it for VMX by default
7883 */
7884 allow_smaller_maxphyaddr = true;
7885
Avi Kivity6aa8b732006-12-10 02:21:36 -08007886 return 0;
7887}
7888module_init(vmx_init);