blob: 9b58921f5cd36d049eb613c658724e23c6a04424 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Sean Christopherson199b1182018-12-03 13:52:53 -080019#include <linux/frame.h>
20#include <linux/highmem.h>
21#include <linux/hrtimer.h>
22#include <linux/kernel.h>
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020025#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070026#include <linux/mod_devicetable.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080027#include <linux/mm.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080028#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040030#include <linux/tboot.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080031#include <linux/trace_events.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032
Sean Christopherson199b1182018-12-03 13:52:53 -080033#include <asm/apic.h>
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020034#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080035#include <asm/cpu.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>
Sean Christopherson199b1182018-12-03 13:52:53 -080046#include <asm/spec-ctrl.h>
47#include <asm/virtext.h>
48#include <asm/vmx.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Sean Christopherson3077c192018-12-03 13:53:02 -080050#include "capabilities.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080051#include "cpuid.h"
Sean Christopherson4cebd742018-12-03 13:52:58 -080052#include "evmcs.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080053#include "irq.h"
54#include "kvm_cache_regs.h"
55#include "lapic.h"
56#include "mmu.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080057#include "nested.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080058#include "ops.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
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Sean Christopherson2c4fd912018-12-03 13:53:03 -080075bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Sean Christopherson2c4fd912018-12-03 13:53:03 -080081bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Sean Christopherson2c4fd912018-12-03 13:53:03 -080084bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Sean Christopherson2c4fd912018-12-03 13:53:03 -080087bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Sean Christopherson2c4fd912018-12-03 13:53:03 -080091bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080092module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Nadav Har'El801d3422011-05-25 23:02:23 +0300103/*
104 * If nested=1, nested virtualization is supported, i.e., guests may use
105 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
106 * use VMX instructions.
107 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200108static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300109module_param(nested, bool, S_IRUGO);
110
Wanpeng Li20300092014-12-02 19:14:59 +0800111static u64 __read_mostly host_xss;
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 Bonzini904e14f2018-01-16 16:51:18 +0100116#define MSR_BITMAP_MODE_X2APIC 1
117#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118
Haozhong Zhang64903d62015-10-20 15:39:09 +0800119#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
120
Yunhong Jiang64672c92016-06-13 14:19:59 -0700121/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
122static int __read_mostly cpu_preemption_timer_multi;
123static bool __read_mostly enable_preemption_timer = 1;
124#ifdef CONFIG_X86_64
125module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
126#endif
127
Sean Christopherson3de63472018-07-13 08:42:30 -0700128#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800129#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
130#define KVM_VM_CR0_ALWAYS_ON \
131 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
132 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200133#define KVM_CR4_GUEST_OWNED_BITS \
134 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800135 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
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
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800143/*
144 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
145 * ple_gap: upper bound on the amount of time between two successive
146 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500147 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800148 * ple_window: upper bound on the amount of time a guest is allowed to execute
149 * in a PAUSE loop. Tests indicate that most spinlocks are held for
150 * less than 2^12 cycles
151 * Time is measured based on a counter that runs at the same rate as the TSC,
152 * refer SDM volume 3b section 21.6.13 & 22.1.3.
153 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400154static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500155module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200156
Babu Moger7fbc85a2018-03-16 16:37:22 -0400157static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
158module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800159
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200160/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400161static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400162module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200163
164/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400165static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400166module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200167
168/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400169static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
170module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200171
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200172static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200173static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200174static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200175
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200176/* Storage for pre module init parameter parsing */
177static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200178
179static const struct {
180 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200181 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200182} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200183 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
184 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
185 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
186 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
187 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
188 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200189};
190
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200191#define L1D_CACHE_ORDER 4
192static void *vmx_l1d_flush_pages;
193
194static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
195{
196 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200197 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200198
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200199 if (!enable_ept) {
200 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
201 return 0;
202 }
203
Yi Wangd806afa2018-08-16 13:42:39 +0800204 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
205 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200206
Yi Wangd806afa2018-08-16 13:42:39 +0800207 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
208 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
209 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
210 return 0;
211 }
212 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200213
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200214 /* If set to auto use the default l1tf mitigation method */
215 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
216 switch (l1tf_mitigation) {
217 case L1TF_MITIGATION_OFF:
218 l1tf = VMENTER_L1D_FLUSH_NEVER;
219 break;
220 case L1TF_MITIGATION_FLUSH_NOWARN:
221 case L1TF_MITIGATION_FLUSH:
222 case L1TF_MITIGATION_FLUSH_NOSMT:
223 l1tf = VMENTER_L1D_FLUSH_COND;
224 break;
225 case L1TF_MITIGATION_FULL:
226 case L1TF_MITIGATION_FULL_FORCE:
227 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
228 break;
229 }
230 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
231 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
232 }
233
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200234 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
235 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
236 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
237 if (!page)
238 return -ENOMEM;
239 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200240
241 /*
242 * Initialize each page with a different pattern in
243 * order to protect against KSM in the nested
244 * virtualization case.
245 */
246 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
247 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
248 PAGE_SIZE);
249 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200250 }
251
252 l1tf_vmx_mitigation = l1tf;
253
Thomas Gleixner895ae472018-07-13 16:23:22 +0200254 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
255 static_branch_enable(&vmx_l1d_should_flush);
256 else
257 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200258
Nicolai Stange427362a2018-07-21 22:25:00 +0200259 if (l1tf == VMENTER_L1D_FLUSH_COND)
260 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200261 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200262 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200263 return 0;
264}
265
266static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200267{
268 unsigned int i;
269
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200270 if (s) {
271 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200272 if (vmentry_l1d_param[i].for_parse &&
273 sysfs_streq(s, vmentry_l1d_param[i].option))
274 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200275 }
276 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200277 return -EINVAL;
278}
279
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200280static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
281{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200282 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200283
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200284 l1tf = vmentry_l1d_flush_parse(s);
285 if (l1tf < 0)
286 return l1tf;
287
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200288 if (!boot_cpu_has(X86_BUG_L1TF))
289 return 0;
290
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200291 /*
292 * Has vmx_init() run already? If not then this is the pre init
293 * parameter parsing. In that case just store the value and let
294 * vmx_init() do the proper setup after enable_ept has been
295 * established.
296 */
297 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
298 vmentry_l1d_flush_param = l1tf;
299 return 0;
300 }
301
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200302 mutex_lock(&vmx_l1d_flush_mutex);
303 ret = vmx_setup_l1d_flush(l1tf);
304 mutex_unlock(&vmx_l1d_flush_mutex);
305 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200306}
307
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200308static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
309{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200310 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
311 return sprintf(s, "???\n");
312
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200313 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200314}
315
316static const struct kernel_param_ops vmentry_l1d_flush_ops = {
317 .set = vmentry_l1d_flush_set,
318 .get = vmentry_l1d_flush_get,
319};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200320module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200321
Gleb Natapovd99e4152012-12-20 16:57:45 +0200322static bool guest_state_valid(struct kvm_vcpu *vcpu);
323static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yi Wang1e4329ee2018-11-08 11:22:21 +0800324static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj15d45072018-02-01 22:59:43 +0100325 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300326
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800328DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300329/*
330 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
331 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
332 */
333static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800334
Feng Wubf9f6ac2015-09-18 22:29:55 +0800335/*
336 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
337 * can find which vCPU should be waken up.
338 */
339static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
340static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
341
Sheng Yang2384d2b2008-01-17 15:14:33 +0800342static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
343static DEFINE_SPINLOCK(vmx_vpid_lock);
344
Sean Christopherson3077c192018-12-03 13:53:02 -0800345struct vmcs_config vmcs_config;
346struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800347
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348#define VMX_SEGMENT_FIELD(seg) \
349 [VCPU_SREG_##seg] = { \
350 .selector = GUEST_##seg##_SELECTOR, \
351 .base = GUEST_##seg##_BASE, \
352 .limit = GUEST_##seg##_LIMIT, \
353 .ar_bytes = GUEST_##seg##_AR_BYTES, \
354 }
355
Mathias Krause772e0312012-08-30 01:30:19 +0200356static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357 unsigned selector;
358 unsigned base;
359 unsigned limit;
360 unsigned ar_bytes;
361} kvm_vmx_segment_fields[] = {
362 VMX_SEGMENT_FIELD(CS),
363 VMX_SEGMENT_FIELD(DS),
364 VMX_SEGMENT_FIELD(ES),
365 VMX_SEGMENT_FIELD(FS),
366 VMX_SEGMENT_FIELD(GS),
367 VMX_SEGMENT_FIELD(SS),
368 VMX_SEGMENT_FIELD(TR),
369 VMX_SEGMENT_FIELD(LDTR),
370};
371
Sean Christophersoncf3646e2018-12-03 13:53:15 -0800372u64 host_efer;
Avi Kivity26bb0982009-09-07 11:14:12 +0300373
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300374/*
Jim Mattson898a8112018-12-05 15:28:59 -0800375 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
376 * will emulate SYSCALL in legacy mode if the vendor string in guest
377 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
378 * support this emulation, IA32_STAR must always be included in
379 * vmx_msr_index[], even in i386 builds.
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300380 */
Sean Christophersoncf3646e2018-12-03 13:53:15 -0800381const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800382#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300383 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400385 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100388#if IS_ENABLED(CONFIG_HYPERV)
389static bool __read_mostly enlightened_vmcs = true;
390module_param(enlightened_vmcs, bool, 0444);
391
Tianyu Lan877ad952018-07-19 08:40:23 +0000392/* check_ept_pointer() should be under protection of ept_pointer_lock. */
393static void check_ept_pointer_match(struct kvm *kvm)
394{
395 struct kvm_vcpu *vcpu;
396 u64 tmp_eptp = INVALID_PAGE;
397 int i;
398
399 kvm_for_each_vcpu(i, vcpu, kvm) {
400 if (!VALID_PAGE(tmp_eptp)) {
401 tmp_eptp = to_vmx(vcpu)->ept_pointer;
402 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
403 to_kvm_vmx(kvm)->ept_pointers_match
404 = EPT_POINTERS_MISMATCH;
405 return;
406 }
407 }
408
409 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
410}
411
412static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
413{
Lan Tianyua5c214d2018-10-13 22:54:05 +0800414 struct kvm_vcpu *vcpu;
415 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +0000416
417 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
418
419 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
420 check_ept_pointer_match(kvm);
421
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +0200422 /*
423 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
424 * base of EPT PML4 table, strip off EPT configuration information.
Lan Tianyu53963a72018-12-06 15:34:36 +0800425 * If ept_pointer is invalid pointer, bypass the flush request.
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +0200426 */
Tianyu Lan877ad952018-07-19 08:40:23 +0000427 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyu53963a72018-12-06 15:34:36 +0800428 kvm_for_each_vcpu(i, vcpu, kvm) {
429 u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
430
431 if (!VALID_PAGE(ept_pointer))
432 continue;
433
Lan Tianyua5c214d2018-10-13 22:54:05 +0800434 ret |= hyperv_flush_guest_mapping(
Lan Tianyu53963a72018-12-06 15:34:36 +0800435 ept_pointer & PAGE_MASK);
436 }
Lan Tianyua5c214d2018-10-13 22:54:05 +0800437 } else {
438 ret = hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -0700439 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
Tianyu Lan877ad952018-07-19 08:40:23 +0000440 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000441
Tianyu Lan877ad952018-07-19 08:40:23 +0000442 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
443 return ret;
444}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100445#endif /* IS_ENABLED(CONFIG_HYPERV) */
446
Yunhong Jiang64672c92016-06-13 14:19:59 -0700447/*
448 * Comment's format: document - errata name - stepping - processor name.
449 * Refer from
450 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
451 */
452static u32 vmx_preemption_cpu_tfms[] = {
453/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
4540x000206E6,
455/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
456/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
457/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
4580x00020652,
459/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
4600x00020655,
461/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
462/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
463/*
464 * 320767.pdf - AAP86 - B1 -
465 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
466 */
4670x000106E5,
468/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
4690x000106A0,
470/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
4710x000106A1,
472/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
4730x000106A4,
474 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
475 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
476 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
4770x000106A5,
Wei Huang3d82c562018-12-03 14:13:32 -0600478 /* Xeon E3-1220 V2 */
4790x000306A8,
Yunhong Jiang64672c92016-06-13 14:19:59 -0700480};
481
482static inline bool cpu_has_broken_vmx_preemption_timer(void)
483{
484 u32 eax = cpuid_eax(0x00000001), i;
485
486 /* Clear the reserved bits */
487 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000488 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700489 if (eax == vmx_preemption_cpu_tfms[i])
490 return true;
491
492 return false;
493}
494
Paolo Bonzini35754c92015-07-29 12:05:37 +0200495static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800496{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200497 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800498}
499
Sheng Yang04547152009-04-01 15:52:31 +0800500static inline bool report_flexpriority(void)
501{
502 return flexpriority_enabled;
503}
504
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800505static inline int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800506{
507 int i;
508
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400509 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300510 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300511 return i;
512 return -1;
513}
514
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800515struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300516{
517 int i;
518
Rusty Russell8b9cf982007-07-30 16:31:43 +1000519 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300520 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400521 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000522 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800523}
524
Sean Christopherson7c97fcb2018-12-03 13:53:17 -0800525void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
526{
527 vmcs_clear(loaded_vmcs->vmcs);
528 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
529 vmcs_clear(loaded_vmcs->shadow_vmcs);
530 loaded_vmcs->cpu = -1;
531 loaded_vmcs->launched = 0;
532}
533
Dave Young2965faa2015-09-09 15:38:55 -0700534#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800535/*
536 * This bitmap is used to indicate whether the vmclear
537 * operation is enabled on all cpus. All disabled by
538 * default.
539 */
540static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
541
542static inline void crash_enable_local_vmclear(int cpu)
543{
544 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
545}
546
547static inline void crash_disable_local_vmclear(int cpu)
548{
549 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
550}
551
552static inline int crash_local_vmclear_enabled(int cpu)
553{
554 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
555}
556
557static void crash_vmclear_local_loaded_vmcss(void)
558{
559 int cpu = raw_smp_processor_id();
560 struct loaded_vmcs *v;
561
562 if (!crash_local_vmclear_enabled(cpu))
563 return;
564
565 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
566 loaded_vmcss_on_cpu_link)
567 vmcs_clear(v->vmcs);
568}
569#else
570static inline void crash_enable_local_vmclear(int cpu) { }
571static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -0700572#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800573
Nadav Har'Eld462b812011-05-24 15:26:10 +0300574static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300576 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800577 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578
Nadav Har'Eld462b812011-05-24 15:26:10 +0300579 if (loaded_vmcs->cpu != cpu)
580 return; /* vcpu migration can race with cpu offline */
581 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800583 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300584 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800585
586 /*
587 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
588 * is before setting loaded_vmcs->vcpu to -1 which is done in
589 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
590 * then adds the vmcs into percpu list before it is deleted.
591 */
592 smp_wmb();
593
Nadav Har'Eld462b812011-05-24 15:26:10 +0300594 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800595 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596}
597
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800598void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800599{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800600 int cpu = loaded_vmcs->cpu;
601
602 if (cpu != -1)
603 smp_call_function_single(cpu,
604 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800605}
606
Avi Kivity2fb92db2011-04-27 19:42:18 +0300607static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
608 unsigned field)
609{
610 bool ret;
611 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
612
613 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
614 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
615 vmx->segment_cache.bitmask = 0;
616 }
617 ret = vmx->segment_cache.bitmask & mask;
618 vmx->segment_cache.bitmask |= mask;
619 return ret;
620}
621
622static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
623{
624 u16 *p = &vmx->segment_cache.seg[seg].selector;
625
626 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
627 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
628 return *p;
629}
630
631static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
632{
633 ulong *p = &vmx->segment_cache.seg[seg].base;
634
635 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
636 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
637 return *p;
638}
639
640static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
641{
642 u32 *p = &vmx->segment_cache.seg[seg].limit;
643
644 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
645 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
646 return *p;
647}
648
649static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
650{
651 u32 *p = &vmx->segment_cache.seg[seg].ar;
652
653 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
654 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
655 return *p;
656}
657
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800658void update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300659{
660 u32 eb;
661
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100662 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800663 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200664 /*
665 * Guest access to VMware backdoor ports could legitimately
666 * trigger #GP because of TSS I/O permission bitmap.
667 * We intercept those #GP and allow access to them anyway
668 * as VMware does.
669 */
670 if (enable_vmware_backdoor)
671 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100672 if ((vcpu->guest_debug &
673 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
674 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
675 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300676 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300677 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200678 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800679 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300680
681 /* When we are running a nested L2 guest and L1 specified for it a
682 * certain exception bitmap, we must trap the same exceptions and pass
683 * them to L1. When running L2, we will only handle the exceptions
684 * specified above if L1 did not want them.
685 */
686 if (is_guest_mode(vcpu))
687 eb |= get_vmcs12(vcpu)->exception_bitmap;
688
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300689 vmcs_write32(EXCEPTION_BITMAP, eb);
690}
691
Ashok Raj15d45072018-02-01 22:59:43 +0100692/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100693 * Check if MSR is intercepted for currently loaded MSR bitmap.
694 */
695static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
696{
697 unsigned long *msr_bitmap;
698 int f = sizeof(unsigned long);
699
700 if (!cpu_has_vmx_msr_bitmap())
701 return true;
702
703 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
704
705 if (msr <= 0x1fff) {
706 return !!test_bit(msr, msr_bitmap + 0x800 / f);
707 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
708 msr &= 0x1fff;
709 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
710 }
711
712 return true;
713}
714
Gleb Natapov2961e8762013-11-25 15:37:13 +0200715static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
716 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200717{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200718 vm_entry_controls_clearbit(vmx, entry);
719 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200720}
721
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400722static int find_msr(struct vmx_msrs *m, unsigned int msr)
723{
724 unsigned int i;
725
726 for (i = 0; i < m->nr; ++i) {
727 if (m->val[i].index == msr)
728 return i;
729 }
730 return -ENOENT;
731}
732
Avi Kivity61d2ef22010-04-28 16:40:38 +0300733static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
734{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400735 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300736 struct msr_autoload *m = &vmx->msr_autoload;
737
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200738 switch (msr) {
739 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800740 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200741 clear_atomic_switch_msr_special(vmx,
742 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200743 VM_EXIT_LOAD_IA32_EFER);
744 return;
745 }
746 break;
747 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800748 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200749 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200750 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
751 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
752 return;
753 }
754 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200755 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400756 i = find_msr(&m->guest, msr);
757 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400758 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400759 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400760 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400761 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200762
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400763skip_guest:
764 i = find_msr(&m->host, msr);
765 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300766 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400767
768 --m->host.nr;
769 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400770 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300771}
772
Gleb Natapov2961e8762013-11-25 15:37:13 +0200773static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
774 unsigned long entry, unsigned long exit,
775 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
776 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200777{
778 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700779 if (host_val_vmcs != HOST_IA32_EFER)
780 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200781 vm_entry_controls_setbit(vmx, entry);
782 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200783}
784
Avi Kivity61d2ef22010-04-28 16:40:38 +0300785static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400786 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300787{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400788 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300789 struct msr_autoload *m = &vmx->msr_autoload;
790
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200791 switch (msr) {
792 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800793 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200794 add_atomic_switch_msr_special(vmx,
795 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200796 VM_EXIT_LOAD_IA32_EFER,
797 GUEST_IA32_EFER,
798 HOST_IA32_EFER,
799 guest_val, host_val);
800 return;
801 }
802 break;
803 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800804 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200805 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200806 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
807 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
808 GUEST_IA32_PERF_GLOBAL_CTRL,
809 HOST_IA32_PERF_GLOBAL_CTRL,
810 guest_val, host_val);
811 return;
812 }
813 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +0100814 case MSR_IA32_PEBS_ENABLE:
815 /* PEBS needs a quiescent period after being disabled (to write
816 * a record). Disabling PEBS through VMX MSR swapping doesn't
817 * provide that period, so a CPU could write host's record into
818 * guest's memory.
819 */
820 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +0200821 }
822
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400823 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400824 if (!entry_only)
825 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300826
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400827 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +0200828 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +0200829 "Can't add msr %x\n", msr);
830 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300831 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400832 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400833 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400834 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400835 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400836 m->guest.val[i].index = msr;
837 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300838
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400839 if (entry_only)
840 return;
841
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400842 if (j < 0) {
843 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400844 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300845 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400846 m->host.val[j].index = msr;
847 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300848}
849
Avi Kivity92c0d902009-10-29 11:00:16 +0200850static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300851{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100852 u64 guest_efer = vmx->vcpu.arch.efer;
853 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +0300854
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100855 if (!enable_ept) {
856 /*
857 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
858 * host CPUID is more efficient than testing guest CPUID
859 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
860 */
861 if (boot_cpu_has(X86_FEATURE_SMEP))
862 guest_efer |= EFER_NX;
863 else if (!(guest_efer & EFER_NX))
864 ignore_bits |= EFER_NX;
865 }
Roel Kluin3a34a882009-08-04 02:08:45 -0700866
Avi Kivity51c6cf62007-08-29 03:48:05 +0300867 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100868 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +0300869 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100870 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300871#ifdef CONFIG_X86_64
872 ignore_bits |= EFER_LMA | EFER_LME;
873 /* SCE is meaningful only in long mode on Intel */
874 if (guest_efer & EFER_LMA)
875 ignore_bits &= ~(u64)EFER_SCE;
876#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +0300877
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800878 /*
879 * On EPT, we can't emulate NX, so we must switch EFER atomically.
880 * On CPUs that support "load IA32_EFER", always switch EFER
881 * atomically, since it's faster than switching it manually.
882 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800883 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800884 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +0300885 if (!(guest_efer & EFER_LMA))
886 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -0800887 if (guest_efer != host_efer)
888 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400889 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -0700890 else
891 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +0300892 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100893 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -0700894 clear_atomic_switch_msr(vmx, MSR_EFER);
895
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100896 guest_efer &= ~ignore_bits;
897 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300898
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100899 vmx->guest_msrs[efer_offset].data = guest_efer;
900 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
901
902 return true;
903 }
Avi Kivity51c6cf62007-08-29 03:48:05 +0300904}
905
Andy Lutomirskie28baea2017-02-20 08:56:11 -0800906#ifdef CONFIG_X86_32
907/*
908 * On 32-bit kernels, VM exits still load the FS and GS bases from the
909 * VMCS rather than the segment table. KVM uses this helper to figure
910 * out the current bases to poke them into the VMCS before entry.
911 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200912static unsigned long segment_base(u16 selector)
913{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800914 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200915 unsigned long v;
916
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800917 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200918 return 0;
919
Thomas Garnier45fc8752017-03-14 10:05:08 -0700920 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200921
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800922 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200923 u16 ldt_selector = kvm_read_ldt();
924
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800925 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200926 return 0;
927
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800928 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200929 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800930 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200931 return v;
932}
Andy Lutomirskie28baea2017-02-20 08:56:11 -0800933#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200934
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800935void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300936{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300937 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -0700938 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +0200939#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +0100940 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +0200941#endif
Sean Christophersone368b872018-07-23 12:32:41 -0700942 unsigned long fs_base, gs_base;
943 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +0300944 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300945
Sean Christophersond264ee02018-08-27 15:21:12 -0700946 vmx->req_immediate_exit = false;
947
Liran Alonf48b4712018-11-20 18:03:25 +0200948 /*
949 * Note that guest MSRs to be saved/restored can also be changed
950 * when guest state is loaded. This happens when guest transitions
951 * to/from long-mode by setting MSR_EFER.LMA.
952 */
953 if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
954 vmx->guest_msrs_dirty = false;
955 for (i = 0; i < vmx->save_nmsrs; ++i)
956 kvm_set_shared_msr(vmx->guest_msrs[i].index,
957 vmx->guest_msrs[i].data,
958 vmx->guest_msrs[i].mask);
959
960 }
961
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700962 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +0300963 return;
964
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700965 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700966 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700967
Avi Kivity33ed6322007-05-02 16:54:03 +0300968 /*
969 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
970 * allow segment selectors with cpl > 0 or ti == 1.
971 */
Sean Christophersond7ee0392018-07-23 12:32:47 -0700972 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +0100973
974#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -0700975 savesegment(ds, host_state->ds_sel);
976 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -0700977
978 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200979 if (likely(is_64bit_mm(current->mm))) {
980 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -0700981 fs_sel = current->thread.fsindex;
982 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200983 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -0700984 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200985 } else {
Sean Christophersone368b872018-07-23 12:32:41 -0700986 savesegment(fs, fs_sel);
987 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200988 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -0700989 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +0300990 }
991
Paolo Bonzini4679b612018-09-24 17:23:01 +0200992 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +0300993#else
Sean Christophersone368b872018-07-23 12:32:41 -0700994 savesegment(fs, fs_sel);
995 savesegment(gs, gs_sel);
996 fs_base = segment_base(fs_sel);
997 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300998#endif
Sean Christophersone368b872018-07-23 12:32:41 -0700999
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07001000 if (unlikely(fs_sel != host_state->fs_sel)) {
1001 if (!(fs_sel & 7))
1002 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1003 else
1004 vmcs_write16(HOST_FS_SELECTOR, 0);
1005 host_state->fs_sel = fs_sel;
1006 }
1007 if (unlikely(gs_sel != host_state->gs_sel)) {
1008 if (!(gs_sel & 7))
1009 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1010 else
1011 vmcs_write16(HOST_GS_SELECTOR, 0);
1012 host_state->gs_sel = gs_sel;
1013 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07001014 if (unlikely(fs_base != host_state->fs_base)) {
1015 vmcs_writel(HOST_FS_BASE, fs_base);
1016 host_state->fs_base = fs_base;
1017 }
1018 if (unlikely(gs_base != host_state->gs_base)) {
1019 vmcs_writel(HOST_GS_BASE, gs_base);
1020 host_state->gs_base = gs_base;
1021 }
Avi Kivity33ed6322007-05-02 16:54:03 +03001022}
1023
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001024static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001025{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001026 struct vmcs_host_state *host_state;
1027
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001028 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03001029 return;
1030
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001031 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001032 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001033
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001034 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001035 vmx->loaded_cpu_state = NULL;
1036
Avi Kivityc8770e72010-11-11 12:37:26 +02001037#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001038 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001039#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001040 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1041 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001042#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001043 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001044#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001045 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001046#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001047 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001048 if (host_state->fs_sel & 7)
1049 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001050#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001051 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1052 loadsegment(ds, host_state->ds_sel);
1053 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001054 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001055#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001056 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001057#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001058 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001059#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001060 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03001061}
1062
Sean Christopherson678e3152018-07-23 12:32:43 -07001063#ifdef CONFIG_X86_64
1064static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001065{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001066 preempt_disable();
1067 if (vmx->loaded_cpu_state)
1068 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1069 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001070 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001071}
1072
Sean Christopherson678e3152018-07-23 12:32:43 -07001073static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1074{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001075 preempt_disable();
1076 if (vmx->loaded_cpu_state)
1077 wrmsrl(MSR_KERNEL_GS_BASE, data);
1078 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001079 vmx->msr_guest_kernel_gs_base = data;
1080}
1081#endif
1082
Feng Wu28b835d2015-09-18 22:29:54 +08001083static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
1084{
1085 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1086 struct pi_desc old, new;
1087 unsigned int dest;
1088
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001089 /*
1090 * In case of hot-plug or hot-unplug, we may have to undo
1091 * vmx_vcpu_pi_put even if there is no assigned device. And we
1092 * always keep PI.NDST up to date for simplicity: it makes the
1093 * code easier, and CPU migration is not a fast path.
1094 */
1095 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08001096 return;
1097
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001098 /*
1099 * First handle the simple case where no cmpxchg is necessary; just
1100 * allow posting non-urgent interrupts.
1101 *
1102 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
1103 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
1104 * expects the VCPU to be on the blocked_vcpu_list that matches
1105 * PI.NDST.
1106 */
1107 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
1108 vcpu->cpu == cpu) {
1109 pi_clear_sn(pi_desc);
1110 return;
1111 }
1112
1113 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08001114 do {
1115 old.control = new.control = pi_desc->control;
1116
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001117 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08001118
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001119 if (x2apic_enabled())
1120 new.ndst = dest;
1121 else
1122 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08001123
Feng Wu28b835d2015-09-18 22:29:54 +08001124 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02001125 } while (cmpxchg64(&pi_desc->control, old.control,
1126 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08001127}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001128
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129/*
1130 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1131 * vcpu mutex is already taken.
1132 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001133void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001135 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001136 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001138 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001139 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001140 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001141 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001142
1143 /*
1144 * Read loaded_vmcs->cpu should be before fetching
1145 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1146 * See the comments in __loaded_vmcs_clear().
1147 */
1148 smp_rmb();
1149
Nadav Har'Eld462b812011-05-24 15:26:10 +03001150 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1151 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001152 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001153 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001154 }
1155
1156 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1157 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1158 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01001159 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001160 }
1161
1162 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001163 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001164 unsigned long sysenter_esp;
1165
1166 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001167
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 /*
1169 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001170 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001172 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001173 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001174 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001176 /*
1177 * VM exits change the host TR limit to 0x67 after a VM
1178 * exit. This is okay, since 0x67 covers everything except
1179 * the IO bitmap and have have code to handle the IO bitmap
1180 * being lost after a VM exit.
1181 */
1182 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
1183
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1185 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001186
Nadav Har'Eld462b812011-05-24 15:26:10 +03001187 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 }
Feng Wu28b835d2015-09-18 22:29:54 +08001189
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001190 /* Setup TSC multiplier */
1191 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001192 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1193 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001194
Feng Wu28b835d2015-09-18 22:29:54 +08001195 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001196 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08001197 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001198}
1199
1200static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
1201{
1202 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1203
1204 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08001205 !irq_remapping_cap(IRQ_POSTING_CAP) ||
1206 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08001207 return;
1208
1209 /* Set SN when the vCPU is preempted */
1210 if (vcpu->preempted)
1211 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212}
1213
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001214void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215{
Feng Wu28b835d2015-09-18 22:29:54 +08001216 vmx_vcpu_pi_put(vcpu);
1217
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001218 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219}
1220
Wanpeng Lif244dee2017-07-20 01:11:54 -07001221static bool emulation_required(struct kvm_vcpu *vcpu)
1222{
1223 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
1224}
1225
Avi Kivityedcafe32009-12-30 18:07:40 +02001226static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1227
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001228unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001230 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001231
Avi Kivity6de12732011-03-07 12:51:22 +02001232 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1233 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1234 rflags = vmcs_readl(GUEST_RFLAGS);
1235 if (to_vmx(vcpu)->rmode.vm86_active) {
1236 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1237 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1238 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1239 }
1240 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001241 }
Avi Kivity6de12732011-03-07 12:51:22 +02001242 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243}
1244
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001245void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246{
Wanpeng Lif244dee2017-07-20 01:11:54 -07001247 unsigned long old_rflags = vmx_get_rflags(vcpu);
1248
Avi Kivity6de12732011-03-07 12:51:22 +02001249 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1250 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001251 if (to_vmx(vcpu)->rmode.vm86_active) {
1252 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001253 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001254 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001256
1257 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
1258 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259}
1260
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001261u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001262{
1263 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1264 int ret = 0;
1265
1266 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001267 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001268 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001269 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001270
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001271 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001272}
1273
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001274void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001275{
1276 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1277 u32 interruptibility = interruptibility_old;
1278
1279 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1280
Jan Kiszka48005f62010-02-19 19:38:07 +01001281 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001282 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001283 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001284 interruptibility |= GUEST_INTR_STATE_STI;
1285
1286 if ((interruptibility != interruptibility_old))
1287 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1288}
1289
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1291{
1292 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001294 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001296 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297
Glauber Costa2809f5d2009-05-12 16:21:05 -04001298 /* skipping an emulated instruction also counts */
1299 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300}
1301
Wanpeng Licaa057a2018-03-12 04:53:03 -07001302static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1303{
1304 /*
1305 * Ensure that we clear the HLT state in the VMCS. We don't need to
1306 * explicitly skip the instruction because if the HLT state is set,
1307 * then the instruction is already executing and RIP has already been
1308 * advanced.
1309 */
1310 if (kvm_hlt_in_guest(vcpu->kvm) &&
1311 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1312 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1313}
1314
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001315static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001316{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001317 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001318 unsigned nr = vcpu->arch.exception.nr;
1319 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001320 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001321 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001322
Jim Mattsonda998b42018-10-16 14:29:22 -07001323 kvm_deliver_exception_payload(vcpu);
1324
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001325 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001326 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001327 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1328 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001329
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001330 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001331 int inc_eip = 0;
1332 if (kvm_exception_is_soft(nr))
1333 inc_eip = vcpu->arch.event_exit_inst_len;
1334 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001335 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001336 return;
1337 }
1338
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001339 WARN_ON_ONCE(vmx->emulation_required);
1340
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001341 if (kvm_exception_is_soft(nr)) {
1342 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1343 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001344 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1345 } else
1346 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1347
1348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001349
1350 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001351}
1352
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001353static bool vmx_rdtscp_supported(void)
1354{
1355 return cpu_has_vmx_rdtscp();
1356}
1357
Mao, Junjiead756a12012-07-02 01:18:48 +00001358static bool vmx_invpcid_supported(void)
1359{
Junaid Shahideb4b2482018-06-27 14:59:14 -07001360 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00001361}
1362
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363/*
Eddie Donga75beee2007-05-17 18:55:15 +03001364 * Swap MSR entry in host/guest MSR entry array.
1365 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001366static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001367{
Avi Kivity26bb0982009-09-07 11:14:12 +03001368 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001369
1370 tmp = vmx->guest_msrs[to];
1371 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1372 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001373}
1374
1375/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001376 * Set up the vmcs to automatically save and restore system
1377 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1378 * mode, as fiddling with msrs is very expensive.
1379 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001380static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001381{
Avi Kivity26bb0982009-09-07 11:14:12 +03001382 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001383
Eddie Donga75beee2007-05-17 18:55:15 +03001384 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001385#ifdef CONFIG_X86_64
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001386 /*
1387 * The SYSCALL MSRs are only needed on long mode guests, and only
1388 * when EFER.SCE is set.
1389 */
1390 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
1391 index = __find_msr_index(vmx, MSR_STAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001392 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001393 move_msr_up(vmx, index, save_nmsrs++);
1394 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001395 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001396 move_msr_up(vmx, index, save_nmsrs++);
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001397 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
1398 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001399 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001400 }
Eddie Donga75beee2007-05-17 18:55:15 +03001401#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001402 index = __find_msr_index(vmx, MSR_EFER);
1403 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001404 move_msr_up(vmx, index, save_nmsrs++);
Jim Mattson0023ef32018-12-05 15:28:58 -08001405 index = __find_msr_index(vmx, MSR_TSC_AUX);
1406 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
1407 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001408
Avi Kivity26bb0982009-09-07 11:14:12 +03001409 vmx->save_nmsrs = save_nmsrs;
Liran Alonf48b4712018-11-20 18:03:25 +02001410 vmx->guest_msrs_dirty = true;
Avi Kivity58972972009-02-24 22:26:47 +02001411
Yang Zhang8d146952013-01-25 10:18:50 +08001412 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001413 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001414}
1415
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001416static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001418 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001420 if (is_guest_mode(vcpu) &&
1421 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1422 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
1423
1424 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425}
1426
Leonid Shatz326e7422018-11-06 12:14:25 +02001427static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001429 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1430 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001431
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001432 /*
1433 * We're here if L1 chose not to trap WRMSR to TSC. According
1434 * to the spec, this should set L1's TSC; The offset that L1
1435 * set for L2 remains unchanged, and still needs to be added
1436 * to the newly set TSC to get L2's TSC.
1437 */
1438 if (is_guest_mode(vcpu) &&
1439 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1440 g_tsc_offset = vmcs12->tsc_offset;
1441
1442 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1443 vcpu->arch.tsc_offset - g_tsc_offset,
1444 offset);
1445 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1446 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447}
1448
Nadav Har'El801d3422011-05-25 23:02:23 +03001449/*
1450 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1451 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1452 * all guests if the "nested" module option is off, and can also be disabled
1453 * for a single guest by disabling its VMX cpuid bit.
1454 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001455bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001456{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001457 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001458}
1459
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001460static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1461 uint64_t val)
1462{
1463 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1464
1465 return !(val & ~valid_bits);
1466}
1467
Tom Lendacky801e4592018-02-21 13:39:51 -06001468static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1469{
Paolo Bonzini13893092018-02-26 13:40:09 +01001470 switch (msr->index) {
1471 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1472 if (!nested)
1473 return 1;
1474 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
1475 default:
1476 return 1;
1477 }
1478
1479 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06001480}
1481
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001482/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 * Reads an msr value (of 'msr_index') into 'pdata'.
1484 * Returns 0 on success, non-0 otherwise.
1485 * Assumes vcpu_load() was already called.
1486 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001487static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001489 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001490 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001492 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001493#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001495 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 break;
1497 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001498 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001500 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001501 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001502 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001503#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001505 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001506 case MSR_IA32_SPEC_CTRL:
1507 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001508 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1509 return 1;
1510
1511 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1512 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01001513 case MSR_IA32_ARCH_CAPABILITIES:
1514 if (!msr_info->host_initiated &&
1515 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
1516 return 1;
1517 msr_info->data = to_vmx(vcpu)->arch_capabilities;
1518 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001520 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521 break;
1522 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001523 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 break;
1525 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001526 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001528 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001529 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001530 (!msr_info->host_initiated &&
1531 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001532 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001533 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001534 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001535 case MSR_IA32_MCG_EXT_CTL:
1536 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001537 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08001538 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01001539 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001540 msr_info->data = vcpu->arch.mcg_ext_ctl;
1541 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01001542 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001543 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001544 break;
1545 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1546 if (!nested_vmx_allowed(vcpu))
1547 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001548 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1549 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08001550 case MSR_IA32_XSS:
1551 if (!vmx_xsaves_supported())
1552 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001553 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08001554 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001555 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001556 if (!msr_info->host_initiated &&
1557 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001558 return 1;
1559 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001561 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001562 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001563 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001564 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001566 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 }
1568
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569 return 0;
1570}
1571
1572/*
1573 * Writes msr value into into the appropriate "register".
1574 * Returns 0 on success, non-0 otherwise.
1575 * Assumes vcpu_load() was already called.
1576 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001577static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001579 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001580 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001581 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08001582 u32 msr_index = msr_info->index;
1583 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03001584
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001586 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08001587 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03001588 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001589#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001591 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592 vmcs_writel(GUEST_FS_BASE, data);
1593 break;
1594 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001595 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 vmcs_writel(GUEST_GS_BASE, data);
1597 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001598 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001599 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001600 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601#endif
1602 case MSR_IA32_SYSENTER_CS:
1603 vmcs_write32(GUEST_SYSENTER_CS, data);
1604 break;
1605 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001606 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607 break;
1608 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001609 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001611 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001612 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001613 (!msr_info->host_initiated &&
1614 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001615 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001616 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07001617 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08001619 vmcs_write64(GUEST_BNDCFGS, data);
1620 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001621 case MSR_IA32_SPEC_CTRL:
1622 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001623 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1624 return 1;
1625
1626 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02001627 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001628 return 1;
1629
1630 vmx->spec_ctrl = data;
1631
1632 if (!data)
1633 break;
1634
1635 /*
1636 * For non-nested:
1637 * When it's written (to non-zero) for the first time, pass
1638 * it through.
1639 *
1640 * For nested:
1641 * The handling of the MSR bitmap for L2 guests is done in
1642 * nested_vmx_merge_msr_bitmap. We should not touch the
1643 * vmcs02.msr_bitmap here since it gets completely overwritten
1644 * in the merging. We update the vmcs01 here for L1 as well
1645 * since it will end up touching the MSR anyway now.
1646 */
1647 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
1648 MSR_IA32_SPEC_CTRL,
1649 MSR_TYPE_RW);
1650 break;
Ashok Raj15d45072018-02-01 22:59:43 +01001651 case MSR_IA32_PRED_CMD:
1652 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01001653 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1654 return 1;
1655
1656 if (data & ~PRED_CMD_IBPB)
1657 return 1;
1658
1659 if (!data)
1660 break;
1661
1662 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
1663
1664 /*
1665 * For non-nested:
1666 * When it's written (to non-zero) for the first time, pass
1667 * it through.
1668 *
1669 * For nested:
1670 * The handling of the MSR bitmap for L2 guests is done in
1671 * nested_vmx_merge_msr_bitmap. We should not touch the
1672 * vmcs02.msr_bitmap here since it gets completely overwritten
1673 * in the merging.
1674 */
1675 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
1676 MSR_TYPE_W);
1677 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01001678 case MSR_IA32_ARCH_CAPABILITIES:
1679 if (!msr_info->host_initiated)
1680 return 1;
1681 vmx->arch_capabilities = data;
1682 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001683 case MSR_IA32_CR_PAT:
1684 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03001685 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
1686 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08001687 vmcs_write64(GUEST_IA32_PAT, data);
1688 vcpu->arch.pat = data;
1689 break;
1690 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001691 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001692 break;
Will Auldba904632012-11-29 12:42:50 -08001693 case MSR_IA32_TSC_ADJUST:
1694 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001695 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001696 case MSR_IA32_MCG_EXT_CTL:
1697 if ((!msr_info->host_initiated &&
1698 !(to_vmx(vcpu)->msr_ia32_feature_control &
1699 FEATURE_CONTROL_LMCE)) ||
1700 (data & ~MCG_EXT_CTL_LMCE_EN))
1701 return 1;
1702 vcpu->arch.mcg_ext_ctl = data;
1703 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01001704 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001705 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08001706 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01001707 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
1708 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001709 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01001710 if (msr_info->host_initiated && data == 0)
1711 vmx_leave_nested(vcpu);
1712 break;
1713 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08001714 if (!msr_info->host_initiated)
1715 return 1; /* they are read-only */
1716 if (!nested_vmx_allowed(vcpu))
1717 return 1;
1718 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08001719 case MSR_IA32_XSS:
1720 if (!vmx_xsaves_supported())
1721 return 1;
1722 /*
1723 * The only supported bit as of Skylake is bit 8, but
1724 * it is not supported on KVM.
1725 */
1726 if (data != 0)
1727 return 1;
1728 vcpu->arch.ia32_xss = data;
1729 if (vcpu->arch.ia32_xss != host_xss)
1730 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001731 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08001732 else
1733 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
1734 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001735 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001736 if (!msr_info->host_initiated &&
1737 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001738 return 1;
1739 /* Check reserved bit, higher 32 bits should be zero */
1740 if ((data >> 32) != 0)
1741 return 1;
1742 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001744 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001745 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07001746 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001747 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03001748 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
1749 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07001750 ret = kvm_set_shared_msr(msr->index, msr->data,
1751 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03001752 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07001753 if (ret)
1754 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03001755 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001756 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001758 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759 }
1760
Eddie Dong2cc51562007-05-21 07:28:09 +03001761 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762}
1763
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001764static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001766 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1767 switch (reg) {
1768 case VCPU_REGS_RSP:
1769 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1770 break;
1771 case VCPU_REGS_RIP:
1772 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1773 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001774 case VCPU_EXREG_PDPTR:
1775 if (enable_ept)
1776 ept_save_pdptrs(vcpu);
1777 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001778 default:
1779 break;
1780 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781}
1782
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783static __init int cpu_has_kvm_support(void)
1784{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001785 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786}
1787
1788static __init int vmx_disabled_by_bios(void)
1789{
1790 u64 msr;
1791
1792 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04001793 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08001794 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04001795 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1796 && tboot_enabled())
1797 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08001798 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04001799 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08001800 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08001801 && !tboot_enabled()) {
1802 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08001803 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04001804 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08001805 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08001806 /* launched w/o TXT and VMX disabled */
1807 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1808 && !tboot_enabled())
1809 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04001810 }
1811
1812 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813}
1814
Dongxiao Xu7725b892010-05-11 18:29:38 +08001815static void kvm_cpu_vmxon(u64 addr)
1816{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001817 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001818 intel_pt_handle_vmx(1);
1819
Uros Bizjak4b1e5472018-10-11 19:40:44 +02001820 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08001821}
1822
Radim Krčmář13a34e02014-08-28 15:13:03 +02001823static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824{
1825 int cpu = raw_smp_processor_id();
1826 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04001827 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001829 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02001830 return -EBUSY;
1831
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001832 /*
1833 * This can happen if we hot-added a CPU but failed to allocate
1834 * VP assist page for it.
1835 */
1836 if (static_branch_unlikely(&enable_evmcs) &&
1837 !hv_get_vp_assist_page(cpu))
1838 return -EFAULT;
1839
Nadav Har'Eld462b812011-05-24 15:26:10 +03001840 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08001841 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
1842 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001843
1844 /*
1845 * Now we can enable the vmclear operation in kdump
1846 * since the loaded_vmcss_on_cpu list on this cpu
1847 * has been initialized.
1848 *
1849 * Though the cpu is not in VMX operation now, there
1850 * is no problem to enable the vmclear operation
1851 * for the loaded_vmcss_on_cpu list is empty!
1852 */
1853 crash_enable_local_vmclear(cpu);
1854
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04001856
1857 test_bits = FEATURE_CONTROL_LOCKED;
1858 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1859 if (tboot_enabled())
1860 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1861
1862 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04001864 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1865 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001866 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02001867 if (enable_ept)
1868 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02001869
1870 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001871}
1872
Nadav Har'Eld462b812011-05-24 15:26:10 +03001873static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03001874{
1875 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001876 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03001877
Nadav Har'Eld462b812011-05-24 15:26:10 +03001878 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
1879 loaded_vmcss_on_cpu_link)
1880 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03001881}
1882
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001883
1884/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1885 * tricks.
1886 */
1887static void kvm_cpu_vmxoff(void)
1888{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02001889 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001890
1891 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001892 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001893}
1894
Radim Krčmář13a34e02014-08-28 15:13:03 +02001895static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001897 vmclear_local_loaded_vmcss();
1898 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899}
1900
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001901static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001902 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903{
1904 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001905 u32 ctl = ctl_min | ctl_opt;
1906
1907 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1908
1909 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1910 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1911
1912 /* Ensure minimum (required) set of control bits are supported. */
1913 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001914 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001915
1916 *result = ctl;
1917 return 0;
1918}
1919
Sean Christopherson7caaa712018-12-03 13:53:01 -08001920static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
1921 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001922{
1923 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001924 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001925 u32 _pin_based_exec_control = 0;
1926 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001927 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001928 u32 _vmexit_control = 0;
1929 u32 _vmentry_control = 0;
1930
Paolo Bonzini13893092018-02-26 13:40:09 +01001931 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05301932 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001933#ifdef CONFIG_X86_64
1934 CPU_BASED_CR8_LOAD_EXITING |
1935 CPU_BASED_CR8_STORE_EXITING |
1936#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001937 CPU_BASED_CR3_LOAD_EXITING |
1938 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08001939 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001940 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001941 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07001942 CPU_BASED_MWAIT_EXITING |
1943 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02001944 CPU_BASED_INVLPG_EXITING |
1945 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06001946
Sheng Yangf78e0e22007-10-29 09:40:42 +08001947 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001948 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001949 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001950 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1951 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001952 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001953#ifdef CONFIG_X86_64
1954 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1955 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1956 ~CPU_BASED_CR8_STORE_EXITING;
1957#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001958 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001959 min2 = 0;
1960 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08001961 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001962 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001963 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001964 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001965 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001966 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02001967 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00001968 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08001969 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08001970 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03001971 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08001972 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08001973 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02001974 SECONDARY_EXEC_RDSEED_EXITING |
1975 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08001976 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04001977 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07001978 SECONDARY_EXEC_ENABLE_VMFUNC |
1979 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08001980 if (adjust_vmx_controls(min2, opt2,
1981 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001982 &_cpu_based_2nd_exec_control) < 0)
1983 return -EIO;
1984 }
1985#ifndef CONFIG_X86_64
1986 if (!(_cpu_based_2nd_exec_control &
1987 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1988 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1989#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08001990
1991 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1992 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08001993 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08001994 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
1995 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08001996
Wanpeng Li61f1dd92017-10-18 16:02:19 -07001997 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08001998 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07001999
Sheng Yangd56f5462008-04-25 10:13:16 +08002000 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002001 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2002 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002003 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2004 CPU_BASED_CR3_STORE_EXITING |
2005 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08002006 } else if (vmx_cap->ept) {
2007 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002008 pr_warn_once("EPT CAP should not exist if not support "
2009 "1-setting enable EPT VM-execution control\n");
2010 }
2011 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002012 vmx_cap->vpid) {
2013 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002014 pr_warn_once("VPID CAP should not exist if not support "
2015 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002016 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002017
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002018 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002019#ifdef CONFIG_X86_64
2020 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2021#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002022 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
2023 VM_EXIT_SAVE_IA32_PAT |
2024 VM_EXIT_LOAD_IA32_PAT |
2025 VM_EXIT_LOAD_IA32_EFER |
2026 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002027 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2028 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002029 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002030
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002031 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2032 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2033 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002034 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2035 &_pin_based_exec_control) < 0)
2036 return -EIO;
2037
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002038 if (cpu_has_broken_vmx_preemption_timer())
2039 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002040 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002041 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002042 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2043
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002044 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002045 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2046 VM_ENTRY_LOAD_IA32_PAT |
2047 VM_ENTRY_LOAD_IA32_EFER |
2048 VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002049 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2050 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002051 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002053 /*
2054 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2055 * can't be used due to an errata where VM Exit may incorrectly clear
2056 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2057 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2058 */
2059 if (boot_cpu_data.x86 == 0x6) {
2060 switch (boot_cpu_data.x86_model) {
2061 case 26: /* AAK155 */
2062 case 30: /* AAP115 */
2063 case 37: /* AAT100 */
2064 case 44: /* BC86,AAY89,BD102 */
2065 case 46: /* BA97 */
2066 _vmexit_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
2067 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2068 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2069 "does not work properly. Using workaround\n");
2070 break;
2071 default:
2072 break;
2073 }
2074 }
2075
2076
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002077 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002078
2079 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2080 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002081 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002082
2083#ifdef CONFIG_X86_64
2084 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2085 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002086 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002087#endif
2088
2089 /* Require Write-Back (WB) memory type for VMCS accesses. */
2090 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002091 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002092
Yang, Sheng002c7f72007-07-31 14:23:01 +03002093 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002094 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002095 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002096
Liran Alon2307af12018-06-29 22:59:04 +03002097 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002098
Yang, Sheng002c7f72007-07-31 14:23:01 +03002099 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2100 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002101 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002102 vmcs_conf->vmexit_ctrl = _vmexit_control;
2103 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002104
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002105 if (static_branch_unlikely(&enable_evmcs))
2106 evmcs_sanitize_exec_ctrls(vmcs_conf);
2107
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002108 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002109}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002111struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112{
2113 int node = cpu_to_node(cpu);
2114 struct page *pages;
2115 struct vmcs *vmcs;
2116
Vlastimil Babka96db8002015-09-08 15:03:50 -07002117 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 if (!pages)
2119 return NULL;
2120 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002121 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002122
2123 /* KVM supports Enlightened VMCS v1 only */
2124 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002125 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002126 else
Liran Alon392b2f22018-06-23 02:35:01 +03002127 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002128
Liran Alon491a6032018-06-23 02:35:12 +03002129 if (shadow)
2130 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 return vmcs;
2132}
2133
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002134void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002136 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137}
2138
Nadav Har'Eld462b812011-05-24 15:26:10 +03002139/*
2140 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2141 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002142void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002143{
2144 if (!loaded_vmcs->vmcs)
2145 return;
2146 loaded_vmcs_clear(loaded_vmcs);
2147 free_vmcs(loaded_vmcs->vmcs);
2148 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002149 if (loaded_vmcs->msr_bitmap)
2150 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002151 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002152}
2153
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002154int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002155{
Liran Alon491a6032018-06-23 02:35:12 +03002156 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002157 if (!loaded_vmcs->vmcs)
2158 return -ENOMEM;
2159
2160 loaded_vmcs->shadow_vmcs = NULL;
2161 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002162
2163 if (cpu_has_vmx_msr_bitmap()) {
2164 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
2165 if (!loaded_vmcs->msr_bitmap)
2166 goto out_vmcs;
2167 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002168
Arnd Bergmann1f008e12018-05-25 17:36:17 +02002169 if (IS_ENABLED(CONFIG_HYPERV) &&
2170 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002171 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2172 struct hv_enlightened_vmcs *evmcs =
2173 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2174
2175 evmcs->hv_enlightenments_control.msr_bitmap = 1;
2176 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002177 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002178
2179 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
2180
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002181 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002182
2183out_vmcs:
2184 free_loaded_vmcs(loaded_vmcs);
2185 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002186}
2187
Sam Ravnborg39959582007-06-01 00:47:13 -07002188static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189{
2190 int cpu;
2191
Zachary Amsden3230bb42009-09-29 11:38:37 -10002192 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002194 per_cpu(vmxarea, cpu) = NULL;
2195 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196}
2197
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198static __init int alloc_kvm_area(void)
2199{
2200 int cpu;
2201
Zachary Amsden3230bb42009-09-29 11:38:37 -10002202 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 struct vmcs *vmcs;
2204
Liran Alon491a6032018-06-23 02:35:12 +03002205 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206 if (!vmcs) {
2207 free_kvm_area();
2208 return -ENOMEM;
2209 }
2210
Liran Alon2307af12018-06-29 22:59:04 +03002211 /*
2212 * When eVMCS is enabled, alloc_vmcs_cpu() sets
2213 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
2214 * revision_id reported by MSR_IA32_VMX_BASIC.
2215 *
2216 * However, even though not explictly documented by
2217 * TLFS, VMXArea passed as VMXON argument should
2218 * still be marked with revision_id reported by
2219 * physical CPU.
2220 */
2221 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002222 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002223
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224 per_cpu(vmxarea, cpu) = vmcs;
2225 }
2226 return 0;
2227}
2228
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002229static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002230 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002232 if (!emulate_invalid_guest_state) {
2233 /*
2234 * CS and SS RPL should be equal during guest entry according
2235 * to VMX spec, but in reality it is not always so. Since vcpu
2236 * is in the middle of the transition from real mode to
2237 * protected mode it is safe to assume that RPL 0 is a good
2238 * default value.
2239 */
2240 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03002241 save->selector &= ~SEGMENT_RPL_MASK;
2242 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02002243 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002245 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246}
2247
2248static void enter_pmode(struct kvm_vcpu *vcpu)
2249{
2250 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252
Gleb Natapovd99e4152012-12-20 16:57:45 +02002253 /*
2254 * Update real mode segment cache. It may be not up-to-date if sement
2255 * register was written while vcpu was in a guest mode.
2256 */
2257 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2258 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2259 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2260 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2261 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2262 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2263
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002264 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265
Avi Kivity2fb92db2011-04-27 19:42:18 +03002266 vmx_segment_cache_clear(vmx);
2267
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002268 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269
2270 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002271 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2272 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 vmcs_writel(GUEST_RFLAGS, flags);
2274
Rusty Russell66aee912007-07-17 23:34:16 +10002275 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2276 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277
2278 update_exception_bitmap(vcpu);
2279
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002280 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2281 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2282 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2283 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2284 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2285 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286}
2287
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002288static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289{
Mathias Krause772e0312012-08-30 01:30:19 +02002290 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002291 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292
Gleb Natapovd99e4152012-12-20 16:57:45 +02002293 var.dpl = 0x3;
2294 if (seg == VCPU_SREG_CS)
2295 var.type = 0x3;
2296
2297 if (!emulate_invalid_guest_state) {
2298 var.selector = var.base >> 4;
2299 var.base = var.base & 0xffff0;
2300 var.limit = 0xffff;
2301 var.g = 0;
2302 var.db = 0;
2303 var.present = 1;
2304 var.s = 1;
2305 var.l = 0;
2306 var.unusable = 0;
2307 var.type = 0x3;
2308 var.avl = 0;
2309 if (save->base & 0xf)
2310 printk_once(KERN_WARNING "kvm: segment base is not "
2311 "paragraph aligned when entering "
2312 "protected mode (seg=%d)", seg);
2313 }
2314
2315 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05002316 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002317 vmcs_write32(sf->limit, var.limit);
2318 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319}
2320
2321static void enter_rmode(struct kvm_vcpu *vcpu)
2322{
2323 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002324 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002325 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002327 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2328 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2329 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2330 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2331 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002332 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2333 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002334
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002335 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336
Gleb Natapov776e58e2011-03-13 12:34:27 +02002337 /*
2338 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002339 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002340 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002341 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002342 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2343 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002344
Avi Kivity2fb92db2011-04-27 19:42:18 +03002345 vmx_segment_cache_clear(vmx);
2346
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002347 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2350
2351 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002352 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002354 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355
2356 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002357 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358 update_exception_bitmap(vcpu);
2359
Gleb Natapovd99e4152012-12-20 16:57:45 +02002360 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2361 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2362 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2363 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2364 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2365 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002366
Eddie Dong8668a3c2007-10-10 14:26:45 +08002367 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368}
2369
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002370void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Amit Shah401d10d2009-02-20 22:53:37 +05302371{
2372 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002373 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2374
2375 if (!msr)
2376 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302377
Avi Kivityf6801df2010-01-21 15:31:50 +02002378 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302379 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002380 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302381 msr->data = efer;
2382 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002383 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302384
2385 msr->data = efer & ~EFER_LME;
2386 }
2387 setup_msrs(vmx);
2388}
2389
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002390#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391
2392static void enter_lmode(struct kvm_vcpu *vcpu)
2393{
2394 u32 guest_tr_ar;
2395
Avi Kivity2fb92db2011-04-27 19:42:18 +03002396 vmx_segment_cache_clear(to_vmx(vcpu));
2397
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002399 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002400 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2401 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002403 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
2404 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 }
Avi Kivityda38f432010-07-06 11:30:49 +03002406 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407}
2408
2409static void exit_lmode(struct kvm_vcpu *vcpu)
2410{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002411 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002412 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413}
2414
2415#endif
2416
Junaid Shahidfaff8752018-06-29 13:10:05 -07002417static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
2418{
2419 int vpid = to_vmx(vcpu)->vpid;
2420
2421 if (!vpid_sync_vcpu_addr(vpid, addr))
2422 vpid_sync_context(vpid);
2423
2424 /*
2425 * If VPIDs are not supported or enabled, then the above is a no-op.
2426 * But we don't really need a TLB flush in that case anyway, because
2427 * each VM entry/exit includes an implicit flush when VPID is 0.
2428 */
2429}
2430
Avi Kivitye8467fd2009-12-29 18:43:06 +02002431static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2432{
2433 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2434
2435 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
2436 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
2437}
2438
Avi Kivityaff48ba2010-12-05 18:56:11 +02002439static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2440{
Sean Christophersonb4d18512018-03-05 12:04:40 -08002441 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02002442 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2443 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2444}
2445
Anthony Liguori25c4c272007-04-27 09:29:21 +03002446static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002447{
Avi Kivityfc78f512009-12-07 12:16:48 +02002448 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2449
2450 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2451 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08002452}
2453
Sheng Yang14394422008-04-28 12:24:45 +08002454static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2455{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002456 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2457
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002458 if (!test_bit(VCPU_EXREG_PDPTR,
2459 (unsigned long *)&vcpu->arch.regs_dirty))
2460 return;
2461
Sheng Yang14394422008-04-28 12:24:45 +08002462 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002463 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
2464 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
2465 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
2466 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002467 }
2468}
2469
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002470void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03002471{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002472 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2473
Avi Kivity8f5d5492009-05-31 18:41:29 +03002474 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002475 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2476 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2477 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2478 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002479 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002480
2481 __set_bit(VCPU_EXREG_PDPTR,
2482 (unsigned long *)&vcpu->arch.regs_avail);
2483 __set_bit(VCPU_EXREG_PDPTR,
2484 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002485}
2486
Sheng Yang14394422008-04-28 12:24:45 +08002487static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2488 unsigned long cr0,
2489 struct kvm_vcpu *vcpu)
2490{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03002491 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
2492 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002493 if (!(cr0 & X86_CR0_PG)) {
2494 /* From paging/starting to nonpaging */
2495 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002496 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08002497 (CPU_BASED_CR3_LOAD_EXITING |
2498 CPU_BASED_CR3_STORE_EXITING));
2499 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002500 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002501 } else if (!is_paging(vcpu)) {
2502 /* From nonpaging to paging */
2503 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002504 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08002505 ~(CPU_BASED_CR3_LOAD_EXITING |
2506 CPU_BASED_CR3_STORE_EXITING));
2507 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002508 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002509 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002510
2511 if (!(cr0 & X86_CR0_WP))
2512 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002513}
2514
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002515void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002517 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002518 unsigned long hw_cr0;
2519
Sean Christopherson3de63472018-07-13 08:42:30 -07002520 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002521 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02002522 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02002523 else {
Gleb Natapov50378782013-02-04 16:00:28 +02002524 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002525
Gleb Natapov218e7632013-01-21 15:36:45 +02002526 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
2527 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528
Gleb Natapov218e7632013-01-21 15:36:45 +02002529 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
2530 enter_rmode(vcpu);
2531 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002533#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002534 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002535 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10002537 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538 exit_lmode(vcpu);
2539 }
2540#endif
2541
Sean Christophersonb4d18512018-03-05 12:04:40 -08002542 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08002543 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2544
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002546 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002547 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02002548
2549 /* depends on vcpu->arch.cr0 to be set to a new value */
2550 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551}
2552
Yu Zhang855feb62017-08-24 20:27:55 +08002553static int get_ept_level(struct kvm_vcpu *vcpu)
2554{
2555 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
2556 return 5;
2557 return 4;
2558}
2559
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002560u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08002561{
Yu Zhang855feb62017-08-24 20:27:55 +08002562 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08002563
Yu Zhang855feb62017-08-24 20:27:55 +08002564 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08002565
Peter Feiner995f00a2017-06-30 17:26:32 -07002566 if (enable_ept_ad_bits &&
2567 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02002568 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08002569 eptp |= (root_hpa & PAGE_MASK);
2570
2571 return eptp;
2572}
2573
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002574void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575{
Tianyu Lan877ad952018-07-19 08:40:23 +00002576 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08002577 unsigned long guest_cr3;
2578 u64 eptp;
2579
2580 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02002581 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07002582 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08002583 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00002584
2585 if (kvm_x86_ops->tlb_remote_flush) {
2586 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
2587 to_vmx(vcpu)->ept_pointer = eptp;
2588 to_kvm_vmx(kvm)->ept_pointers_match
2589 = EPT_POINTERS_CHECK;
2590 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
2591 }
2592
Sean Christophersone90008d2018-03-05 12:04:37 -08002593 if (enable_unrestricted_guest || is_paging(vcpu) ||
2594 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02002595 guest_cr3 = kvm_read_cr3(vcpu);
2596 else
Tianyu Lan877ad952018-07-19 08:40:23 +00002597 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02002598 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002599 }
2600
Sheng Yang14394422008-04-28 12:24:45 +08002601 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602}
2603
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002604int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605{
Ben Serebrin085e68e2015-04-16 11:58:05 -07002606 /*
2607 * Pass through host's Machine Check Enable value to hw_cr4, which
2608 * is in force while we are in guest mode. Do not let guests control
2609 * this bit, even if host CR4.MCE == 0.
2610 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08002611 unsigned long hw_cr4;
2612
2613 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
2614 if (enable_unrestricted_guest)
2615 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
2616 else if (to_vmx(vcpu)->rmode.vm86_active)
2617 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
2618 else
2619 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002620
Sean Christopherson64f7a112018-04-30 10:01:06 -07002621 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
2622 if (cr4 & X86_CR4_UMIP) {
2623 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02002624 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07002625 hw_cr4 &= ~X86_CR4_UMIP;
2626 } else if (!is_guest_mode(vcpu) ||
2627 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
2628 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
2629 SECONDARY_EXEC_DESC);
2630 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02002631
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002632 if (cr4 & X86_CR4_VMXE) {
2633 /*
2634 * To use VMXON (and later other VMX instructions), a guest
2635 * must first be able to turn on cr4.VMXE (see handle_vmon()).
2636 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02002637 * is here. We operate under the default treatment of SMM,
2638 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002639 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02002640 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002641 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01002642 }
David Matlack38991522016-11-29 18:14:08 -08002643
2644 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002645 return 1;
2646
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002647 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08002648
Sean Christopherson5dc1f042018-03-05 12:04:39 -08002649 if (!enable_unrestricted_guest) {
2650 if (enable_ept) {
2651 if (!is_paging(vcpu)) {
2652 hw_cr4 &= ~X86_CR4_PAE;
2653 hw_cr4 |= X86_CR4_PSE;
2654 } else if (!(cr4 & X86_CR4_PAE)) {
2655 hw_cr4 &= ~X86_CR4_PAE;
2656 }
2657 }
2658
Radim Krčmář656ec4a2015-11-02 22:20:00 +01002659 /*
Huaitong Handdba2622016-03-22 16:51:15 +08002660 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
2661 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
2662 * to be manually disabled when guest switches to non-paging
2663 * mode.
2664 *
2665 * If !enable_unrestricted_guest, the CPU is always running
2666 * with CR0.PG=1 and CR4 needs to be modified.
2667 * If enable_unrestricted_guest, the CPU automatically
2668 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01002669 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08002670 if (!is_paging(vcpu))
2671 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
2672 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01002673
Sheng Yang14394422008-04-28 12:24:45 +08002674 vmcs_writel(CR4_READ_SHADOW, cr4);
2675 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002676 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677}
2678
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002679void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680{
Avi Kivitya9179492011-01-03 14:28:52 +02002681 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 u32 ar;
2683
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002684 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002685 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02002686 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03002687 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002688 return;
Avi Kivity1390a282012-08-21 17:07:08 +03002689 var->base = vmx_read_guest_seg_base(vmx, seg);
2690 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2691 return;
Avi Kivitya9179492011-01-03 14:28:52 +02002692 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002693 var->base = vmx_read_guest_seg_base(vmx, seg);
2694 var->limit = vmx_read_guest_seg_limit(vmx, seg);
2695 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2696 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03002697 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 var->type = ar & 15;
2699 var->s = (ar >> 4) & 1;
2700 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03002701 /*
2702 * Some userspaces do not preserve unusable property. Since usable
2703 * segment has to be present according to VMX spec we can use present
2704 * property to amend userspace bug by making unusable segment always
2705 * nonpresent. vmx_segment_access_rights() already marks nonpresent
2706 * segment as unusable.
2707 */
2708 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709 var->avl = (ar >> 12) & 1;
2710 var->l = (ar >> 13) & 1;
2711 var->db = (ar >> 14) & 1;
2712 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713}
2714
Avi Kivitya9179492011-01-03 14:28:52 +02002715static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2716{
Avi Kivitya9179492011-01-03 14:28:52 +02002717 struct kvm_segment s;
2718
2719 if (to_vmx(vcpu)->rmode.vm86_active) {
2720 vmx_get_segment(vcpu, &s, seg);
2721 return s.base;
2722 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002723 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002724}
2725
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002726int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02002727{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02002728 struct vcpu_vmx *vmx = to_vmx(vcpu);
2729
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002730 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02002731 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002732 else {
2733 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002734 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02002735 }
Avi Kivity69c73022011-03-07 15:26:44 +02002736}
2737
Avi Kivity653e3102007-05-07 10:55:37 +03002738static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 u32 ar;
2741
Avi Kivityf0495f92012-06-07 17:06:10 +03002742 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 ar = 1 << 16;
2744 else {
2745 ar = var->type & 15;
2746 ar |= (var->s & 1) << 4;
2747 ar |= (var->dpl & 3) << 5;
2748 ar |= (var->present & 1) << 7;
2749 ar |= (var->avl & 1) << 12;
2750 ar |= (var->l & 1) << 13;
2751 ar |= (var->db & 1) << 14;
2752 ar |= (var->g & 1) << 15;
2753 }
Avi Kivity653e3102007-05-07 10:55:37 +03002754
2755 return ar;
2756}
2757
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002758void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03002759{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002760 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02002761 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03002762
Avi Kivity2fb92db2011-04-27 19:42:18 +03002763 vmx_segment_cache_clear(vmx);
2764
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02002765 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
2766 vmx->rmode.segs[seg] = *var;
2767 if (seg == VCPU_SREG_TR)
2768 vmcs_write16(sf->selector, var->selector);
2769 else if (var->s)
2770 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002771 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03002772 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02002773
Avi Kivity653e3102007-05-07 10:55:37 +03002774 vmcs_writel(sf->base, var->base);
2775 vmcs_write32(sf->limit, var->limit);
2776 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002777
2778 /*
2779 * Fix the "Accessed" bit in AR field of segment registers for older
2780 * qemu binaries.
2781 * IA32 arch specifies that at the time of processor reset the
2782 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08002783 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002784 * state vmexit when "unrestricted guest" mode is turned on.
2785 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2786 * tree. Newer qemu binaries with that qemu fix would not need this
2787 * kvm hack.
2788 */
2789 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02002790 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002791
Gleb Natapovf924d662012-12-12 19:10:55 +02002792 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02002793
2794out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01002795 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796}
2797
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2799{
Avi Kivity2fb92db2011-04-27 19:42:18 +03002800 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801
2802 *db = (ar >> 14) & 1;
2803 *l = (ar >> 13) & 1;
2804}
2805
Gleb Natapov89a27f42010-02-16 10:51:48 +02002806static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002808 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2809 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810}
2811
Gleb Natapov89a27f42010-02-16 10:51:48 +02002812static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002814 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2815 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816}
2817
Gleb Natapov89a27f42010-02-16 10:51:48 +02002818static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002820 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2821 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822}
2823
Gleb Natapov89a27f42010-02-16 10:51:48 +02002824static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002826 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2827 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828}
2829
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002830static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2831{
2832 struct kvm_segment var;
2833 u32 ar;
2834
2835 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02002836 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02002837 if (seg == VCPU_SREG_CS)
2838 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002839 ar = vmx_segment_access_rights(&var);
2840
2841 if (var.base != (var.selector << 4))
2842 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02002843 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002844 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02002845 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002846 return false;
2847
2848 return true;
2849}
2850
2851static bool code_segment_valid(struct kvm_vcpu *vcpu)
2852{
2853 struct kvm_segment cs;
2854 unsigned int cs_rpl;
2855
2856 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03002857 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002858
Avi Kivity1872a3f2009-01-04 23:26:52 +02002859 if (cs.unusable)
2860 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002861 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002862 return false;
2863 if (!cs.s)
2864 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002865 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002866 if (cs.dpl > cs_rpl)
2867 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002868 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002869 if (cs.dpl != cs_rpl)
2870 return false;
2871 }
2872 if (!cs.present)
2873 return false;
2874
2875 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2876 return true;
2877}
2878
2879static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2880{
2881 struct kvm_segment ss;
2882 unsigned int ss_rpl;
2883
2884 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03002885 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002886
Avi Kivity1872a3f2009-01-04 23:26:52 +02002887 if (ss.unusable)
2888 return true;
2889 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002890 return false;
2891 if (!ss.s)
2892 return false;
2893 if (ss.dpl != ss_rpl) /* DPL != RPL */
2894 return false;
2895 if (!ss.present)
2896 return false;
2897
2898 return true;
2899}
2900
2901static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2902{
2903 struct kvm_segment var;
2904 unsigned int rpl;
2905
2906 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03002907 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002908
Avi Kivity1872a3f2009-01-04 23:26:52 +02002909 if (var.unusable)
2910 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002911 if (!var.s)
2912 return false;
2913 if (!var.present)
2914 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002915 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002916 if (var.dpl < rpl) /* DPL < RPL */
2917 return false;
2918 }
2919
2920 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2921 * rights flags
2922 */
2923 return true;
2924}
2925
2926static bool tr_valid(struct kvm_vcpu *vcpu)
2927{
2928 struct kvm_segment tr;
2929
2930 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2931
Avi Kivity1872a3f2009-01-04 23:26:52 +02002932 if (tr.unusable)
2933 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03002934 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002935 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002936 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002937 return false;
2938 if (!tr.present)
2939 return false;
2940
2941 return true;
2942}
2943
2944static bool ldtr_valid(struct kvm_vcpu *vcpu)
2945{
2946 struct kvm_segment ldtr;
2947
2948 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2949
Avi Kivity1872a3f2009-01-04 23:26:52 +02002950 if (ldtr.unusable)
2951 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03002952 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002953 return false;
2954 if (ldtr.type != 2)
2955 return false;
2956 if (!ldtr.present)
2957 return false;
2958
2959 return true;
2960}
2961
2962static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2963{
2964 struct kvm_segment cs, ss;
2965
2966 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2967 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2968
Nadav Amitb32a9912015-03-29 16:33:04 +03002969 return ((cs.selector & SEGMENT_RPL_MASK) ==
2970 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002971}
2972
2973/*
2974 * Check if guest state is valid. Returns true if valid, false if
2975 * not.
2976 * We assume that registers are always usable
2977 */
2978static bool guest_state_valid(struct kvm_vcpu *vcpu)
2979{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02002980 if (enable_unrestricted_guest)
2981 return true;
2982
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002983 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03002984 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002985 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2986 return false;
2987 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2988 return false;
2989 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2990 return false;
2991 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2992 return false;
2993 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2994 return false;
2995 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2996 return false;
2997 } else {
2998 /* protected mode guest state checks */
2999 if (!cs_ss_rpl_check(vcpu))
3000 return false;
3001 if (!code_segment_valid(vcpu))
3002 return false;
3003 if (!stack_segment_valid(vcpu))
3004 return false;
3005 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3006 return false;
3007 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3008 return false;
3009 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3010 return false;
3011 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3012 return false;
3013 if (!tr_valid(vcpu))
3014 return false;
3015 if (!ldtr_valid(vcpu))
3016 return false;
3017 }
3018 /* TODO:
3019 * - Add checks on RIP
3020 * - Add checks on RFLAGS
3021 */
3022
3023 return true;
3024}
3025
Mike Dayd77c26f2007-10-08 09:02:08 -04003026static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003028 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003029 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003030 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003032 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003033 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003034 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3035 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003036 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003037 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003038 r = kvm_write_guest_page(kvm, fn++, &data,
3039 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003040 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003041 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003042 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3043 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003044 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003045 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3046 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003047 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003048 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003049 r = kvm_write_guest_page(kvm, fn, &data,
3050 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3051 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003052out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003053 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003054 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055}
3056
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003057static int init_rmode_identity_map(struct kvm *kvm)
3058{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003059 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08003060 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08003061 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003062 u32 tmp;
3063
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003064 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003065 mutex_lock(&kvm->slots_lock);
3066
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003067 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08003068 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08003069
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003070 if (!kvm_vmx->ept_identity_map_addr)
3071 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
3072 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08003073
David Hildenbrandd8a6e362017-08-24 20:51:34 +02003074 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003075 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08003076 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08003077 goto out2;
3078
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003079 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003080 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3081 if (r < 0)
3082 goto out;
3083 /* Set up identity-mapping pagetable for EPT in real mode */
3084 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3085 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3086 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3087 r = kvm_write_guest_page(kvm, identity_map_pfn,
3088 &tmp, i * sizeof(tmp), sizeof(tmp));
3089 if (r < 0)
3090 goto out;
3091 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003092 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003093
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003094out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003095 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08003096
3097out2:
3098 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003099 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003100}
3101
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102static void seg_setup(int seg)
3103{
Mathias Krause772e0312012-08-30 01:30:19 +02003104 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003105 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106
3107 vmcs_write16(sf->selector, 0);
3108 vmcs_writel(sf->base, 0);
3109 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003110 ar = 0x93;
3111 if (seg == VCPU_SREG_CS)
3112 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003113
3114 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115}
3116
Sheng Yangf78e0e22007-10-29 09:40:42 +08003117static int alloc_apic_access_page(struct kvm *kvm)
3118{
Xiao Guangrong44841412012-09-07 14:14:20 +08003119 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003120 int r = 0;
3121
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003122 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003123 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003124 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02003125 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3126 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003127 if (r)
3128 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003129
Tang Chen73a6d942014-09-11 13:38:00 +08003130 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003131 if (is_error_page(page)) {
3132 r = -EFAULT;
3133 goto out;
3134 }
3135
Tang Chenc24ae0d2014-09-24 15:57:58 +08003136 /*
3137 * Do not pin the page in memory, so that memory hot-unplug
3138 * is able to migrate it.
3139 */
3140 put_page(page);
3141 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003142out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003143 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003144 return r;
3145}
3146
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003147int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003148{
3149 int vpid;
3150
Avi Kivity919818a2009-03-23 18:01:29 +02003151 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003152 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003153 spin_lock(&vmx_vpid_lock);
3154 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003155 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003156 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003157 else
3158 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003159 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003160 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003161}
3162
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003163void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003164{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003165 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003166 return;
3167 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003168 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003169 spin_unlock(&vmx_vpid_lock);
3170}
3171
Yi Wang1e4329ee2018-11-08 11:22:21 +08003172static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003173 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003174{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003175 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003176
3177 if (!cpu_has_vmx_msr_bitmap())
3178 return;
3179
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003180 if (static_branch_unlikely(&enable_evmcs))
3181 evmcs_touch_msr_bitmap();
3182
Sheng Yang25c5f222008-03-28 13:18:56 +08003183 /*
3184 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3185 * have the write-low and read-high bitmap offsets the wrong way round.
3186 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3187 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003188 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003189 if (type & MSR_TYPE_R)
3190 /* read-low */
3191 __clear_bit(msr, msr_bitmap + 0x000 / f);
3192
3193 if (type & MSR_TYPE_W)
3194 /* write-low */
3195 __clear_bit(msr, msr_bitmap + 0x800 / f);
3196
Sheng Yang25c5f222008-03-28 13:18:56 +08003197 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3198 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003199 if (type & MSR_TYPE_R)
3200 /* read-high */
3201 __clear_bit(msr, msr_bitmap + 0x400 / f);
3202
3203 if (type & MSR_TYPE_W)
3204 /* write-high */
3205 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3206
3207 }
3208}
3209
Yi Wang1e4329ee2018-11-08 11:22:21 +08003210static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003211 u32 msr, int type)
3212{
3213 int f = sizeof(unsigned long);
3214
3215 if (!cpu_has_vmx_msr_bitmap())
3216 return;
3217
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003218 if (static_branch_unlikely(&enable_evmcs))
3219 evmcs_touch_msr_bitmap();
3220
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003221 /*
3222 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3223 * have the write-low and read-high bitmap offsets the wrong way round.
3224 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3225 */
3226 if (msr <= 0x1fff) {
3227 if (type & MSR_TYPE_R)
3228 /* read-low */
3229 __set_bit(msr, msr_bitmap + 0x000 / f);
3230
3231 if (type & MSR_TYPE_W)
3232 /* write-low */
3233 __set_bit(msr, msr_bitmap + 0x800 / f);
3234
3235 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3236 msr &= 0x1fff;
3237 if (type & MSR_TYPE_R)
3238 /* read-high */
3239 __set_bit(msr, msr_bitmap + 0x400 / f);
3240
3241 if (type & MSR_TYPE_W)
3242 /* write-high */
3243 __set_bit(msr, msr_bitmap + 0xc00 / f);
3244
3245 }
3246}
3247
Yi Wang1e4329ee2018-11-08 11:22:21 +08003248static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003249 u32 msr, int type, bool value)
3250{
3251 if (value)
3252 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
3253 else
3254 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
3255}
3256
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003257static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003258{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003259 u8 mode = 0;
3260
3261 if (cpu_has_secondary_exec_ctrls() &&
3262 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
3263 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3264 mode |= MSR_BITMAP_MODE_X2APIC;
3265 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3266 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3267 }
3268
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003269 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003270}
3271
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003272static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
3273 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003274{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003275 int msr;
3276
3277 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3278 unsigned word = msr / BITS_PER_LONG;
3279 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
3280 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003281 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003282
3283 if (mode & MSR_BITMAP_MODE_X2APIC) {
3284 /*
3285 * TPR reads and writes can be virtualized even if virtual interrupt
3286 * delivery is not in use.
3287 */
3288 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
3289 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
3290 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
3291 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3292 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
3293 }
3294 }
3295}
3296
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003297void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003298{
3299 struct vcpu_vmx *vmx = to_vmx(vcpu);
3300 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3301 u8 mode = vmx_msr_bitmap_mode(vcpu);
3302 u8 changed = mode ^ vmx->msr_bitmap_mode;
3303
3304 if (!changed)
3305 return;
3306
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003307 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
3308 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
3309
3310 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003311}
3312
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05003313static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02003314{
Andrey Smetanind62caab2015-11-10 15:36:33 +03003315 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02003316}
3317
Liran Alone6c67d82018-09-04 10:56:52 +03003318static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3319{
3320 struct vcpu_vmx *vmx = to_vmx(vcpu);
3321 void *vapic_page;
3322 u32 vppr;
3323 int rvi;
3324
3325 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3326 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
3327 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
3328 return false;
3329
Paolo Bonzini7e712682018-10-03 13:44:26 +02003330 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003331
3332 vapic_page = kmap(vmx->nested.virtual_apic_page);
3333 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
3334 kunmap(vmx->nested.virtual_apic_page);
3335
3336 return ((rvi & 0xf0) > (vppr & 0xf0));
3337}
3338
Wincy Van06a55242017-04-28 13:13:59 +08003339static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
3340 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003341{
3342#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08003343 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
3344
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003345 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08003346 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003347 * The vector of interrupt to be delivered to vcpu had
3348 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08003349 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003350 * Following cases will be reached in this block, and
3351 * we always send a notification event in all cases as
3352 * explained below.
3353 *
3354 * Case 1: vcpu keeps in non-root mode. Sending a
3355 * notification event posts the interrupt to vcpu.
3356 *
3357 * Case 2: vcpu exits to root mode and is still
3358 * runnable. PIR will be synced to vIRR before the
3359 * next vcpu entry. Sending a notification event in
3360 * this case has no effect, as vcpu is not in root
3361 * mode.
3362 *
3363 * Case 3: vcpu exits to root mode and is blocked.
3364 * vcpu_block() has already synced PIR to vIRR and
3365 * never blocks vcpu if vIRR is not cleared. Therefore,
3366 * a blocked vcpu here does not wait for any requested
3367 * interrupts in PIR, and sending a notification event
3368 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08003369 */
Feng Wu28b835d2015-09-18 22:29:54 +08003370
Wincy Van06a55242017-04-28 13:13:59 +08003371 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003372 return true;
3373 }
3374#endif
3375 return false;
3376}
3377
Wincy Van705699a2015-02-03 23:58:17 +08003378static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
3379 int vector)
3380{
3381 struct vcpu_vmx *vmx = to_vmx(vcpu);
3382
3383 if (is_guest_mode(vcpu) &&
3384 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08003385 /*
3386 * If a posted intr is not recognized by hardware,
3387 * we will accomplish it in the next vmentry.
3388 */
3389 vmx->nested.pi_pending = true;
3390 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02003391 /* the PIR and ON have been set by L1. */
3392 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
3393 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08003394 return 0;
3395 }
3396 return -1;
3397}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003398/*
Yang Zhanga20ed542013-04-11 19:25:15 +08003399 * Send interrupt to vcpu via posted interrupt way.
3400 * 1. If target vcpu is running(non-root mode), send posted interrupt
3401 * notification to vcpu and hardware will sync PIR to vIRR atomically.
3402 * 2. If target vcpu isn't running(root mode), kick it to pick up the
3403 * interrupt from PIR in next vmentry.
3404 */
3405static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
3406{
3407 struct vcpu_vmx *vmx = to_vmx(vcpu);
3408 int r;
3409
Wincy Van705699a2015-02-03 23:58:17 +08003410 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
3411 if (!r)
3412 return;
3413
Yang Zhanga20ed542013-04-11 19:25:15 +08003414 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
3415 return;
3416
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003417 /* If a previous notification has sent the IPI, nothing to do. */
3418 if (pi_test_and_set_on(&vmx->pi_desc))
3419 return;
3420
Wincy Van06a55242017-04-28 13:13:59 +08003421 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08003422 kvm_vcpu_kick(vcpu);
3423}
3424
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003426 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3427 * will not change in the lifetime of the guest.
3428 * Note that host-state that does change is set elsewhere. E.g., host-state
3429 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3430 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003431void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003432{
3433 u32 low32, high32;
3434 unsigned long tmpl;
3435 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003436 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003437
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07003438 cr0 = read_cr0();
3439 WARN_ON(cr0 & X86_CR0_TS);
3440 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003441
3442 /*
3443 * Save the most likely value for this task's CR3 in the VMCS.
3444 * We can't use __get_current_cr3_fast() because we're not atomic.
3445 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07003446 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003447 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003448 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003449
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003450 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003451 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003452 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003453 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003454
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003455 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003456#ifdef CONFIG_X86_64
3457 /*
3458 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003459 * vmx_prepare_switch_to_host(), in case userspace uses
3460 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03003461 */
3462 vmcs_write16(HOST_DS_SELECTOR, 0);
3463 vmcs_write16(HOST_ES_SELECTOR, 0);
3464#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003465 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3466 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003467#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003468 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3469 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3470
Juergen Gross87930012017-09-04 12:25:27 +02003471 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003472 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08003473 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003474
Avi Kivity83287ea422012-09-16 15:10:57 +03003475 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003476
3477 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
3478 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
3479 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
3480 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
3481
3482 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3483 rdmsr(MSR_IA32_CR_PAT, low32, high32);
3484 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
3485 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003486
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003487 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003488 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003489}
3490
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003491void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003492{
3493 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
3494 if (enable_ept)
3495 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003496 if (is_guest_mode(&vmx->vcpu))
3497 vmx->vcpu.arch.cr4_guest_owned_bits &=
3498 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003499 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
3500}
3501
Yang Zhang01e439b2013-04-11 19:25:12 +08003502static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
3503{
3504 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
3505
Andrey Smetanind62caab2015-11-10 15:36:33 +03003506 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08003507 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003508
3509 if (!enable_vnmi)
3510 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
3511
Yunhong Jiang64672c92016-06-13 14:19:59 -07003512 /* Enable the preemption timer dynamically */
3513 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003514 return pin_based_exec_ctrl;
3515}
3516
Andrey Smetanind62caab2015-11-10 15:36:33 +03003517static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
3518{
3519 struct vcpu_vmx *vmx = to_vmx(vcpu);
3520
3521 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03003522 if (cpu_has_secondary_exec_ctrls()) {
3523 if (kvm_vcpu_apicv_active(vcpu))
3524 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
3525 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3526 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3527 else
3528 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
3529 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3530 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3531 }
3532
3533 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003534 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003535}
3536
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08003537u32 vmx_exec_control(struct vcpu_vmx *vmx)
3538{
3539 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
3540
3541 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
3542 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
3543
3544 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
3545 exec_control &= ~CPU_BASED_TPR_SHADOW;
3546#ifdef CONFIG_X86_64
3547 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3548 CPU_BASED_CR8_LOAD_EXITING;
3549#endif
3550 }
3551 if (!enable_ept)
3552 exec_control |= CPU_BASED_CR3_STORE_EXITING |
3553 CPU_BASED_CR3_LOAD_EXITING |
3554 CPU_BASED_INVLPG_EXITING;
3555 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
3556 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
3557 CPU_BASED_MONITOR_EXITING);
3558 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
3559 exec_control &= ~CPU_BASED_HLT_EXITING;
3560 return exec_control;
3561}
3562
3563
Paolo Bonzini80154d72017-08-24 13:55:35 +02003564static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003565{
Paolo Bonzini80154d72017-08-24 13:55:35 +02003566 struct kvm_vcpu *vcpu = &vmx->vcpu;
3567
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003568 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02003569
Paolo Bonzini80154d72017-08-24 13:55:35 +02003570 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003571 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3572 if (vmx->vpid == 0)
3573 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
3574 if (!enable_ept) {
3575 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
3576 enable_unrestricted_guest = 0;
3577 }
3578 if (!enable_unrestricted_guest)
3579 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07003580 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003581 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02003582 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08003583 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
3584 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08003585 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02003586
3587 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
3588 * in vmx_set_cr4. */
3589 exec_control &= ~SECONDARY_EXEC_DESC;
3590
Abel Gordonabc4fc52013-04-18 14:35:25 +03003591 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
3592 (handle_vmptrld).
3593 We can NOT enable shadow_vmcs here because we don't have yet
3594 a current VMCS12
3595 */
3596 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08003597
3598 if (!enable_pml)
3599 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08003600
Paolo Bonzini3db13482017-08-24 14:48:03 +02003601 if (vmx_xsaves_supported()) {
3602 /* Exposing XSAVES only when XSAVE is exposed */
3603 bool xsaves_enabled =
3604 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
3605 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
3606
3607 if (!xsaves_enabled)
3608 exec_control &= ~SECONDARY_EXEC_XSAVES;
3609
3610 if (nested) {
3611 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003612 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02003613 SECONDARY_EXEC_XSAVES;
3614 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003615 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02003616 ~SECONDARY_EXEC_XSAVES;
3617 }
3618 }
3619
Paolo Bonzini80154d72017-08-24 13:55:35 +02003620 if (vmx_rdtscp_supported()) {
3621 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
3622 if (!rdtscp_enabled)
3623 exec_control &= ~SECONDARY_EXEC_RDTSCP;
3624
3625 if (nested) {
3626 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003627 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003628 SECONDARY_EXEC_RDTSCP;
3629 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003630 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003631 ~SECONDARY_EXEC_RDTSCP;
3632 }
3633 }
3634
3635 if (vmx_invpcid_supported()) {
3636 /* Exposing INVPCID only when PCID is exposed */
3637 bool invpcid_enabled =
3638 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
3639 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
3640
3641 if (!invpcid_enabled) {
3642 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
3643 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
3644 }
3645
3646 if (nested) {
3647 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003648 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003649 SECONDARY_EXEC_ENABLE_INVPCID;
3650 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003651 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003652 ~SECONDARY_EXEC_ENABLE_INVPCID;
3653 }
3654 }
3655
Jim Mattson45ec3682017-08-23 16:32:04 -07003656 if (vmx_rdrand_supported()) {
3657 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
3658 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02003659 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07003660
3661 if (nested) {
3662 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003663 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003664 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07003665 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003666 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003667 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07003668 }
3669 }
3670
Jim Mattson75f4fc82017-08-23 16:32:03 -07003671 if (vmx_rdseed_supported()) {
3672 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
3673 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02003674 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07003675
3676 if (nested) {
3677 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003678 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003679 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07003680 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003681 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003682 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07003683 }
3684 }
3685
Paolo Bonzini80154d72017-08-24 13:55:35 +02003686 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003687}
3688
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003689static void ept_set_mmio_spte_mask(void)
3690{
3691 /*
3692 * EPT Misconfigurations can be generated if the value of bits 2:0
3693 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003694 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07003695 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
3696 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003697}
3698
Wanpeng Lif53cd632014-12-02 19:14:58 +08003699#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700
Sean Christopherson944c3462018-12-03 13:53:09 -08003701/*
3702 * Sets up the vmcs for emulated real mode.
3703 */
3704static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
3705{
3706 int i;
3707
3708 if (nested)
3709 nested_vmx_vcpu_setup();
3710
Sheng Yang25c5f222008-03-28 13:18:56 +08003711 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003712 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08003713
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
3715
Avi Kivity6aa8b732006-12-10 02:21:36 -08003716 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08003717 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07003718 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003719
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003720 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003721
Dan Williamsdfa169b2016-06-02 11:17:24 -07003722 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02003723 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003724 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02003725 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07003726 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08003727
Andrey Smetanind62caab2015-11-10 15:36:33 +03003728 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08003729 vmcs_write64(EOI_EXIT_BITMAP0, 0);
3730 vmcs_write64(EOI_EXIT_BITMAP1, 0);
3731 vmcs_write64(EOI_EXIT_BITMAP2, 0);
3732 vmcs_write64(EOI_EXIT_BITMAP3, 0);
3733
3734 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08003735
Li RongQing0bcf2612015-12-03 13:29:34 +08003736 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08003737 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08003738 }
3739
Wanpeng Lib31c1142018-03-12 04:53:04 -07003740 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003741 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02003742 vmx->ple_window = ple_window;
3743 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003744 }
3745
Xiao Guangrongc3707952011-07-12 03:28:04 +08003746 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
3747 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003748 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
3749
Avi Kivity9581d442010-10-19 16:46:55 +02003750 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
3751 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08003752 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003753 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
3754 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755
Bandan Das2a499e42017-08-03 15:54:41 -04003756 if (cpu_has_vmx_vmfunc())
3757 vmcs_write64(VM_FUNCTION_CONTROL, 0);
3758
Eddie Dong2cc51562007-05-21 07:28:09 +03003759 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
3760 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04003761 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03003762 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04003763 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764
Radim Krčmář74545702015-04-27 15:11:25 +02003765 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
3766 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08003767
Paolo Bonzini03916db2014-07-24 14:21:57 +02003768 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003769 u32 index = vmx_msr_index[i];
3770 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003771 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003772
3773 if (rdmsr_safe(index, &data_low, &data_high) < 0)
3774 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08003775 if (wrmsr_safe(index, data_low, data_high) < 0)
3776 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03003777 vmx->guest_msrs[j].index = i;
3778 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02003779 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003780 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003781 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02003783 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02003784
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003785 vm_exit_controls_init(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786
3787 /* 22.2.1, 20.8.1 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003788 vm_entry_controls_init(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003789
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08003790 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
3791 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
3792
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003793 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003794
Wanpeng Lif53cd632014-12-02 19:14:58 +08003795 if (vmx_xsaves_supported())
3796 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
3797
Peter Feiner4e595162016-07-07 14:49:58 -07003798 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07003799 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
3800 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
3801 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07003802
3803 if (cpu_has_vmx_encls_vmexit())
3804 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003805}
3806
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003807static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003808{
3809 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01003810 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003811 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003812
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003813 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003814 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003815
Wanpeng Li518e7b92018-02-28 14:03:31 +08003816 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003817 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003818 kvm_set_cr8(vcpu, 0);
3819
3820 if (!init_event) {
3821 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
3822 MSR_IA32_APICBASE_ENABLE;
3823 if (kvm_vcpu_is_reset_bsp(vcpu))
3824 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
3825 apic_base_msr.host_initiated = true;
3826 kvm_set_apic_base(vcpu, &apic_base_msr);
3827 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003828
Avi Kivity2fb92db2011-04-27 19:42:18 +03003829 vmx_segment_cache_clear(vmx);
3830
Avi Kivity5706be02008-08-20 15:07:31 +03003831 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01003832 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01003833 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003834
3835 seg_setup(VCPU_SREG_DS);
3836 seg_setup(VCPU_SREG_ES);
3837 seg_setup(VCPU_SREG_FS);
3838 seg_setup(VCPU_SREG_GS);
3839 seg_setup(VCPU_SREG_SS);
3840
3841 vmcs_write16(GUEST_TR_SELECTOR, 0);
3842 vmcs_writel(GUEST_TR_BASE, 0);
3843 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
3844 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3845
3846 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
3847 vmcs_writel(GUEST_LDTR_BASE, 0);
3848 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
3849 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
3850
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003851 if (!init_event) {
3852 vmcs_write32(GUEST_SYSENTER_CS, 0);
3853 vmcs_writel(GUEST_SYSENTER_ESP, 0);
3854 vmcs_writel(GUEST_SYSENTER_EIP, 0);
3855 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
3856 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003857
Wanpeng Lic37c2872017-11-20 14:52:21 -08003858 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01003859 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003860
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003861 vmcs_writel(GUEST_GDTR_BASE, 0);
3862 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
3863
3864 vmcs_writel(GUEST_IDTR_BASE, 0);
3865 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
3866
Anthony Liguori443381a2010-12-06 10:53:38 -06003867 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003868 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01003869 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07003870 if (kvm_mpx_supported())
3871 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003872
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003873 setup_msrs(vmx);
3874
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
3876
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003877 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08003878 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003879 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08003880 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003881 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08003882 vmcs_write32(TPR_THRESHOLD, 0);
3883 }
3884
Paolo Bonzinia73896c2014-11-02 07:54:30 +01003885 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886
Sheng Yang2384d2b2008-01-17 15:14:33 +08003887 if (vmx->vpid != 0)
3888 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
3889
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003890 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003891 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06003892 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003893 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02003894 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08003895
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003896 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003898 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003899 if (init_event)
3900 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003901}
3902
Jan Kiszkac9a79532014-03-07 20:03:15 +01003903static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003904{
Paolo Bonzini47c01522016-12-19 11:44:07 +01003905 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
3906 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003907}
3908
Jan Kiszkac9a79532014-03-07 20:03:15 +01003909static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003910{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003911 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003912 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01003913 enable_irq_window(vcpu);
3914 return;
3915 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02003916
Paolo Bonzini47c01522016-12-19 11:44:07 +01003917 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
3918 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003919}
3920
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003921static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03003922{
Avi Kivity9c8cba32007-11-22 11:42:59 +02003923 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003924 uint32_t intr;
3925 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02003926
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003927 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003928
Avi Kivityfa89a812008-09-01 15:57:51 +03003929 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003930 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003931 int inc_eip = 0;
3932 if (vcpu->arch.interrupt.soft)
3933 inc_eip = vcpu->arch.event_exit_inst_len;
3934 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003935 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003936 return;
3937 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003938 intr = irq | INTR_INFO_VALID_MASK;
3939 if (vcpu->arch.interrupt.soft) {
3940 intr |= INTR_TYPE_SOFT_INTR;
3941 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3942 vmx->vcpu.arch.event_exit_inst_len);
3943 } else
3944 intr |= INTR_TYPE_EXT_INTR;
3945 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003946
3947 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003948}
3949
Sheng Yangf08864b2008-05-15 18:23:25 +08003950static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
3951{
Jan Kiszka66a5a342008-09-26 09:30:51 +02003952 struct vcpu_vmx *vmx = to_vmx(vcpu);
3953
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003954 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003955 /*
3956 * Tracking the NMI-blocked state in software is built upon
3957 * finding the next open IRQ window. This, in turn, depends on
3958 * well-behaving guests: They have to keep IRQs disabled at
3959 * least as long as the NMI handler runs. Otherwise we may
3960 * cause NMI nesting, maybe breaking the guest. But as this is
3961 * highly unlikely, we can live with the residual risk.
3962 */
3963 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
3964 vmx->loaded_vmcs->vnmi_blocked_time = 0;
3965 }
3966
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003967 ++vcpu->stat.nmi_injections;
3968 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003969
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003970 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003971 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003972 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02003973 return;
3974 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08003975
Sheng Yangf08864b2008-05-15 18:23:25 +08003976 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
3977 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003978
3979 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003980}
3981
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003982bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003983{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003984 struct vcpu_vmx *vmx = to_vmx(vcpu);
3985 bool masked;
3986
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003987 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003988 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003989 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02003990 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003991 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
3992 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
3993 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003994}
3995
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003996void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003997{
3998 struct vcpu_vmx *vmx = to_vmx(vcpu);
3999
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004000 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004001 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4002 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4003 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4004 }
4005 } else {
4006 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4007 if (masked)
4008 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4009 GUEST_INTR_STATE_NMI);
4010 else
4011 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4012 GUEST_INTR_STATE_NMI);
4013 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004014}
4015
Jan Kiszka2505dc92013-04-14 12:12:47 +02004016static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4017{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004018 if (to_vmx(vcpu)->nested.nested_run_pending)
4019 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004020
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004021 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004022 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4023 return 0;
4024
Jan Kiszka2505dc92013-04-14 12:12:47 +02004025 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4026 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4027 | GUEST_INTR_STATE_NMI));
4028}
4029
Gleb Natapov78646122009-03-23 12:12:11 +02004030static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4031{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004032 return (!to_vmx(vcpu)->nested.nested_run_pending &&
4033 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03004034 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4035 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004036}
4037
Izik Eiduscbc94022007-10-25 00:29:55 +02004038static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4039{
4040 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004041
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004042 if (enable_unrestricted_guest)
4043 return 0;
4044
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004045 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4046 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02004047 if (ret)
4048 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004049 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004050 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004051}
4052
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004053static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4054{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004055 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004056 return 0;
4057}
4058
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004059static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004061 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004062 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004063 /*
4064 * Update instruction length as we may reinject the exception
4065 * from user space while in guest debugging mode.
4066 */
4067 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4068 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004069 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004070 return false;
4071 /* fall through */
4072 case DB_VECTOR:
4073 if (vcpu->guest_debug &
4074 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4075 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004076 /* fall through */
4077 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004078 case OF_VECTOR:
4079 case BR_VECTOR:
4080 case UD_VECTOR:
4081 case DF_VECTOR:
4082 case SS_VECTOR:
4083 case GP_VECTOR:
4084 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004085 return true;
4086 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004087 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004088 return false;
4089}
4090
4091static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4092 int vec, u32 err_code)
4093{
4094 /*
4095 * Instruction with address size override prefix opcode 0x67
4096 * Cause the #SS fault with 0 error code in VM86 mode.
4097 */
4098 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004099 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004100 if (vcpu->arch.halt_request) {
4101 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004102 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004103 }
4104 return 1;
4105 }
4106 return 0;
4107 }
4108
4109 /*
4110 * Forward all other exceptions that are valid in real mode.
4111 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4112 * the required debugging infrastructure rework.
4113 */
4114 kvm_queue_exception(vcpu, vec);
4115 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004116}
4117
Andi Kleena0861c02009-06-08 17:37:09 +08004118/*
4119 * Trigger machine check on the host. We assume all the MSRs are already set up
4120 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4121 * We pass a fake environment to the machine check handler because we want
4122 * the guest to be always treated like user space, no matter what context
4123 * it used internally.
4124 */
4125static void kvm_machine_check(void)
4126{
4127#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4128 struct pt_regs regs = {
4129 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4130 .flags = X86_EFLAGS_IF,
4131 };
4132
4133 do_machine_check(&regs, 0);
4134#endif
4135}
4136
Avi Kivity851ba692009-08-24 11:10:17 +03004137static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004138{
4139 /* already handled by vcpu_run */
4140 return 1;
4141}
4142
Avi Kivity851ba692009-08-24 11:10:17 +03004143static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144{
Avi Kivity1155f762007-11-22 11:30:47 +02004145 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004146 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004147 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004148 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149 u32 vect_info;
4150 enum emulation_result er;
4151
Avi Kivity1155f762007-11-22 11:30:47 +02004152 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004153 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154
Andi Kleena0861c02009-06-08 17:37:09 +08004155 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004156 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004157
Jim Mattsonef85b672016-12-12 11:01:37 -08004158 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02004159 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004160
Wanpeng Li082d06e2018-04-03 16:28:48 -07004161 if (is_invalid_opcode(intr_info))
4162 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004163
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004165 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004167
Liran Alon9e869482018-03-12 13:12:51 +02004168 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4169 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004170 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02004171 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
4172 if (er == EMULATE_USER_EXIT)
4173 return 0;
4174 else if (er != EMULATE_DONE)
4175 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4176 return 1;
4177 }
4178
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004179 /*
4180 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4181 * MMIO, it is better to report an internal error.
4182 * See the comments in vmx_handle_exit.
4183 */
4184 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4185 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4186 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4187 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004188 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004189 vcpu->run->internal.data[0] = vect_info;
4190 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004191 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004192 return 0;
4193 }
4194
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195 if (is_page_fault(intr_info)) {
4196 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07004197 /* EPT won't cause page fault directly */
4198 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02004199 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004200 }
4201
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004202 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004203
4204 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4205 return handle_rmode_exception(vcpu, ex_no, error_code);
4206
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004207 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01004208 case AC_VECTOR:
4209 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4210 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004211 case DB_VECTOR:
4212 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4213 if (!(vcpu->guest_debug &
4214 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01004215 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03004216 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004217 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01004218 skip_emulated_instruction(vcpu);
4219
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004220 kvm_queue_exception(vcpu, DB_VECTOR);
4221 return 1;
4222 }
4223 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4224 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4225 /* fall through */
4226 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004227 /*
4228 * Update instruction length as we may reinject #BP from
4229 * user space while in guest debugging mode. Reading it for
4230 * #DB as well causes no harm, it is not used in that case.
4231 */
4232 vmx->vcpu.arch.event_exit_inst_len =
4233 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004235 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004236 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4237 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004238 break;
4239 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004240 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4241 kvm_run->ex.exception = ex_no;
4242 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004243 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004244 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245 return 0;
4246}
4247
Avi Kivity851ba692009-08-24 11:10:17 +03004248static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004250 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251 return 1;
4252}
4253
Avi Kivity851ba692009-08-24 11:10:17 +03004254static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004255{
Avi Kivity851ba692009-08-24 11:10:17 +03004256 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004257 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004258 return 0;
4259}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004260
Avi Kivity851ba692009-08-24 11:10:17 +03004261static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004262{
He, Qingbfdaab02007-09-12 14:18:28 +08004263 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004264 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004265 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004266
He, Qingbfdaab02007-09-12 14:18:28 +08004267 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004268 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004269
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004270 ++vcpu->stat.io_exits;
4271
Sean Christopherson432baf62018-03-08 08:57:26 -08004272 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004273 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004274
4275 port = exit_qualification >> 16;
4276 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004277 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004278
Sean Christophersondca7f122018-03-08 08:57:27 -08004279 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004280}
4281
Ingo Molnar102d8322007-02-19 14:37:47 +02004282static void
4283vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4284{
4285 /*
4286 * Patch in the VMCALL instruction:
4287 */
4288 hypercall[0] = 0x0f;
4289 hypercall[1] = 0x01;
4290 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004291}
4292
Guo Chao0fa06072012-06-28 15:16:19 +08004293/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004294static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4295{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004296 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004297 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4298 unsigned long orig_val = val;
4299
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004300 /*
4301 * We get here when L2 changed cr0 in a way that did not change
4302 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004303 * but did change L0 shadowed bits. So we first calculate the
4304 * effective cr0 value that L1 would like to write into the
4305 * hardware. It consists of the L2-owned bits from the new
4306 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004307 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004308 val = (val & ~vmcs12->cr0_guest_host_mask) |
4309 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4310
David Matlack38991522016-11-29 18:14:08 -08004311 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004312 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004313
4314 if (kvm_set_cr0(vcpu, val))
4315 return 1;
4316 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004317 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004318 } else {
4319 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08004320 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004321 return 1;
David Matlack38991522016-11-29 18:14:08 -08004322
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004323 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004324 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004325}
4326
4327static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4328{
4329 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004330 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4331 unsigned long orig_val = val;
4332
4333 /* analogously to handle_set_cr0 */
4334 val = (val & ~vmcs12->cr4_guest_host_mask) |
4335 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4336 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004337 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004338 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004339 return 0;
4340 } else
4341 return kvm_set_cr4(vcpu, val);
4342}
4343
Paolo Bonzini0367f202016-07-12 10:44:55 +02004344static int handle_desc(struct kvm_vcpu *vcpu)
4345{
4346 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004347 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004348}
4349
Avi Kivity851ba692009-08-24 11:10:17 +03004350static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004352 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004353 int cr;
4354 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004355 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004356 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357
He, Qingbfdaab02007-09-12 14:18:28 +08004358 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359 cr = exit_qualification & 15;
4360 reg = (exit_qualification >> 8) & 15;
4361 switch ((exit_qualification >> 4) & 3) {
4362 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03004363 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004364 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365 switch (cr) {
4366 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004367 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004368 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004369 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004370 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03004371 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004372 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004373 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004374 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004375 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004376 case 8: {
4377 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03004378 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004379 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004380 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004381 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08004382 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004383 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08004384 return ret;
4385 /*
4386 * TODO: we might be squashing a
4387 * KVM_GUESTDBG_SINGLESTEP-triggered
4388 * KVM_EXIT_DEBUG here.
4389 */
Avi Kivity851ba692009-08-24 11:10:17 +03004390 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004391 return 0;
4392 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02004393 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004394 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03004395 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004396 WARN_ONCE(1, "Guest should always own CR0.TS");
4397 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02004398 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08004399 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400 case 1: /*mov from cr*/
4401 switch (cr) {
4402 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004403 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02004404 val = kvm_read_cr3(vcpu);
4405 kvm_register_write(vcpu, reg, val);
4406 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004407 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004408 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004409 val = kvm_get_cr8(vcpu);
4410 kvm_register_write(vcpu, reg, val);
4411 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004412 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004413 }
4414 break;
4415 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02004416 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004417 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02004418 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004419
Kyle Huey6affcbe2016-11-29 12:40:40 -08004420 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004421 default:
4422 break;
4423 }
Avi Kivity851ba692009-08-24 11:10:17 +03004424 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03004425 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08004426 (int)(exit_qualification >> 4) & 3, cr);
4427 return 0;
4428}
4429
Avi Kivity851ba692009-08-24 11:10:17 +03004430static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004431{
He, Qingbfdaab02007-09-12 14:18:28 +08004432 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004433 int dr, dr7, reg;
4434
4435 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4436 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4437
4438 /* First, if DR does not exist, trigger UD */
4439 if (!kvm_require_dr(vcpu, dr))
4440 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441
Jan Kiszkaf2483412010-01-20 18:20:20 +01004442 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03004443 if (!kvm_require_cpl(vcpu, 0))
4444 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004445 dr7 = vmcs_readl(GUEST_DR7);
4446 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004447 /*
4448 * As the vm-exit takes precedence over the debug trap, we
4449 * need to emulate the latter, either for the host or the
4450 * guest debugging itself.
4451 */
4452 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03004453 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004454 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02004455 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004456 vcpu->run->debug.arch.exception = DB_VECTOR;
4457 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004458 return 0;
4459 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02004460 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03004461 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004462 kvm_queue_exception(vcpu, DB_VECTOR);
4463 return 1;
4464 }
4465 }
4466
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004467 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01004468 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
4469 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004470
4471 /*
4472 * No more DR vmexits; force a reload of the debug registers
4473 * and reenter on this instruction. The next vmexit will
4474 * retrieve the full state of the debug registers.
4475 */
4476 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
4477 return 1;
4478 }
4479
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004480 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4481 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03004482 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004483
4484 if (kvm_get_dr(vcpu, dr, &val))
4485 return 1;
4486 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03004487 } else
Nadav Amit57773922014-06-18 17:19:23 +03004488 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004489 return 1;
4490
Kyle Huey6affcbe2016-11-29 12:40:40 -08004491 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004492}
4493
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004494static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
4495{
4496 return vcpu->arch.dr6;
4497}
4498
4499static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
4500{
4501}
4502
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004503static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
4504{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004505 get_debugreg(vcpu->arch.db[0], 0);
4506 get_debugreg(vcpu->arch.db[1], 1);
4507 get_debugreg(vcpu->arch.db[2], 2);
4508 get_debugreg(vcpu->arch.db[3], 3);
4509 get_debugreg(vcpu->arch.dr6, 6);
4510 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
4511
4512 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01004513 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004514}
4515
Gleb Natapov020df072010-04-13 10:05:23 +03004516static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
4517{
4518 vmcs_writel(GUEST_DR7, val);
4519}
4520
Avi Kivity851ba692009-08-24 11:10:17 +03004521static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004522{
Kyle Huey6a908b62016-11-29 12:40:37 -08004523 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004524}
4525
Avi Kivity851ba692009-08-24 11:10:17 +03004526static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004527{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004528 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004529 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004530
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004531 msr_info.index = ecx;
4532 msr_info.host_initiated = false;
4533 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02004534 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004535 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004536 return 1;
4537 }
4538
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004539 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004540
Avi Kivity6aa8b732006-12-10 02:21:36 -08004541 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004542 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
4543 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004544 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004545}
4546
Avi Kivity851ba692009-08-24 11:10:17 +03004547static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004548{
Will Auld8fe8ab42012-11-29 12:42:12 -08004549 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004550 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
4551 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
4552 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004553
Will Auld8fe8ab42012-11-29 12:42:12 -08004554 msr.data = data;
4555 msr.index = ecx;
4556 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004557 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02004558 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004559 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004560 return 1;
4561 }
4562
Avi Kivity59200272010-01-25 19:47:02 +02004563 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004564 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565}
4566
Avi Kivity851ba692009-08-24 11:10:17 +03004567static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004568{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01004569 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004570 return 1;
4571}
4572
Avi Kivity851ba692009-08-24 11:10:17 +03004573static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004574{
Paolo Bonzini47c01522016-12-19 11:44:07 +01004575 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
4576 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004577
Avi Kivity3842d132010-07-27 12:30:24 +03004578 kvm_make_request(KVM_REQ_EVENT, vcpu);
4579
Jan Kiszkaa26bf122008-09-26 09:30:45 +02004580 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004581 return 1;
4582}
4583
Avi Kivity851ba692009-08-24 11:10:17 +03004584static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004585{
Avi Kivityd3bef152007-06-05 15:53:05 +03004586 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004587}
4588
Avi Kivity851ba692009-08-24 11:10:17 +03004589static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02004590{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03004591 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02004592}
4593
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004594static int handle_invd(struct kvm_vcpu *vcpu)
4595{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004596 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004597}
4598
Avi Kivity851ba692009-08-24 11:10:17 +03004599static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03004600{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004601 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004602
4603 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004604 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004605}
4606
Avi Kivityfee84b02011-11-10 14:57:25 +02004607static int handle_rdpmc(struct kvm_vcpu *vcpu)
4608{
4609 int err;
4610
4611 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004612 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02004613}
4614
Avi Kivity851ba692009-08-24 11:10:17 +03004615static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02004616{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004617 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02004618}
4619
Dexuan Cui2acf9232010-06-10 11:27:12 +08004620static int handle_xsetbv(struct kvm_vcpu *vcpu)
4621{
4622 u64 new_bv = kvm_read_edx_eax(vcpu);
4623 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4624
4625 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08004626 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08004627 return 1;
4628}
4629
Wanpeng Lif53cd632014-12-02 19:14:58 +08004630static int handle_xsaves(struct kvm_vcpu *vcpu)
4631{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004632 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08004633 WARN(1, "this should never happen\n");
4634 return 1;
4635}
4636
4637static int handle_xrstors(struct kvm_vcpu *vcpu)
4638{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004639 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08004640 WARN(1, "this should never happen\n");
4641 return 1;
4642}
4643
Avi Kivity851ba692009-08-24 11:10:17 +03004644static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004645{
Kevin Tian58fbbf22011-08-30 13:56:17 +03004646 if (likely(fasteoi)) {
4647 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4648 int access_type, offset;
4649
4650 access_type = exit_qualification & APIC_ACCESS_TYPE;
4651 offset = exit_qualification & APIC_ACCESS_OFFSET;
4652 /*
4653 * Sane guest uses MOV to write EOI, with written value
4654 * not cared. So make a short-circuit here by avoiding
4655 * heavy instruction emulation.
4656 */
4657 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
4658 (offset == APIC_EOI)) {
4659 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004660 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03004661 }
4662 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004663 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004664}
4665
Yang Zhangc7c9c562013-01-25 10:18:51 +08004666static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
4667{
4668 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4669 int vector = exit_qualification & 0xff;
4670
4671 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
4672 kvm_apic_set_eoi_accelerated(vcpu, vector);
4673 return 1;
4674}
4675
Yang Zhang83d4c282013-01-25 10:18:49 +08004676static int handle_apic_write(struct kvm_vcpu *vcpu)
4677{
4678 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4679 u32 offset = exit_qualification & 0xfff;
4680
4681 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
4682 kvm_apic_write_nodecode(vcpu, offset);
4683 return 1;
4684}
4685
Avi Kivity851ba692009-08-24 11:10:17 +03004686static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02004687{
Jan Kiszka60637aa2008-09-26 09:30:47 +02004688 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004689 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02004690 bool has_error_code = false;
4691 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004692 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004693 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004694
4695 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004696 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004697 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02004698
4699 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4700
4701 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004702 if (reason == TASK_SWITCH_GATE && idt_v) {
4703 switch (type) {
4704 case INTR_TYPE_NMI_INTR:
4705 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02004706 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004707 break;
4708 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004709 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004710 kvm_clear_interrupt_queue(vcpu);
4711 break;
4712 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02004713 if (vmx->idt_vectoring_info &
4714 VECTORING_INFO_DELIVER_CODE_MASK) {
4715 has_error_code = true;
4716 error_code =
4717 vmcs_read32(IDT_VECTORING_ERROR_CODE);
4718 }
4719 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004720 case INTR_TYPE_SOFT_EXCEPTION:
4721 kvm_clear_exception_queue(vcpu);
4722 break;
4723 default:
4724 break;
4725 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02004726 }
Izik Eidus37817f22008-03-24 23:14:53 +02004727 tss_selector = exit_qualification;
4728
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004729 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
4730 type != INTR_TYPE_EXT_INTR &&
4731 type != INTR_TYPE_NMI_INTR))
4732 skip_emulated_instruction(vcpu);
4733
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004734 if (kvm_task_switch(vcpu, tss_selector,
4735 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
4736 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03004737 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4738 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4739 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004740 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03004741 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004742
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004743 /*
4744 * TODO: What about debug traps on tss switch?
4745 * Are we supposed to inject them and update dr6?
4746 */
4747
4748 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02004749}
4750
Avi Kivity851ba692009-08-24 11:10:17 +03004751static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08004752{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004753 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08004754 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01004755 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08004756
Sheng Yangf9c617f2009-03-25 10:08:52 +08004757 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08004758
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03004759 /*
4760 * EPT violation happened while executing iret from NMI,
4761 * "blocked by NMI" bit has to be set before next VM entry.
4762 * There are errata that may cause this bit to not be set:
4763 * AAK134, BY25.
4764 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03004765 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004766 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03004767 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03004768 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
4769
Sheng Yang14394422008-04-28 12:24:45 +08004770 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004771 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004772
Junaid Shahid27959a42016-12-06 16:46:10 -08004773 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08004774 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08004775 ? PFERR_USER_MASK : 0;
4776 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08004777 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08004778 ? PFERR_WRITE_MASK : 0;
4779 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08004780 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08004781 ? PFERR_FETCH_MASK : 0;
4782 /* ept page table entry is present? */
4783 error_code |= (exit_qualification &
4784 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
4785 EPT_VIOLATION_EXECUTABLE))
4786 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004787
Paolo Bonzinieebed242016-11-28 14:39:58 +01004788 error_code |= (exit_qualification & 0x100) != 0 ?
4789 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03004790
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004791 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004792 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08004793}
4794
Avi Kivity851ba692009-08-24 11:10:17 +03004795static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004796{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004797 gpa_t gpa;
4798
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02004799 /*
4800 * A nested guest cannot optimize MMIO vmexits, because we have an
4801 * nGPA here instead of the required GPA.
4802 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004803 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02004804 if (!is_guest_mode(vcpu) &&
4805 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08004806 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01004807 /*
4808 * Doing kvm_skip_emulated_instruction() depends on undefined
4809 * behavior: Intel's manual doesn't mandate
4810 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
4811 * occurs and while on real hardware it was observed to be set,
4812 * other hypervisors (namely Hyper-V) don't set it, we end up
4813 * advancing IP with some random value. Disable fast mmio when
4814 * running nested and keep it for real hardware in hope that
4815 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
4816 */
4817 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
4818 return kvm_skip_emulated_instruction(vcpu);
4819 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004820 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07004821 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03004822 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004823
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07004824 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004825}
4826
Avi Kivity851ba692009-08-24 11:10:17 +03004827static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08004828{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004829 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01004830 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
4831 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08004832 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03004833 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004834
4835 return 1;
4836}
4837
Mohammed Gamal80ced182009-09-01 12:48:18 +02004838static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004839{
Avi Kivity8b3079a2009-01-05 12:10:54 +02004840 struct vcpu_vmx *vmx = to_vmx(vcpu);
4841 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02004842 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02004843 u32 cpu_exec_ctrl;
4844 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03004845 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02004846
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07004847 /*
4848 * We should never reach the point where we are emulating L2
4849 * due to invalid guest state as that means we incorrectly
4850 * allowed a nested VMEntry with an invalid vmcs12.
4851 */
4852 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
4853
Avi Kivity49e9d552010-09-19 14:34:08 +02004854 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4855 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004856
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004857 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03004858 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02004859 return handle_interrupt_window(&vmx->vcpu);
4860
Radim Krčmář72875d82017-04-26 22:32:19 +02004861 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03004862 return 1;
4863
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004864 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004865
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02004866 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02004867 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02004868 ret = 0;
4869 goto out;
4870 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01004871
Sean Christophersonadd5ff72018-03-23 09:34:00 -07004872 if (err != EMULATE_DONE)
4873 goto emulation_error;
4874
4875 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
4876 vcpu->arch.exception.pending)
4877 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004878
Gleb Natapov8d76c492013-05-08 18:38:44 +03004879 if (vcpu->arch.halt_request) {
4880 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004881 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03004882 goto out;
4883 }
4884
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004885 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02004886 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004887 if (need_resched())
4888 schedule();
4889 }
4890
Mohammed Gamal80ced182009-09-01 12:48:18 +02004891out:
4892 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004893
Sean Christophersonadd5ff72018-03-23 09:34:00 -07004894emulation_error:
4895 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4896 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4897 vcpu->run->internal.ndata = 0;
4898 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004899}
4900
4901static void grow_ple_window(struct kvm_vcpu *vcpu)
4902{
4903 struct vcpu_vmx *vmx = to_vmx(vcpu);
4904 int old = vmx->ple_window;
4905
Babu Mogerc8e88712018-03-16 16:37:24 -04004906 vmx->ple_window = __grow_ple_window(old, ple_window,
4907 ple_window_grow,
4908 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004909
4910 if (vmx->ple_window != old)
4911 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02004912
4913 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004914}
4915
4916static void shrink_ple_window(struct kvm_vcpu *vcpu)
4917{
4918 struct vcpu_vmx *vmx = to_vmx(vcpu);
4919 int old = vmx->ple_window;
4920
Babu Mogerc8e88712018-03-16 16:37:24 -04004921 vmx->ple_window = __shrink_ple_window(old, ple_window,
4922 ple_window_shrink,
4923 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004924
4925 if (vmx->ple_window != old)
4926 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02004927
4928 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004929}
4930
4931/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08004932 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
4933 */
4934static void wakeup_handler(void)
4935{
4936 struct kvm_vcpu *vcpu;
4937 int cpu = smp_processor_id();
4938
4939 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
4940 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
4941 blocked_vcpu_list) {
4942 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
4943
4944 if (pi_test_on(pi_desc) == 1)
4945 kvm_vcpu_kick(vcpu);
4946 }
4947 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
4948}
4949
Peng Haoe01bca22018-04-07 05:47:32 +08004950static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08004951{
4952 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
4953 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
4954 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
4955 0ull, VMX_EPT_EXECUTABLE_MASK,
4956 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05004957 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08004958
4959 ept_set_mmio_spte_mask();
4960 kvm_enable_tdp();
4961}
4962
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004964 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
4965 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
4966 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03004967static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004968{
Wanpeng Lib31c1142018-03-12 04:53:04 -07004969 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004970 grow_ple_window(vcpu);
4971
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004972 /*
4973 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
4974 * VM-execution control is ignored if CPL > 0. OTOH, KVM
4975 * never set PAUSE_EXITING and just set PLE if supported,
4976 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
4977 */
4978 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004979 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004980}
4981
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004982static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08004983{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004984 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08004985}
4986
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004987static int handle_mwait(struct kvm_vcpu *vcpu)
4988{
4989 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4990 return handle_nop(vcpu);
4991}
4992
Jim Mattson45ec3682017-08-23 16:32:04 -07004993static int handle_invalid_op(struct kvm_vcpu *vcpu)
4994{
4995 kvm_queue_exception(vcpu, UD_VECTOR);
4996 return 1;
4997}
4998
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03004999static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5000{
5001 return 1;
5002}
5003
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005004static int handle_monitor(struct kvm_vcpu *vcpu)
5005{
5006 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
5007 return handle_nop(vcpu);
5008}
5009
Junaid Shahideb4b2482018-06-27 14:59:14 -07005010static int handle_invpcid(struct kvm_vcpu *vcpu)
5011{
5012 u32 vmx_instruction_info;
5013 unsigned long type;
5014 bool pcid_enabled;
5015 gva_t gva;
5016 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005017 unsigned i;
5018 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005019 struct {
5020 u64 pcid;
5021 u64 gla;
5022 } operand;
5023
5024 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5025 kvm_queue_exception(vcpu, UD_VECTOR);
5026 return 1;
5027 }
5028
5029 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5030 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5031
5032 if (type > 3) {
5033 kvm_inject_gp(vcpu, 0);
5034 return 1;
5035 }
5036
5037 /* According to the Intel instruction reference, the memory operand
5038 * is read even if it isn't needed (e.g., for type==all)
5039 */
5040 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5041 vmx_instruction_info, false, &gva))
5042 return 1;
5043
5044 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
5045 kvm_inject_page_fault(vcpu, &e);
5046 return 1;
5047 }
5048
5049 if (operand.pcid >> 12 != 0) {
5050 kvm_inject_gp(vcpu, 0);
5051 return 1;
5052 }
5053
5054 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
5055
5056 switch (type) {
5057 case INVPCID_TYPE_INDIV_ADDR:
5058 if ((!pcid_enabled && (operand.pcid != 0)) ||
5059 is_noncanonical_address(operand.gla, vcpu)) {
5060 kvm_inject_gp(vcpu, 0);
5061 return 1;
5062 }
5063 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
5064 return kvm_skip_emulated_instruction(vcpu);
5065
5066 case INVPCID_TYPE_SINGLE_CTXT:
5067 if (!pcid_enabled && (operand.pcid != 0)) {
5068 kvm_inject_gp(vcpu, 0);
5069 return 1;
5070 }
5071
5072 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
5073 kvm_mmu_sync_roots(vcpu);
5074 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
5075 }
5076
Junaid Shahidb94742c2018-06-27 14:59:20 -07005077 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005078 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07005079 == operand.pcid)
5080 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07005081
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02005082 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005083 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07005084 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07005085 * given PCID, then nothing needs to be done here because a
5086 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07005087 */
5088
5089 return kvm_skip_emulated_instruction(vcpu);
5090
5091 case INVPCID_TYPE_ALL_NON_GLOBAL:
5092 /*
5093 * Currently, KVM doesn't mark global entries in the shadow
5094 * page tables, so a non-global flush just degenerates to a
5095 * global flush. If needed, we could optimize this later by
5096 * keeping track of global entries in shadow page tables.
5097 */
5098
5099 /* fall-through */
5100 case INVPCID_TYPE_ALL_INCL_GLOBAL:
5101 kvm_mmu_unload(vcpu);
5102 return kvm_skip_emulated_instruction(vcpu);
5103
5104 default:
5105 BUG(); /* We have already checked above that type <= 3 */
5106 }
5107}
5108
Kai Huang843e4332015-01-28 10:54:28 +08005109static int handle_pml_full(struct kvm_vcpu *vcpu)
5110{
5111 unsigned long exit_qualification;
5112
5113 trace_kvm_pml_full(vcpu->vcpu_id);
5114
5115 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5116
5117 /*
5118 * PML buffer FULL happened while executing iret from NMI,
5119 * "blocked by NMI" bit has to be set before next VM entry.
5120 */
5121 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005122 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005123 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5124 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5125 GUEST_INTR_STATE_NMI);
5126
5127 /*
5128 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5129 * here.., and there's no userspace involvement needed for PML.
5130 */
5131 return 1;
5132}
5133
Yunhong Jiang64672c92016-06-13 14:19:59 -07005134static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5135{
Sean Christophersond264ee02018-08-27 15:21:12 -07005136 if (!to_vmx(vcpu)->req_immediate_exit)
5137 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005138 return 1;
5139}
5140
Sean Christophersone4027cf2018-12-03 13:53:12 -08005141/*
5142 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5143 * are overwritten by nested_vmx_setup() when nested=1.
5144 */
5145static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5146{
5147 kvm_queue_exception(vcpu, UD_VECTOR);
5148 return 1;
5149}
5150
Sean Christopherson0b665d32018-08-14 09:33:34 -07005151static int handle_encls(struct kvm_vcpu *vcpu)
5152{
5153 /*
5154 * SGX virtualization is not yet supported. There is no software
5155 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
5156 * to prevent the guest from executing ENCLS.
5157 */
5158 kvm_queue_exception(vcpu, UD_VECTOR);
5159 return 1;
5160}
5161
Nadav Har'El0140cae2011-05-25 23:06:28 +03005162/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005163 * The exit handlers return 1 if the exit was handled fully and guest execution
5164 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5165 * to be done to userspace and return 0.
5166 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005167static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005168 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
5169 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005170 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005171 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005172 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005173 [EXIT_REASON_CR_ACCESS] = handle_cr,
5174 [EXIT_REASON_DR_ACCESS] = handle_dr,
5175 [EXIT_REASON_CPUID] = handle_cpuid,
5176 [EXIT_REASON_MSR_READ] = handle_rdmsr,
5177 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
5178 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
5179 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005180 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005181 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005182 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005183 [EXIT_REASON_VMCALL] = handle_vmcall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005184 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5185 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5186 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5187 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5188 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5189 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5190 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5191 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5192 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005193 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5194 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005195 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005196 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005197 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005198 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005199 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005200 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005201 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5202 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005203 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5204 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005205 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005206 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005207 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005208 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005209 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5210 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Jim Mattson45ec3682017-08-23 16:32:04 -07005211 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07005212 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08005213 [EXIT_REASON_XSAVES] = handle_xsaves,
5214 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08005215 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005216 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005217 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005218 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005219 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005220};
5221
5222static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005223 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005224
Avi Kivity586f9602010-11-18 13:09:54 +02005225static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
5226{
5227 *info1 = vmcs_readl(EXIT_QUALIFICATION);
5228 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
5229}
5230
Kai Huanga3eaa862015-11-04 13:46:05 +08005231static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005232{
Kai Huanga3eaa862015-11-04 13:46:05 +08005233 if (vmx->pml_pg) {
5234 __free_page(vmx->pml_pg);
5235 vmx->pml_pg = NULL;
5236 }
Kai Huang843e4332015-01-28 10:54:28 +08005237}
5238
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005239static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005240{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005241 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005242 u64 *pml_buf;
5243 u16 pml_idx;
5244
5245 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5246
5247 /* Do nothing if PML buffer is empty */
5248 if (pml_idx == (PML_ENTITY_NUM - 1))
5249 return;
5250
5251 /* PML index always points to next available PML buffer entity */
5252 if (pml_idx >= PML_ENTITY_NUM)
5253 pml_idx = 0;
5254 else
5255 pml_idx++;
5256
5257 pml_buf = page_address(vmx->pml_pg);
5258 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5259 u64 gpa;
5260
5261 gpa = pml_buf[pml_idx];
5262 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005263 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005264 }
5265
5266 /* reset PML index */
5267 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5268}
5269
5270/*
5271 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
5272 * Called before reporting dirty_bitmap to userspace.
5273 */
5274static void kvm_flush_pml_buffers(struct kvm *kvm)
5275{
5276 int i;
5277 struct kvm_vcpu *vcpu;
5278 /*
5279 * We only need to kick vcpu out of guest mode here, as PML buffer
5280 * is flushed at beginning of all VMEXITs, and it's obvious that only
5281 * vcpus running in guest are possible to have unflushed GPAs in PML
5282 * buffer.
5283 */
5284 kvm_for_each_vcpu(i, vcpu, kvm)
5285 kvm_vcpu_kick(vcpu);
5286}
5287
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005288static void vmx_dump_sel(char *name, uint32_t sel)
5289{
5290 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005291 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005292 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5293 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5294 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5295}
5296
5297static void vmx_dump_dtsel(char *name, uint32_t limit)
5298{
5299 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5300 name, vmcs_read32(limit),
5301 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5302}
5303
5304static void dump_vmcs(void)
5305{
5306 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5307 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5308 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5309 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5310 u32 secondary_exec_control = 0;
5311 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005312 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005313 int i, n;
5314
5315 if (cpu_has_secondary_exec_ctrls())
5316 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5317
5318 pr_err("*** Guest State ***\n");
5319 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5320 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5321 vmcs_readl(CR0_GUEST_HOST_MASK));
5322 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5323 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5324 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
5325 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
5326 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
5327 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005328 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5329 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5330 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5331 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005332 }
5333 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5334 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5335 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5336 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5337 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5338 vmcs_readl(GUEST_SYSENTER_ESP),
5339 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5340 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5341 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5342 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5343 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5344 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5345 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5346 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5347 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5348 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5349 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
5350 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
5351 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005352 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5353 efer, vmcs_read64(GUEST_IA32_PAT));
5354 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5355 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005356 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005357 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005358 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005359 pr_err("PerfGlobCtl = 0x%016llx\n",
5360 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005361 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005362 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005363 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5364 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5365 vmcs_read32(GUEST_ACTIVITY_STATE));
5366 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5367 pr_err("InterruptStatus = %04x\n",
5368 vmcs_read16(GUEST_INTR_STATUS));
5369
5370 pr_err("*** Host State ***\n");
5371 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5372 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5373 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5374 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5375 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5376 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5377 vmcs_read16(HOST_TR_SELECTOR));
5378 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5379 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5380 vmcs_readl(HOST_TR_BASE));
5381 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5382 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5383 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5384 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5385 vmcs_readl(HOST_CR4));
5386 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5387 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5388 vmcs_read32(HOST_IA32_SYSENTER_CS),
5389 vmcs_readl(HOST_IA32_SYSENTER_EIP));
5390 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005391 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5392 vmcs_read64(HOST_IA32_EFER),
5393 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005394 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005395 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005396 pr_err("PerfGlobCtl = 0x%016llx\n",
5397 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005398
5399 pr_err("*** Control State ***\n");
5400 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5401 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5402 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5403 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5404 vmcs_read32(EXCEPTION_BITMAP),
5405 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5406 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5407 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5408 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5409 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5410 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5411 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5412 vmcs_read32(VM_EXIT_INTR_INFO),
5413 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5414 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5415 pr_err(" reason=%08x qualification=%016lx\n",
5416 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5417 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5418 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5419 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005420 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005421 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005422 pr_err("TSC Multiplier = 0x%016llx\n",
5423 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005424 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
5425 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
5426 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5427 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5428 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005429 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005430 n = vmcs_read32(CR3_TARGET_COUNT);
5431 for (i = 0; i + 1 < n; i += 4)
5432 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
5433 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
5434 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
5435 if (i < n)
5436 pr_err("CR3 target%u=%016lx\n",
5437 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
5438 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5439 pr_err("PLE Gap=%08x Window=%08x\n",
5440 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5441 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5442 pr_err("Virtual processor ID = 0x%04x\n",
5443 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5444}
5445
Avi Kivity6aa8b732006-12-10 02:21:36 -08005446/*
5447 * The guest has exited. See if we can fix it or if we need userspace
5448 * assistance.
5449 */
Avi Kivity851ba692009-08-24 11:10:17 +03005450static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005451{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005452 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005453 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005454 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005455
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01005456 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
5457
Kai Huang843e4332015-01-28 10:54:28 +08005458 /*
5459 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5460 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5461 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5462 * mode as if vcpus is in root mode, the PML buffer must has been
5463 * flushed already.
5464 */
5465 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005466 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005467
Mohammed Gamal80ced182009-09-01 12:48:18 +02005468 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02005469 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02005470 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005471
Paolo Bonzini7313c692017-07-27 10:31:25 +02005472 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
5473 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03005474
Mohammed Gamal51207022010-05-31 22:40:54 +03005475 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005476 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03005477 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5478 vcpu->run->fail_entry.hardware_entry_failure_reason
5479 = exit_reason;
5480 return 0;
5481 }
5482
Avi Kivity29bd8a72007-09-10 17:27:03 +03005483 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005484 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5485 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03005486 = vmcs_read32(VM_INSTRUCTION_ERROR);
5487 return 0;
5488 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005489
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005490 /*
5491 * Note:
5492 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
5493 * delivery event since it indicates guest is accessing MMIO.
5494 * The vm-exit can be triggered again after return to guest that
5495 * will cause infinite loop.
5496 */
Mike Dayd77c26f2007-10-08 09:02:08 -04005497 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08005498 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02005499 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00005500 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005501 exit_reason != EXIT_REASON_TASK_SWITCH)) {
5502 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5503 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005504 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005505 vcpu->run->internal.data[0] = vectoring_info;
5506 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005507 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
5508 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
5509 vcpu->run->internal.ndata++;
5510 vcpu->run->internal.data[3] =
5511 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5512 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005513 return 0;
5514 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005515
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005516 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005517 vmx->loaded_vmcs->soft_vnmi_blocked)) {
5518 if (vmx_interrupt_allowed(vcpu)) {
5519 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5520 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
5521 vcpu->arch.nmi_pending) {
5522 /*
5523 * This CPU don't support us in finding the end of an
5524 * NMI-blocked window if the guest runs with IRQs
5525 * disabled. So we pull the trigger after 1 s of
5526 * futile waiting, but inform the user about this.
5527 */
5528 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
5529 "state on VCPU %d after 1 s timeout\n",
5530 __func__, vcpu->vcpu_id);
5531 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5532 }
5533 }
5534
Avi Kivity6aa8b732006-12-10 02:21:36 -08005535 if (exit_reason < kvm_vmx_max_exit_handlers
5536 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03005537 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005538 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01005539 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
5540 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03005541 kvm_queue_exception(vcpu, UD_VECTOR);
5542 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005543 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005544}
5545
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005546/*
5547 * Software based L1D cache flush which is used when microcode providing
5548 * the cache control MSR is not loaded.
5549 *
5550 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
5551 * flush it is required to read in 64 KiB because the replacement algorithm
5552 * is not exactly LRU. This could be sized at runtime via topology
5553 * information but as all relevant affected CPUs have 32KiB L1D cache size
5554 * there is no point in doing so.
5555 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005556static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005557{
5558 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005559
5560 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02005561 * This code is only executed when the the flush mode is 'cond' or
5562 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005563 */
Nicolai Stange427362a2018-07-21 22:25:00 +02005564 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02005565 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005566
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005567 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02005568 * Clear the per-vcpu flush bit, it gets set again
5569 * either from vcpu_run() or from one of the unsafe
5570 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005571 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02005572 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02005573 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02005574
5575 /*
5576 * Clear the per-cpu flush bit, it gets set again from
5577 * the interrupt handlers.
5578 */
5579 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
5580 kvm_clear_cpu_l1tf_flush_l1d();
5581
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005582 if (!flush_l1d)
5583 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005584 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005585
5586 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005587
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02005588 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
5589 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
5590 return;
5591 }
5592
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005593 asm volatile(
5594 /* First ensure the pages are in the TLB */
5595 "xorl %%eax, %%eax\n"
5596 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02005597 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005598 "addl $4096, %%eax\n\t"
5599 "cmpl %%eax, %[size]\n\t"
5600 "jne .Lpopulate_tlb\n\t"
5601 "xorl %%eax, %%eax\n\t"
5602 "cpuid\n\t"
5603 /* Now fill the cache */
5604 "xorl %%eax, %%eax\n"
5605 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02005606 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005607 "addl $64, %%eax\n\t"
5608 "cmpl %%eax, %[size]\n\t"
5609 "jne .Lfill_cache\n\t"
5610 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02005611 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005612 [size] "r" (size)
5613 : "eax", "ebx", "ecx", "edx");
5614}
5615
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005616static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005617{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08005618 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5619
5620 if (is_guest_mode(vcpu) &&
5621 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
5622 return;
5623
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005624 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005625 vmcs_write32(TPR_THRESHOLD, 0);
5626 return;
5627 }
5628
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005629 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005630}
5631
Sean Christopherson97b7ead2018-12-03 13:53:16 -08005632void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005633{
5634 u32 sec_exec_control;
5635
Jim Mattson8d860bb2018-05-09 16:56:05 -04005636 if (!lapic_in_kernel(vcpu))
5637 return;
5638
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07005639 if (!flexpriority_enabled &&
5640 !cpu_has_vmx_virtualize_x2apic_mode())
5641 return;
5642
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02005643 /* Postpone execution until vmcs01 is the current VMCS. */
5644 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04005645 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02005646 return;
5647 }
5648
Yang Zhang8d146952013-01-25 10:18:50 +08005649 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04005650 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
5651 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08005652
Jim Mattson8d860bb2018-05-09 16:56:05 -04005653 switch (kvm_get_apic_mode(vcpu)) {
5654 case LAPIC_MODE_INVALID:
5655 WARN_ONCE(true, "Invalid local APIC state");
5656 case LAPIC_MODE_DISABLED:
5657 break;
5658 case LAPIC_MODE_XAPIC:
5659 if (flexpriority_enabled) {
5660 sec_exec_control |=
5661 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5662 vmx_flush_tlb(vcpu, true);
5663 }
5664 break;
5665 case LAPIC_MODE_X2APIC:
5666 if (cpu_has_vmx_virtualize_x2apic_mode())
5667 sec_exec_control |=
5668 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
5669 break;
Yang Zhang8d146952013-01-25 10:18:50 +08005670 }
5671 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
5672
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005673 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08005674}
5675
Tang Chen38b99172014-09-24 15:57:54 +08005676static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
5677{
Jim Mattsonab5df312018-05-09 17:02:03 -04005678 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08005679 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07005680 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07005681 }
Tang Chen38b99172014-09-24 15:57:54 +08005682}
5683
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005684static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005685{
5686 u16 status;
5687 u8 old;
5688
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005689 if (max_isr == -1)
5690 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08005691
5692 status = vmcs_read16(GUEST_INTR_STATUS);
5693 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005694 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005695 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005696 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08005697 vmcs_write16(GUEST_INTR_STATUS, status);
5698 }
5699}
5700
5701static void vmx_set_rvi(int vector)
5702{
5703 u16 status;
5704 u8 old;
5705
Wei Wang4114c272014-11-05 10:53:43 +08005706 if (vector == -1)
5707 vector = 0;
5708
Yang Zhangc7c9c562013-01-25 10:18:51 +08005709 status = vmcs_read16(GUEST_INTR_STATUS);
5710 old = (u8)status & 0xff;
5711 if ((u8)vector != old) {
5712 status &= ~0xff;
5713 status |= (u8)vector;
5714 vmcs_write16(GUEST_INTR_STATUS, status);
5715 }
5716}
5717
5718static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
5719{
Liran Alon851c1a182017-12-24 18:12:56 +02005720 /*
5721 * When running L2, updating RVI is only relevant when
5722 * vmcs12 virtual-interrupt-delivery enabled.
5723 * However, it can be enabled only when L1 also
5724 * intercepts external-interrupts and in that case
5725 * we should not update vmcs02 RVI but instead intercept
5726 * interrupt. Therefore, do nothing when running L2.
5727 */
5728 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08005729 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005730}
5731
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005732static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01005733{
5734 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005735 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02005736 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01005737
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005738 WARN_ON(!vcpu->arch.apicv_active);
5739 if (pi_test_on(&vmx->pi_desc)) {
5740 pi_clear_on(&vmx->pi_desc);
5741 /*
5742 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
5743 * But on x86 this is just a compiler barrier anyway.
5744 */
5745 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02005746 max_irr_updated =
5747 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
5748
5749 /*
5750 * If we are running L2 and L1 has a new pending interrupt
5751 * which can be injected, we should re-evaluate
5752 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02005753 * If L1 intercepts external-interrupts, we should
5754 * exit from L2 to L1. Otherwise, interrupt should be
5755 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02005756 */
Liran Alon851c1a182017-12-24 18:12:56 +02005757 if (is_guest_mode(vcpu) && max_irr_updated) {
5758 if (nested_exit_on_intr(vcpu))
5759 kvm_vcpu_exiting_guest_mode(vcpu);
5760 else
5761 kvm_make_request(KVM_REQ_EVENT, vcpu);
5762 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005763 } else {
5764 max_irr = kvm_lapic_find_highest_irr(vcpu);
5765 }
5766 vmx_hwapic_irr_update(vcpu, max_irr);
5767 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01005768}
5769
Andrey Smetanin63086302015-11-10 15:36:32 +03005770static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005771{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005772 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08005773 return;
5774
Yang Zhangc7c9c562013-01-25 10:18:51 +08005775 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
5776 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
5777 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
5778 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
5779}
5780
Paolo Bonzini967235d2016-12-19 14:03:45 +01005781static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
5782{
5783 struct vcpu_vmx *vmx = to_vmx(vcpu);
5784
5785 pi_clear_on(&vmx->pi_desc);
5786 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
5787}
5788
Avi Kivity51aa01d2010-07-20 14:31:20 +03005789static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03005790{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005791 u32 exit_intr_info = 0;
5792 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02005793
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005794 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
5795 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02005796 return;
5797
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005798 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
5799 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5800 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08005801
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005802 /* if exit due to PF check for async PF */
5803 if (is_page_fault(exit_intr_info))
5804 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
5805
Andi Kleena0861c02009-06-08 17:37:09 +08005806 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005807 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
5808 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08005809 kvm_machine_check();
5810
Gleb Natapov20f65982009-05-11 13:35:55 +03005811 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08005812 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07005813 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03005814 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07005815 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005816 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03005817}
Gleb Natapov20f65982009-05-11 13:35:55 +03005818
Yang Zhanga547c6d2013-04-11 19:25:10 +08005819static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
5820{
5821 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5822
Yang Zhanga547c6d2013-04-11 19:25:10 +08005823 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
5824 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
5825 unsigned int vector;
5826 unsigned long entry;
5827 gate_desc *desc;
5828 struct vcpu_vmx *vmx = to_vmx(vcpu);
5829#ifdef CONFIG_X86_64
5830 unsigned long tmp;
5831#endif
5832
5833 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
5834 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02005835 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08005836 asm volatile(
5837#ifdef CONFIG_X86_64
5838 "mov %%" _ASM_SP ", %[sp]\n\t"
5839 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
5840 "push $%c[ss]\n\t"
5841 "push %[sp]\n\t"
5842#endif
5843 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08005844 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01005845 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08005846 :
5847#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06005848 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08005849#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05005850 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08005851 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01005852 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08005853 [ss]"i"(__KERNEL_DS),
5854 [cs]"i"(__KERNEL_CS)
5855 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005856 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08005857}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005858STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08005859
Tom Lendackybc226f02018-05-10 22:06:39 +02005860static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005861{
Tom Lendackybc226f02018-05-10 22:06:39 +02005862 switch (index) {
5863 case MSR_IA32_SMBASE:
5864 /*
5865 * We cannot do SMM unless we can run the guest in big
5866 * real mode.
5867 */
5868 return enable_unrestricted_guest || emulate_invalid_guest_state;
5869 case MSR_AMD64_VIRT_SPEC_CTRL:
5870 /* This is AMD only. */
5871 return false;
5872 default:
5873 return true;
5874 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005875}
5876
Avi Kivity51aa01d2010-07-20 14:31:20 +03005877static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
5878{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02005879 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03005880 bool unblock_nmi;
5881 u8 vector;
5882 bool idtv_info_valid;
5883
5884 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03005885
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005886 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005887 if (vmx->loaded_vmcs->nmi_known_unmasked)
5888 return;
5889 /*
5890 * Can't use vmx->exit_intr_info since we're not sure what
5891 * the exit reason is.
5892 */
5893 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5894 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
5895 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
5896 /*
5897 * SDM 3: 27.7.1.2 (September 2008)
5898 * Re-set bit "block by NMI" before VM entry if vmexit caused by
5899 * a guest IRET fault.
5900 * SDM 3: 23.2.2 (September 2008)
5901 * Bit 12 is undefined in any of the following cases:
5902 * If the VM exit sets the valid bit in the IDT-vectoring
5903 * information field.
5904 * If the VM exit is due to a double fault.
5905 */
5906 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
5907 vector != DF_VECTOR && !idtv_info_valid)
5908 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5909 GUEST_INTR_STATE_NMI);
5910 else
5911 vmx->loaded_vmcs->nmi_known_unmasked =
5912 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
5913 & GUEST_INTR_STATE_NMI);
5914 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
5915 vmx->loaded_vmcs->vnmi_blocked_time +=
5916 ktime_to_ns(ktime_sub(ktime_get(),
5917 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03005918}
5919
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005920static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03005921 u32 idt_vectoring_info,
5922 int instr_len_field,
5923 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03005924{
Avi Kivity51aa01d2010-07-20 14:31:20 +03005925 u8 vector;
5926 int type;
5927 bool idtv_info_valid;
5928
5929 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03005930
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005931 vcpu->arch.nmi_injected = false;
5932 kvm_clear_exception_queue(vcpu);
5933 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005934
5935 if (!idtv_info_valid)
5936 return;
5937
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005938 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03005939
Avi Kivity668f6122008-07-02 09:28:55 +03005940 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
5941 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03005942
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005943 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03005944 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005945 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03005946 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03005947 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03005948 * Clear bit "block by NMI" before VM entry if a NMI
5949 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03005950 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005951 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005952 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03005953 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005954 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005955 /* fall through */
5956 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03005957 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03005958 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03005959 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03005960 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03005961 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005962 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005963 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005964 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005965 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03005966 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005967 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005968 break;
5969 default:
5970 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03005971 }
Avi Kivitycf393f72008-07-01 16:20:21 +03005972}
5973
Avi Kivity83422e12010-07-20 14:43:23 +03005974static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
5975{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005976 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03005977 VM_EXIT_INSTRUCTION_LEN,
5978 IDT_VECTORING_ERROR_CODE);
5979}
5980
Avi Kivityb463a6f2010-07-20 15:06:17 +03005981static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
5982{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005983 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03005984 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5985 VM_ENTRY_INSTRUCTION_LEN,
5986 VM_ENTRY_EXCEPTION_ERROR_CODE);
5987
5988 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
5989}
5990
Gleb Natapovd7cd9792011-10-05 14:01:23 +02005991static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
5992{
5993 int i, nr_msrs;
5994 struct perf_guest_switch_msr *msrs;
5995
5996 msrs = perf_guest_get_msrs(&nr_msrs);
5997
5998 if (!msrs)
5999 return;
6000
6001 for (i = 0; i < nr_msrs; i++)
6002 if (msrs[i].host == msrs[i].guest)
6003 clear_atomic_switch_msr(vmx, msrs[i].msr);
6004 else
6005 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006006 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006007}
6008
Sean Christophersonf459a702018-08-27 15:21:11 -07006009static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
6010{
6011 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
6012 if (!vmx->loaded_vmcs->hv_timer_armed)
6013 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
6014 PIN_BASED_VMX_PREEMPTION_TIMER);
6015 vmx->loaded_vmcs->hv_timer_armed = true;
6016}
6017
6018static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006019{
6020 struct vcpu_vmx *vmx = to_vmx(vcpu);
6021 u64 tscl;
6022 u32 delta_tsc;
6023
Sean Christophersond264ee02018-08-27 15:21:12 -07006024 if (vmx->req_immediate_exit) {
6025 vmx_arm_hv_timer(vmx, 0);
6026 return;
6027 }
6028
Sean Christophersonf459a702018-08-27 15:21:11 -07006029 if (vmx->hv_deadline_tsc != -1) {
6030 tscl = rdtsc();
6031 if (vmx->hv_deadline_tsc > tscl)
6032 /* set_hv_timer ensures the delta fits in 32-bits */
6033 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6034 cpu_preemption_timer_multi);
6035 else
6036 delta_tsc = 0;
6037
6038 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -07006039 return;
Sean Christophersonf459a702018-08-27 15:21:11 -07006040 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006041
Sean Christophersonf459a702018-08-27 15:21:11 -07006042 if (vmx->loaded_vmcs->hv_timer_armed)
6043 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
6044 PIN_BASED_VMX_PREEMPTION_TIMER);
6045 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006046}
6047
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08006048static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006049{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006050 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006051 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02006052
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006053 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006054 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006055 vmx->loaded_vmcs->soft_vnmi_blocked))
6056 vmx->loaded_vmcs->entry_time = ktime_get();
6057
Avi Kivity104f2262010-11-18 13:12:52 +02006058 /* Don't enter VMX if guest state is invalid, let the exit handler
6059 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02006060 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02006061 return;
6062
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006063 if (vmx->ple_window_dirty) {
6064 vmx->ple_window_dirty = false;
6065 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6066 }
6067
Sean Christophersonff241482018-12-03 13:53:14 -08006068 if (vmx->nested.need_vmcs12_sync)
6069 nested_sync_from_vmcs12(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03006070
Avi Kivity104f2262010-11-18 13:12:52 +02006071 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
6072 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
6073 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
6074 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6075
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006076 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -07006077 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006078 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -07006079 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006080 }
6081
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006082 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -07006083 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006084 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -07006085 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006086 }
6087
Avi Kivity104f2262010-11-18 13:12:52 +02006088 /* When single-stepping over STI and MOV SS, we must clear the
6089 * corresponding interruptibility bits in the guest state. Otherwise
6090 * vmentry fails as it then expects bit 14 (BS) in pending debug
6091 * exceptions being set, but that's not correct for the guest debugging
6092 * case. */
6093 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6094 vmx_set_interrupt_shadow(vcpu, 0);
6095
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02006096 if (static_cpu_has(X86_FEATURE_PKU) &&
6097 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
6098 vcpu->arch.pkru != vmx->host_pkru)
6099 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006100
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006101 atomic_switch_perf_msrs(vmx);
6102
Sean Christophersonf459a702018-08-27 15:21:11 -07006103 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07006104
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006105 /*
6106 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6107 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6108 * is no need to worry about the conditional branch over the wrmsr
6109 * being speculatively taken.
6110 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006111 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006112
Nadav Har'Eld462b812011-05-24 15:26:10 +03006113 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006114
6115 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
6116 (unsigned long)&current_evmcs->host_rsp : 0;
6117
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006118 if (static_branch_unlikely(&vmx_l1d_should_flush))
6119 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006120
Avi Kivity104f2262010-11-18 13:12:52 +02006121 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08006122 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006123 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
6124 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
6125 "push %%" _ASM_CX " \n\t"
6126 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03006127 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006128 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006129 /* Avoid VMWRITE when Enlightened VMCS is in use */
6130 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
6131 "jz 2f \n\t"
6132 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
6133 "jmp 1f \n\t"
6134 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02006135 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03006136 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006137 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006138 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
6139 "mov %%cr2, %%" _ASM_DX " \n\t"
6140 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006141 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006142 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006143 "3: \n\t"
Uros Bizjak00df9182018-10-23 00:09:11 +02006144 /* Check if vmlaunch or vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02006145 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006146 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006147 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
6148 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
6149 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
6150 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
6151 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
6152 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006153#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006154 "mov %c[r8](%0), %%r8 \n\t"
6155 "mov %c[r9](%0), %%r9 \n\t"
6156 "mov %c[r10](%0), %%r10 \n\t"
6157 "mov %c[r11](%0), %%r11 \n\t"
6158 "mov %c[r12](%0), %%r12 \n\t"
6159 "mov %c[r13](%0), %%r13 \n\t"
6160 "mov %c[r14](%0), %%r14 \n\t"
6161 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006162#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006163 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03006164
Avi Kivity6aa8b732006-12-10 02:21:36 -08006165 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03006166 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02006167 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006168 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02006169 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006170 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08006171 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006172 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02006173 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08006174 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006175 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
6176 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
6177 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
6178 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
6179 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
6180 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
6181 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006182#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006183 "mov %%r8, %c[r8](%0) \n\t"
6184 "mov %%r9, %c[r9](%0) \n\t"
6185 "mov %%r10, %c[r10](%0) \n\t"
6186 "mov %%r11, %c[r11](%0) \n\t"
6187 "mov %%r12, %c[r12](%0) \n\t"
6188 "mov %%r13, %c[r13](%0) \n\t"
6189 "mov %%r14, %c[r14](%0) \n\t"
6190 "mov %%r15, %c[r15](%0) \n\t"
Uros Bizjak43ce76c2018-10-17 16:46:57 +02006191 /*
6192 * Clear host registers marked as clobbered to prevent
6193 * speculative use.
6194 */
Jim Mattson0cb5b302018-01-03 14:31:38 -08006195 "xor %%r8d, %%r8d \n\t"
6196 "xor %%r9d, %%r9d \n\t"
6197 "xor %%r10d, %%r10d \n\t"
6198 "xor %%r11d, %%r11d \n\t"
6199 "xor %%r12d, %%r12d \n\t"
6200 "xor %%r13d, %%r13d \n\t"
6201 "xor %%r14d, %%r14d \n\t"
6202 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006203#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006204 "mov %%cr2, %%" _ASM_AX " \n\t"
6205 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03006206
Jim Mattson0cb5b302018-01-03 14:31:38 -08006207 "xor %%eax, %%eax \n\t"
6208 "xor %%ebx, %%ebx \n\t"
6209 "xor %%esi, %%esi \n\t"
6210 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006211 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006212 ".pushsection .rodata \n\t"
6213 ".global vmx_return \n\t"
6214 "vmx_return: " _ASM_PTR " 2b \n\t"
6215 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006216 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03006217 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02006218 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03006219 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006220 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
6221 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
6222 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
6223 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
6224 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
6225 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
6226 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006227#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006228 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
6229 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
6230 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
6231 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
6232 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
6233 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
6234 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
6235 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08006236#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02006237 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
6238 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02006239 : "cc", "memory"
6240#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006241 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006242 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006243#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006244 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006245#endif
6246 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08006247
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006248 /*
6249 * We do not use IBRS in the kernel. If this vCPU has used the
6250 * SPEC_CTRL MSR it may have left it on; save the value and
6251 * turn it off. This is much more efficient than blindly adding
6252 * it to the atomic save/restore list. Especially as the former
6253 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6254 *
6255 * For non-nested case:
6256 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6257 * save it.
6258 *
6259 * For nested case:
6260 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6261 * save it.
6262 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006263 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006264 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006265
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006266 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006267
David Woodhouse117cc7a2018-01-12 11:11:27 +00006268 /* Eliminate branch target predictions from guest mode */
6269 vmexit_fill_RSB();
6270
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006271 /* All fields are clean at this point */
6272 if (static_branch_unlikely(&enable_evmcs))
6273 current_evmcs->hv_clean_fields |=
6274 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6275
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006276 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006277 if (vmx->host_debugctlmsr)
6278 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006279
Avi Kivityaa67f602012-08-01 16:48:03 +03006280#ifndef CONFIG_X86_64
6281 /*
6282 * The sysexit path does not restore ds/es, so we must set them to
6283 * a reasonable value ourselves.
6284 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006285 * We can't defer this to vmx_prepare_switch_to_host() since that
6286 * function may be executed in interrupt context, which saves and
6287 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006288 */
6289 loadsegment(ds, __USER_DS);
6290 loadsegment(es, __USER_DS);
6291#endif
6292
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006293 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02006294 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006295 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03006296 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006297 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006298 vcpu->arch.regs_dirty = 0;
6299
Gleb Natapove0b890d2013-09-25 12:51:33 +03006300 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006301 * eager fpu is enabled if PKEY is supported and CR4 is switched
6302 * back on host, so it is safe to read guest PKRU from current
6303 * XSAVE.
6304 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02006305 if (static_cpu_has(X86_FEATURE_PKU) &&
6306 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
6307 vcpu->arch.pkru = __read_pkru();
6308 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006309 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006310 }
6311
Gleb Natapove0b890d2013-09-25 12:51:33 +03006312 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006313 vmx->idt_vectoring_info = 0;
6314
6315 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
6316 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
6317 return;
6318
6319 vmx->loaded_vmcs->launched = 1;
6320 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03006321
Avi Kivity51aa01d2010-07-20 14:31:20 +03006322 vmx_complete_atomic_exit(vmx);
6323 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006324 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05006326STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006327
Sean Christopherson434a1e92018-03-20 12:17:18 -07006328static struct kvm *vmx_vm_alloc(void)
6329{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07006330 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006331 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07006332}
6333
6334static void vmx_vm_free(struct kvm *kvm)
6335{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07006336 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07006337}
6338
Avi Kivity6aa8b732006-12-10 02:21:36 -08006339static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6340{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006341 struct vcpu_vmx *vmx = to_vmx(vcpu);
6342
Kai Huang843e4332015-01-28 10:54:28 +08006343 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006344 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006345 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006346 leave_guest_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006347 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006348 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006349 kfree(vmx->guest_msrs);
6350 kvm_vcpu_uninit(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08006351 kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
Rusty Russella4770342007-08-01 14:46:11 +10006352 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006353}
6354
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006355static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006356{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006357 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10006358 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006359 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03006360 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006361
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006362 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006363 return ERR_PTR(-ENOMEM);
6364
Marc Orrb666a4b2018-11-06 14:53:56 -08006365 vmx->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, GFP_KERNEL);
6366 if (!vmx->vcpu.arch.guest_fpu) {
6367 printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
6368 err = -ENOMEM;
6369 goto free_partial_vcpu;
6370 }
6371
Wanpeng Li991e7a02015-09-16 17:30:05 +08006372 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08006373
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006374 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
6375 if (err)
6376 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006377
Peter Feiner4e595162016-07-07 14:49:58 -07006378 err = -ENOMEM;
6379
6380 /*
6381 * If PML is turned on, failure on enabling PML just results in failure
6382 * of creating the vcpu, therefore we can simplify PML logic (by
6383 * avoiding dealing with cases, such as enabling PML partially on vcpus
6384 * for the guest, etc.
6385 */
6386 if (enable_pml) {
6387 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
6388 if (!vmx->pml_pg)
6389 goto uninit_vcpu;
6390 }
6391
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006392 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02006393 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
6394 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03006395
Peter Feiner4e595162016-07-07 14:49:58 -07006396 if (!vmx->guest_msrs)
6397 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006398
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006399 err = alloc_loaded_vmcs(&vmx->vmcs01);
6400 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006401 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006402
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006403 msr_bitmap = vmx->vmcs01.msr_bitmap;
Jim Mattson788fc1e2018-11-09 09:35:11 -08006404 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006405 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
6406 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
6407 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
6408 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6409 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6410 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
6411 vmx->msr_bitmap_mode = 0;
6412
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006413 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03006414 cpu = get_cpu();
6415 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10006416 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02006417 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006418 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006419 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02006420 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006421 err = alloc_apic_access_page(kvm);
6422 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006423 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02006424 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006425
Sean Christophersone90008d2018-03-05 12:04:37 -08006426 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +08006427 err = init_rmode_identity_map(kvm);
6428 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02006429 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006430 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006431
Roman Kagan63aff652018-07-19 21:59:07 +03006432 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006433 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
Sean Christopherson7caaa712018-12-03 13:53:01 -08006434 vmx_capability.ept,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006435 kvm_vcpu_apicv_active(&vmx->vcpu));
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006436 else
6437 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08006438
Wincy Van705699a2015-02-03 23:58:17 +08006439 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006440 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006441
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006442 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
6443
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02006444 /*
6445 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
6446 * or POSTED_INTR_WAKEUP_VECTOR.
6447 */
6448 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
6449 vmx->pi_desc.sn = 1;
6450
Lan Tianyu53963a72018-12-06 15:34:36 +08006451 vmx->ept_pointer = INVALID_PAGE;
6452
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006453 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006454
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006455free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006456 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006457free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006458 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07006459free_pml:
6460 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006461uninit_vcpu:
6462 kvm_vcpu_uninit(&vmx->vcpu);
6463free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08006464 free_vpid(vmx->vpid);
Marc Orrb666a4b2018-11-06 14:53:56 -08006465 kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
6466free_partial_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10006467 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006468 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006469}
6470
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006471#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/l1tf.html for details.\n"
6472#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/l1tf.html for details.\n"
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006473
Wanpeng Lib31c1142018-03-12 04:53:04 -07006474static int vmx_vm_init(struct kvm *kvm)
6475{
Tianyu Lan877ad952018-07-19 08:40:23 +00006476 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
6477
Wanpeng Lib31c1142018-03-12 04:53:04 -07006478 if (!ple_gap)
6479 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006480
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006481 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
6482 switch (l1tf_mitigation) {
6483 case L1TF_MITIGATION_OFF:
6484 case L1TF_MITIGATION_FLUSH_NOWARN:
6485 /* 'I explicitly don't care' is set */
6486 break;
6487 case L1TF_MITIGATION_FLUSH:
6488 case L1TF_MITIGATION_FLUSH_NOSMT:
6489 case L1TF_MITIGATION_FULL:
6490 /*
6491 * Warn upon starting the first VM in a potentially
6492 * insecure environment.
6493 */
6494 if (cpu_smt_control == CPU_SMT_ENABLED)
6495 pr_warn_once(L1TF_MSG_SMT);
6496 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
6497 pr_warn_once(L1TF_MSG_L1D);
6498 break;
6499 case L1TF_MITIGATION_FULL_FORCE:
6500 /* Flush is enforced */
6501 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006502 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006503 }
Wanpeng Lib31c1142018-03-12 04:53:04 -07006504 return 0;
6505}
6506
Yang, Sheng002c7f72007-07-31 14:23:01 +03006507static void __init vmx_check_processor_compat(void *rtn)
6508{
6509 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08006510 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03006511
6512 *(int *)rtn = 0;
Sean Christopherson7caaa712018-12-03 13:53:01 -08006513 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03006514 *(int *)rtn = -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006515 if (nested)
6516 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept,
6517 enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +03006518 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6519 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6520 smp_processor_id());
6521 *(int *)rtn = -EIO;
6522 }
6523}
6524
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006525static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08006526{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006527 u8 cache;
6528 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006529
Sheng Yang522c68c2009-04-27 20:35:43 +08006530 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02006531 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08006532 * 2. EPT with VT-d:
6533 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02006534 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08006535 * b. VT-d with snooping control feature: snooping control feature of
6536 * VT-d engine can guarantee the cache correctness. Just set it
6537 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08006538 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08006539 * consistent with host MTRR
6540 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02006541 if (is_mmio) {
6542 cache = MTRR_TYPE_UNCACHABLE;
6543 goto exit;
6544 }
6545
6546 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006547 ipat = VMX_EPT_IPAT_BIT;
6548 cache = MTRR_TYPE_WRBACK;
6549 goto exit;
6550 }
6551
6552 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
6553 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02006554 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08006555 cache = MTRR_TYPE_WRBACK;
6556 else
6557 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006558 goto exit;
6559 }
6560
Xiao Guangrongff536042015-06-15 16:55:22 +08006561 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006562
6563exit:
6564 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08006565}
6566
Sheng Yang17cc3932010-01-05 19:02:27 +08006567static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02006568{
Sheng Yang878403b2010-01-05 19:02:29 +08006569 if (enable_ept && !cpu_has_vmx_ept_1g_page())
6570 return PT_DIRECTORY_LEVEL;
6571 else
6572 /* For shadow and EPT supported 1GB page */
6573 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02006574}
6575
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006576static void vmcs_set_secondary_exec_control(u32 new_ctl)
6577{
6578 /*
6579 * These bits in the secondary execution controls field
6580 * are dynamic, the others are mostly based on the hypervisor
6581 * architecture and the guest's CPUID. Do not touch the
6582 * dynamic bits.
6583 */
6584 u32 mask =
6585 SECONDARY_EXEC_SHADOW_VMCS |
6586 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02006587 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6588 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006589
6590 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6591
6592 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6593 (new_ctl & ~mask) | (cur_ctl & mask));
6594}
6595
David Matlack8322ebb2016-11-29 18:14:09 -08006596/*
6597 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
6598 * (indicating "allowed-1") if they are supported in the guest's CPUID.
6599 */
6600static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
6601{
6602 struct vcpu_vmx *vmx = to_vmx(vcpu);
6603 struct kvm_cpuid_entry2 *entry;
6604
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006605 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
6606 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08006607
6608#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
6609 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006610 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08006611} while (0)
6612
6613 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
6614 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
6615 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
6616 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
6617 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
6618 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
6619 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
6620 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
6621 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
6622 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
6623 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
6624 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
6625 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
6626 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
6627 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
6628
6629 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
6630 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
6631 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
6632 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
6633 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +01006634 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -08006635
6636#undef cr4_fixed1_update
6637}
6638
Liran Alon5f76f6f2018-09-14 03:25:52 +03006639static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
6640{
6641 struct vcpu_vmx *vmx = to_vmx(vcpu);
6642
6643 if (kvm_mpx_supported()) {
6644 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
6645
6646 if (mpx_enabled) {
6647 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
6648 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
6649 } else {
6650 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
6651 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
6652 }
6653 }
6654}
6655
Sheng Yang0e851882009-12-18 16:48:46 +08006656static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
6657{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006658 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006659
Paolo Bonzini80154d72017-08-24 13:55:35 +02006660 if (cpu_has_secondary_exec_ctrls()) {
6661 vmx_compute_secondary_exec_control(vmx);
6662 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006663 }
Mao, Junjiead756a12012-07-02 01:18:48 +00006664
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006665 if (nested_vmx_allowed(vcpu))
6666 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
6667 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
6668 else
6669 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
6670 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08006671
Liran Alon5f76f6f2018-09-14 03:25:52 +03006672 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08006673 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03006674 nested_vmx_entry_exit_ctls_update(vcpu);
6675 }
Sheng Yang0e851882009-12-18 16:48:46 +08006676}
6677
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006678static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
6679{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03006680 if (func == 1 && nested)
6681 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006682}
6683
Sean Christophersond264ee02018-08-27 15:21:12 -07006684static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
6685{
6686 to_vmx(vcpu)->req_immediate_exit = true;
6687}
6688
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006689static int vmx_check_intercept(struct kvm_vcpu *vcpu,
6690 struct x86_instruction_info *info,
6691 enum x86_intercept_stage stage)
6692{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02006693 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6694 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
6695
6696 /*
6697 * RDPID causes #UD if disabled through secondary execution controls.
6698 * Because it is marked as EmulateOnUD, we need to intercept it here.
6699 */
6700 if (info->intercept == x86_intercept_rdtscp &&
6701 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
6702 ctxt->exception.vector = UD_VECTOR;
6703 ctxt->exception.error_code_valid = false;
6704 return X86EMUL_PROPAGATE_FAULT;
6705 }
6706
6707 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006708 return X86EMUL_CONTINUE;
6709}
6710
Yunhong Jiang64672c92016-06-13 14:19:59 -07006711#ifdef CONFIG_X86_64
6712/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
6713static inline int u64_shl_div_u64(u64 a, unsigned int shift,
6714 u64 divisor, u64 *result)
6715{
6716 u64 low = a << shift, high = a >> (64 - shift);
6717
6718 /* To avoid the overflow on divq */
6719 if (high >= divisor)
6720 return 1;
6721
6722 /* Low hold the result, high hold rem which is discarded */
6723 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
6724 "rm" (divisor), "0" (low), "1" (high));
6725 *result = low;
6726
6727 return 0;
6728}
6729
6730static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
6731{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02006732 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08006733 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02006734
6735 if (kvm_mwait_in_guest(vcpu->kvm))
6736 return -EOPNOTSUPP;
6737
6738 vmx = to_vmx(vcpu);
6739 tscl = rdtsc();
6740 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
6741 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08006742 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
6743
6744 if (delta_tsc > lapic_timer_advance_cycles)
6745 delta_tsc -= lapic_timer_advance_cycles;
6746 else
6747 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006748
6749 /* Convert to host delta tsc if tsc scaling is enabled */
6750 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
6751 u64_shl_div_u64(delta_tsc,
6752 kvm_tsc_scaling_ratio_frac_bits,
6753 vcpu->arch.tsc_scaling_ratio,
6754 &delta_tsc))
6755 return -ERANGE;
6756
6757 /*
6758 * If the delta tsc can't fit in the 32 bit after the multi shift,
6759 * we can't use the preemption timer.
6760 * It's possible that it fits on later vmentries, but checking
6761 * on every vmentry is costly so we just use an hrtimer.
6762 */
6763 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
6764 return -ERANGE;
6765
6766 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -07006767 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006768}
6769
6770static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
6771{
Sean Christophersonf459a702018-08-27 15:21:11 -07006772 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006773}
6774#endif
6775
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006776static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006777{
Wanpeng Lib31c1142018-03-12 04:53:04 -07006778 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006779 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006780}
6781
Kai Huang843e4332015-01-28 10:54:28 +08006782static void vmx_slot_enable_log_dirty(struct kvm *kvm,
6783 struct kvm_memory_slot *slot)
6784{
6785 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
6786 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
6787}
6788
6789static void vmx_slot_disable_log_dirty(struct kvm *kvm,
6790 struct kvm_memory_slot *slot)
6791{
6792 kvm_mmu_slot_set_dirty(kvm, slot);
6793}
6794
6795static void vmx_flush_log_dirty(struct kvm *kvm)
6796{
6797 kvm_flush_pml_buffers(kvm);
6798}
6799
Bandan Dasc5f983f2017-05-05 15:25:14 -04006800static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
6801{
6802 struct vmcs12 *vmcs12;
6803 struct vcpu_vmx *vmx = to_vmx(vcpu);
6804 gpa_t gpa;
6805 struct page *page = NULL;
6806 u64 *pml_address;
6807
6808 if (is_guest_mode(vcpu)) {
6809 WARN_ON_ONCE(vmx->nested.pml_full);
6810
6811 /*
6812 * Check if PML is enabled for the nested guest.
6813 * Whether eptp bit 6 is set is already checked
6814 * as part of A/D emulation.
6815 */
6816 vmcs12 = get_vmcs12(vcpu);
6817 if (!nested_cpu_has_pml(vmcs12))
6818 return 0;
6819
Dan Carpenter47698862017-05-10 22:43:17 +03006820 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -04006821 vmx->nested.pml_full = true;
6822 return 1;
6823 }
6824
6825 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
6826
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02006827 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
6828 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -04006829 return 0;
6830
6831 pml_address = kmap(page);
6832 pml_address[vmcs12->guest_pml_index--] = gpa;
6833 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02006834 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -04006835 }
6836
6837 return 0;
6838}
6839
Kai Huang843e4332015-01-28 10:54:28 +08006840static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
6841 struct kvm_memory_slot *memslot,
6842 gfn_t offset, unsigned long mask)
6843{
6844 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
6845}
6846
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006847static void __pi_post_block(struct kvm_vcpu *vcpu)
6848{
6849 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6850 struct pi_desc old, new;
6851 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006852
6853 do {
6854 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006855 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
6856 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006857
6858 dest = cpu_physical_id(vcpu->cpu);
6859
6860 if (x2apic_enabled())
6861 new.ndst = dest;
6862 else
6863 new.ndst = (dest << 8) & 0xFF00;
6864
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006865 /* set 'NV' to 'notification vector' */
6866 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02006867 } while (cmpxchg64(&pi_desc->control, old.control,
6868 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006869
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006870 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
6871 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006872 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006873 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006874 vcpu->pre_pcpu = -1;
6875 }
6876}
6877
Feng Wuefc64402015-09-18 22:29:51 +08006878/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08006879 * This routine does the following things for vCPU which is going
6880 * to be blocked if VT-d PI is enabled.
6881 * - Store the vCPU to the wakeup list, so when interrupts happen
6882 * we can find the right vCPU to wake up.
6883 * - Change the Posted-interrupt descriptor as below:
6884 * 'NDST' <-- vcpu->pre_pcpu
6885 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
6886 * - If 'ON' is set during this process, which means at least one
6887 * interrupt is posted for this vCPU, we cannot block it, in
6888 * this case, return 1, otherwise, return 0.
6889 *
6890 */
Yunhong Jiangbc225122016-06-13 14:19:58 -07006891static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006892{
Feng Wubf9f6ac2015-09-18 22:29:55 +08006893 unsigned int dest;
6894 struct pi_desc old, new;
6895 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6896
6897 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08006898 !irq_remapping_cap(IRQ_POSTING_CAP) ||
6899 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +08006900 return 0;
6901
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006902 WARN_ON(irqs_disabled());
6903 local_irq_disable();
6904 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
6905 vcpu->pre_pcpu = vcpu->cpu;
6906 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
6907 list_add_tail(&vcpu->blocked_vcpu_list,
6908 &per_cpu(blocked_vcpu_on_cpu,
6909 vcpu->pre_pcpu));
6910 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
6911 }
Feng Wubf9f6ac2015-09-18 22:29:55 +08006912
6913 do {
6914 old.control = new.control = pi_desc->control;
6915
Feng Wubf9f6ac2015-09-18 22:29:55 +08006916 WARN((pi_desc->sn == 1),
6917 "Warning: SN field of posted-interrupts "
6918 "is set before blocking\n");
6919
6920 /*
6921 * Since vCPU can be preempted during this process,
6922 * vcpu->cpu could be different with pre_pcpu, we
6923 * need to set pre_pcpu as the destination of wakeup
6924 * notification event, then we can find the right vCPU
6925 * to wakeup in wakeup handler if interrupts happen
6926 * when the vCPU is in blocked state.
6927 */
6928 dest = cpu_physical_id(vcpu->pre_pcpu);
6929
6930 if (x2apic_enabled())
6931 new.ndst = dest;
6932 else
6933 new.ndst = (dest << 8) & 0xFF00;
6934
6935 /* set 'NV' to 'wakeup vector' */
6936 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02006937 } while (cmpxchg64(&pi_desc->control, old.control,
6938 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006939
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006940 /* We should not block the vCPU if an interrupt is posted for it. */
6941 if (pi_test_on(pi_desc) == 1)
6942 __pi_post_block(vcpu);
6943
6944 local_irq_enable();
6945 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006946}
6947
Yunhong Jiangbc225122016-06-13 14:19:58 -07006948static int vmx_pre_block(struct kvm_vcpu *vcpu)
6949{
6950 if (pi_pre_block(vcpu))
6951 return 1;
6952
Yunhong Jiang64672c92016-06-13 14:19:59 -07006953 if (kvm_lapic_hv_timer_in_use(vcpu))
6954 kvm_lapic_switch_to_sw_timer(vcpu);
6955
Yunhong Jiangbc225122016-06-13 14:19:58 -07006956 return 0;
6957}
6958
6959static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006960{
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006961 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006962 return;
6963
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006964 WARN_ON(irqs_disabled());
6965 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006966 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006967 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +08006968}
6969
Yunhong Jiangbc225122016-06-13 14:19:58 -07006970static void vmx_post_block(struct kvm_vcpu *vcpu)
6971{
Yunhong Jiang64672c92016-06-13 14:19:59 -07006972 if (kvm_x86_ops->set_hv_timer)
6973 kvm_lapic_switch_to_hv_timer(vcpu);
6974
Yunhong Jiangbc225122016-06-13 14:19:58 -07006975 pi_post_block(vcpu);
6976}
6977
Feng Wubf9f6ac2015-09-18 22:29:55 +08006978/*
Feng Wuefc64402015-09-18 22:29:51 +08006979 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
6980 *
6981 * @kvm: kvm
6982 * @host_irq: host irq of the interrupt
6983 * @guest_irq: gsi of the interrupt
6984 * @set: set or unset PI
6985 * returns 0 on success, < 0 on failure
6986 */
6987static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
6988 uint32_t guest_irq, bool set)
6989{
6990 struct kvm_kernel_irq_routing_entry *e;
6991 struct kvm_irq_routing_table *irq_rt;
6992 struct kvm_lapic_irq irq;
6993 struct kvm_vcpu *vcpu;
6994 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01006995 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +08006996
6997 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08006998 !irq_remapping_cap(IRQ_POSTING_CAP) ||
6999 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +08007000 return 0;
7001
7002 idx = srcu_read_lock(&kvm->irq_srcu);
7003 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01007004 if (guest_irq >= irq_rt->nr_rt_entries ||
7005 hlist_empty(&irq_rt->map[guest_irq])) {
7006 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
7007 guest_irq, irq_rt->nr_rt_entries);
7008 goto out;
7009 }
Feng Wuefc64402015-09-18 22:29:51 +08007010
7011 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
7012 if (e->type != KVM_IRQ_ROUTING_MSI)
7013 continue;
7014 /*
7015 * VT-d PI cannot support posting multicast/broadcast
7016 * interrupts to a vCPU, we still use interrupt remapping
7017 * for these kind of interrupts.
7018 *
7019 * For lowest-priority interrupts, we only support
7020 * those with single CPU as the destination, e.g. user
7021 * configures the interrupts via /proc/irq or uses
7022 * irqbalance to make the interrupts single-CPU.
7023 *
7024 * We will support full lowest-priority interrupt later.
7025 */
7026
Radim Krčmář371313132016-07-12 22:09:27 +02007027 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +08007028 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
7029 /*
7030 * Make sure the IRTE is in remapped mode if
7031 * we don't handle it in posted mode.
7032 */
7033 ret = irq_set_vcpu_affinity(host_irq, NULL);
7034 if (ret < 0) {
7035 printk(KERN_INFO
7036 "failed to back to remapped mode, irq: %u\n",
7037 host_irq);
7038 goto out;
7039 }
7040
Feng Wuefc64402015-09-18 22:29:51 +08007041 continue;
Feng Wu23a1c252016-01-25 16:53:32 +08007042 }
Feng Wuefc64402015-09-18 22:29:51 +08007043
7044 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
7045 vcpu_info.vector = irq.vector;
7046
hu huajun2698d822018-04-11 15:16:40 +08007047 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +08007048 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
7049
7050 if (set)
7051 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +08007052 else
Feng Wuefc64402015-09-18 22:29:51 +08007053 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +08007054
7055 if (ret < 0) {
7056 printk(KERN_INFO "%s: failed to update PI IRTE\n",
7057 __func__);
7058 goto out;
7059 }
7060 }
7061
7062 ret = 0;
7063out:
7064 srcu_read_unlock(&kvm->irq_srcu, idx);
7065 return ret;
7066}
7067
Ashok Rajc45dcc72016-06-22 14:59:56 +08007068static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7069{
7070 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7071 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
7072 FEATURE_CONTROL_LMCE;
7073 else
7074 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
7075 ~FEATURE_CONTROL_LMCE;
7076}
7077
Ladi Prosek72d7b372017-10-11 16:54:41 +02007078static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
7079{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007080 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7081 if (to_vmx(vcpu)->nested.nested_run_pending)
7082 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +02007083 return 1;
7084}
7085
Ladi Prosek0234bf82017-10-11 16:54:40 +02007086static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7087{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007088 struct vcpu_vmx *vmx = to_vmx(vcpu);
7089
7090 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7091 if (vmx->nested.smm.guest_mode)
7092 nested_vmx_vmexit(vcpu, -1, 0, 0);
7093
7094 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7095 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007096 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007097 return 0;
7098}
7099
7100static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
7101{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007102 struct vcpu_vmx *vmx = to_vmx(vcpu);
7103 int ret;
7104
7105 if (vmx->nested.smm.vmxon) {
7106 vmx->nested.vmxon = true;
7107 vmx->nested.smm.vmxon = false;
7108 }
7109
7110 if (vmx->nested.smm.guest_mode) {
7111 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -07007112 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007113 vcpu->arch.hflags |= HF_SMM_MASK;
7114 if (ret)
7115 return ret;
7116
7117 vmx->nested.smm.guest_mode = false;
7118 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007119 return 0;
7120}
7121
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007122static int enable_smi_window(struct kvm_vcpu *vcpu)
7123{
7124 return 0;
7125}
7126
Sean Christophersona3203382018-12-03 13:53:11 -08007127static __init int hardware_setup(void)
7128{
7129 unsigned long host_bndcfgs;
7130 int r, i;
7131
7132 rdmsrl_safe(MSR_EFER, &host_efer);
7133
7134 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7135 kvm_define_shared_msr(i, vmx_msr_index[i]);
7136
7137 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
7138 return -EIO;
7139
7140 if (boot_cpu_has(X86_FEATURE_NX))
7141 kvm_enable_efer_bits(EFER_NX);
7142
7143 if (boot_cpu_has(X86_FEATURE_MPX)) {
7144 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7145 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7146 }
7147
7148 if (boot_cpu_has(X86_FEATURE_XSAVES))
7149 rdmsrl(MSR_IA32_XSS, host_xss);
7150
7151 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7152 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7153 enable_vpid = 0;
7154
7155 if (!cpu_has_vmx_ept() ||
7156 !cpu_has_vmx_ept_4levels() ||
7157 !cpu_has_vmx_ept_mt_wb() ||
7158 !cpu_has_vmx_invept_global())
7159 enable_ept = 0;
7160
7161 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7162 enable_ept_ad_bits = 0;
7163
7164 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
7165 enable_unrestricted_guest = 0;
7166
7167 if (!cpu_has_vmx_flexpriority())
7168 flexpriority_enabled = 0;
7169
7170 if (!cpu_has_virtual_nmis())
7171 enable_vnmi = 0;
7172
7173 /*
7174 * set_apic_access_page_addr() is used to reload apic access
7175 * page upon invalidation. No need to do anything if not
7176 * using the APIC_ACCESS_ADDR VMCS field.
7177 */
7178 if (!flexpriority_enabled)
7179 kvm_x86_ops->set_apic_access_page_addr = NULL;
7180
7181 if (!cpu_has_vmx_tpr_shadow())
7182 kvm_x86_ops->update_cr8_intercept = NULL;
7183
7184 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7185 kvm_disable_largepages();
7186
7187#if IS_ENABLED(CONFIG_HYPERV)
7188 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7189 && enable_ept)
7190 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7191#endif
7192
7193 if (!cpu_has_vmx_ple()) {
7194 ple_gap = 0;
7195 ple_window = 0;
7196 ple_window_grow = 0;
7197 ple_window_max = 0;
7198 ple_window_shrink = 0;
7199 }
7200
7201 if (!cpu_has_vmx_apicv()) {
7202 enable_apicv = 0;
7203 kvm_x86_ops->sync_pir_to_irr = NULL;
7204 }
7205
7206 if (cpu_has_vmx_tsc_scaling()) {
7207 kvm_has_tsc_control = true;
7208 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7209 kvm_tsc_scaling_ratio_frac_bits = 48;
7210 }
7211
7212 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7213
7214 if (enable_ept)
7215 vmx_enable_tdp();
7216 else
7217 kvm_disable_tdp();
7218
Sean Christophersona3203382018-12-03 13:53:11 -08007219 /*
7220 * Only enable PML when hardware supports PML feature, and both EPT
7221 * and EPT A/D bit features are enabled -- PML depends on them to work.
7222 */
7223 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7224 enable_pml = 0;
7225
7226 if (!enable_pml) {
7227 kvm_x86_ops->slot_enable_log_dirty = NULL;
7228 kvm_x86_ops->slot_disable_log_dirty = NULL;
7229 kvm_x86_ops->flush_log_dirty = NULL;
7230 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7231 }
7232
7233 if (!cpu_has_vmx_preemption_timer())
7234 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7235
7236 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7237 u64 vmx_msr;
7238
7239 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7240 cpu_preemption_timer_multi =
7241 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7242 } else {
7243 kvm_x86_ops->set_hv_timer = NULL;
7244 kvm_x86_ops->cancel_hv_timer = NULL;
7245 }
7246
Sean Christophersona3203382018-12-03 13:53:11 -08007247 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Sean Christophersona3203382018-12-03 13:53:11 -08007248
7249 kvm_mce_cap_supported |= MCG_LMCE_P;
7250
7251 if (nested) {
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007252 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
7253 vmx_capability.ept, enable_apicv);
7254
Sean Christophersone4027cf2018-12-03 13:53:12 -08007255 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Sean Christophersona3203382018-12-03 13:53:11 -08007256 if (r)
7257 return r;
7258 }
7259
7260 r = alloc_kvm_area();
7261 if (r)
7262 nested_vmx_hardware_unsetup();
7263 return r;
7264}
7265
7266static __exit void hardware_unsetup(void)
7267{
7268 if (nested)
7269 nested_vmx_hardware_unsetup();
7270
7271 free_kvm_area();
7272}
7273
Kees Cook404f6aa2016-08-08 16:29:06 -07007274static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007275 .cpu_has_kvm_support = cpu_has_kvm_support,
7276 .disabled_by_bios = vmx_disabled_by_bios,
7277 .hardware_setup = hardware_setup,
7278 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007279 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007280 .hardware_enable = hardware_enable,
7281 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007282 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007283 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007284
Wanpeng Lib31c1142018-03-12 04:53:04 -07007285 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -07007286 .vm_alloc = vmx_vm_alloc,
7287 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -07007288
Avi Kivity6aa8b732006-12-10 02:21:36 -08007289 .vcpu_create = vmx_create_vcpu,
7290 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007291 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007292
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007293 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007294 .vcpu_load = vmx_vcpu_load,
7295 .vcpu_put = vmx_vcpu_put,
7296
Paolo Bonzinia96036b2015-11-10 11:55:36 +01007297 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007298 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007299 .get_msr = vmx_get_msr,
7300 .set_msr = vmx_set_msr,
7301 .get_segment_base = vmx_get_segment_base,
7302 .get_segment = vmx_get_segment,
7303 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007304 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007305 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007306 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007307 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007308 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007309 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007310 .set_cr3 = vmx_set_cr3,
7311 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007312 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007313 .get_idt = vmx_get_idt,
7314 .set_idt = vmx_set_idt,
7315 .get_gdt = vmx_get_gdt,
7316 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007317 .get_dr6 = vmx_get_dr6,
7318 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03007319 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007320 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007321 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007322 .get_rflags = vmx_get_rflags,
7323 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007324
Avi Kivity6aa8b732006-12-10 02:21:36 -08007325 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007326 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007327
Avi Kivity6aa8b732006-12-10 02:21:36 -08007328 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007329 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007330 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007331 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7332 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007333 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007334 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007335 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007336 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007337 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007338 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007339 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007340 .get_nmi_mask = vmx_get_nmi_mask,
7341 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007342 .enable_nmi_window = enable_nmi_window,
7343 .enable_irq_window = enable_irq_window,
7344 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007345 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007346 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007347 .get_enable_apicv = vmx_get_enable_apicv,
7348 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007349 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007350 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007351 .hwapic_irr_update = vmx_hwapic_irr_update,
7352 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007353 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007354 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7355 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007356
Izik Eiduscbc94022007-10-25 00:29:55 +02007357 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007358 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007359 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007360 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007361
Avi Kivity586f9602010-11-18 13:09:54 +02007362 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007363
Sheng Yang17cc3932010-01-05 19:02:27 +08007364 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007365
7366 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007367
7368 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007369 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007370
7371 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007372
7373 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007374
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02007375 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Leonid Shatz326e7422018-11-06 12:14:25 +02007376 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007377
7378 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007379
7380 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08007381 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +00007382 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08007383 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02007384 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007385
Sean Christophersond264ee02018-08-27 15:21:12 -07007386 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007387
7388 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007389
7390 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
7391 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
7392 .flush_log_dirty = vmx_flush_log_dirty,
7393 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -04007394 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +02007395
Feng Wubf9f6ac2015-09-18 22:29:55 +08007396 .pre_block = vmx_pre_block,
7397 .post_block = vmx_post_block,
7398
Wei Huang25462f72015-06-19 15:45:05 +02007399 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007400
7401 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007402
7403#ifdef CONFIG_X86_64
7404 .set_hv_timer = vmx_set_hv_timer,
7405 .cancel_hv_timer = vmx_cancel_hv_timer,
7406#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007407
7408 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007409
Ladi Prosek72d7b372017-10-11 16:54:41 +02007410 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007411 .pre_enter_smm = vmx_pre_enter_smm,
7412 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007413 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007414
Sean Christophersone4027cf2018-12-03 13:53:12 -08007415 .check_nested_events = NULL,
7416 .get_nested_state = NULL,
7417 .set_nested_state = NULL,
7418 .get_vmcs12_pages = NULL,
7419 .nested_enable_evmcs = NULL,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007420};
7421
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +02007422static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02007423{
7424 if (vmx_l1d_flush_pages) {
7425 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
7426 vmx_l1d_flush_pages = NULL;
7427 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +02007428 /* Restore state so sysfs ignores VMX */
7429 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +02007430}
7431
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007432static void vmx_exit(void)
7433{
7434#ifdef CONFIG_KEXEC_CORE
7435 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
7436 synchronize_rcu();
7437#endif
7438
7439 kvm_exit();
7440
7441#if IS_ENABLED(CONFIG_HYPERV)
7442 if (static_branch_unlikely(&enable_evmcs)) {
7443 int cpu;
7444 struct hv_vp_assist_page *vp_ap;
7445 /*
7446 * Reset everything to support using non-enlightened VMCS
7447 * access later (e.g. when we reload the module with
7448 * enlightened_vmcs=0)
7449 */
7450 for_each_online_cpu(cpu) {
7451 vp_ap = hv_get_vp_assist_page(cpu);
7452
7453 if (!vp_ap)
7454 continue;
7455
7456 vp_ap->current_nested_vmcs = 0;
7457 vp_ap->enlighten_vmentry = 0;
7458 }
7459
7460 static_branch_disable(&enable_evmcs);
7461 }
7462#endif
7463 vmx_cleanup_l1d_flush();
7464}
7465module_exit(vmx_exit);
7466
Avi Kivity6aa8b732006-12-10 02:21:36 -08007467static int __init vmx_init(void)
7468{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01007469 int r;
7470
7471#if IS_ENABLED(CONFIG_HYPERV)
7472 /*
7473 * Enlightened VMCS usage should be recommended and the host needs
7474 * to support eVMCS v1 or above. We can also disable eVMCS support
7475 * with module parameter.
7476 */
7477 if (enlightened_vmcs &&
7478 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
7479 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
7480 KVM_EVMCS_VERSION) {
7481 int cpu;
7482
7483 /* Check that we have assist pages on all online CPUs */
7484 for_each_online_cpu(cpu) {
7485 if (!hv_get_vp_assist_page(cpu)) {
7486 enlightened_vmcs = false;
7487 break;
7488 }
7489 }
7490
7491 if (enlightened_vmcs) {
7492 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
7493 static_branch_enable(&enable_evmcs);
7494 }
7495 } else {
7496 enlightened_vmcs = false;
7497 }
7498#endif
7499
7500 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007501 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03007502 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007503 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +08007504
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007505 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +02007506 * Must be called after kvm_init() so enable_ept is properly set
7507 * up. Hand the parameter mitigation value in which was stored in
7508 * the pre module init parser. If no parameter was given, it will
7509 * contain 'auto' which will be turned into the default 'cond'
7510 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007511 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +02007512 if (boot_cpu_has(X86_BUG_L1TF)) {
7513 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
7514 if (r) {
7515 vmx_exit();
7516 return r;
7517 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02007518 }
7519
Dave Young2965faa2015-09-09 15:38:55 -07007520#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007521 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
7522 crash_vmclear_local_loaded_vmcss);
7523#endif
Jim Mattson21ebf532018-05-01 15:40:28 -07007524 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007525
He, Qingfdef3ad2007-04-30 09:45:24 +03007526 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007527}
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007528module_init(vmx_init);